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

/* Variables and functions */
int /*<<< orphan*/ call_foo(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ pthread_self();
int /*<<< orphan*/ sleep(int);
void *thr_fun(void *arg) {
  int n = (int)arg, i = 0;
  if (n == 0) {
    sleep(1);
    printf("[%lu %lu] before fork\n", getpid(), pthread_self());
    pid_t pid = fork();
    if (pid < 0) {
      exit(-1);
    } else if (pid == 0) {
      printf("[%lu %lu] after fork\n", getpid(), pthread_self());
    } else {
      printf("[%lu %lu] after fork\n", getpid(), pthread_self());
    }
  }
  {
    for (i = 0; i < 5; ++i) {
      call_foo(i);
    }
  }
  return NULL;
}


x86 assembly (clang, -O0):
<thr_fun>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    123e <thr_fun+0xce>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1060 <pthread_self@plt>
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
lea    0xe45(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11e0 <thr_fun+0x70>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1212 <thr_fun+0xa2>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1060 <pthread_self@plt>
mov    -0x20(%rbp),%esi
mov    %eax,%edx
lea    0xe0f(%rip),%rdi
call   1040 <printf@plt>
jmp    1234 <thr_fun+0xc4>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1060 <pthread_self@plt>
mov    -0x24(%rbp),%esi
mov    %eax,%edx
lea    0xde8(%rip),%rdi
call   1040 <printf@plt>
jmp    1239 <thr_fun+0xc9>
jmp    123e <thr_fun+0xce>
movl   $0x0,-0x10(%rbp)
cmpl   $0x5,-0x10(%rbp)
jge    1265 <thr_fun+0xf5>
mov    -0x10(%rbp),%edi
call   1050 <call_foo@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1245 <thr_fun+0xd5>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thr_fun>:
push   %rbx
test   %edi,%edi
jne    11cb <thr_fun+0x5b>
mov    $0x1,%edi
call   1080 <sleep@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <pthread_self@plt>
lea    0xe6a(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     11fe <thr_fun+0x8e>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <pthread_self@plt>
lea    0xe55(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %edi,%edi
call   1050 <call_foo@plt>
mov    $0x1,%edi
call   1050 <call_foo@plt>
mov    $0x2,%edi
call   1050 <call_foo@plt>
mov    $0x3,%edi
call   1050 <call_foo@plt>
mov    $0x4,%edi
call   1050 <call_foo@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [23 x i8] c"[%lu %lu] before fork\0A\00"
@.str.1 = [22 x i8] c"[%lu %lu] after fork\0A\00"
define i8* @thr_fun(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = ptrtoint i8* %6 to i32
store i32 %7, i32* %3
store i32 0, i32* %4
%8 = load i32, i32* %3
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %33
10: ; preds = %1
%11 = call i32 @sleep(i32 1)
%12 = call i32 (...) @getpid()
%13 = call i32 (...) @pthread_self()
%14 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %13)
%15 = call i64 (...) @fork()
store i64 %15, i64* %5
%16 = load i64, i64* %5
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %10
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %10
%21 = load i64, i64* %5
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = call i32 (...) @getpid()
%25 = call i32 (...) @pthread_self()
%26 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %24, i32 %25)
br label %31
27: ; preds = %20
%28 = call i32 (...) @getpid()
%29 = call i32 (...) @pthread_self()
%30 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %28, i32 %29)
br label %31
31: ; preds = %27, %23
br label %32
32: ; preds = %31
br label %33
33: ; preds = %32, %1
store i32 0, i32* %4
br label %34
34: ; preds = %40, %33
%35 = load i32, i32* %4
%36 = icmp slt i32 %35, 5
br i1 %36, label %37, label %43
37: ; preds = %34
%38 = load i32, i32* %4
%39 = call i32 @call_foo(i32 %38)
br label %40
40: ; preds = %37
%41 = load i32, i32* %4
%42 = add nsw i32 %41, 1
store i32 %42, i32* %4
br label %34
43: ; preds = %34
ret i8* null
}

LLVM IR (-O2):
@.str = [23 x i8] c"[%lu %lu] before fork\0A\00"
@.str.1 = [22 x i8] c"[%lu %lu] after fork\0A\00"
define noalias i8* @thr_fun(i8* %0) {
%2 = ptrtoint i8* %0 to i64
%3 = trunc i64 %2 to i32
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %18
5: ; preds = %1
%6 = tail call i32 @sleep(i32 1)
%7 = tail call i32 (...) @getpid()
%8 = tail call i32 (...) @pthread_self()
%9 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %8)
%10 = tail call i64 (...) @fork()
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %5
%13 = tail call i32 @exit(i32 -1)
unreachable
14: ; preds = %5
%15 = tail call i32 (...) @getpid()
%16 = tail call i32 (...) @pthread_self()
%17 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 %16)
br label %18
18: ; preds = %14, %1
%19 = tail call i32 @call_foo(i32 0)
%20 = tail call i32 @call_foo(i32 1)
%21 = tail call i32 @call_foo(i32 2)
%22 = tail call i32 @call_foo(i32 3)
%23 = tail call i32 @call_foo(i32 4)
ret i8* null
}