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

/* Variables and functions */
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ wait(int *);
int main() {
  if (!fork()) {
    printf("I am child!\n");
    exit(0);
  } else {
    int status;
    if (wait(&status) < 0) {
      perror("wait");
      exit(-1);
    }
    if (WIFEXITED(status))
      printf("WEXITSTATUS is %d\n", WEXITSTATUS(status));
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%eax
jne    11a4 <main+0x34>
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
lea    -0x8(%rbp),%rdi
call   1080 <wait@plt>
cmp    $0x0,%rax
jge    11cd <main+0x5d>
lea    0xe4f(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11f7 <main+0x87>
mov    -0x8(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11fc <main+0x8c>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11c6 <main+0x46>
lea    0x4(%rsp),%rdi
call   1090 <wait@plt>
test   %rax,%rax
js     11d9 <main+0x59>
mov    0x4(%rsp),%edi
call   1070 <WIFEXITED@plt>
test   %rax,%rax
je     11c2 <main+0x42>
mov    0x4(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
lea    0xe4c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe4b(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1080 <exit@plt>
lea    0xe20(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
@.str.1 = [5 x i8] c"wait\00"
@.str.2 = [19 x i8] c"WEXITSTATUS is %d\0A\00"
@str = [12 x i8] c"I am child!\00"
define i32 @main() {
%1 = alloca i32
%2 = tail call i32 (...) @fork()
%3 = icmp eq i32 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 0)
unreachable
7: ; preds = %0
%8 = bitcast i32* %1 to i8*
%9 = call i64 @wait(i32* %1)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %7
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 -1)
unreachable
14: ; preds = %7
%15 = load i32, i32* %1
%16 = call i64 @WIFEXITED(i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %14
%19 = load i32, i32* %1
%20 = call i32 @WEXITSTATUS(i32 %19)
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %18, %14
ret i32 0
}