#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 execl(char *, char *, int /*<<< orphan*/ *);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
void main() {
  pid_t pid;
  pid = fork();
  if (pid == -1) {
    perror("fork");
  }
  if (!pid) {
    int ret;
    ret = execl("/usr/bin/vi", "vi", NULL);
    if (ret == -1) {
      perror("execv");
    }
  }
  printf("process id is: %d\n", pid);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    1168 <main+0x28>
lea    0xe9d(%rip),%rdi
call   1040 <perror@plt>
cmpl   $0x0,-0x4(%rbp)
jne    11a7 <main+0x67>
lea    0xe8c(%rip),%rdi
lea    0xe8e(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <execl@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11a2 <main+0x62>
lea    0xe74(%rip),%rdi
call   1040 <perror@plt>
jmp    11a7 <main+0x67>
mov    -0x4(%rbp),%esi
lea    0xe66(%rip),%rdi
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1060 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
je     115c <main+0x1c>
cmp    $0xffffffff,%ebx
jne    1176 <main+0x36>
lea    0xea6(%rip),%rdi
jmp    118c <main+0x4c>
lea    0xea2(%rip),%rdi
lea    0xea4(%rip),%rsi
xor    %edx,%edx
call   1050 <execl@plt>
cmp    $0xffffffff,%eax
je     1185 <main+0x45>
lea    0xe9a(%rip),%rdi
mov    %ebx,%esi
pop    %rbx
jmp    1030 <printf@plt>
lea    0xe85(%rip),%rdi
call   1040 <perror@plt>
jmp    1176 <main+0x36>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@.str.1 = [12 x i8] c"/usr/bin/vi\00"
@.str.2 = [3 x i8] c"vi\00"
@.str.3 = [6 x i8] c"execv\00"
@.str.4 = [19 x i8] c"process id is: %d\0A\00"
define void @main() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 (...) @fork()
store i32 %3, i32* %1
%4 = load i32, i32* %1
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
%9 = load i32, i32* %1
%10 = icmp ne i32 %9, 0
br i1 %10, label %18, label %11
11: ; preds = %8
%12 = call i32 @execl(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* null)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %17
17: ; preds = %15, %11
br label %18
18: ; preds = %17, %8
%19 = load i32, i32* %1
%20 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %19)
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@.str.1 = [12 x i8] c"/usr/bin/vi\00"
@.str.2 = [3 x i8] c"vi\00"
@.str.3 = [6 x i8] c"execv\00"
@.str.4 = [19 x i8] c"process id is: %d\0A\00"
define void @main() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %8 [
i32 -1, label %5
i32 0, label %2
]
2: ; preds = %0
%3 = tail call i32 @execl(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* null)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %8
5: ; preds = %2, %0
%6 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %0 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), %2 ]
%7 = tail call i32 @perror(i8* %6)
br label %8
8: ; preds = %5, %2, %0
%9 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %1)
ret void
}