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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
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 thread_start(void *(*fn)(int), int id) {
  pthread_t thread_id;
  pthread_attr_t thread_attr;
  int rc;
  typedef void *(*thread_fn_t)(void *);
  pthread_attr_init(&thread_attr);
  pthread_attr_setdetachstate(&thread_attr, 0);
  rc = pthread_create(&thread_id, &thread_attr, (thread_fn_t)fn,
                      (void *)(intptr_t)id);
  pthread_attr_destroy(&thread_attr);
  if (rc != 0) {
    fprintf(stderr, "Thread create failed for id %d\n", id);
    exit(1);
  }
  return thread_id;
}


x86 assembly (clang, -O0):
<thread_start>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x14(%rbp),%rdi
call   1030 <pthread_attr_init@plt>
lea    -0x14(%rbp),%rdi
xor    %esi,%esi
call   1050 <pthread_attr_setdetachstate@plt>
mov    -0x8(%rbp),%rdx
movslq -0xc(%rbp),%rcx
lea    -0x10(%rbp),%rdi
lea    -0x14(%rbp),%rsi
call   1060 <pthread_create@plt>
mov    %eax,-0x18(%rbp)
lea    -0x14(%rbp),%rdi
call   1070 <pthread_attr_destroy@plt>
cmpl   $0x0,-0x18(%rbp)
je     11d0 <thread_start+0x70>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xe3f(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thread_start>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1030 <pthread_attr_init@plt>
mov    %rbp,%rdi
xor    %esi,%esi
call   1050 <pthread_attr_setdetachstate@plt>
movslq %r14d,%rcx
lea    0x8(%rsp),%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1060 <pthread_create@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1070 <pthread_attr_destroy@plt>
test   %ebx,%ebx
jne    11b3 <thread_start+0x53>
mov    0x8(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
lea    0xe3d(%rip),%rsi
mov    %r14d,%edx
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [32 x i8] c"Thread create failed for id %d\0A\00"
define i32 @thread_start(i8* (i32)* %0, i32 %1) {
%3 = alloca i8* (i32)*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* (i32)* %0, i8* (i32)** %3
store i32 %1, i32* %4
%8 = call i32 @pthread_attr_init(i32* %6)
%9 = call i32 @pthread_attr_setdetachstate(i32* %6, i32 0)
%10 = load i8* (i32)*, i8* (i32)** %3
%11 = bitcast i8* (i32)* %10 to i8* (i8*)*
%12 = load i32, i32* %4
%13 = sext i32 %12 to i64
%14 = inttoptr i64 %13 to i8*
%15 = call i32 @pthread_create(i32* %5, i32* %6, i8* (i8*)* %11, i8* %14)
store i32 %15, i32* %7
%16 = call i32 @pthread_attr_destroy(i32* %6)
%17 = load i32, i32* %7
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %2
%20 = load i32, i32* @stderr
%21 = load i32, i32* %4
%22 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %2
%25 = load i32, i32* %5
ret i32 %25
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [32 x i8] c"Thread create failed for id %d\0A\00"
define i32 @thread_start(i8* (i32)* %0, i32 %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 = call i32 @pthread_attr_setdetachstate(i32* %4, i32 0)
%9 = bitcast i8* (i32)* %0 to i8* (i8*)*
%10 = sext i32 %1 to i64
%11 = inttoptr i64 %10 to i8*
%12 = call i32 @pthread_create(i32* %3, i32* %4, i8* (i8*)* %9, i8* %11)
%13 = call i32 @pthread_attr_destroy(i32* %4)
%14 = icmp eq i32 %12, 0
br i1 %14, label %19, label %15
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %1)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i32, i32* %3
ret i32 %20
}