#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 WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int /*<<< orphan*/ WIFSIGNALED(int);
int WTERMSIG(int);
int execv(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int wait(int *);
int main(int argc, char *argv[]) {
  char *args[] = {
      "child",
      NULL,
  };
  int status;
  pid_t pid;
  if (argc != 2) {
    fprintf(stderr, "Usage: father <path to child program>\n");
    return 1;
  }
  printf("I am the High-Father\n");
  pid = fork();
  switch (pid) {
  case 0:
    if (execv(argv[1], args) == -1) {
      perror("Failed to execute child");
      exit(255);
    }
    break;
  case -1:
    perror("Error trying to fork");
    return 2;
    break;
  default:
    if (wait(&status) == -1) {
      perror("Error waiting for child");
      return 3;
    }
    if (WIFEXITED(status)) {
      printf("Child exited with code %d\n", WEXITSTATUS(status));
    }
    if (WIFSIGNALED(status)) {
      printf("Child terminated by signal %d\n", WTERMSIG(status));
    }
    break;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2c33(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2c30(%rip),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1207 <main+0x57>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    0xe0a(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    131c <main+0x16c>
lea    0xe19(%rip),%rdi
xor    %eax,%eax
mov    %al,-0x2d(%rbp)
call   1030 <printf@plt>
mov    -0x2d(%rbp),%al
call   10d0 <fork@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x2c(%rbp)
sub    $0xffffffff,%eax
je     127c <main+0xcc>
jmp    1237 <main+0x87>
mov    -0x2c(%rbp),%eax
test   %eax,%eax
jne    1294 <main+0xe4>
jmp    1247 <main+0x97>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x20(%rbp),%rsi
call   10c0 <execv@plt>
cmp    $0xffffffff,%eax
jne    1277 <main+0xc7>
lea    0xdd5(%rip),%rdi
call   1080 <perror@plt>
mov    $0xff,%edi
call   10a0 <exit@plt>
jmp    1315 <main+0x165>
lea    0xdd2(%rip),%rdi
call   1080 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    131c <main+0x16c>
lea    -0x24(%rbp),%rdi
call   10b0 <wait@plt>
cmp    $0xffffffff,%eax
jne    12be <main+0x10e>
lea    0xdbd(%rip),%rdi
call   1080 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    131c <main+0x16c>
mov    -0x24(%rbp),%edi
call   1090 <WIFEXITED@plt>
cmp    $0x0,%eax
je     12e7 <main+0x137>
mov    -0x24(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xda2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x24(%rbp),%edi
call   1040 <WIFSIGNALED@plt>
cmp    $0x0,%eax
je     1310 <main+0x160>
mov    -0x24(%rbp),%edi
call   1060 <WTERMSIG@plt>
mov    %eax,%esi
lea    0xd94(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1315 <main+0x165>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x28,%rsp
movaps 0x2c32(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
cmp    $0x2,%edi
jne    1268 <main+0xa8>
mov    %rsi,%r14
lea    0xec0(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
je     1284 <main+0xc4>
cmp    $0xffffffff,%eax
je     12b9 <main+0xf9>
lea    0xc(%rsp),%rdi
call   10c0 <wait@plt>
cmp    $0xffffffff,%eax
je     12cc <main+0x10c>
mov    0xc(%rsp),%edi
call   10a0 <WIFEXITED@plt>
test   %eax,%eax
je     123e <main+0x7e>
mov    0xc(%rsp),%edi
call   1080 <WEXITSTATUS@plt>
lea    0xe37(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rsp),%edi
call   1050 <WIFSIGNALED@plt>
test   %eax,%eax
je     12ad <main+0xed>
mov    0xc(%rsp),%edi
call   1070 <WTERMSIG@plt>
lea    0xe2c(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    12af <main+0xef>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
lea    0xd88(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%ebx
jmp    12af <main+0xef>
mov    0x8(%r14),%rdi
lea    0x10(%rsp),%rsi
call   10d0 <execv@plt>
cmp    $0xffffffff,%eax
jne    12af <main+0xef>
lea    0xd89(%rip),%rdi
call   1090 <perror@plt>
mov    $0xff,%edi
call   10b0 <exit@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd7f(%rip),%rdi
call   1090 <perror@plt>
mov    $0x2,%ebx
jmp    12af <main+0xef>
lea    0xd81(%rip),%rdi
call   1090 <perror@plt>
mov    $0x3,%ebx
jmp    12af <main+0xef>

LLVM IR (-O0):
@.str = [6 x i8] c"child\00"
@__const.main.args = [2 x i8*] [i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i32 0, i32 0), i8* null]
@stderr = global i32 0
@.str.1 = [39 x i8] c"Usage: father <path to child program>\0A\00"
@.str.2 = [22 x i8] c"I am the High-Father\0A\00"
@.str.3 = [24 x i8] c"Failed to execute child\00"
@.str.4 = [21 x i8] c"Error trying to fork\00"
@.str.5 = [24 x i8] c"Error waiting for child\00"
@.str.6 = [27 x i8] c"Child exited with code %d\0A\00"
@.str.7 = [31 x i8] c"Child terminated by signal %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i8*]
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = bitcast [2 x i8*]* %6 to i8*
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %55
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 (...) @fork()
store i32 %17, i32* %8
%18 = load i32, i32* %8
switch i32 %18, label %32 [
i32 0, label %19
i32 -1, label %30
]
19: ; preds = %15
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = getelementptr inbounds [2 x i8*], [2 x i8*]* %6, i64 0, i64 0
%24 = call i32 @execv(i8* %22, i8** %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %19
%27 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @exit(i32 255)
unreachable
29: ; preds = %19
br label %54
30: ; preds = %15
%31 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
store i32 2, i32* %3
br label %55
32: ; preds = %15
%33 = call i32 @wait(i32* %7)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
store i32 3, i32* %3
br label %55
37: ; preds = %32
%38 = load i32, i32* %7
%39 = call i32 @WIFEXITED(i32 %38)
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %37
%42 = load i32, i32* %7
%43 = call i32 @WEXITSTATUS(i32 %42)
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i32 %43)
br label %45
45: ; preds = %41, %37
%46 = load i32, i32* %7
%47 = call i32 @WIFSIGNALED(i32 %46)
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %45
%50 = load i32, i32* %7
%51 = call i32 @WTERMSIG(i32 %50)
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0), i32 %51)
br label %53
53: ; preds = %49, %45
br label %54
54: ; preds = %53, %29
store i32 0, i32* %3
br label %55
55: ; preds = %54, %35, %30, %12
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
@.str = [6 x i8] c"child\00"
@__const.main.args = [2 x i8*] [i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i32 0, i32 0), i8* null]
@stderr = global i32 0
@.str.1 = [39 x i8] c"Usage: father <path to child program>\0A\00"
@.str.3 = [24 x i8] c"Failed to execute child\00"
@.str.4 = [21 x i8] c"Error trying to fork\00"
@.str.5 = [24 x i8] c"Error waiting for child\00"
@.str.6 = [27 x i8] c"Child exited with code %d\0A\00"
@.str.7 = [31 x i8] c"Child terminated by signal %d\0A\00"
@str = [21 x i8] c"I am the High-Father\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x i8*]
%4 = alloca i32
%5 = bitcast [2 x i8*]* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = icmp eq i32 %0, 2
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %46
11: ; preds = %2
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%13 = tail call i32 (...) @fork()
switch i32 %13, label %25 [
i32 0, label %14
i32 -1, label %23
]
14: ; preds = %11
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = getelementptr inbounds [2 x i8*], [2 x i8*]* %3, i64 0, i64 0
%18 = call i32 @execv(i8* %16, i8** %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %46
20: ; preds = %14
%21 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%22 = call i32 @exit(i32 255)
unreachable
23: ; preds = %11
%24 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %46
25: ; preds = %11
%26 = call i32 @wait(i32* %4)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
br label %46
30: ; preds = %25
%31 = load i32, i32* %4
%32 = call i32 @WIFEXITED(i32 %31)
%33 = icmp eq i32 %32, 0
br i1 %33, label %38, label %34
34: ; preds = %30
%35 = load i32, i32* %4
%36 = call i32 @WEXITSTATUS(i32 %35)
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %34, %30
%39 = load i32, i32* %4
%40 = call i32 @WIFSIGNALED(i32 %39)
%41 = icmp eq i32 %40, 0
br i1 %41, label %46, label %42
42: ; preds = %38
%43 = load i32, i32* %4
%44 = call i32 @WTERMSIG(i32 %43)
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0), i32 %44)
br label %46
46: ; preds = %14, %42, %38, %28, %23, %8
%47 = phi i32 [ 1, %8 ], [ 3, %28 ], [ 2, %23 ], [ 0, %38 ], [ 0, %42 ], [ 0, %14 ]
ret i32 %47
}