#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_attr_init(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_attr_setdetachstate(int /*<<< orphan*/ *, int);
int pthread_create(int /*<<< orphan*/ *, int /*<<< orphan*/ *, void *, void *);
int /*<<< orphan*/ sleep(int);
int thread_create(void *function, void *arg, int retries, pthread_t *tid,
                  int detachstate) {
  int res;
  pthread_attr_t attr;
  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr, detachstate);
  while (retries) {
    res = pthread_create(tid, &attr, (void *)function, arg);
    if (res != 0) {
      retries--;
      sleep(1);
    } else
      break;
  }
  return (res);
}


x86 assembly (clang, -O0):
<thread_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
lea    -0x2c(%rbp),%rdi
call   1030 <pthread_attr_init@plt>
mov    -0x24(%rbp),%esi
lea    -0x2c(%rbp),%rdi
call   1040 <pthread_attr_setdetachstate@plt>
cmpl   $0x0,-0x14(%rbp)
je     11be <thread_create+0x7e>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    -0x2c(%rbp),%rsi
call   1050 <pthread_create@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     11b4 <thread_create+0x74>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    $0x1,%edi
call   1060 <sleep@plt>
jmp    11b9 <thread_create+0x79>
jmp    11be <thread_create+0x7e>
jmp    1170 <thread_create+0x30>
mov    -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thread_create>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%ebx
mov    %rcx,%r14
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%r13
lea    0x4(%rsp),%r12
mov    %r12,%rdi
call   1030 <pthread_attr_init@plt>
mov    %r12,%rdi
mov    %ebx,%esi
call   1040 <pthread_attr_setdetachstate@plt>
test   %ebp,%ebp
je     11a8 <thread_create+0x68>
lea    0x4(%rsp),%r12
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
mov    %r15,%rcx
call   1050 <pthread_create@plt>
test   %eax,%eax
je     11aa <thread_create+0x6a>
mov    %eax,%ebx
mov    $0x1,%edi
call   1060 <sleep@plt>
add    $0xffffffff,%ebp
jne    1180 <thread_create+0x40>
jmp    11ac <thread_create+0x6c>
jmp    11ac <thread_create+0x6c>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @thread_create(i8* %0, i8* %1, i32 %2, i32* %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32* %3, i32** %9
store i32 %4, i32* %10
%13 = call i32 @pthread_attr_init(i32* %12)
%14 = load i32, i32* %10
%15 = call i32 @pthread_attr_setdetachstate(i32* %12, i32 %14)
br label %16
16: ; preds = %31, %5
%17 = load i32, i32* %8
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %32
19: ; preds = %16
%20 = load i32*, i32** %9
%21 = load i8*, i8** %6
%22 = load i8*, i8** %7
%23 = call i32 @pthread_create(i32* %20, i32* %12, i8* %21, i8* %22)
store i32 %23, i32* %11
%24 = load i32, i32* %11
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %19
%27 = load i32, i32* %8
%28 = add nsw i32 %27, -1
store i32 %28, i32* %8
%29 = call i32 @sleep(i32 1)
br label %31
30: ; preds = %19
br label %32
31: ; preds = %26
br label %16
32: ; preds = %30, %16
%33 = load i32, i32* %11
ret i32 %33
}

LLVM IR (-O2):
define i32 @thread_create(i8* %0, i8* %1, i32 %2, i32* %3, i32 %4) {
%6 = alloca i32
%7 = bitcast i32* %6 to i8*
%8 = call i32 @pthread_attr_init(i32* %6)
%9 = call i32 @pthread_attr_setdetachstate(i32* %6, i32 %4)
%10 = icmp eq i32 %2, 0
br i1 %10, label %19, label %11
11: ; preds = %5, %15
%12 = phi i32 [ %16, %15 ], [ %2, %5 ]
%13 = call i32 @pthread_create(i32* %3, i32* %6, i8* %0, i8* %1)
%14 = icmp eq i32 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %11
%16 = add nsw i32 %12, -1
%17 = call i32 @sleep(i32 1)
%18 = icmp eq i32 %16, 0
br i1 %18, label %19, label %11
19: ; preds = %15, %11, %5
%20 = phi i32 [ undef, %5 ], [ 0, %11 ], [ %13, %15 ]
ret i32 %20
}