#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 scalar_t__ pid_t;

/* Variables and functions */
int WEXITSTATUS(int);
int /*<<< orphan*/ execvp(char *, char **);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  char *arguments[10];
  pid_t child;
  arguments[0] = "./arg-printer";
  arguments[1] = "arguments";
  arguments[2] = "are";
  arguments[3] = "useful";
  arguments[4] = NULL;
  child = fork();
  if (0 == child) {
    execvp(arguments[0], arguments);
    printf("Something went wrong!\n");
  } else if (0 < child) {
    int status;
    waitpid(child, &status, 0);
    printf("Child process finished with return code %d\n", WEXITSTATUS(status));
  } else {
    perror("fork");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe83(%rip),%rax
mov    %rax,-0x60(%rbp)
lea    0xe86(%rip),%rax
mov    %rax,-0x58(%rbp)
lea    0xe85(%rip),%rax
mov    %rax,-0x50(%rbp)
lea    0xe7e(%rip),%rax
mov    %rax,-0x48(%rbp)
movq   $0x0,-0x40(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x68(%rbp)
xor    %eax,%eax
cmp    -0x68(%rbp),%rax
jne    11e1 <main+0x81>
mov    -0x60(%rbp),%rdi
lea    -0x60(%rbp),%rsi
call   1070 <execvp@plt>
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    122a <main+0xca>
xor    %eax,%eax
cmp    -0x68(%rbp),%rax
jge    1219 <main+0xb9>
mov    -0x68(%rbp),%rdi
lea    -0x6c(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    -0x6c(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1225 <main+0xc5>
lea    0xe46(%rip),%rdi
call   1060 <perror@plt>
jmp    122a <main+0xca>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x68,%rsp
lea    0xe85(%rip),%rax
mov    %rax,0x10(%rsp)
lea    0xe87(%rip),%rax
mov    %rax,0x18(%rsp)
lea    0xe85(%rip),%rax
mov    %rax,0x20(%rsp)
lea    0xe7d(%rip),%rax
mov    %rax,0x28(%rsp)
movq   $0x0,0x30(%rsp)
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     11e5 <main+0x75>
jle    1209 <main+0x99>
lea    0xc(%rsp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    0xc(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
lea    0xe49(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1202 <main+0x92>
lea    0xe14(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1080 <execvp@plt>
lea    0xe57(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x68,%rsp
ret
lea    0xe3f(%rip),%rdi
call   1070 <perror@plt>
jmp    1202 <main+0x92>

LLVM IR (-O0):
@.str = [14 x i8] c"./arg-printer\00"
@.str.1 = [10 x i8] c"arguments\00"
@.str.2 = [4 x i8] c"are\00"
@.str.3 = [7 x i8] c"useful\00"
@.str.4 = [23 x i8] c"Something went wrong!\0A\00"
@.str.5 = [44 x i8] c"Child process finished with return code %d\0A\00"
@.str.6 = [5 x i8] c"fork\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [10 x i8*]
%7 = alloca i64
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = getelementptr inbounds [10 x i8*], [10 x i8*]* %6, i64 0, i64 0
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8** %9
%10 = getelementptr inbounds [10 x i8*], [10 x i8*]* %6, i64 0, i64 1
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8** %10
%11 = getelementptr inbounds [10 x i8*], [10 x i8*]* %6, i64 0, i64 2
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %11
%12 = getelementptr inbounds [10 x i8*], [10 x i8*]* %6, i64 0, i64 3
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8** %12
%13 = getelementptr inbounds [10 x i8*], [10 x i8*]* %6, i64 0, i64 4
store i8* null, i8** %13
%14 = call i64 (...) @fork()
store i64 %14, i64* %7
%15 = load i64, i64* %7
%16 = icmp eq i64 0, %15
br i1 %16, label %17, label %23
17: ; preds = %2
%18 = getelementptr inbounds [10 x i8*], [10 x i8*]* %6, i64 0, i64 0
%19 = load i8*, i8** %18
%20 = getelementptr inbounds [10 x i8*], [10 x i8*]* %6, i64 0, i64 0
%21 = call i32 @execvp(i8* %19, i8** %20)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
br label %35
23: ; preds = %2
%24 = load i64, i64* %7
%25 = icmp slt i64 0, %24
br i1 %25, label %26, label %32
26: ; preds = %23
%27 = load i64, i64* %7
%28 = call i32 @waitpid(i64 %27, i32* %8, i32 0)
%29 = load i32, i32* %8
%30 = call i32 @WEXITSTATUS(i32 %29)
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0), i32 %30)
br label %34
32: ; preds = %23
%33 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
br label %34
34: ; preds = %32, %26
br label %35
35: ; preds = %34, %17
ret i32 0
}

LLVM IR (-O2):
@.str = [14 x i8] c"./arg-printer\00"
@.str.1 = [10 x i8] c"arguments\00"
@.str.2 = [4 x i8] c"are\00"
@.str.3 = [7 x i8] c"useful\00"
@.str.5 = [44 x i8] c"Child process finished with return code %d\0A\00"
@.str.6 = [5 x i8] c"fork\00"
@str = [22 x i8] c"Something went wrong!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [10 x i8*]
%4 = alloca i32
%5 = bitcast [10 x i8*]* %3 to i8*
%6 = getelementptr inbounds [10 x i8*], [10 x i8*]* %3, i64 0, i64 0
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8** %6
%7 = getelementptr inbounds [10 x i8*], [10 x i8*]* %3, i64 0, i64 1
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8** %7
%8 = getelementptr inbounds [10 x i8*], [10 x i8*]* %3, i64 0, i64 2
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %8
%9 = getelementptr inbounds [10 x i8*], [10 x i8*]* %3, i64 0, i64 3
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8** %9
%10 = getelementptr inbounds [10 x i8*], [10 x i8*]* %3, i64 0, i64 4
store i8* null, i8** %10
%11 = tail call i64 (...) @fork()
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @execvp(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8** %6)
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %26
16: ; preds = %2
%17 = icmp sgt i64 %11, 0
br i1 %17, label %18, label %24
18: ; preds = %16
%19 = bitcast i32* %4 to i8*
%20 = call i32 @waitpid(i64 %11, i32* %4, i32 0)
%21 = load i32, i32* %4
%22 = call i32 @WEXITSTATUS(i32 %21)
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0), i32 %22)
br label %26
24: ; preds = %16
%25 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
br label %26
26: ; preds = %18, %24, %13
ret i32 0
}