#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 /*<<< orphan*/ execv(char *, char **);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ sleep(int);
int main(int argc, char *argv[]) {
  pid_t pid = fork();
  if (pid < 0) {
    perror("fork error");
    return 1;
  }
  int i;
  for (i = 0; i < argc; ++i) {
    printf("%d: %s\n", i, argv[i]);
  }
  if (pid == 0) {
    sleep(2);
    printf("child here: %d!\n", getpid());
  } else {
    execv("/bin/ls", argv);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11a4 <main+0x44>
lea    0xe6d(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    122a <main+0xca>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11e2 <main+0x82>
mov    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11ab <main+0x4b>
cmpq   $0x0,-0x18(%rbp)
jne    1213 <main+0xb3>
mov    $0x2,%edi
call   1060 <sleep@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1223 <main+0xc3>
mov    -0x10(%rbp),%rsi
lea    0xe06(%rip),%rdi
call   1070 <execv@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r12
mov    %edi,%ebx
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11f0 <main+0x90>
mov    %rax,%r14
test   %ebx,%ebx
jle    11a9 <main+0x49>
mov    %ebx,%r13d
lea    0xe80(%rip),%r15
xor    %ebx,%ebx
nopl   (%rax)
mov    (%r12,%rbx,8),%rdx
mov    %r15,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1190 <main+0x30>
test   %r14,%r14
je     11c1 <main+0x61>
lea    0xe6f(%rip),%rdi
mov    %r12,%rsi
call   1070 <execv@plt>
xor    %ebx,%ebx
jmp    11e4 <main+0x84>
mov    $0x2,%edi
call   1060 <sleep@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe38(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xe09(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%ebx
jmp    11e4 <main+0x84>

LLVM IR (-O0):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [8 x i8] c"%d: %s\0A\00"
@.str.2 = [17 x i8] c"child here: %d!\0A\00"
@.str.3 = [8 x i8] c"/bin/ls\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i64 (...) @fork()
store i64 %8, i64* %6
%9 = load i64, i64* %6
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %40
13: ; preds = %2
store i32 0, i32* %7
br label %14
14: ; preds = %26, %13
%15 = load i32, i32* %7
%16 = load i32, i32* %4
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %29
18: ; preds = %14
%19 = load i32, i32* %7
%20 = load i8**, i8*** %5
%21 = load i32, i32* %7
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8*, i8** %20, i64 %22
%24 = load i8*, i8** %23
%25 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %19, i8* %24)
br label %26
26: ; preds = %18
%27 = load i32, i32* %7
%28 = add nsw i32 %27, 1
store i32 %28, i32* %7
br label %14
29: ; preds = %14
%30 = load i64, i64* %6
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %29
%33 = call i32 @sleep(i32 2)
%34 = call i32 (...) @getpid()
%35 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %34)
br label %39
36: ; preds = %29
%37 = load i8**, i8*** %5
%38 = call i32 @execv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8** %37)
br label %39
39: ; preds = %36, %32
store i32 0, i32* %3
br label %40
40: ; preds = %39, %11
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [8 x i8] c"%d: %s\0A\00"
@.str.2 = [17 x i8] c"child here: %d!\0A\00"
@.str.3 = [8 x i8] c"/bin/ls\00"
define i32 @main(i32 %0, i8** %1) {
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %9, label %5
5: ; preds = %2
%6 = icmp sgt i32 %0, 0
br i1 %6, label %7, label %19
7: ; preds = %5
%8 = zext i32 %0 to i64
br label %11
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %27
11: ; preds = %7, %11
%12 = phi i64 [ 0, %7 ], [ %17, %11 ]
%13 = getelementptr inbounds i8*, i8** %1, i64 %12
%14 = load i8*, i8** %13
%15 = trunc i64 %12 to i32
%16 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %15, i8* %14)
%17 = add nuw nsw i64 %12, 1
%18 = icmp eq i64 %17, %8
br i1 %18, label %19, label %11
19: ; preds = %11, %5
%20 = icmp eq i64 %3, 0
br i1 %20, label %21, label %25
21: ; preds = %19
%22 = tail call i32 @sleep(i32 2)
%23 = tail call i32 (...) @getpid()
%24 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %23)
br label %27
25: ; preds = %19
%26 = tail call i32 @execv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8** %1)
br label %27
27: ; preds = %21, %25, %9
%28 = phi i32 [ 1, %9 ], [ 0, %25 ], [ 0, %21 ]
ret i32 %28
}