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

/* Variables and functions */
int WEXITSTATUS(int);
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int waitpid(int, int *, int /*<<< orphan*/);
int main() {
  pid_t t;
  int status;
  switch (t = fork()) {
  case -1:
    perror("fork");
    _exit(1);
  case 0:
    fprintf(stderr, "child, my pid is %u\n", getpid());
    sleep(1);
    _exit(23);
  }
  printf("waitpid returned %u\n", waitpid(-1, &status, 0));
  printf("status was: %d\n", WEXITSTATUS(status));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x10(%rbp)
sub    $0xffffffff,%eax
je     11cc <main+0x3c>
jmp    11bc <main+0x2c>
mov    -0x10(%rbp),%eax
test   %eax,%eax
je     11e2 <main+0x52>
jmp    121a <main+0x8a>
lea    0xe2d(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x14(%rbp),%edi
mov    %eax,%edx
lea    0xe04(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    $0x17,%edi
call   1030 <_exit@plt>
mov    $0xffffffff,%edi
lea    -0xc(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    %eax,%esi
lea    0xde7(%rip),%rdi
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xde6(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     11e3 <main+0x53>
cmp    $0xffffffff,%eax
je     1217 <main+0x87>
lea    0xc(%rsp),%rsi
mov    $0xffffffff,%edi
xor    %edx,%edx
call   1080 <waitpid@plt>
lea    0xe5d(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    0xc(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
lea    0xe5b(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    0x2df6(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe0b(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    $0x17,%edi
call   1030 <_exit@plt>
lea    0xde2(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"child, my pid is %u\0A\00"
@.str.2 = [21 x i8] c"waitpid returned %u\0A\00"
@.str.3 = [16 x i8] c"status was: %d\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
switch i32 %4, label %14 [
i32 -1, label %5
i32 0, label %8
]
5: ; preds = %0
%6 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @_exit(i32 1)
unreachable
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = call i32 (...) @getpid()
%11 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = call i32 @sleep(i32 1)
%13 = call i32 @_exit(i32 23)
unreachable
14: ; preds = %0
%15 = call i32 @waitpid(i32 -1, i32* %3, i32 0)
%16 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = load i32, i32* %3
%18 = call i32 @WEXITSTATUS(i32 %17)
%19 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %18)
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"child, my pid is %u\0A\00"
@.str.2 = [21 x i8] c"waitpid returned %u\0A\00"
@.str.3 = [16 x i8] c"status was: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (...) @fork()
switch i32 %3, label %13 [
i32 -1, label %4
i32 0, label %7
]
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @_exit(i32 1)
unreachable
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = tail call i32 (...) @getpid()
%10 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = tail call i32 @sleep(i32 1)
%12 = tail call i32 @_exit(i32 23)
unreachable
13: ; preds = %0
%14 = call i32 @waitpid(i32 -1, i32* %1, i32 0)
%15 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%16 = load i32, i32* %1
%17 = call i32 @WEXITSTATUS(i32 %16)
%18 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %17)
ret i32 0
}