#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ execl(char *, char *, char *, char *, char *);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int const);
int waitpid(int, int *, int /*<<< orphan*/);
int main(void) {
  pid_t p = fork();
  if (p == -1) {
    perror("fork failed");
    return EXIT_FAILURE;
  } else if (p == 0) {
    execl("/bin/sh", "bin/sh", "-c", "./failing", "NULL");
    return EXIT_FAILURE;
  }
  int status;
  if (waitpid(p, &status, 0) == -1) {
    perror("waitpid failed");
    return EXIT_FAILURE;
  }
  if (WIFEXITED(status)) {
    const int es = WEXITSTATUS(status);
    printf("exit status was %d\n", es);
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11b0 <main+0x40>
lea    0xe66(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1264 <main+0xf4>
cmpl   $0x0,-0x8(%rbp)
jne    11f3 <main+0x83>
lea    0xe4b(%rip),%rdi
lea    0xe45(%rip),%rsi
lea    0xe45(%rip),%rdx
lea    0xe41(%rip),%rcx
lea    0xe44(%rip),%r8
call   1080 <execl@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1264 <main+0xf4>
jmp    11f8 <main+0x88>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    122c <main+0xbc>
lea    0xe10(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1264 <main+0xf4>
mov    -0xc(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1258 <main+0xe8>
mov    -0xc(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%esi
lea    0xde2(%rip),%rdi
call   1030 <printf@plt>
mov    0x2d79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     11c8 <main+0x58>
cmp    $0xffffffff,%eax
je     11ff <main+0x8f>
lea    0xc(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    $0xffffffff,%eax
je     120d <main+0x9d>
mov    0xc(%rsp),%edi
call   1070 <WIFEXITED@plt>
mov    0x2e30(%rip),%rbx
test   %rax,%rax
je     11c4 <main+0x54>
mov    0xc(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
lea    0xe78(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    (%rbx),%eax
jmp    11f9 <main+0x89>
lea    0xe3d(%rip),%rdi
lea    0xe37(%rip),%rsi
lea    0xe37(%rip),%rdx
lea    0xe33(%rip),%rcx
lea    0xe36(%rip),%r8
call   1080 <execl@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xdfa(%rip),%rdi
call   1060 <perror@plt>
jmp    11f0 <main+0x80>
lea    0xe12(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dc0(%rip),%rbx
jmp    11c4 <main+0x54>

LLVM IR (-O0):
@.str = [12 x i8] c"fork failed\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [8 x i8] c"/bin/sh\00"
@.str.2 = [7 x i8] c"bin/sh\00"
@.str.3 = [3 x i8] c"-c\00"
@.str.4 = [10 x i8] c"./failing\00"
@.str.5 = [5 x i8] c"NULL\00"
@.str.6 = [15 x i8] c"waitpid failed\00"
@.str.7 = [20 x i8] c"exit status was %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 (...) @fork()
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
store i32 %10, i32* %1
br label %36
11: ; preds = %0
%12 = load i32, i32* %2
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
store i32 %16, i32* %1
br label %36
17: ; preds = %11
br label %18
18: ; preds = %17
%19 = load i32, i32* %2
%20 = call i32 @waitpid(i32 %19, i32* %3, i32 0)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
store i32 %24, i32* %1
br label %36
25: ; preds = %18
%26 = load i32, i32* %3
%27 = call i64 @WIFEXITED(i32 %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %25
%30 = load i32, i32* %3
%31 = call i32 @WEXITSTATUS(i32 %30)
store i32 %31, i32* %4
%32 = load i32, i32* %4
%33 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0), i32 %32)
br label %34
34: ; preds = %29, %25
%35 = load i32, i32* @EXIT_SUCCESS
store i32 %35, i32* %1
br label %36
36: ; preds = %34, %22, %14, %8
%37 = load i32, i32* %1
ret i32 %37
}

LLVM IR (-O2):
@.str = [12 x i8] c"fork failed\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [8 x i8] c"/bin/sh\00"
@.str.2 = [7 x i8] c"bin/sh\00"
@.str.3 = [3 x i8] c"-c\00"
@.str.4 = [10 x i8] c"./failing\00"
@.str.5 = [5 x i8] c"NULL\00"
@.str.6 = [15 x i8] c"waitpid failed\00"
@.str.7 = [20 x i8] c"exit status was %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = tail call i32 (...) @fork()
switch i32 %2, label %9 [
i32 -1, label %3
i32 0, label %6
]
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @EXIT_FAILURE
br label %26
6: ; preds = %0
%7 = tail call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
br label %26
9: ; preds = %0
%10 = bitcast i32* %1 to i8*
%11 = call i32 @waitpid(i32 %2, i32* %1, i32 0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
br label %23
15: ; preds = %9
%16 = load i32, i32* %1
%17 = call i64 @WIFEXITED(i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %15
%20 = load i32, i32* %1
%21 = call i32 @WEXITSTATUS(i32 %20)
%22 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %15, %19, %13
%24 = phi i32* [ @EXIT_FAILURE, %13 ], [ @EXIT_SUCCESS, %19 ], [ @EXIT_SUCCESS, %15 ]
%25 = load i32, i32* %24
br label %26
26: ; preds = %23, %6, %3
%27 = phi i32 [ %5, %3 ], [ %8, %6 ], [ %25, %23 ]
ret i32 %27
}