#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*/ execve(char *, char *const *, int /*<<< orphan*/ *);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *);
int main() {
  pid_t ret;
  printf("This is the main process\n");
  ret = fork();
  if (ret) {
    char *const agv[] = {"exec1", NULL};
    printf("In the child process\n");
    execve("exec1", agv, NULL);
    printf("child1 exec failed\n");
  } else {
    printf("In the parent process\n");
    execve("exec2", NULL, NULL);
    printf("parent exec failed\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeba(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1050 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11a8 <main+0x78>
mov    0x2ca8(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2ca5(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xea2(%rip),%rdi
call   1030 <printf@plt>
lea    -0x20(%rbp),%rsi
lea    0xe8c(%rip),%rdi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <execve@plt>
lea    0xe98(%rip),%rdi
call   1030 <printf@plt>
jmp    11d3 <main+0xa3>
lea    0xe9b(%rip),%rdi
call   1030 <printf@plt>
lea    0xea6(%rip),%rdi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1040 <execve@plt>
lea    0xe99(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x18,%rsp
lea    0xed1(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1050 <fork@plt>
test   %rax,%rax
je     117d <main+0x4d>
movaps 0x2cbd(%rip),%xmm0
movaps %xmm0,(%rsp)
lea    0xef0(%rip),%rdi
call   1030 <puts@plt>
lea    0xe96(%rip),%rdi
mov    %rsp,%rsi
xor    %edx,%edx
call   1040 <execve@plt>
lea    0xee8(%rip),%rdi
jmp    11a0 <main+0x70>
lea    0xea1(%rip),%rdi
call   1030 <puts@plt>
lea    0xe76(%rip),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <execve@plt>
lea    0xe9b(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"This is the main process\0A\00"
@.str.1 = [6 x i8] c"exec1\00"
@__const.main.agv = [2 x i8*] [i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i32 0, i32 0), i8* null]
@.str.2 = [22 x i8] c"In the child process\0A\00"
@.str.3 = [20 x i8] c"child1 exec failed\0A\00"
@.str.4 = [23 x i8] c"In the parent process\0A\00"
@.str.5 = [6 x i8] c"exec2\00"
@.str.6 = [20 x i8] c"parent exec failed\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca [2 x i8*]
store i32 0, i32* %1
%4 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%5 = call i64 (...) @fork()
store i64 %5, i64* %2
%6 = load i64, i64* %2
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %14
8: ; preds = %0
%9 = bitcast [2 x i8*]* %3 to i8*
%10 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%11 = getelementptr inbounds [2 x i8*], [2 x i8*]* %3, i64 0, i64 0
%12 = call i32 @execve(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8** %11, i32* null)
%13 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %18
14: ; preds = %0
%15 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%16 = call i32 @execve(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8** null, i32* null)
%17 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
br label %18
18: ; preds = %14, %8
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [6 x i8] c"exec1\00"
@__const.main.agv = [2 x i8*] [i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i32 0, i32 0), i8* null]
@.str.5 = [6 x i8] c"exec2\00"
@str = [25 x i8] c"This is the main process\00"
@str.7 = [22 x i8] c"In the parent process\00"
@str.8 = [19 x i8] c"parent exec failed\00"
@str.9 = [21 x i8] c"In the child process\00"
@str.10 = [19 x i8] c"child1 exec failed\00"
define i32 @main() {
%1 = alloca [2 x i8*]
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%3 = tail call i64 (...) @fork()
%4 = icmp eq i64 %3, 0
br i1 %4, label %11, label %5
5: ; preds = %0
%6 = bitcast [2 x i8*]* %1 to i8*
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.9, i64 0, i64 0))
%8 = getelementptr inbounds [2 x i8*], [2 x i8*]* %1, i64 0, i64 0
%9 = call i32 @execve(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8** %8, i32* null)
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.10, i64 0, i64 0))
br label %15
11: ; preds = %0
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.7, i64 0, i64 0))
%13 = tail call i32 @execve(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8** null, i32* null)
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.8, i64 0, i64 0))
br label %15
15: ; preds = %11, %5
ret i32 0
}