#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 BUFSIZ;
int EXIT_SUCCESS;
int WEXITSTATUS(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ logInfo(char *, char *);
int /*<<< orphan*/ pexit(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int wait(int *);
int main() {
  int status;
  char buf[BUFSIZ];
  logInfo("Parent", "creating child");
  switch (fork()) {
  case -1:
    pexit("fork");
    break;
  case 0:
    logInfo("Child", "invoking exit(-1)");
    exit(-1);
    break;
  default:
    if (wait(&status) == -1) {
      pexit("wait");
    }
    snprintf(buf, BUFSIZ, "child has exited with status %d",
             WEXITSTATUS(status));
    logInfo("Parent", buf);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
lea    0xe4e(%rip),%rdi
lea    0xe4e(%rip),%rsi
call   1040 <logInfo@plt>
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x1c(%rbp)
sub    $0xffffffff,%eax
je     11e8 <main+0x78>
jmp    11d8 <main+0x68>
mov    -0x1c(%rbp),%eax
test   %eax,%eax
je     11f9 <main+0x89>
jmp    1216 <main+0xa6>
lea    0xe27(%rip),%rdi
call   1070 <pexit@plt>
jmp    126d <main+0xfd>
lea    0xe1b(%rip),%rdi
lea    0xe1a(%rip),%rsi
call   1040 <logInfo@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
lea    -0x8(%rbp),%rdi
call   1080 <wait@plt>
cmp    $0xffffffff,%eax
jne    1234 <main+0xc4>
lea    0xe04(%rip),%rdi
call   1070 <pexit@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    -0x2c(%rbp),%esi
mov    -0x28(%rbp),%rdi
mov    %eax,%ecx
lea    0xde0(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x28(%rbp),%rsi
lea    0xd98(%rip),%rdi
call   1040 <logInfo@plt>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e5e(%rip),%r14
mov    (%r14),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe63(%rip),%rdi
lea    0xe63(%rip),%rsi
call   1040 <logInfo@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     11c7 <main+0x57>
cmp    $0xffffffff,%eax
jne    11e4 <main+0x74>
lea    0xe56(%rip),%rdi
call   1070 <pexit@plt>
jmp    122c <main+0xbc>
lea    0xe4d(%rip),%rdi
lea    0xe4c(%rip),%rsi
call   1040 <logInfo@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
lea    -0x14(%rbp),%rdi
call   1080 <wait@plt>
cmp    $0xffffffff,%eax
jne    11fe <main+0x8e>
lea    0xe3a(%rip),%rdi
call   1070 <pexit@plt>
mov    (%r14),%r14d
mov    -0x14(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
lea    0xe28(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %eax,%ecx
call   1030 <snprintf@plt>
lea    0xddc(%rip),%rdi
mov    %rbx,%rsi
call   1040 <logInfo@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [7 x i8] c"Parent\00"
@.str.1 = [15 x i8] c"creating child\00"
@.str.2 = [5 x i8] c"fork\00"
@.str.3 = [6 x i8] c"Child\00"
@.str.4 = [18 x i8] c"invoking exit(-1)\00"
@.str.5 = [5 x i8] c"wait\00"
@.str.6 = [32 x i8] c"child has exited with status %d\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
store i32 0, i32* %1
%5 = load i32, i32* @BUFSIZ
%6 = zext i32 %5 to i64
%7 = call i8* @llvm.stacksave()
store i8* %7, i8** %3
%8 = alloca i8, i64 %6
store i64 %6, i64* %4
%9 = call i32 @logInfo(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 (...) @fork()
switch i32 %10, label %16 [
i32 -1, label %11
i32 0, label %13
]
11: ; preds = %0
%12 = call i32 @pexit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %27
13: ; preds = %0
%14 = call i32 @logInfo(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %0
%17 = call i32 @wait(i32* %2)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @pexit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
%22 = load i32, i32* @BUFSIZ
%23 = load i32, i32* %2
%24 = call i32 @WEXITSTATUS(i32 %23)
%25 = call i32 @snprintf(i8* %8, i32 %22, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0), i32 %24)
%26 = call i32 @logInfo(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %27
27: ; preds = %21, %11
%28 = load i32, i32* @EXIT_SUCCESS
%29 = call i32 @exit(i32 %28)
unreachable
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [7 x i8] c"Parent\00"
@.str.1 = [15 x i8] c"creating child\00"
@.str.2 = [5 x i8] c"fork\00"
@.str.3 = [6 x i8] c"Child\00"
@.str.4 = [18 x i8] c"invoking exit(-1)\00"
@.str.5 = [5 x i8] c"wait\00"
@.str.6 = [32 x i8] c"child has exited with status %d\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = load i32, i32* @BUFSIZ
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = tail call i32 @logInfo(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%7 = tail call i32 (...) @fork()
switch i32 %7, label %13 [
i32 -1, label %8
i32 0, label %10
]
8: ; preds = %0
%9 = tail call i32 @pexit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %24
10: ; preds = %0
%11 = tail call i32 @logInfo(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %0
%14 = call i32 @wait(i32* %1)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @pexit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %18
18: ; preds = %16, %13
%19 = load i32, i32* @BUFSIZ
%20 = load i32, i32* %1
%21 = call i32 @WEXITSTATUS(i32 %20)
%22 = call i32 @snprintf(i8* %5, i32 %19, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0), i32 %21)
%23 = call i32 @logInfo(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %24
24: ; preds = %18, %8
%25 = load i32, i32* @EXIT_SUCCESS
%26 = call i32 @exit(i32 %25)
unreachable
}