#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*/ execl(char *, char *, char *, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int func2() {
  pid_t child_id = fork();
  if (child_id == -1) {
    perror("fork");
    exit(EXIT_FAILURE);
  }
  if (child_id > 0) {
    int status;
    waitpid(child_id, &status, 0);
    printf("Exiting from parent...");
    return 0;
  } else {
    sleep(2);
    execl("/bin/ls", "ls", "-alh", (char *)NULL);
    perror("exec failed!");
  }
}


x86 assembly (clang, -O0):
<func2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11a6 <func2+0x36>
lea    0xe6d(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jle    11d6 <func2+0x66>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
lea    0xe40(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    120a <func2+0x9a>
mov    $0x2,%edi
call   1080 <sleep@plt>
lea    0xe35(%rip),%rdi
lea    0xe33(%rip),%rsi
lea    0xe2f(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1070 <execl@plt>
lea    0xe24(%rip),%rdi
call   1050 <perror@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<func2>:
push   %rax
xor    %eax,%eax
call   1090 <fork@plt>
cmp    $0xffffffff,%eax
je     11d3 <func2+0x63>
test   %eax,%eax
jle    119f <func2+0x2f>
lea    0x4(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1040 <waitpid@plt>
lea    0xe6f(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret
mov    $0x2,%edi
call   1080 <sleep@plt>
lea    0xe6c(%rip),%rdi
lea    0xe6a(%rip),%rsi
lea    0xe66(%rip),%rdx
xor    %ecx,%ecx
call   1070 <execl@plt>
lea    0xe5d(%rip),%rdi
call   1050 <perror@plt>
jmp    119b <func2+0x2b>
lea    0xe26(%rip),%rdi
call   1050 <perror@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [23 x i8] c"Exiting from parent...\00"
@.str.2 = [8 x i8] c"/bin/ls\00"
@.str.3 = [3 x i8] c"ls\00"
@.str.4 = [5 x i8] c"-alh\00"
@.str.5 = [13 x i8] c"exec failed!\00"
define i32 @func2() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = call i32 (...) @fork()
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
%12 = load i32, i32* %2
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* %2
%16 = call i32 @waitpid(i32 %15, i32* %3, i32 0)
%17 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %22
18: ; preds = %11
%19 = call i32 @sleep(i32 2)
%20 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* null)
%21 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %22
22: ; preds = %14, %18
%23 = load i32, i32* %1
ret i32 %23
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [23 x i8] c"Exiting from parent...\00"
@.str.2 = [8 x i8] c"/bin/ls\00"
@.str.3 = [3 x i8] c"ls\00"
@.str.4 = [5 x i8] c"-alh\00"
@.str.5 = [13 x i8] c"exec failed!\00"
define i32 @func2() {
%1 = alloca i32
%2 = tail call i32 (...) @fork()
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %8
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %0
%9 = icmp sgt i32 %2, 0
br i1 %9, label %10, label %14
10: ; preds = %8
%11 = bitcast i32* %1 to i8*
%12 = call i32 @waitpid(i32 %2, i32* %1, i32 0)
%13 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %18
14: ; preds = %8
%15 = tail call i32 @sleep(i32 2)
%16 = tail call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* null)
%17 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %18
18: ; preds = %14, %10
ret i32 0
}