#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 */
struct timespec {
  int tv_nsec;
  int /*<<< orphan*/ tv_sec;
};
typedef int pid_t;

/* Variables and functions */
int TEMP_FAILURE_RETRY(int /*<<< orphan*/);
int WEXITSTATUS(int);
int /*<<< orphan*/ _exit(int);
int fork();
scalar_t__ getpid();
int /*<<< orphan*/ nanosleep(struct timespec *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
void *thread_function(void *arg) {
  int i = (intptr_t)arg;
  int status;
  pid_t pid;
  pid_t pid2;
  pid = fork();
  switch (pid) {
  case 0:
    printf("%ld for %d\n", (long int)getpid(), i);
    struct timespec ts = {.tv_sec = 0, .tv_nsec = 100000000 * i};
    nanosleep(&ts, NULL);
    _exit(i);
    break;
  case -1:
    printf("fork: %m\n");
    return (void *)1l;
    break;
  }
  pid2 = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
  if (pid2 != pid) {
    printf("waitpid returned %ld, expected %ld\n", (long int)pid2,
           (long int)pid);
    return (void *)1l;
  }
  printf("%ld with %d, expected %d\n", (long int)pid, WEXITSTATUS(status), i);
  return WEXITSTATUS(status) == i ? NULL : (void *)1l;
}


x86 assembly (clang, -O0):
<thread_function>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
sub    $0xffffffff,%eax
je     1201 <thread_function+0x81>
jmp    11b0 <thread_function+0x30>
mov    -0x2c(%rbp),%eax
test   %eax,%eax
jne    121d <thread_function+0x9d>
jmp    11c0 <thread_function+0x40>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,%rsi
mov    -0x14(%rbp),%edx
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
imul   $0x5f5e100,-0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <nanosleep@plt>
mov    -0x14(%rbp),%edi
call   1030 <_exit@plt>
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%eax
mov    %rax,-0x8(%rbp)
jmp    12aa <thread_function+0x12a>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    %eax,%edi
call   1090 <TEMP_FAILURE_RETRY@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     1265 <thread_function+0xe5>
movslq -0x20(%rbp),%rsi
movslq -0x1c(%rbp),%rdx
lea    0xdc6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%eax
mov    %rax,-0x8(%rbp)
jmp    12aa <thread_function+0x12a>
movslq -0x1c(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
mov    -0x14(%rbp),%ecx
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,%edx
mov    -0x14(%rbp),%esi
mov    $0x1,%eax
xor    %ecx,%ecx
cmp    %esi,%edx
cmove  %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thread_function>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
xor    %eax,%eax
call   10a0 <fork@plt>
cmp    $0xffffffff,%eax
je     11e9 <thread_function+0x69>
mov    %eax,%ebp
test   %eax,%eax
je     1219 <thread_function+0x99>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    %eax,%edi
call   1090 <TEMP_FAILURE_RETRY@plt>
cmp    %ebp,%eax
jne    11f9 <thread_function+0x79>
movslq %ebp,%rbp
mov    0xc(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
lea    0xe73(%rip),%rdi
mov    %rbp,%rsi
mov    %eax,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0xc(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    %ebx,%ecx
setne  %al
jmp    1212 <thread_function+0x92>
lea    0xe1c(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    120d <thread_function+0x8d>
movslq %eax,%rsi
movslq %ebp,%rdx
lea    0xe10(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdd9(%rip),%rdi
mov    %rax,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
imul   $0x5f5e100,%ebx,%eax
mov    %eax,0x10(%rsp)
movl   $0x0,0x14(%rsp)
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1060 <nanosleep@plt>
mov    %ebx,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@.str = [12 x i8] c"%ld for %d\0A\00"
@.str.1 = [10 x i8] c"fork: %m\0A\00"
@.str.2 = [36 x i8] c"waitpid returned %ld, expected %ld\0A\00"
@.str.3 = [26 x i8] c"%ld with %d, expected %d\0A\00"
define i8* @thread_function(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timespec
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = ptrtoint i8* %9 to i64
%11 = trunc i64 %10 to i32
store i32 %11, i32* %4
%12 = call i32 (...) @fork()
store i32 %12, i32* %6
%13 = load i32, i32* %6
switch i32 %13, label %27 [
i32 0, label %14
i32 -1, label %25
]
14: ; preds = %1
%15 = call i64 (...) @getpid()
%16 = load i32, i32* %4
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i64 %15, i32 %16)
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
%19 = load i32, i32* %4
%20 = mul nsw i32 100000000, %19
store i32 %20, i32* %18
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 1
store i32 0, i32* %21
%22 = call i32 @nanosleep(%struct.timespec* %8, i32* null)
%23 = load i32, i32* %4
%24 = call i32 @_exit(i32 %23)
unreachable
25: ; preds = %1
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i8* inttoptr (i64 1 to i8*), i8** %2
br label %53
27: ; preds = %1
%28 = load i32, i32* %6
%29 = call i32 @waitpid(i32 %28, i32* %5, i32 0)
%30 = call i32 @TEMP_FAILURE_RETRY(i32 %29)
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = load i32, i32* %6
%33 = icmp ne i32 %31, %32
br i1 %33, label %34, label %40
34: ; preds = %27
%35 = load i32, i32* %7
%36 = sext i32 %35 to i64
%37 = load i32, i32* %6
%38 = sext i32 %37 to i64
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i64 %36, i64 %38)
store i8* inttoptr (i64 1 to i8*), i8** %2
br label %53
40: ; preds = %27
%41 = load i32, i32* %6
%42 = sext i32 %41 to i64
%43 = load i32, i32* %5
%44 = call i32 @WEXITSTATUS(i32 %43)
%45 = load i32, i32* %4
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i64 %42, i32 %44, i32 %45)
%47 = load i32, i32* %5
%48 = call i32 @WEXITSTATUS(i32 %47)
%49 = load i32, i32* %4
%50 = icmp eq i32 %48, %49
%51 = zext i1 %50 to i64
%52 = select i1 %50, i8* null, i8* inttoptr (i64 1 to i8*)
store i8* %52, i8** %2
br label %53
53: ; preds = %40, %34, %25
%54 = load i8*, i8** %2
ret i8* %54
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@.str = [12 x i8] c"%ld for %d\0A\00"
@.str.1 = [10 x i8] c"fork: %m\0A\00"
@.str.2 = [36 x i8] c"waitpid returned %ld, expected %ld\0A\00"
@.str.3 = [26 x i8] c"%ld with %d, expected %d\0A\00"
define i8* @thread_function(i8* %0) {
%2 = alloca i32
%3 = alloca %struct.timespec
%4 = ptrtoint i8* %0 to i64
%5 = trunc i64 %4 to i32
%6 = bitcast i32* %2 to i8*
%7 = tail call i32 (...) @fork()
switch i32 %7, label %18 [
i32 0, label %8
i32 -1, label %16
]
8: ; preds = %1
%9 = tail call i64 (...) @getpid()
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i64 %9, i32 %5)
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%12 = mul nsw i32 %5, 100000000
store i32 %12, i32* %11
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
store i32 0, i32* %13
%14 = call i32 @nanosleep(%struct.timespec* %3, i32* null)
%15 = call i32 @_exit(i32 %5)
unreachable
16: ; preds = %1
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %35
18: ; preds = %1
%19 = call i32 @waitpid(i32 %7, i32* %2, i32 0)
%20 = call i32 @TEMP_FAILURE_RETRY(i32 %19)
%21 = icmp eq i32 %20, %7
br i1 %21, label %26, label %22
22: ; preds = %18
%23 = sext i32 %20 to i64
%24 = sext i32 %7 to i64
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i64 %23, i64 %24)
br label %35
26: ; preds = %18
%27 = sext i32 %7 to i64
%28 = load i32, i32* %2
%29 = call i32 @WEXITSTATUS(i32 %28)
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i64 %27, i32 %29, i32 %5)
%31 = load i32, i32* %2
%32 = call i32 @WEXITSTATUS(i32 %31)
%33 = icmp eq i32 %32, %5
%34 = select i1 %33, i8* null, i8* inttoptr (i64 1 to i8*)
br label %35
35: ; preds = %26, %22, %16
%36 = phi i8* [ inttoptr (i64 1 to i8*), %22 ], [ %34, %26 ], [ inttoptr (i64 1 to i8*), %16 ]
ret i8* %36
}