Merge "Revert "More pthreads cleanup.""
diff --git a/libc/Android.mk b/libc/Android.mk
index d21878a..0f61bcf 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -494,7 +494,7 @@
 	bionic/ptrace.c
 
 libc_static_common_src_files += \
-    bionic/pthread.c
+    bionic/pthread.c \
     bionic/pthread_key.cpp \
 
 libc_arch_static_src_files := \
diff --git a/libc/arch-arm/bionic/__get_sp.S b/libc/arch-arm/bionic/__get_sp.S
index 0a313a3..2a7e7b7 100644
--- a/libc/arch-arm/bionic/__get_sp.S
+++ b/libc/arch-arm/bionic/__get_sp.S
@@ -25,10 +25,10 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
-.global __get_sp
-.type __get_sp, %function
 
-__get_sp:
-	mov r0, sp
-	bx lr
+#include <machine/asm.h>
 
+ENTRY(__get_sp)
+  mov r0, sp
+  bx lr
+END(__get_sp)
diff --git a/libc/arch-arm/bionic/_exit_with_stack_teardown.S b/libc/arch-arm/bionic/_exit_with_stack_teardown.S
index c2d7758..b00ea28 100644
--- a/libc/arch-arm/bionic/_exit_with_stack_teardown.S
+++ b/libc/arch-arm/bionic/_exit_with_stack_teardown.S
@@ -25,27 +25,20 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
-#include <asm/unistd.h>
+
 #include <machine/asm.h>
+#include <sys/linux-syscalls.h>
 
-@ void _exit_with_stack_teardown(void * stackBase, int stackSize, int retCode)
+// void _exit_with_stack_teardown(void* stackBase, int stackSize, int retCode)
 ENTRY(_exit_with_stack_teardown)
-
-#if __ARM_EABI__
     mov     lr, r2
     ldr     r7, =__NR_munmap
-    swi     #0              @ the stack is destroyed by this call
+    swi     #0              // the stack is destroyed by this call
     mov     r0, lr
     ldr     r7, =__NR_exit
     swi     #0
-#else
-    mov     lr, r2
-    swi     # __NR_munmap   @ the stack is destroyed by this call
-    mov     r0, lr
-    swi     # __NR_exit
-#endif
 
-    @ exit() should never return, cause a crash if it does
-    mov		r0, #0
-    ldr		r0, [r0]
+    // exit() should never return, cause a crash if it does
+    mov     r0, #0
+    ldr     r0, [r0]
 END(_exit_with_stack_teardown)
diff --git a/libc/arch-arm/bionic/clone.S b/libc/arch-arm/bionic/clone.S
index 7ff3d0d..6bd6bdf 100644
--- a/libc/arch-arm/bionic/clone.S
+++ b/libc/arch-arm/bionic/clone.S
@@ -25,47 +25,39 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
-#include <sys/linux-syscalls.h>
-#include <machine/asm.h>
 
+#include <machine/asm.h>
+#include <sys/linux-syscalls.h>
+
+// int  __pthread_clone(int (*fn)(void*), void* child_stack, int flags, void* arg);
 ENTRY(__pthread_clone)
-    @ insert the args onto the new stack
+    # Copy the args onto the new stack.
     stmdb r1!, {r0, r3}
 
-    @ do the system call
-    @ get flags
-
+    # The sys_clone system call only takes two arguments: 'flags' and 'child_stack'.
+    # 'child_stack' is already in r1, but we need to move 'flags' into position.
     mov     r0, r2
-
-    @ new sp is already in r1
-
-#if __ARM_EABI__
     stmfd   sp!, {r4, r7}
+
+    # System call.
     ldr     r7, =__NR_clone
     swi     #0
-#else
-    swi     #__NR_clone
-#endif
-
     movs    r0, r0
-#if __ARM_EABI__
-    ldmnefd sp!, {r4, r7}
-#endif
-    blt     __error
+    beq     1f
+
+    # In parent, reload saved registers then either exit or set errno.
+    ldmfd   sp!, {r4, r7}
     bxne    lr
+    b       __set_syscall_errno
 
-
-    @ pick the function arg and call address off the stack and jump
-    @ to the C __thread_entry function which does some setup and then
-    @ calls the thread's start function
-
+1:  # The child.
+    # pick the function arg and call address off the stack and jump
+    # to the C __thread_entry function which does some setup and then
+    # calls the thread's start function
     pop     {r0, r1}
-    mov     r2, sp			@ __thread_entry needs the TLS pointer
+    # __thread_entry needs the TLS pointer
+    mov     r2, sp
     b       __thread_entry
-
-__error:
-    mov     r0, #-1
-    bx      lr
 END(__pthread_clone)
 
 
@@ -76,8 +68,8 @@
     #                          pid_t *pid, void *tls, pid_t *ctid,
     #                          int  (*fn)(void *), void* arg );
     #
-    # NOTE: This is not the same signature than the GLibc
-    #       __clone function here !! Placing 'fn' and 'arg'
+    # NOTE: This is not the same signature as the glibc
+    #       __clone function. Placing 'fn' and 'arg'
     #       at the end of the parameter list makes the
     #       implementation much simpler.
     #
@@ -96,20 +88,18 @@
     str     r5, [r1, #-4]
     str     r6, [r1, #-8]
 
-    # system call
+    # System call
     ldr     r7, =__NR_clone
     swi     #0
     movs    r0, r0
     beq     1f
 
-    # in parent, reload saved registers
-    # then either exit or error
-    #
+    # In the parent, reload saved registers then either exit or set errno.
     ldmfd   sp!, {r4, r5, r6, r7}
     bxne    lr
     b       __set_syscall_errno
 
-1:  # in the child - pick arguments
+1:  # The child.
     ldr    r0, [sp, #-4]
     ldr    r1, [sp, #-8]
     b      __bionic_clone_entry
diff --git a/libc/arch-arm/bionic/futex_arm.S b/libc/arch-arm/bionic/futex_arm.S
index 7041663..bbd0629 100644
--- a/libc/arch-arm/bionic/futex_arm.S
+++ b/libc/arch-arm/bionic/futex_arm.S
@@ -25,32 +25,14 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
+
 #include <sys/linux-syscalls.h>
 #include <machine/asm.h>
-#include <machine/cpu-features.h>
 
 #define FUTEX_WAIT 0
 #define FUTEX_WAKE 1
 
-/* __futex_wait(*ftx, val, *timespec) */
-/* __futex_wake(*ftx, counter) */
-/* __futex_syscall3(*ftx, op, val) */
-/* __futex_syscall4(*ftx, op, val, *timespec) */
-
-.global __futex_wait
-.type __futex_wait, %function
-
-.global __futex_wake
-.type __futex_wake, %function
-
-.global __futex_syscall3
-.type __futex_syscall3, %function
-
-.global __futex_syscall4
-.type __futex_syscall4, %function
-
-#if __ARM_EABI__
-
+// __futex_syscall3(*ftx, op, val)
 ENTRY(__futex_syscall3)
     stmdb   sp!, {r4, r7}
     .save   {r4, r7}
@@ -60,53 +42,32 @@
     bx      lr
 END(__futex_syscall3)
 
-ENTRY(__futex_wait)
-    stmdb   sp!, {r4, r7}
-    .save   {r4, r7}
-    mov     r3, r2
-    mov     r2, r1
-    mov     r1, #FUTEX_WAIT
-    ldr     r7, =__NR_futex
-    swi     #0
-    ldmia   sp!, {r4, r7}
-    bx      lr
-END(__futex_wait)
-
-ENTRY(__futex_wake)
-    .save   {r4, r7}
-    stmdb   sp!, {r4, r7}
-    mov     r2, r1
-    mov     r1, #FUTEX_WAKE
-    ldr     r7, =__NR_futex
-    swi     #0
-    ldmia   sp!, {r4, r7}
-    bx      lr
-END(__futex_wake)
-
-#else
-
-ENTRY(__futex_syscall3)
-    swi     #__NR_futex
-    bx      lr
-END(__futex_syscall3)
-
-ENTRY(__futex_wait)
-    mov     r3, r2
-    mov     r2, r1
-    mov     r1, #FUTEX_WAIT
-    swi     #__NR_futex
-    bx      lr
-END(__futex_wait)
-
-ENTRY(__futex_wake)
-    mov     r2, r1
-    mov     r1, #FUTEX_WAKE
-    swi     #__NR_futex
-    bx      lr
-END(__futex_wake)
-
-#endif
-
+// __futex_syscall4(*ftx, op, val, *timespec)
 ENTRY(__futex_syscall4)
     b __futex_syscall3
 END(__futex_syscall4)
+
+// __futex_wait(*ftx, val, *timespec)
+ENTRY(__futex_wait)
+    stmdb   sp!, {r4, r7}
+    .save   {r4, r7}
+    mov     r3, r2
+    mov     r2, r1
+    mov     r1, #FUTEX_WAIT
+    ldr     r7, =__NR_futex
+    swi     #0
+    ldmia   sp!, {r4, r7}
+    bx      lr
+END(__futex_wait)
+
+// __futex_wake(*ftx, counter)
+ENTRY(__futex_wake)
+    .save   {r4, r7}
+    stmdb   sp!, {r4, r7}
+    mov     r2, r1
+    mov     r1, #FUTEX_WAKE
+    ldr     r7, =__NR_futex
+    swi     #0
+    ldmia   sp!, {r4, r7}
+    bx      lr
+END(__futex_wake)
diff --git a/libc/arch-arm/bionic/syscall.S b/libc/arch-arm/bionic/syscall.S
index ada12a6..69f3bec 100644
--- a/libc/arch-arm/bionic/syscall.S
+++ b/libc/arch-arm/bionic/syscall.S
@@ -25,47 +25,21 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
+
+#include <machine/asm.h>
 #include <sys/linux-syscalls.h>
 
-
-	.text
-	.align 4
-	.type syscall,#function
-	.globl syscall
- 
-	.text
-	.align
-
-#if __ARM_EABI__
-
-syscall:
+ENTRY(syscall)
     mov     ip, sp
-    stmfd	sp!, {r4, r5, r6, r7}
+    stmfd   sp!, {r4, r5, r6, r7}
     mov     r7, r0
     mov     r0, r1
     mov     r1, r2
     mov     r2, r3
-    ldmfd	ip, {r3, r4, r5, r6}
+    ldmfd   ip, {r3, r4, r5, r6}
     swi     #0
     ldmfd   sp!, {r4, r5, r6, r7}
     movs    r0, r0
     bxpl    lr
     b       __set_syscall_errno
-
-#else
-
-#ifndef __NR_syscall
-#define __NR_syscall    113
-#endif
-
-syscall:
-    stmfd   sp!, {r4, r5, lr}
-    ldr     r4, [sp, #12]
-    ldr     r5, [sp, #16]
-    swi     __NR_syscall
-    ldmfd   sp!, {r4, r5, lr}
-    movs    r0, r0
-    bxpl    lr
-    b       __set_syscall_errno
-
-#endif
+END(syscall)
diff --git a/libc/arch-x86/bionic/clone.S b/libc/arch-x86/bionic/clone.S
index 54b6ef2..9266f84 100644
--- a/libc/arch-x86/bionic/clone.S
+++ b/libc/arch-x86/bionic/clone.S
@@ -2,10 +2,7 @@
 
 .text
 
-/*
- * int  __pthread_clone(int (*fn)(void*), void *tls, int flags,
- *                      void *arg);
- */
+// int  __pthread_clone(int (*fn)(void*), void* tls, int flags, void* arg);
 .globl __pthread_clone
 .type __pthread_clone, @function
 .align 4
diff --git a/tests/dirent_test.cpp b/tests/dirent_test.cpp
index 8f3c249..48ca819 100644
--- a/tests/dirent_test.cpp
+++ b/tests/dirent_test.cpp
@@ -66,12 +66,12 @@
 
 TEST(dirent, fdopendir_invalid) {
   ASSERT_TRUE(fdopendir(-1) == NULL);
-  ASSERT_EQ(errno, EBADF);
+  ASSERT_EQ(EBADF, errno);
 
   int fd = open("/dev/null", O_RDONLY);
   ASSERT_NE(fd, -1);
   ASSERT_TRUE(fdopendir(fd) == NULL);
-  ASSERT_EQ(errno, ENOTDIR);
+  ASSERT_EQ(ENOTDIR, errno);
   close(fd);
 }
 
@@ -85,15 +85,15 @@
 
   // fdopendir(3) took ownership, so closedir(3) closed our fd.
   ASSERT_EQ(close(fd), -1);
-  ASSERT_EQ(errno, EBADF);
+  ASSERT_EQ(EBADF, errno);
 }
 
 TEST(dirent, opendir_invalid) {
   ASSERT_TRUE(opendir("/does/not/exist") == NULL);
-  ASSERT_EQ(errno, ENOENT);
+  ASSERT_EQ(ENOENT, errno);
 
   ASSERT_TRUE(opendir("/dev/null") == NULL);
-  ASSERT_EQ(errno, ENOTDIR);
+  ASSERT_EQ(ENOTDIR, errno);
 }
 
 TEST(dirent, opendir) {
@@ -107,7 +107,7 @@
 TEST(dirent, closedir_invalid) {
   DIR* d = NULL;
   ASSERT_EQ(closedir(d), -1);
-  ASSERT_EQ(errno, EINVAL);
+  ASSERT_EQ(EINVAL, errno);
 }
 
 TEST(dirent, closedir) {
@@ -127,7 +127,7 @@
   }
   // Reading to the end of the directory is not an error.
   // readdir(3) returns NULL, but leaves errno as 0.
-  ASSERT_EQ(errno, 0);
+  ASSERT_EQ(0, errno);
   ASSERT_EQ(closedir(d), 0);
 
   CheckProcSelf(name_set);
@@ -145,7 +145,7 @@
   }
   // Reading to the end of the directory is not an error.
   // readdir_r(3) returns NULL, but leaves errno as 0.
-  ASSERT_EQ(errno, 0);
+  ASSERT_EQ(0, errno);
   ASSERT_EQ(closedir(d), 0);
 
   CheckProcSelf(name_set);
diff --git a/tests/getcwd_test.cpp b/tests/getcwd_test.cpp
index 45ff395..b3b4136 100644
--- a/tests/getcwd_test.cpp
+++ b/tests/getcwd_test.cpp
@@ -25,7 +25,7 @@
   errno = 0;
   char* cwd = getcwd(NULL, 0);
   ASSERT_TRUE(cwd != NULL);
-  ASSERT_EQ(errno, 0);
+  ASSERT_EQ(0, errno);
   ASSERT_GE(strlen(cwd), 1U);
   free(cwd);
 }
@@ -35,7 +35,7 @@
   errno = 0;
   char* cwd = getcwd(NULL, PATH_MAX);
   ASSERT_TRUE(cwd != NULL);
-  ASSERT_EQ(errno, 0);
+  ASSERT_EQ(0, errno);
   ASSERT_GE(strlen(cwd), 1U);
   free(cwd);
 }
@@ -45,7 +45,7 @@
   errno = 0;
   char* cwd = getcwd(NULL, 1);
   ASSERT_TRUE(cwd == NULL);
-  ASSERT_EQ(errno, ERANGE);
+  ASSERT_EQ(ERANGE, errno);
 }
 
 TEST(getcwd, auto_too_large) {
@@ -53,7 +53,7 @@
   errno = 0;
   char* cwd = getcwd(NULL, static_cast<size_t>(-1));
   ASSERT_TRUE(cwd == NULL);
-  ASSERT_EQ(errno, ENOMEM);
+  ASSERT_EQ(ENOMEM, errno);
 }
 
 TEST(getcwd, manual_too_small) {
@@ -62,7 +62,7 @@
   errno = 0;
   char* cwd = getcwd(tiny_buf, sizeof(tiny_buf));
   ASSERT_TRUE(cwd == NULL);
-  ASSERT_EQ(errno, ERANGE);
+  ASSERT_EQ(ERANGE, errno);
 }
 
 TEST(getcwd, manual_zero) {
@@ -71,7 +71,7 @@
   errno = 0;
   char* cwd = getcwd(tiny_buf, 0);
   ASSERT_TRUE(cwd == NULL);
-  ASSERT_EQ(errno, EINVAL);
+  ASSERT_EQ(EINVAL, errno);
 }
 
 TEST(getcwd, manual_path_max) {
@@ -79,7 +79,7 @@
   errno = 0;
   char* cwd = getcwd(buf, PATH_MAX);
   ASSERT_TRUE(cwd == buf);
-  ASSERT_EQ(errno, 0);
+  ASSERT_EQ(0, errno);
   ASSERT_GE(strlen(cwd), 1U);
   delete[] cwd;
 }
diff --git a/tests/pthread_test.cpp b/tests/pthread_test.cpp
index 2cf45f3..0ccd948 100644
--- a/tests/pthread_test.cpp
+++ b/tests/pthread_test.cpp
@@ -173,3 +173,13 @@
   ASSERT_EQ(SIGUSR1, received_signal);
   ASSERT_EQ(0, reinterpret_cast<int>(join_result));
 }
+
+#if !defined(__GLIBC__)
+extern "C" int  __pthread_clone(int (*fn)(void*), void* child_stack, int flags, void* arg);
+TEST(pthread, __pthread_clone) {
+  uintptr_t fake_child_stack[16];
+  errno = 0;
+  ASSERT_EQ(-1, __pthread_clone(NULL, &fake_child_stack[0], CLONE_THREAD, NULL));
+  ASSERT_EQ(EINVAL, errno);
+}
+#endif
diff --git a/tests/stdio_test.cpp b/tests/stdio_test.cpp
index 39f9b0f..70a71fb 100644
--- a/tests/stdio_test.cpp
+++ b/tests/stdio_test.cpp
@@ -75,7 +75,7 @@
   // It should set the end-of-file indicator for the stream, though.
   errno = 0;
   ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), -1);
-  ASSERT_EQ(errno, 0);
+  ASSERT_EQ(0, errno);
   ASSERT_TRUE(feof(fp));
 
   free(word_read);
@@ -91,18 +91,18 @@
   // The first argument can't be NULL.
   errno = 0;
   ASSERT_EQ(getdelim(NULL, &buffer_length, ' ', fp), -1);
-  ASSERT_EQ(errno, EINVAL);
+  ASSERT_EQ(EINVAL, errno);
 
   // The second argument can't be NULL.
   errno = 0;
   ASSERT_EQ(getdelim(&buffer, NULL, ' ', fp), -1);
-  ASSERT_EQ(errno, EINVAL);
+  ASSERT_EQ(EINVAL, errno);
 
   // The stream can't be closed.
   fclose(fp);
   errno = 0;
   ASSERT_EQ(getdelim(&buffer, &buffer_length, ' ', fp), -1);
-  ASSERT_EQ(errno, EBADF);
+  ASSERT_EQ(EBADF, errno);
 }
 
 TEST(stdio, getline) {
@@ -140,7 +140,7 @@
   // It should set the end-of-file indicator for the stream, though.
   errno = 0;
   ASSERT_EQ(getline(&line_read, &allocated_length, fp), -1);
-  ASSERT_EQ(errno, 0);
+  ASSERT_EQ(0, errno);
   ASSERT_TRUE(feof(fp));
 
   free(line_read);
@@ -156,16 +156,16 @@
   // The first argument can't be NULL.
   errno = 0;
   ASSERT_EQ(getline(NULL, &buffer_length, fp), -1);
-  ASSERT_EQ(errno, EINVAL);
+  ASSERT_EQ(EINVAL, errno);
 
   // The second argument can't be NULL.
   errno = 0;
   ASSERT_EQ(getline(&buffer, NULL, fp), -1);
-  ASSERT_EQ(errno, EINVAL);
+  ASSERT_EQ(EINVAL, errno);
 
   // The stream can't be closed.
   fclose(fp);
   errno = 0;
   ASSERT_EQ(getline(&buffer, &buffer_length, fp), -1);
-  ASSERT_EQ(errno, EBADF);
+  ASSERT_EQ(EBADF, errno);
 }
diff --git a/tests/stubs_test.cpp b/tests/stubs_test.cpp
index 9daaa22..2e1acc1 100644
--- a/tests/stubs_test.cpp
+++ b/tests/stubs_test.cpp
@@ -38,7 +38,7 @@
   errno = 0;
   passwd* pwd = getpwuid(uid);
   ASSERT_TRUE(pwd != NULL);
-  ASSERT_EQ(errno, 0);
+  ASSERT_EQ(0, errno);
   EXPECT_STREQ(username, pwd->pw_name);
   EXPECT_EQ(uid, pwd->pw_uid);
   EXPECT_EQ(uid, pwd->pw_gid);