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

/* Variables and functions */
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ waitall();
int main() {
  pid_t pid;
  int a_var;
  pid = fork();
  if (pid == 0) {
    a_var = 0;
    printf("%d: hello, I'm child\n", a_var);
  } else if (pid < 0) {
    perror("fork failed");
    return -1;
  } else {
    a_var = pid;
    waitall();
    printf("%d: hello, I'm papa\n", a_var);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    117e <main+0x3e>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe8c(%rip),%rdi
call   1030 <printf@plt>
jmp    11c1 <main+0x81>
cmpl   $0x0,-0x8(%rbp)
jge    11a0 <main+0x60>
lea    0xe87(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c8 <main+0x88>
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1040 <waitall@plt>
mov    -0xc(%rbp),%esi
lea    0xe6b(%rip),%rdi
call   1030 <printf@plt>
jmp    11c1 <main+0x81>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <fork@plt>
test   %eax,%eax
je     1168 <main+0x28>
js     117f <main+0x3f>
mov    %eax,%ebp
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <waitall@plt>
lea    0xebe(%rip),%rdi
mov    %ebp,%esi
jmp    1171 <main+0x31>
lea    0xe91(%rip),%rdi
xor    %esi,%esi
call   1030 <printf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe90(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1176 <main+0x36>

LLVM IR (-O0):
@.str = [22 x i8] c"%d: hello, I'm child\0A\00"
@.str.1 = [12 x i8] c"fork failed\00"
@.str.2 = [21 x i8] c"%d: hello, I'm papa\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32 (...) @fork()
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
store i32 0, i32* %3
%8 = load i32, i32* %3
%9 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %21
10: ; preds = %0
%11 = load i32, i32* %2
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %22
15: ; preds = %10
%16 = load i32, i32* %2
store i32 %16, i32* %3
%17 = call i32 (...) @waitall()
%18 = load i32, i32* %3
%19 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %18)
br label %20
20: ; preds = %15
br label %21
21: ; preds = %20, %7
store i32 0, i32* %1
br label %22
22: ; preds = %21, %13
%23 = load i32, i32* %1
ret i32 %23
}

LLVM IR (-O2):
@.str = [22 x i8] c"%d: hello, I'm child\0A\00"
@.str.1 = [12 x i8] c"fork failed\00"
@.str.2 = [21 x i8] c"%d: hello, I'm papa\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @fork()
%2 = icmp eq i32 %1, 0
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 0)
br label %12
5: ; preds = %0
%6 = icmp slt i32 %1, 0
br i1 %6, label %7, label %9
7: ; preds = %5
%8 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %12
9: ; preds = %5
%10 = tail call i32 (...) @waitall()
%11 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %1)
br label %12
12: ; preds = %3, %9, %7
%13 = phi i32 [ -1, %7 ], [ 0, %9 ], [ 0, %3 ]
ret i32 %13
}