#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 int pid_t;

/* Variables and functions */
int SIGCHLD;
int clone(int (*)(void *), void *, int, void *, int /*<<< orphan*/ *,
          int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
void *stack_alloc(int);
int /*<<< orphan*/ stack_free(void *, int);
int /*<<< orphan*/ stderr;
pid_t create_thread(int (*fcn)(void *), void *arg, void **stack) {
  pid_t newpid;
  int flags;
  void *my_stack;
  my_stack = stack_alloc((32 * 1024));
  flags = SIGCHLD | 0x00010000 | 0x00000100 | 0x00200000 | 0x00000200 |
          0x00000400 | 0x00000800;
  newpid = clone(fcn, my_stack, flags, arg, NULL, NULL, NULL);
  if (newpid == -1) {
    fprintf(stderr, "smp.c: Error calling clone\n");
    stack_free(my_stack, (32 * 1024));
    return -1;
  }
  *stack = my_stack;
  return newpid;
}


x86 assembly (clang, -O0):
<create_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    $0x8000,%edi
call   1060 <stack_alloc@plt>
mov    %rax,-0x30(%rbp)
mov    0x2e7f(%rip),%rax
mov    (%rax),%eax
or     $0x10000,%eax
or     $0x100,%eax
or     $0x200000,%eax
or     $0x200,%eax
or     $0x400,%eax
or     $0x800,%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%edx
mov    -0x18(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%r8
movq   $0x0,(%rsp)
call   1040 <clone@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11ec <create_thread+0xac>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
lea    0xe33(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%rdi
mov    $0x8000,%esi
call   1030 <stack_free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fd <create_thread+0xbd>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_thread>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbp
mov    $0x8000,%edi
call   1060 <stack_alloc@plt>
mov    %rax,%rbx
mov    0x2e84(%rip),%rax
mov    $0x210f00,%edx
or     (%rax),%edx
movq   $0x0,(%rsp)
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r15,%rcx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1040 <clone@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     1193 <create_thread+0x53>
mov    %rbx,(%r14)
jmp    11b5 <create_thread+0x75>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
lea    0xe5d(%rip),%rsi
call   1050 <fprintf@plt>
mov    %rbx,%rdi
mov    $0x8000,%esi
call   1030 <stack_free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SIGCHLD = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"smp.c: Error calling clone\0A\00"
define i32 @create_thread(i32 (i8*)* %0, i8* %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32 (i8*)*
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i32 (i8*)* %0, i32 (i8*)** %5
store i8* %1, i8** %6
store i8** %2, i8*** %7
%11 = call i8* @stack_alloc(i32 32768)
store i8* %11, i8** %10
%12 = load i32, i32* @SIGCHLD
%13 = or i32 %12, 65536
%14 = or i32 %13, 256
%15 = or i32 %14, 2097152
%16 = or i32 %15, 512
%17 = or i32 %16, 1024
%18 = or i32 %17, 2048
store i32 %18, i32* %9
%19 = load i32 (i8*)*, i32 (i8*)** %5
%20 = load i8*, i8** %10
%21 = load i32, i32* %9
%22 = load i8*, i8** %6
%23 = call i32 @clone(i32 (i8*)* %19, i8* %20, i32 %21, i8* %22, i32* null, i32* null, i32* null)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %31
26: ; preds = %3
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%29 = load i8*, i8** %10
%30 = call i32 @stack_free(i8* %29, i32 32768)
store i32 -1, i32* %4
br label %35
31: ; preds = %3
%32 = load i8*, i8** %10
%33 = load i8**, i8*** %7
store i8* %32, i8** %33
%34 = load i32, i32* %8
store i32 %34, i32* %4
br label %35
35: ; preds = %31, %26
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
@SIGCHLD = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"smp.c: Error calling clone\0A\00"
define i32 @create_thread(i32 (i8*)* %0, i8* %1, i8** nocapture writeonly %2) {
%4 = tail call i8* @stack_alloc(i32 32768)
%5 = load i32, i32* @SIGCHLD
%6 = or i32 %5, 2166528
%7 = tail call i32 @clone(i32 (i8*)* %0, i8* %4, i32 %6, i8* %1, i32* null, i32* null, i32* null)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %13
9: ; preds = %3
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @stack_free(i8* %4, i32 32768)
br label %14
13: ; preds = %3
store i8* %4, i8** %2
br label %14
14: ; preds = %13, %9
ret i32 %7
}