#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 /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stderr;
int waitpid(int, int *, int /*<<< orphan*/);
int rebuild() {
  pid_t pid;
  pid = fork();
  if (pid == -1) {
    fprintf(stderr, "Failed to rebuild!\n");
    perror("fork");
    return 1;
  } else if (pid == 0) {
    printf("[%d] -> In child process!\n", getpid());
    char *gccargs[] = {"make", 0};
    execvp(gccargs[0], gccargs);
    perror("ERROR: Failed to run rebuild command");
    exit(1);
  } else {
    printf("-> The child has a pid of %d\n", pid);
    int status = 0;
    pid = waitpid(pid, &status, 0);
    if (pid < 0)
      perror("waitpid");
    if (status != 0) {
      fprintf(stderr, "=> An error has occured during the build\n");
      return 1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<rebuild>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11c9 <rebuild+0x49>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
lea    0xe54(%rip),%rsi
call   1050 <fprintf@plt>
lea    0xe5c(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    129a <rebuild+0x11a>
cmpl   $0x0,-0x8(%rbp)
jne    1221 <rebuild+0xa1>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe36(%rip),%rdi
call   1040 <printf@plt>
mov    0x2c11(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2c0e(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <execvp@plt>
lea    0xe27(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%esi
lea    0xe33(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    125e <rebuild+0xde>
lea    0xe23(%rip),%rdi
call   1070 <perror@plt>
cmpl   $0x0,-0x24(%rbp)
je     1289 <rebuild+0x109>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
lea    0xe0c(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    129a <rebuild+0x11a>
jmp    128e <rebuild+0x10e>
jmp    1293 <rebuild+0x113>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rebuild>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   10a0 <fork@plt>
cmp    $0xffffffff,%eax
je     11e9 <rebuild+0x69>
mov    %eax,%ebx
test   %eax,%eax
je     121f <rebuild+0x9f>
lea    0xebc(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
movl   $0x0,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1060 <waitpid@plt>
test   %eax,%eax
js     1211 <rebuild+0x91>
mov    (%rsp),%ebx
test   %ebx,%ebx
je     11dc <rebuild+0x5c>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xead(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
test   %ebx,%ebx
setne  %al
add    $0x10,%rsp
pop    %rbx
ret
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    0xe07(%rip),%rsi
call   1050 <fprintf@plt>
lea    0xe0f(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%eax
jmp    11e3 <rebuild+0x63>
lea    0xe64(%rip),%rdi
call   1070 <perror@plt>
jmp    11c0 <rebuild+0x40>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xdec(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
movaps 0x2bc5(%rip),%xmm0
movaps %xmm0,(%rsp)
lea    0xdee(%rip),%rdi
mov    %rsp,%rsi
call   1080 <execvp@plt>
lea    0xde4(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [20 x i8] c"Failed to rebuild!\0A\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [27 x i8] c"[%d] -> In child process!\0A\00"
@.str.3 = [5 x i8] c"make\00"
@__const.rebuild.gccargs = [2 x i8*] [i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i32 0, i32 0), i8* null]
@.str.4 = [37 x i8] c"ERROR: Failed to run rebuild command\00"
@.str.5 = [30 x i8] c"-> The child has a pid of %d\0A\00"
@.str.6 = [8 x i8] c"waitpid\00"
@.str.7 = [42 x i8] c"=> An error has occured during the build\0A\00"
define i32 @rebuild() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [2 x i8*]
%4 = alloca i32
%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 %12
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %43
12: ; preds = %0
%13 = load i32, i32* %2
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %25
15: ; preds = %12
%16 = call i32 (...) @getpid()
%17 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = bitcast [2 x i8*]* %3 to i8*
%19 = getelementptr inbounds [2 x i8*], [2 x i8*]* %3, i64 0, i64 0
%20 = load i8*, i8** %19
%21 = getelementptr inbounds [2 x i8*], [2 x i8*]* %3, i64 0, i64 0
%22 = call i32 @execvp(i8* %20, i8** %21)
%23 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %12
%26 = load i32, i32* %2
%27 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %26)
store i32 0, i32* %4
%28 = load i32, i32* %2
%29 = call i32 @waitpid(i32 %28, i32* %4, i32 0)
store i32 %29, i32* %2
%30 = load i32, i32* %2
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %25
%33 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
br label %34
34: ; preds = %32, %25
%35 = load i32, i32* %4
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = load i32, i32* @stderr
%39 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %1
br label %43
40: ; preds = %34
br label %41
41: ; preds = %40
br label %42
42: ; preds = %41
store i32 0, i32* %1
br label %43
43: ; preds = %42, %37, %8
%44 = load i32, i32* %1
ret i32 %44
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [20 x i8] c"Failed to rebuild!\0A\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [27 x i8] c"[%d] -> In child process!\0A\00"
@.str.3 = [5 x i8] c"make\00"
@__const.rebuild.gccargs = [2 x i8*] [i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i32 0, i32 0), i8* null]
@.str.4 = [37 x i8] c"ERROR: Failed to run rebuild command\00"
@.str.5 = [30 x i8] c"-> The child has a pid of %d\0A\00"
@.str.6 = [8 x i8] c"waitpid\00"
@.str.7 = [42 x i8] c"=> An error has occured during the build\0A\00"
define i32 @rebuild() {
%1 = alloca [2 x i8*]
%2 = alloca i32
%3 = tail call i32 (...) @fork()
switch i32 %3, label %16 [
i32 -1, label %4
i32 0, label %8
]
4: ; preds = %0
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %32
8: ; preds = %0
%9 = tail call i32 (...) @getpid()
%10 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = bitcast [2 x i8*]* %1 to i8*
%12 = getelementptr inbounds [2 x i8*], [2 x i8*]* %1, i64 0, i64 0
%13 = call i32 @execvp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8** %12)
%14 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = tail call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %3)
%18 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%19 = call i32 @waitpid(i32 %3, i32* %2, i32 0)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
br label %23
23: ; preds = %21, %16
%24 = load i32, i32* %2
%25 = icmp eq i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %23
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.7, i64 0, i64 0))
br label %29
29: ; preds = %23, %26
%30 = xor i1 %25, true
%31 = zext i1 %30 to i32
br label %32
32: ; preds = %29, %4
%33 = phi i32 [ 1, %4 ], [ %31, %29 ]
ret i32 %33
}