#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*/ fprintf(int /*<<< orphan*/, char *, char *, int);
int pthread_create(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                   void *(*)(void *), void *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
char *strerror(int);
void create_thread(pthread_attr_t *attr, void *(*start_routine)(void *),
                   void *arg) {
  pthread_t child;
  int rc;
  while ((rc = pthread_create(&child, attr, start_routine, arg)) != 0) {
    fprintf(stderr, "unexpected error from pthread_create: %s (%d)\n",
            strerror(rc), rc);
    sleep(1);
  }
}


x86 assembly (clang, -O0):
<create_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    -0x1c(%rbp),%rdi
call   1040 <pthread_create@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
je     11ad <create_thread+0x6d>
mov    0x2e64(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%edi
call   1050 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
mov    -0x20(%rbp),%ecx
lea    0xe67(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
jmp    1154 <create_thread+0x14>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_thread>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0x4(%rsp),%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1040 <pthread_create@plt>
test   %eax,%eax
je     11c6 <create_thread+0x86>
mov    %eax,%ebp
lea    0x4(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x2e59(%rip),%rax
mov    (%rax),%r12d
mov    %ebp,%edi
call   1050 <strerror@plt>
mov    %r12d,%edi
lea    0xe65(%rip),%rsi
mov    %rax,%rdx
mov    %ebp,%ecx
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1040 <pthread_create@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1180 <create_thread+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [47 x i8] c"unexpected error from pthread_create: %s (%d)\0A\00"
define void @create_thread(i32* %0, i8* (i8*)* %1, i8* %2) {
%4 = alloca i32*
%5 = alloca i8* (i8*)*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %4
store i8* (i8*)* %1, i8* (i8*)** %5
store i8* %2, i8** %6
br label %9
9: ; preds = %15, %3
%10 = load i32*, i32** %4
%11 = load i8* (i8*)*, i8* (i8*)** %5
%12 = load i8*, i8** %6
%13 = call i32 @pthread_create(i32* %7, i32* %10, i8* (i8*)* %11, i8* %12)
store i32 %13, i32* %8
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %9
%16 = load i32, i32* @stderr
%17 = load i32, i32* %8
%18 = call i8* @strerror(i32 %17)
%19 = load i32, i32* %8
%20 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8* %18, i32 %19)
%21 = call i32 @sleep(i32 1)
br label %9
22: ; preds = %9
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [47 x i8] c"unexpected error from pthread_create: %s (%d)\0A\00"
define void @create_thread(i32* %0, i8* (i8*)* %1, i8* %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = call i32 @pthread_create(i32* %4, i32* %0, i8* (i8*)* %1, i8* %2)
%7 = icmp eq i32 %6, 0
br i1 %7, label %16, label %8
8: ; preds = %3, %8
%9 = phi i32 [ %14, %8 ], [ %6, %3 ]
%10 = load i32, i32* @stderr
%11 = call i8* @strerror(i32 %9)
%12 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8* %11, i32 %9)
%13 = call i32 @sleep(i32 1)
%14 = call i32 @pthread_create(i32* %4, i32* %0, i8* (i8*)* %1, i8* %2)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %8
16: ; preds = %8, %3
ret void
}