#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*/ STDOUT_FILENO;
int creat(char *, int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execlp(char *, char *, char *, char *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[], char *envp[]) {
  pid_t pid;
  if (argc < 2) {
    printf("usage: %s <dirname>\n", argv[0]);
    exit(1);
  }
  char *output = NULL;
  if (argc == 3)
    output = argv[2];
  pid = fork();
  if (pid > 0)
    printf("My child is going to execute command \"ls -laR %s\"\n", argv[1]);
  else if (pid == 0) {
    if (output) {
      int fd = creat(output, 0777);
      if (fd < 0)
        perror("creat");
      else
        dup2(fd, STDOUT_FILENO);
    }
    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)
jge    11b3 <main+0x43>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movq   $0x0,-0x28(%rbp)
cmpl   $0x3,-0x8(%rbp)
jne    11d1 <main+0x61>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jle    1202 <main+0x92>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1295 <main+0x125>
cmpq   $0x0,-0x20(%rbp)
jne    1290 <main+0x120>
cmpq   $0x0,-0x28(%rbp)
je     125a <main+0xea>
mov    -0x28(%rbp),%rdi
mov    $0x1ff,%esi
call   1060 <creat@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1244 <main+0xd4>
lea    0xe0e(%rip),%rdi
call   1050 <perror@plt>
jmp    1255 <main+0xe5>
mov    -0x2c(%rbp),%edi
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
jmp    125a <main+0xea>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xde5(%rip),%rsi
lea    0xde1(%rip),%rdx
mov    %rsi,%rdi
call   1080 <execlp@plt>
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    1295 <main+0x125>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     11a7 <main+0x27>
mov    (%rbx),%rsi
lea    0xe6a(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmp    $0x3,%edi
jne    11b2 <main+0x32>
mov    0x10(%rbx),%r14
jmp    11b5 <main+0x35>
xor    %r14d,%r14d
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
jle    11d5 <main+0x55>
mov    0x8(%rbx),%rsi
lea    0xe49(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1229 <main+0xa9>
jne    1229 <main+0xa9>
test   %r14,%r14
je     11f9 <main+0x79>
mov    %r14,%rdi
mov    $0x1ff,%esi
call   1070 <creat@plt>
test   %eax,%eax
jns    1230 <main+0xb0>
lea    0xe54(%rip),%rdi
call   1060 <perror@plt>
mov    0x8(%rbx),%rcx
lea    0xe4a(%rip),%rdi
lea    0xe46(%rip),%rdx
mov    %rdi,%rsi
call   1090 <execlp@plt>
lea    0xe3c(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    0x2db9(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1040 <dup2@plt>
jmp    11f9 <main+0x79>

LLVM IR (-O0):
@.str = [21 x i8] c"usage: %s <dirname>\0A\00"
@.str.1 = [51 x i8] c"My child is going to execute command \22ls -laR %s\22\0A\00"
@.str.2 = [6 x i8] c"creat\00"
@STDOUT_FILENO = global i32 0
@.str.3 = [3 x i8] c"ls\00"
@.str.4 = [5 x i8] c"-laR\00"
@.str.5 = [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 i8*
%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 slt i32 %11, 2
br i1 %12, label %13, label %19
13: ; preds = %3
%14 = load i8**, i8*** %6
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %3
store i8* null, i8** %9
%20 = load i32, i32* %5
%21 = icmp eq i32 %20, 3
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = load i8**, i8*** %6
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
store i8* %25, i8** %9
br label %26
26: ; preds = %22, %19
%27 = call i64 (...) @fork()
store i64 %27, i64* %8
%28 = load i64, i64* %8
%29 = icmp sgt i64 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %26
%31 = load i8**, i8*** %6
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i8* %33)
br label %61
35: ; preds = %26
%36 = load i64, i64* %8
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %60
38: ; preds = %35
%39 = load i8*, i8** %9
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %53
41: ; preds = %38
%42 = load i8*, i8** %9
%43 = call i32 @creat(i8* %42, i32 511)
store i32 %43, i32* %10
%44 = load i32, i32* %10
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %41
%47 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %52
48: ; preds = %41
%49 = load i32, i32* %10
%50 = load i32, i32* @STDOUT_FILENO
%51 = call i32 @dup2(i32 %49, i32 %50)
br label %52
52: ; preds = %48, %46
br label %53
53: ; preds = %52, %38
%54 = load i8**, i8*** %6
%55 = getelementptr inbounds i8*, i8** %54, i64 1
%56 = load i8*, i8** %55
%57 = call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, 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* %56)
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %35
br label %61
61: ; preds = %60, %30
%62 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [21 x i8] c"usage: %s <dirname>\0A\00"
@.str.1 = [51 x i8] c"My child is going to execute command \22ls -laR %s\22\0A\00"
@.str.2 = [6 x i8] c"creat\00"
@STDOUT_FILENO = global i32 0
@.str.3 = [3 x i8] c"ls\00"
@.str.4 = [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 = icmp slt i32 %0, 2
br i1 %4, label %5, label %9
5: ; preds = %3
%6 = load i8*, i8** %1
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %3
%10 = icmp eq i32 %0, 3
br i1 %10, label %11, label %14
11: ; preds = %9
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
br label %14
14: ; preds = %11, %9
%15 = phi i8* [ %13, %11 ], [ null, %9 ]
%16 = tail call i64 (...) @fork()
%17 = icmp sgt i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i8* %20)
br label %40
22: ; preds = %14
%23 = icmp eq i64 %16, 0
br i1 %23, label %24, label %40
24: ; preds = %22
%25 = icmp eq i8* %15, null
br i1 %25, label %34, label %26
26: ; preds = %24
%27 = tail call i32 @creat(i8* %15, i32 511)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %34
31: ; preds = %26
%32 = load i32, i32* @STDOUT_FILENO
%33 = tail call i32 @dup2(i32 %27, i32 %32)
br label %34
34: ; preds = %29, %31, %24
%35 = getelementptr inbounds i8*, i8** %1, i64 1
%36 = load i8*, i8** %35
%37 = tail call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, 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* %36)
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%39 = tail call i32 @exit(i32 1)
unreachable
40: ; preds = %22, %18
%41 = tail call i32 @exit(i32 0)
unreachable
}