#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef long pthread_t;
typedef int /*<<< orphan*/ pthread_attr_t;

/* Variables and functions */
int SIGCHLD;
long clone(int (*)(void *), void **, int, void *);
long errno;
scalar_t__ malloc(int);
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                   void *(*fn)(void *), void *data) {
  long retval;
  void **newstack;
  int (*clonefunc)(void *) = (int (*)(void *))(fn);
  newstack = (void **)malloc(8096);
  if (!newstack)
    return -1;
  newstack = (void **)(8096 + (char *)newstack);
  *--newstack = data;
  retval =
      clone(clonefunc, newstack,
            0x00000100 | 0x00000200 | 0x00000400 | 0x00000800 | SIGCHLD, data);
  if (retval < 0) {
    errno = -retval;
    *thread = 0;
    retval = -1;
  } else {
    *thread = retval;
    retval = 0;
  }
  return retval;
}


x86 assembly (clang, -O0):
<pthread_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x1fa0,%edi
call   1040 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1165 <pthread_create+0x45>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fd <pthread_create+0xdd>
mov    -0x38(%rbp),%rax
add    $0x1fa0,%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x38(%rbp),%rax
mov    %rax,%rdx
add    $0xfffffffffffffff8,%rdx
mov    %rdx,-0x38(%rbp)
mov    %rcx,-0x8(%rax)
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    0x2e4f(%rip),%rax
mov    (%rax),%edx
or     $0xf00,%edx
mov    -0x28(%rbp),%rcx
call   1030 <clone@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    11e3 <pthread_create+0xc3>
xor    %eax,%eax
mov    %eax,%ecx
sub    -0x30(%rbp),%rcx
mov    0x2e10(%rip),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
movq   $0xffffffffffffffff,-0x30(%rbp)
ff
jmp    11f6 <pthread_create+0xd6>
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pthread_create>:
push   %r15
push   %r14
push   %rbx
mov    %rcx,%rbx
mov    %rdx,%r15
mov    %rdi,%r14
mov    $0x1fa0,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     1170 <pthread_create+0x50>
mov    %rax,%rsi
add    $0x1f98,%rsi
mov    %rbx,0x1f98(%rax)
mov    0x2e93(%rip),%rax
mov    $0xf00,%edx
or     (%rax),%edx
mov    %r15,%rdi
mov    %rbx,%rcx
call   1030 <clone@plt>
test   %rax,%rax
js     1177 <pthread_create+0x57>
xor    %ecx,%ecx
jmp    118b <pthread_create+0x6b>
mov    $0xffffffff,%ecx
jmp    118e <pthread_create+0x6e>
neg    %rax
mov    0x2e57(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%ecx
xor    %eax,%eax
mov    %rax,(%r14)
mov    %ecx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@SIGCHLD = global i32 0
@errno = global i64 0
define i32 @pthread_create(i64* %0, i32* %1, i8* (i8*)* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i64*
%7 = alloca i32*
%8 = alloca i8* (i8*)*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i8**
%12 = alloca i32 (i8*)*
store i64* %0, i64** %6
store i32* %1, i32** %7
store i8* (i8*)* %2, i8* (i8*)** %8
store i8* %3, i8** %9
%13 = load i8* (i8*)*, i8* (i8*)** %8
%14 = bitcast i8* (i8*)* %13 to i32 (i8*)*
store i32 (i8*)* %14, i32 (i8*)** %12
%15 = call i64 @malloc(i32 8096)
%16 = inttoptr i64 %15 to i8**
store i8** %16, i8*** %11
%17 = load i8**, i8*** %11
%18 = icmp ne i8** %17, null
br i1 %18, label %20, label %19
19: ; preds = %4
store i32 -1, i32* %5
br label %46
20: ; preds = %4
%21 = load i8**, i8*** %11
%22 = bitcast i8** %21 to i8*
%23 = getelementptr inbounds i8, i8* %22, i64 8096
%24 = bitcast i8* %23 to i8**
store i8** %24, i8*** %11
%25 = load i8*, i8** %9
%26 = load i8**, i8*** %11
%27 = getelementptr inbounds i8*, i8** %26, i32 -1
store i8** %27, i8*** %11
store i8* %25, i8** %27
%28 = load i32 (i8*)*, i32 (i8*)** %12
%29 = load i8**, i8*** %11
%30 = load i32, i32* @SIGCHLD
%31 = or i32 3840, %30
%32 = load i8*, i8** %9
%33 = call i64 @clone(i32 (i8*)* %28, i8** %29, i32 %31, i8* %32)
store i64 %33, i64* %10
%34 = load i64, i64* %10
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %20
%37 = load i64, i64* %10
%38 = sub nsw i64 0, %37
store i64 %38, i64* @errno
%39 = load i64*, i64** %6
store i64 0, i64* %39
store i64 -1, i64* %10
br label %43
40: ; preds = %20
%41 = load i64, i64* %10
%42 = load i64*, i64** %6
store i64 %41, i64* %42
store i64 0, i64* %10
br label %43
43: ; preds = %40, %36
%44 = load i64, i64* %10
%45 = trunc i64 %44 to i32
store i32 %45, i32* %5
br label %46
46: ; preds = %43, %19
%47 = load i32, i32* %5
ret i32 %47
}

LLVM IR (-O2):
@SIGCHLD = global i32 0
@errno = global i64 0
define i32 @pthread_create(i64* nocapture writeonly %0, i32* nocapture readnone %1, i8* (i8*)* %2, i8* %3) {
%5 = tail call i64 @malloc(i32 8096)
%6 = icmp eq i64 %5, 0
br i1 %6, label %21, label %7
7: ; preds = %4
%8 = bitcast i8* (i8*)* %2 to i32 (i8*)*
%9 = inttoptr i64 %5 to i8*
%10 = getelementptr inbounds i8, i8* %9, i64 8088
%11 = bitcast i8* %10 to i8**
store i8* %3, i8** %11
%12 = load i32, i32* @SIGCHLD
%13 = or i32 %12, 3840
%14 = tail call i64 @clone(i32 (i8*)* %8, i8** %11, i32 %13, i8* %3)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %7
%17 = sub nsw i64 0, %14
store i64 %17, i64* @errno
br label %18
18: ; preds = %7, %16
%19 = phi i64 [ 0, %16 ], [ %14, %7 ]
%20 = phi i32 [ -1, %16 ], [ 0, %7 ]
store i64 %19, i64* %0
br label %21
21: ; preds = %4, %18
%22 = phi i32 [ %20, %18 ], [ -1, %4 ]
ret i32 %22
}