#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 /*<<< orphan*/ WUNTRACED;
int /*<<< orphan*/ execv(char *, char *const *);
int /*<<< orphan*/ exit(int);
int fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(void) {
  int pid;
  char *const args[] = {"ps", "aux", NULL};
  pid = fork();
  if (pid < 0) {
    printf("Can't fork\n");
    exit(1);
  }
  if (!pid) {
    execv("/bin/ps", args);
  } else {
    waitpid(pid, NULL, WUNTRACED);
    printf("I am parent, pid %d ", getpid());
    printf("Child %d\n", pid);
    exit(0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2c8a(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2c87(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0x2c84(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11bc <main+0x5c>
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    11db <main+0x7b>
lea    -0x20(%rbp),%rsi
lea    0xe3f(%rip),%rdi
call   1070 <execv@plt>
jmp    121f <main+0xbf>
mov    -0x8(%rbp),%edi
mov    0x2dfb(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1050 <waitpid@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%esi
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
mov    0x2c94(%rip),%rax
mov    %rax,0x10(%rsp)
movaps 0x2c78(%rip),%xmm0
movaps %xmm0,(%rsp)
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
js     11b0 <main+0x40>
jne    11c6 <main+0x56>
lea    0xe64(%rip),%rdi
mov    %rsp,%rsi
call   1080 <execv@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe74(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    %eax,%ebx
mov    0x2e11(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <waitpid@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe24(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe29(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"ps\00"
@.str.1 = [4 x i8] c"aux\00"
@__const.main.args = [3 x i8*] [i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i32 0, i32 0), i8* null]
@.str.2 = [12 x i8] c"Can't fork\0A\00"
@.str.3 = [8 x i8] c"/bin/ps\00"
@WUNTRACED = global i32 0
@.str.4 = [21 x i8] c"I am parent, pid %d \00"
@.str.5 = [10 x i8] c"Child %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [3 x i8*]
store i32 0, i32* %1
%4 = bitcast [3 x i8*]* %3 to i8*
%5 = call i32 (...) @fork()
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = load i32, i32* %2
%13 = icmp ne i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 0
%16 = call i32 @execv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8** %15)
br label %26
17: ; preds = %11
%18 = load i32, i32* %2
%19 = load i32, i32* @WUNTRACED
%20 = call i32 @waitpid(i32 %18, i32* null, i32 %19)
%21 = call i32 (...) @getpid()
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %21)
%23 = load i32, i32* %2
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %23)
%25 = call i32 @exit(i32 0)
unreachable
26: ; preds = %14
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"ps\00"
@.str.1 = [4 x i8] c"aux\00"
@__const.main.args = [3 x i8*] [i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i32 0, i32 0), i8* null]
@.str.3 = [8 x i8] c"/bin/ps\00"
@WUNTRACED = global i32 0
@.str.4 = [21 x i8] c"I am parent, pid %d \00"
@.str.5 = [10 x i8] c"Child %d\0A\00"
@str = [11 x i8] c"Can't fork\00"
define i32 @main() {
%1 = alloca [3 x i8*]
%2 = bitcast [3 x i8*]* %1 to i8*
%3 = tail call i32 (...) @fork()
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = icmp eq i32 %3, 0
br i1 %9, label %10, label %13
10: ; preds = %8
%11 = getelementptr inbounds [3 x i8*], [3 x i8*]* %1, i64 0, i64 0
%12 = call i32 @execv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8** %11)
ret i32 0
13: ; preds = %8
%14 = load i32, i32* @WUNTRACED
%15 = tail call i32 @waitpid(i32 %3, i32* null, i32 %14)
%16 = tail call i32 (...) @getpid()
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %16)
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %3)
%19 = tail call i32 @exit(i32 0)
unreachable
}