#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*/ alarm(int);
scalar_t__ execl(char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int wait(int *);
int main() {
  pid_t childpid;
  int status;
  if ((childpid = fork()) == -1) {
    perror("Error: fork()");
    exit(1);
  }
  if (childpid == 0) {
    sleep(5);
    if (execl("/usr/bin/ls", "ls", "-l", NULL) < 0) {
      perror("Execution of ls failed");
      exit(1);
    }
  } else {
    alarm(2);
    if (childpid != wait(&status))
      perror("A signal occurred before child exited");
    else
      printf("Child exited\n");
  }
  fflush(stdout);
  fflush(stderr);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11c8 <main+0x38>
lea    0xe47(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    121f <main+0x8f>
mov    $0x5,%edi
call   1090 <sleep@plt>
lea    0xe2b(%rip),%rdi
lea    0xe2d(%rip),%rsi
lea    0xe29(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1080 <execl@plt>
cmp    $0x0,%rax
jge    121a <main+0x8a>
lea    0xe12(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    1267 <main+0xd7>
mov    $0x2,%edi
call   1040 <alarm@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
lea    -0xc(%rbp),%rdi
call   10a0 <wait@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%eax
cmp    %ecx,%eax
je     1256 <main+0xc6>
lea    0xde8(%rip),%rdi
call   1060 <perror@plt>
jmp    1262 <main+0xd2>
lea    0xdfd(%rip),%rdi
call   1030 <printf@plt>
jmp    1267 <main+0xd7>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     11f7 <main+0x67>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11ee <main+0x5e>
mov    $0x2,%edi
call   1040 <alarm@plt>
lea    0xc(%rsp),%rdi
call   10a0 <wait@plt>
cmp    %eax,%ebx
je     1238 <main+0xa8>
lea    0xe6e(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
xor    %edi,%edi
call   1070 <exit@plt>
lea    0xe0b(%rip),%rdi
jmp    1229 <main+0x99>
mov    $0x5,%edi
call   1090 <sleep@plt>
lea    0xe06(%rip),%rdi
lea    0xe08(%rip),%rsi
lea    0xe04(%rip),%rdx
xor    %ecx,%ecx
call   1080 <execl@plt>
test   %rax,%rax
jns    11cb <main+0x3b>
lea    0xdf4(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe1b(%rip),%rdi
call   1030 <puts@plt>
jmp    11cb <main+0x3b>

LLVM IR (-O0):
@.str = [14 x i8] c"Error: fork()\00"
@.str.1 = [12 x i8] c"/usr/bin/ls\00"
@.str.2 = [3 x i8] c"ls\00"
@.str.3 = [3 x i8] c"-l\00"
@.str.4 = [23 x i8] c"Execution of ls failed\00"
@.str.5 = [38 x i8] c"A signal occurred before child exited\00"
@.str.6 = [14 x i8] c"Child exited\0A\00"
@stdout = global i32 0
@stderr = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32 (...) @fork()
store i32 %4, i32* %2
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = load i32, i32* %2
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %20
12: ; preds = %9
%13 = call i32 @sleep(i32 5)
%14 = call i64 @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), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* null)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %12
br label %30
20: ; preds = %9
%21 = call i32 @alarm(i32 2)
%22 = load i32, i32* %2
%23 = call i32 @wait(i32* %3)
%24 = icmp ne i32 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0))
br label %29
27: ; preds = %20
%28 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
br label %29
29: ; preds = %27, %25
br label %30
30: ; preds = %29, %19
%31 = load i32, i32* @stdout
%32 = call i32 @fflush(i32 %31)
%33 = load i32, i32* @stderr
%34 = call i32 @fflush(i32 %33)
%35 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [14 x i8] c"Error: fork()\00"
@.str.1 = [12 x i8] c"/usr/bin/ls\00"
@.str.2 = [3 x i8] c"ls\00"
@.str.3 = [3 x i8] c"-l\00"
@.str.4 = [23 x i8] c"Execution of ls failed\00"
@.str.5 = [38 x i8] c"A signal occurred before child exited\00"
@stdout = global i32 0
@stderr = global i32 0
@str = [13 x i8] c"Child exited\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (...) @fork()
switch i32 %3, label %14 [
i32 -1, label %4
i32 0, label %7
]
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %0
%8 = tail call i32 @sleep(i32 5)
%9 = tail call i64 @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), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* null)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %22
11: ; preds = %7
%12 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = tail call i32 @alarm(i32 2)
%16 = call i32 @wait(i32* %1)
%17 = icmp eq i32 %3, %16
br i1 %17, label %20, label %18
18: ; preds = %14
%19 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0))
br label %22
20: ; preds = %14
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %22
22: ; preds = %18, %20, %7
%23 = load i32, i32* @stdout
%24 = call i32 @fflush(i32 %23)
%25 = load i32, i32* @stderr
%26 = call i32 @fflush(i32 %25)
%27 = call i32 @exit(i32 0)
unreachable
}