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

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ pthread_create(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          void *(*)(void *), void *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int tryStartThread(pthread_t *t, void *(*routine)(void *), void *arg,
                   char *message) {
  int count = 1;
  errno = 0;
  while (pthread_create(t, NULL, routine, arg) != 0 && errno == EAGAIN &&
         count <= 5) {
    errno = 0;
    {
      fprintf(stderr, "No enough resource for starting %s, retry soon...",
              message);
    }
    count++;
    sleep(1);
  }
  if (count > 5)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<tryStartThread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    0x2e82(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%esi
call   1040 <pthread_create@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x2d(%rbp)
je     11ad <tryStartThread+0x7d>
mov    0x2e4d(%rip),%rax
mov    (%rax),%rcx
xor    %eax,%eax
mov    0x2e31(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x2d(%rbp)
jne    11ad <tryStartThread+0x7d>
cmpl   $0x5,-0x2c(%rbp)
setle  %al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    11bd <tryStartThread+0x8d>
jmp    11fc <tryStartThread+0xcc>
mov    0x2e14(%rip),%rax
movq   $0x0,(%rax)
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xe21(%rip),%rsi
call   1030 <fprintf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    $0x1,%edi
call   1050 <sleep@plt>
jmp    115d <tryStartThread+0x2d>
cmpl   $0x5,-0x2c(%rbp)
jle    1212 <tryStartThread+0xe2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1219 <tryStartThread+0xe9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tryStartThread>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%r13
mov    %rdi,%rbx
mov    0x2e8a(%rip),%r14
movq   $0x0,(%r14)
xor    %esi,%esi
mov    %r13,%rdx
mov    %r12,%rcx
call   1040 <pthread_create@plt>
mov    $0x1,%ebp
test   %rax,%rax
je     11c5 <tryStartThread+0x95>
nopl   0x0(%rax)
mov    (%r14),%rax
mov    0x2e4e(%rip),%rcx
cmp    (%rcx),%rax
jne    11c5 <tryStartThread+0x95>
cmp    $0x5,%ebp
ja     11c5 <tryStartThread+0x95>
movq   $0x0,(%r14)
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
lea    0xe65(%rip),%rsi
mov    %r15,%rdx
call   1030 <fprintf@plt>
add    $0x1,%ebp
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r13,%rdx
mov    %r12,%rcx
call   1040 <pthread_create@plt>
test   %rax,%rax
jne    1170 <tryStartThread+0x40>
cmp    $0x6,%ebp
mov    $0x0,%eax
adc    $0xffffffff,%eax
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
@stderr = global i32 0
@.str = [50 x i8] c"No enough resource for starting %s, retry soon...\00"
define i32 @tryStartThread(i32* %0, i8* (i8*)* %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8* (i8*)*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
store i32* %0, i32** %6
store i8* (i8*)* %1, i8* (i8*)** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i32 1, i32* %10
store i64 0, i64* @errno
br label %11
11: ; preds = %26, %4
%12 = load i32*, i32** %6
%13 = load i8* (i8*)*, i8* (i8*)** %7
%14 = load i8*, i8** %8
%15 = call i64 @pthread_create(i32* %12, i32* null, i8* (i8*)* %13, i8* %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %11
%18 = load i64, i64* @errno
%19 = load i64, i64* @EAGAIN
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i32, i32* %10
%23 = icmp sle i32 %22, 5
br label %24
24: ; preds = %21, %17, %11
%25 = phi i1 [ false, %17 ], [ false, %11 ], [ %23, %21 ]
br i1 %25, label %26, label %33
26: ; preds = %24
store i64 0, i64* @errno
%27 = load i32, i32* @stderr
%28 = load i8*, i8** %9
%29 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i8* %28)
%30 = load i32, i32* %10
%31 = add nsw i32 %30, 1
store i32 %31, i32* %10
%32 = call i32 @sleep(i32 1)
br label %11
33: ; preds = %24
%34 = load i32, i32* %10
%35 = icmp sgt i32 %34, 5
br i1 %35, label %36, label %37
36: ; preds = %33
store i32 -1, i32* %5
br label %38
37: ; preds = %33
store i32 0, i32* %5
br label %38
38: ; preds = %37, %36
%39 = load i32, i32* %5
ret i32 %39
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
@stderr = global i32 0
@.str = [50 x i8] c"No enough resource for starting %s, retry soon...\00"
define i32 @tryStartThread(i32* %0, i8* (i8*)* %1, i8* %2, i8* %3) {
store i64 0, i64* @errno
%5 = tail call i64 @pthread_create(i32* %0, i32* null, i8* (i8*)* %1, i8* %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %21, label %7
7: ; preds = %4, %14
%8 = phi i32 [ %17, %14 ], [ 1, %4 ]
%9 = load i64, i64* @errno
%10 = load i64, i64* @EAGAIN
%11 = icmp eq i64 %9, %10
%12 = icmp ult i32 %8, 6
%13 = select i1 %11, i1 %12, i1 false
br i1 %13, label %14, label %21
14: ; preds = %7
store i64 0, i64* @errno
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i8* %3)
%17 = add nuw nsw i32 %8, 1
%18 = tail call i32 @sleep(i32 1)
%19 = tail call i64 @pthread_create(i32* %0, i32* null, i8* (i8*)* %1, i8* %2)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %7
21: ; preds = %7, %14, %4
%22 = phi i32 [ 1, %4 ], [ %17, %14 ], [ %8, %7 ]
%23 = icmp ugt i32 %22, 5
%24 = sext i1 %23 to i32
ret i32 %24
}