#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 */
scalar_t__ EAGAIN;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ pthread_attr_destroy(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_attr_init(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_attr_setstacksize(int /*<<< orphan*/ *, int);
scalar_t__ pthread_create(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          void *(*)(void *), void *);
int /*<<< orphan*/ usleep(int);
void thread_start(void *(*fn)(void *), void *arg) {
  pthread_t th;
  pthread_attr_t attr;
  pthread_attr_init(&attr);
  pthread_attr_setstacksize(&attr, 128 << 10);
  int i = 0;
  for (; i < 100; i++) {
    if (pthread_create(&th, &attr, fn, arg) == 0) {
      pthread_attr_destroy(&attr);
      return;
    }
    if (errno == EAGAIN) {
      usleep(50);
      continue;
    }
    break;
  }
  exit(1);
}


x86 assembly (clang, -O0):
<thread_start>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
call   1030 <pthread_attr_init@plt>
lea    -0x18(%rbp),%rdi
mov    $0x20000,%esi
call   1080 <pthread_attr_setstacksize@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x64,-0x1c(%rbp)
jge    1202 <thread_start+0xa2>
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    -0x14(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <pthread_create@plt>
cmp    $0x0,%rax
jne    11c6 <thread_start+0x66>
lea    -0x18(%rbp),%rdi
call   1050 <pthread_attr_destroy@plt>
add    $0x20,%rsp
pop    %rbp
ret
mov    0x2e13(%rip),%rax
mov    (%rax),%rax
mov    0x2df9(%rip),%rcx
cmp    (%rcx),%rax
jne    11ef <thread_start+0x8f>
mov    $0x32,%edi
call   1070 <usleep@plt>
jmp    11f4 <thread_start+0x94>
jmp    1202 <thread_start+0xa2>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    118e <thread_start+0x2e>
mov    $0x1,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<thread_start>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1030 <pthread_attr_init@plt>
mov    %rbp,%rdi
mov    $0x20000,%esi
call   1080 <pthread_attr_setstacksize@plt>
mov    $0x64,%ebp
lea    0x4(%rsp),%r12
mov    0x2e46(%rip),%r13
mov    0x2e2f(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %rsp,%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1040 <pthread_create@plt>
test   %rax,%rax
je     11e8 <thread_start+0x88>
mov    0x0(%r13),%rax
cmp    (%r15),%rax
jne    11de <thread_start+0x7e>
mov    $0x32,%edi
call   1070 <usleep@plt>
add    $0xffffffff,%ebp
jne    11b0 <thread_start+0x50>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    %rsp,%rdi
call   1050 <pthread_attr_destroy@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
define void @thread_start(i8* (i8*)* %0, i8* %1) {
%3 = alloca i8* (i8*)*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* (i8*)* %0, i8* (i8*)** %3
store i8* %1, i8** %4
%8 = call i32 @pthread_attr_init(i32* %6)
%9 = call i32 @pthread_attr_setstacksize(i32* %6, i32 131072)
store i32 0, i32* %7
br label %10
10: ; preds = %27, %2
%11 = load i32, i32* %7
%12 = icmp slt i32 %11, 100
br i1 %12, label %13, label %30
13: ; preds = %10
%14 = load i8* (i8*)*, i8* (i8*)** %3
%15 = load i8*, i8** %4
%16 = call i64 @pthread_create(i32* %5, i32* %6, i8* (i8*)* %14, i8* %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = call i32 @pthread_attr_destroy(i32* %6)
ret void
20: ; preds = %13
%21 = load i64, i64* @errno
%22 = load i64, i64* @EAGAIN
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @usleep(i32 50)
br label %27
26: ; preds = %20
br label %30
27: ; preds = %24
%28 = load i32, i32* %7
%29 = add nsw i32 %28, 1
store i32 %29, i32* %7
br label %10
30: ; preds = %26, %10
%31 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
define void @thread_start(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 = call i32 @pthread_attr_setstacksize(i32* %4, i32 131072)
br label %9
9: ; preds = %2, %19
%10 = phi i32 [ 0, %2 ], [ %21, %19 ]
%11 = call i64 @pthread_create(i32* %3, i32* %4, i8* (i8*)* %0, i8* %1)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = call i32 @pthread_attr_destroy(i32* %4)
ret void
15: ; preds = %9
%16 = load i64, i64* @errno
%17 = load i64, i64* @EAGAIN
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = call i32 @usleep(i32 50)
%21 = add nuw nsw i32 %10, 1
%22 = icmp eq i32 %21, 100
br i1 %22, label %23, label %9
23: ; preds = %15, %19
%24 = call i32 @exit(i32 1)
unreachable
}