#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 /*<<< orphan*/ pthread_t;
typedef int /*<<< orphan*/ pthread_attr_t;

/* Variables and functions */
int /*<<< orphan*/ PTHREAD_CREATE_JOINABLE;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ pthread_attr_destroy(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_attr_init(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_attr_setdetachstate(int /*<<< orphan*/ *,
                                               int /*<<< orphan*/);
int pthread_create(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                   void *(*)(void *), void *);
int /*<<< orphan*/ stderr;
pthread_t makeThread(void *(*f)(void *arg), void *arg) {
  int err;
  pthread_t retVal;
  pthread_attr_t attr;
  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  err = pthread_create(&retVal, &attr, f, arg);
  if (err) {
    fprintf(stderr, "ERROR: failed to spawn thread, err#: %d\n", err);
    pthread_attr_destroy(&attr);
    return (pthread_t)NULL;
  } else {
    pthread_attr_destroy(&attr);
    return retVal;
  }
}


x86 assembly (clang, -O0):
<makeThread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x24(%rbp),%rdi
call   1030 <pthread_attr_init@plt>
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
lea    -0x24(%rbp),%rdi
call   1050 <pthread_attr_setdetachstate@plt>
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    -0x20(%rbp),%rdi
lea    -0x24(%rbp),%rsi
call   1060 <pthread_create@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11ca <makeThread+0x7a>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xe50(%rip),%rsi
call   1040 <fprintf@plt>
lea    -0x24(%rbp),%rdi
call   1070 <pthread_attr_destroy@plt>
movl   $0x0,-0x4(%rbp)
jmp    11d9 <makeThread+0x89>
lea    -0x24(%rbp),%rdi
call   1070 <pthread_attr_destroy@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<makeThread>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <pthread_attr_init@plt>
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <pthread_attr_setdetachstate@plt>
lea    0xc(%rsp),%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1060 <pthread_create@plt>
test   %eax,%eax
je     11b9 <makeThread+0x69>
mov    0x2e3d(%rip),%rcx
mov    (%rcx),%edi
lea    0xe5c(%rip),%rsi
mov    %eax,%edx
call   1040 <fprintf@plt>
lea    0x8(%rsp),%rdi
call   1070 <pthread_attr_destroy@plt>
xor    %eax,%eax
jmp    11c7 <makeThread+0x77>
lea    0x8(%rsp),%rdi
call   1070 <pthread_attr_destroy@plt>
mov    0xc(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@PTHREAD_CREATE_JOINABLE = global i32 0
@stderr = global i32 0
@.str = [41 x i8] c"ERROR: failed to spawn thread, err#: %d\0A\00"
define i32 @makeThread(i8* (i8*)* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8* (i8*)*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* (i8*)* %0, i8* (i8*)** %4
store i8* %1, i8** %5
%9 = call i32 @pthread_attr_init(i32* %8)
%10 = load i32, i32* @PTHREAD_CREATE_JOINABLE
%11 = call i32 @pthread_attr_setdetachstate(i32* %8, i32 %10)
%12 = load i8* (i8*)*, i8* (i8*)** %4
%13 = load i8*, i8** %5
%14 = call i32 @pthread_create(i32* %7, i32* %8, i8* (i8*)* %12, i8* %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = load i32, i32* %6
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = call i32 @pthread_attr_destroy(i32* %8)
store i32 0, i32* %3
br label %25
22: ; preds = %2
%23 = call i32 @pthread_attr_destroy(i32* %8)
%24 = load i32, i32* %7
store i32 %24, i32* %3
br label %25
25: ; preds = %22, %17
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
@PTHREAD_CREATE_JOINABLE = global i32 0
@stderr = global i32 0
@.str = [41 x i8] c"ERROR: failed to spawn thread, err#: %d\0A\00"
define i32 @makeThread(i8* (i8*)* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = call i32 @pthread_attr_init(i32* %4)
%8 = load i32, i32* @PTHREAD_CREATE_JOINABLE
%9 = call i32 @pthread_attr_setdetachstate(i32* %4, i32 %8)
%10 = call i32 @pthread_create(i32* %3, i32* %4, i8* (i8*)* %0, i8* %1)
%11 = icmp eq i32 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %10)
%15 = call i32 @pthread_attr_destroy(i32* %4)
br label %19
16: ; preds = %2
%17 = call i32 @pthread_attr_destroy(i32* %4)
%18 = load i32, i32* %3
br label %19
19: ; preds = %16, %12
%20 = phi i32 [ 0, %12 ], [ %18, %16 ]
ret i32 %20
}