#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*/ EXIT_FAILURE;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ execv(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int main(void) {
  pid_t pid;
  char *args[] = {"/bin/cat", "a.c", NULL};
  pid = fork();
  if (pid == -1) {
    fprintf(stderr, "can't fork, error %d\n", errno);
    exit(EXIT_FAILURE);
  }
  if (pid == 0) {
    execv("/bin/cat", args);
    int j;
    for (j = 0; j < 10; j++) {
      printf("child: %d\n", j);
      sleep(1);
    }
    _exit(0);
  } else {
    int i;
    for (i = 0; i < 10; i++) {
      printf("parent: %d\n", i);
      sleep(1);
    }
    exit(0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2c6a(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2c67(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0x2c64(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11e0 <main+0x70>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
mov    0x2e14(%rip),%rax
mov    (%rax),%edx
lea    0xe40(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    1239 <main+0xc9>
lea    -0x20(%rbp),%rsi
lea    0xe0b(%rip),%rdi
call   1080 <execv@plt>
movl   $0x0,-0x24(%rbp)
cmpl   $0xa,-0x24(%rbp)
jge    1232 <main+0xc2>
mov    -0x24(%rbp),%esi
lea    0xe0e(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1201 <main+0x91>
xor    %edi,%edi
call   1030 <_exit@plt>
movl   $0x0,-0x28(%rbp)
cmpl   $0xa,-0x28(%rbp)
jge    1271 <main+0x101>
mov    -0x28(%rbp),%esi
lea    0xdda(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1240 <main+0xd0>
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
mov    0x2c84(%rip),%rax
mov    %rax,0x10(%rsp)
movaps 0x2c68(%rip),%xmm0
movaps %xmm0,(%rsp)
xor    %eax,%eax
call   1090 <fork@plt>
cmp    $0xffffffff,%eax
je     12a4 <main+0x134>
test   %eax,%eax
jne    12d0 <main+0x160>
lea    0xe55(%rip),%rdi
mov    %rsp,%rsi
call   1080 <execv@plt>
lea    0xe69(%rip),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x2,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x3,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x4,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x5,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x6,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x7,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x8,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x9,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
mov    0x2d24(%rip),%rax
mov    (%rax),%edx
lea    0xd50(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d07(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
lea    0xd57(%rip),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x2,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x3,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x4,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x5,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x6,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x7,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x8,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x9,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"/bin/cat\00"
@.str.1 = [4 x i8] c"a.c\00"
@__const.main.args = [3 x i8*] [i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i32 0, i32 0), i8* null]
@stderr = global i32 0
@.str.2 = [22 x i8] c"can't fork, error %d\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.3 = [11 x i8] c"child: %d\0A\00"
@.str.4 = [12 x i8] c"parent: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [3 x i8*]
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = bitcast [3 x i8*]* %3 to i8*
%7 = call i32 (...) @fork()
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %16
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %0
%17 = load i32, i32* %2
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %34
19: ; preds = %16
%20 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 0
%21 = call i32 @execv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %20)
store i32 0, i32* %4
br label %22
22: ; preds = %29, %19
%23 = load i32, i32* %4
%24 = icmp slt i32 %23, 10
br i1 %24, label %25, label %32
25: ; preds = %22
%26 = load i32, i32* %4
%27 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %26)
%28 = call i32 @sleep(i32 1)
br label %29
29: ; preds = %25
%30 = load i32, i32* %4
%31 = add nsw i32 %30, 1
store i32 %31, i32* %4
br label %22
32: ; preds = %22
%33 = call i32 @_exit(i32 0)
unreachable
34: ; preds = %16
store i32 0, i32* %5
br label %35
35: ; preds = %42, %34
%36 = load i32, i32* %5
%37 = icmp slt i32 %36, 10
br i1 %37, label %38, label %45
38: ; preds = %35
%39 = load i32, i32* %5
%40 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 %39)
%41 = call i32 @sleep(i32 1)
br label %42
42: ; preds = %38
%43 = load i32, i32* %5
%44 = add nsw i32 %43, 1
store i32 %44, i32* %5
br label %35
45: ; preds = %35
%46 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [9 x i8] c"/bin/cat\00"
@.str.1 = [4 x i8] c"a.c\00"
@__const.main.args = [3 x i8*] [i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i32 0, i32 0), i8* null]
@stderr = global i32 0
@.str.2 = [22 x i8] c"can't fork, error %d\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.3 = [11 x i8] c"child: %d\0A\00"
@.str.4 = [12 x i8] c"parent: %d\0A\00"
define i32 @main() {
%1 = alloca [3 x i8*]
%2 = bitcast [3 x i8*]* %1 to i8*
%3 = tail call i32 (...) @fork()
switch i32 %3, label %4 [
i32 -1, label %26
i32 0, label %32
]
4: ; preds = %0
%5 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 0)
%6 = tail call i32 @sleep(i32 1)
%7 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 1)
%8 = tail call i32 @sleep(i32 1)
%9 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 2)
%10 = tail call i32 @sleep(i32 1)
%11 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 3)
%12 = tail call i32 @sleep(i32 1)
%13 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 4)
%14 = tail call i32 @sleep(i32 1)
%15 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 5)
%16 = tail call i32 @sleep(i32 1)
%17 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 6)
%18 = tail call i32 @sleep(i32 1)
%19 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 7)
%20 = tail call i32 @sleep(i32 1)
%21 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 8)
%22 = tail call i32 @sleep(i32 1)
%23 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 9)
%24 = tail call i32 @sleep(i32 1)
%25 = tail call i32 @exit(i32 0)
unreachable
26: ; preds = %0
%27 = load i32, i32* @stderr
%28 = load i32, i32* @errno
%29 = tail call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%30 = load i32, i32* @EXIT_FAILURE
%31 = tail call i32 @exit(i32 %30)
unreachable
32: ; preds = %0
%33 = getelementptr inbounds [3 x i8*], [3 x i8*]* %1, i64 0, i64 0
%34 = call i32 @execv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %33)
%35 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 0)
%36 = call i32 @sleep(i32 1)
%37 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 1)
%38 = call i32 @sleep(i32 1)
%39 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 2)
%40 = call i32 @sleep(i32 1)
%41 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 3)
%42 = call i32 @sleep(i32 1)
%43 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 4)
%44 = call i32 @sleep(i32 1)
%45 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 5)
%46 = call i32 @sleep(i32 1)
%47 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 6)
%48 = call i32 @sleep(i32 1)
%49 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 7)
%50 = call i32 @sleep(i32 1)
%51 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 8)
%52 = call i32 @sleep(i32 1)
%53 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 9)
%54 = call i32 @sleep(i32 1)
%55 = call i32 @_exit(i32 0)
unreachable
}