#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 O_CREAT;
int O_EXCL;
int O_WRONLY;
int /*<<< orphan*/ STDOUT_FILENO;
int WEXITSTATUS(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execlp(char *, char *, char *, char *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main(int argc, char *argv[], char *envp[]) {
  if (argc != 2 && argc != 3) {
    printf("usage: %s <dirname> <?outputfile>\n", argv[0]);
    exit(1);
  }
  pid_t pid = fork();
  if (pid > 0) {
    printf("My child is going to execute command \"ls -laR %s\"\n", argv[1]);
    int status;
    waitpid(pid, &status, 0);
    if (WIFSIGNALED(status) != 0) {
      printf("Command was killed with signal: %d\n", WTERMSIG(status));
    } else {
      printf("Command exited with code: %d\n", WEXITSTATUS(status));
    }
  } else if (pid == 0) {
    if (argc == 3) {
      int fd = open(argv[2], O_CREAT | O_WRONLY | O_EXCL);
      if (fd != -1)
        dup2(fd, STDOUT_FILENO);
      else
        printf("Could not create file %s", argv[2]);
    }
    execlp("ls", "ls", "-laR", argv[1]);
    printf("Command not executed !\n");
    exit(1);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11ed <main+0x4d>
cmpl   $0x3,-0x8(%rbp)
je     11ed <main+0x4d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jle    1274 <main+0xd4>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
lea    -0x24(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    -0x24(%rbp),%edi
call   1050 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1257 <main+0xb7>
mov    -0x24(%rbp),%edi
call   1060 <WTERMSIG@plt>
mov    %eax,%esi
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    126f <main+0xcf>
mov    -0x24(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    132a <main+0x18a>
cmpq   $0x0,-0x20(%rbp)
jne    1325 <main+0x185>
cmpl   $0x3,-0x8(%rbp)
jne    12ef <main+0x14f>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2d60(%rip),%rax
mov    (%rax),%esi
mov    0x2d1f(%rip),%rax
or     (%rax),%esi
mov    0x2d26(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
je     12d4 <main+0x134>
mov    -0x28(%rbp),%edi
mov    0x2d20(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
jmp    12ea <main+0x14a>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12ef <main+0x14f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xdb3(%rip),%rsi
lea    0xdaf(%rip),%rdx
mov    %rsi,%rdi
call   10b0 <execlp@plt>
lea    0xda5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    132a <main+0x18a>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%eax
and    $0xfffffffe,%eax
cmp    $0x2,%eax
jne    1219 <main+0x69>
mov    %edi,%ebp
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
jle    1234 <main+0x84>
mov    %rax,%rbx
mov    0x8(%r14),%rsi
lea    0xe42(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    0xc(%rsp),%edi
call   1060 <WIFSIGNALED@plt>
mov    0xc(%rsp),%edi
test   %rax,%rax
jne    1278 <main+0xc8>
call   1080 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xe63(%rip),%rdi
jmp    1286 <main+0xd6>
mov    (%r14),%rsi
lea    0xddd(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jne    128d <main+0xdd>
cmp    $0x3,%ebp
jne    12a4 <main+0xf4>
mov    0x2db6(%rip),%rax
mov    0x2d77(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x10(%r14),%rdi
mov    0x2d78(%rip),%rax
or     (%rax),%esi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
jne    1294 <main+0xe4>
mov    0x10(%r14),%rsi
lea    0xe29(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12a4 <main+0xf4>
call   1070 <WTERMSIG@plt>
mov    %eax,%esi
lea    0xdd0(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    0x2d4d(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1040 <dup2@plt>
mov    0x8(%r14),%rcx
lea    0xe02(%rip),%rdi
lea    0xdfe(%rip),%rdx
mov    %rdi,%rsi
call   10c0 <execlp@plt>
lea    0xdf4(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [35 x i8] c"usage: %s <dirname> <?outputfile>\0A\00"
@.str.1 = [51 x i8] c"My child is going to execute command \22ls -laR %s\22\0A\00"
@.str.2 = [36 x i8] c"Command was killed with signal: %d\0A\00"
@.str.3 = [30 x i8] c"Command exited with code: %d\0A\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_EXCL = global i32 0
@STDOUT_FILENO = global i32 0
@.str.4 = [25 x i8] c"Could not create file %s\00"
@.str.5 = [3 x i8] c"ls\00"
@.str.6 = [5 x i8] c"-laR\00"
@.str.7 = [24 x i8] c"Command not executed !\0A\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%11 = load i32, i32* %5
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %22
13: ; preds = %3
%14 = load i32, i32* %5
%15 = icmp ne i32 %14, 3
br i1 %15, label %16, label %22
16: ; preds = %13
%17 = load i8**, i8*** %6
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %13, %3
%23 = call i64 (...) @fork()
store i64 %23, i64* %8
%24 = load i64, i64* %8
%25 = icmp sgt i64 %24, 0
br i1 %25, label %26, label %45
26: ; preds = %22
%27 = load i8**, i8*** %6
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = load i64, i64* %8
%32 = call i32 @waitpid(i64 %31, i32* %9, i32 0)
%33 = load i32, i32* %9
%34 = call i64 @WIFSIGNALED(i32 %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %26
%37 = load i32, i32* %9
%38 = call i32 @WTERMSIG(i32 %37)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %38)
br label %44
40: ; preds = %26
%41 = load i32, i32* %9
%42 = call i32 @WEXITSTATUS(i32 %41)
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %42)
br label %44
44: ; preds = %40, %36
br label %81
45: ; preds = %22
%46 = load i64, i64* %8
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %80
48: ; preds = %45
%49 = load i32, i32* %5
%50 = icmp eq i32 %49, 3
br i1 %50, label %51, label %73
51: ; preds = %48
%52 = load i8**, i8*** %6
%53 = getelementptr inbounds i8*, i8** %52, i64 2
%54 = load i8*, i8** %53
%55 = load i32, i32* @O_CREAT
%56 = load i32, i32* @O_WRONLY
%57 = or i32 %55, %56
%58 = load i32, i32* @O_EXCL
%59 = or i32 %57, %58
%60 = call i32 @open(i8* %54, i32 %59)
store i32 %60, i32* %10
%61 = load i32, i32* %10
%62 = icmp ne i32 %61, -1
br i1 %62, label %63, label %67
63: ; preds = %51
%64 = load i32, i32* %10
%65 = load i32, i32* @STDOUT_FILENO
%66 = call i32 @dup2(i32 %64, i32 %65)
br label %72
67: ; preds = %51
%68 = load i8**, i8*** %6
%69 = getelementptr inbounds i8*, i8** %68, i64 2
%70 = load i8*, i8** %69
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %70)
br label %72
72: ; preds = %67, %63
br label %73
73: ; preds = %72, %48
%74 = load i8**, i8*** %6
%75 = getelementptr inbounds i8*, i8** %74, i64 1
%76 = load i8*, i8** %75
%77 = call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i8* %76)
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
%79 = call i32 @exit(i32 1)
unreachable
80: ; preds = %45
br label %81
81: ; preds = %80, %44
%82 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [35 x i8] c"usage: %s <dirname> <?outputfile>\0A\00"
@.str.1 = [51 x i8] c"My child is going to execute command \22ls -laR %s\22\0A\00"
@.str.2 = [36 x i8] c"Command was killed with signal: %d\0A\00"
@.str.3 = [30 x i8] c"Command exited with code: %d\0A\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_EXCL = global i32 0
@STDOUT_FILENO = global i32 0
@.str.4 = [25 x i8] c"Could not create file %s\00"
@.str.5 = [3 x i8] c"ls\00"
@.str.6 = [5 x i8] c"-laR\00"
@str = [23 x i8] c"Command not executed !\00"
define i32 @main(i32 %0, i8** nocapture %1, i8** nocapture readnone %2) {
%4 = alloca i32
%5 = and i32 %0, -2
%6 = icmp eq i32 %5, 2
br i1 %6, label %11, label %7
7: ; preds = %3
%8 = load i8*, i8** %1
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %3
%12 = tail call i64 (...) @fork()
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %32
14: ; preds = %11
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = bitcast i32* %4 to i8*
%19 = call i32 @waitpid(i64 %12, i32* %4, i32 0)
%20 = load i32, i32* %4
%21 = call i64 @WIFSIGNALED(i32 %20)
%22 = icmp eq i64 %21, 0
%23 = load i32, i32* %4
br i1 %22, label %26, label %24
24: ; preds = %14
%25 = call i32 @WTERMSIG(i32 %23)
br label %28
26: ; preds = %14
%27 = call i32 @WEXITSTATUS(i32 %23)
br label %28
28: ; preds = %26, %24
%29 = phi i32 [ %27, %26 ], [ %25, %24 ]
%30 = phi i8* [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), %26 ], [ getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), %24 ]
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) %30, i32 %29)
br label %58
32: ; preds = %11
%33 = icmp eq i64 %12, 0
br i1 %33, label %34, label %58
34: ; preds = %32
%35 = icmp eq i32 %0, 3
br i1 %35, label %36, label %52
36: ; preds = %34
%37 = getelementptr inbounds i8*, i8** %1, i64 2
%38 = load i8*, i8** %37
%39 = load i32, i32* @O_CREAT
%40 = load i32, i32* @O_WRONLY
%41 = or i32 %40, %39
%42 = load i32, i32* @O_EXCL
%43 = or i32 %41, %42
%44 = tail call i32 @open(i8* %38, i32 %43)
%45 = icmp eq i32 %44, -1
br i1 %45, label %49, label %46
46: ; preds = %36
%47 = load i32, i32* @STDOUT_FILENO
%48 = tail call i32 @dup2(i32 %44, i32 %47)
br label %52
49: ; preds = %36
%50 = load i8*, i8** %37
%51 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %50)
br label %52
52: ; preds = %46, %49, %34
%53 = getelementptr inbounds i8*, i8** %1, i64 1
%54 = load i8*, i8** %53
%55 = tail call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i8* %54)
%56 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%57 = tail call i32 @exit(i32 1)
unreachable
58: ; preds = %32, %28
%59 = call i32 @exit(i32 0)
unreachable
}