#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int atoi(char *);
int execvp(char *, char **);
int fork();
int /*<<< orphan*/ free_cmd(char **);
char **get_cmd_array(int);
int /*<<< orphan*/ printf(char *);
scalar_t__ strcmp(char *, char *);
int waitpid(int, int *, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int launch_shell(int fd, int int_mode) {
  while (1) {
    if (int_mode == 1) {
      write(fd, "$ ", 2);
    }
    char **cmd = get_cmd_array(fd);
    if (cmd == NULL) {
      if (!int_mode) {
        return EXIT_SUCCESS;
      }
      continue;
    }
    size_t cmd_size = 0;
    for (; cmd[cmd_size] != NULL; cmd_size++)
      ;
    if (strcmp(cmd[0], "exit") == 0) {
      if (cmd_size == 2) {
        int status = atoi(cmd[1]);
        free_cmd(cmd);
        return status;
      }
      free_cmd(cmd);
      if (cmd_size == 1) {
        return EXIT_SUCCESS;
      } else {
        printf("mysh: unknow option for exit");
        return EXIT_FAILURE;
      }
    }
    pid_t pid = fork();
    if (pid == 0) {
      if (execvp(cmd[0], cmd) == -1) {
        free_cmd(cmd);
        return EXIT_FAILURE;
      }
    } else if (pid == -1) {
      free_cmd(cmd);
      return EXIT_FAILURE;
    } else {
      pid_t w;
      int wstatus;
      w = waitpid(pid, &wstatus, 0);
      if (w == -1) {
        free_cmd(cmd);
        return EXIT_FAILURE;
      }
    }
    free_cmd(cmd);
  }
}


x86 assembly (clang, -O0):
<launch_shell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x1,-0xc(%rbp)
jne    11bc <launch_shell+0x2c>
mov    -0x8(%rbp),%edi
lea    0xe4e(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   10a0 <get_cmd_array@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f3 <launch_shell+0x63>
cmpl   $0x0,-0xc(%rbp)
jne    11ee <launch_shell+0x5e>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    136f <launch_shell+0x1df>
jmp    119e <launch_shell+0xe>
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     1224 <launch_shell+0x94>
jmp    1213 <launch_shell+0x83>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11fb <launch_shell+0x6b>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xde9(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    12b2 <launch_shell+0x122>
cmpq   $0x2,-0x20(%rbp)
jne    1270 <launch_shell+0xe0>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <free_cmd@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    136f <launch_shell+0x1df>
mov    -0x18(%rbp),%rdi
call   1060 <free_cmd@plt>
cmpq   $0x1,-0x20(%rbp)
jne    1295 <launch_shell+0x105>
mov    0x2d4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    136f <launch_shell+0x1df>
lea    0xd67(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d38(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    136f <launch_shell+0x1df>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    12fe <launch_shell+0x16e>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <execvp@plt>
cmp    $0xffffffff,%eax
jne    12f9 <launch_shell+0x169>
mov    -0x18(%rbp),%rdi
call   1060 <free_cmd@plt>
mov    0x2cf1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    136f <launch_shell+0x1df>
jmp    1361 <launch_shell+0x1d1>
cmpl   $0xffffffff,-0x28(%rbp)
jne    1322 <launch_shell+0x192>
mov    -0x18(%rbp),%rdi
call   1060 <free_cmd@plt>
mov    0x2cc8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    136f <launch_shell+0x1df>
mov    -0x28(%rbp),%edi
lea    -0x30(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1357 <launch_shell+0x1c7>
mov    -0x18(%rbp),%rdi
call   1060 <free_cmd@plt>
mov    0x2c93(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    136f <launch_shell+0x1df>
jmp    135c <launch_shell+0x1cc>
jmp    1361 <launch_shell+0x1d1>
mov    -0x18(%rbp),%rdi
call   1060 <free_cmd@plt>
jmp    119e <launch_shell+0xe>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<launch_shell>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%ebx
cmp    $0x1,%esi
mov    $0x2,%r14d
sbb    $0x0,%r14d
lea    0xe68(%rip),%r12
jmp    11cc <launch_shell+0x3c>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14d,%eax
cmp    $0x1,%eax
je     12aa <launch_shell+0x11a>
cmp    $0x1,%ebp
jne    11e4 <launch_shell+0x54>
mov    %ebx,%edi
lea    0xe26(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   10a0 <get_cmd_array@plt>
test   %rax,%rax
je     1250 <launch_shell+0xc0>
mov    %rax,%r13
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
cmpq   $0x0,0x0(%r13,%r15,8)
lea    0x1(%r15),%r15
jne    1200 <launch_shell+0x70>
mov    0x0(%r13),%rdi
mov    %r12,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     12bc <launch_shell+0x12c>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     1270 <launch_shell+0xe0>
cmp    $0xffffffff,%eax
je     1296 <launch_shell+0x106>
mov    %eax,%edi
lea    0x4(%rsp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    1281 <launch_shell+0xf1>
jmp    1296 <launch_shell+0x106>
cs nopw 0x0(%rax,%rax,1)
test   %ebp,%ebp
jne    11c0 <launch_shell+0x30>
mov    0x2d79(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
jmp    11c0 <launch_shell+0x30>
nopl   0x0(%rax)
mov    0x0(%r13),%rdi
mov    %r13,%rsi
call   1080 <execvp@plt>
cmp    $0xffffffff,%eax
je     1296 <launch_shell+0x106>
mov    %r13,%rdi
call   1060 <free_cmd@plt>
xor    %eax,%eax
cmp    $0x1,%eax
jne    11cc <launch_shell+0x3c>
jmp    12aa <launch_shell+0x11a>
mov    %r13,%rdi
call   1060 <free_cmd@plt>
mov    0x2d3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    (%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
cmp    $0x3,%r15
jne    12d8 <launch_shell+0x148>
mov    0x8(%r13),%rdi
call   1090 <atoi@plt>
mov    %eax,(%rsp)
mov    %r13,%rdi
call   1060 <free_cmd@plt>
jmp    12aa <launch_shell+0x11a>
mov    %r13,%rdi
call   1060 <free_cmd@plt>
cmp    $0x2,%r15
jne    12ef <launch_shell+0x15f>
mov    0x2ceb(%rip),%rax
jmp    12a5 <launch_shell+0x115>
lea    0xd0d(%rip),%rdi
call   1040 <printf@plt>
jmp    129e <launch_shell+0x10e>

LLVM IR (-O0):
@.str = [3 x i8] c"$ \00"
@EXIT_SUCCESS = global i32 0
@.str.1 = [5 x i8] c"exit\00"
@.str.2 = [29 x i8] c"mysh: unknow option for exit\00"
@EXIT_FAILURE = global i32 0
define i32 @launch_shell(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
br label %12
12: ; preds = %2, %28, %101
%13 = load i32, i32* %5
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i32, i32* %4
%17 = call i32 @write(i32 %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
br label %18
18: ; preds = %15, %12
%19 = load i32, i32* %4
%20 = call i8** @get_cmd_array(i32 %19)
store i8** %20, i8*** %6
%21 = load i8**, i8*** %6
%22 = icmp eq i8** %21, null
br i1 %22, label %23, label %29
23: ; preds = %18
%24 = load i32, i32* %5
%25 = icmp ne i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = load i32, i32* @EXIT_SUCCESS
store i32 %27, i32* %3
br label %104
28: ; preds = %23
br label %12
29: ; preds = %18
store i64 0, i64* %7
br label %30
30: ; preds = %37, %29
%31 = load i8**, i8*** %6
%32 = load i64, i64* %7
%33 = getelementptr inbounds i8*, i8** %31, i64 %32
%34 = load i8*, i8** %33
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %40
36: ; preds = %30
br label %37
37: ; preds = %36
%38 = load i64, i64* %7
%39 = add i64 %38, 1
store i64 %39, i64* %7
br label %30
40: ; preds = %30
%41 = load i8**, i8*** %6
%42 = getelementptr inbounds i8*, i8** %41, i64 0
%43 = load i8*, i8** %42
%44 = call i64 @strcmp(i8* %43, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %67
46: ; preds = %40
%47 = load i64, i64* %7
%48 = icmp eq i64 %47, 2
br i1 %48, label %49, label %57
49: ; preds = %46
%50 = load i8**, i8*** %6
%51 = getelementptr inbounds i8*, i8** %50, i64 1
%52 = load i8*, i8** %51
%53 = call i32 @atoi(i8* %52)
store i32 %53, i32* %8
%54 = load i8**, i8*** %6
%55 = call i32 @free_cmd(i8** %54)
%56 = load i32, i32* %8
store i32 %56, i32* %3
br label %104
57: ; preds = %46
%58 = load i8**, i8*** %6
%59 = call i32 @free_cmd(i8** %58)
%60 = load i64, i64* %7
%61 = icmp eq i64 %60, 1
br i1 %61, label %62, label %64
62: ; preds = %57
%63 = load i32, i32* @EXIT_SUCCESS
store i32 %63, i32* %3
br label %104
64: ; preds = %57
%65 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%66 = load i32, i32* @EXIT_FAILURE
store i32 %66, i32* %3
br label %104
67: ; preds = %40
%68 = call i32 (...) @fork()
store i32 %68, i32* %9
%69 = load i32, i32* %9
%70 = icmp eq i32 %69, 0
br i1 %70, label %71, label %83
71: ; preds = %67
%72 = load i8**, i8*** %6
%73 = getelementptr inbounds i8*, i8** %72, i64 0
%74 = load i8*, i8** %73
%75 = load i8**, i8*** %6
%76 = call i32 @execvp(i8* %74, i8** %75)
%77 = icmp eq i32 %76, -1
br i1 %77, label %78, label %82
78: ; preds = %71
%79 = load i8**, i8*** %6
%80 = call i32 @free_cmd(i8** %79)
%81 = load i32, i32* @EXIT_FAILURE
store i32 %81, i32* %3
br label %104
82: ; preds = %71
br label %101
83: ; preds = %67
%84 = load i32, i32* %9
%85 = icmp eq i32 %84, -1
br i1 %85, label %86, label %90
86: ; preds = %83
%87 = load i8**, i8*** %6
%88 = call i32 @free_cmd(i8** %87)
%89 = load i32, i32* @EXIT_FAILURE
store i32 %89, i32* %3
br label %104
90: ; preds = %83
%91 = load i32, i32* %9
%92 = call i32 @waitpid(i32 %91, i32* %11, i32 0)
store i32 %92, i32* %10
%93 = load i32, i32* %10
%94 = icmp eq i32 %93, -1
br i1 %94, label %95, label %99
95: ; preds = %90
%96 = load i8**, i8*** %6
%97 = call i32 @free_cmd(i8** %96)
%98 = load i32, i32* @EXIT_FAILURE
store i32 %98, i32* %3
br label %104
99: ; preds = %90
br label %100
100: ; preds = %99
br label %101
101: ; preds = %100, %82
%102 = load i8**, i8*** %6
%103 = call i32 @free_cmd(i8** %102)
br label %12
104: ; preds = %95, %86, %78, %64, %62, %49, %26
%105 = load i32, i32* %3
ret i32 %105
}

LLVM IR (-O2):
@.str = [3 x i8] c"$ \00"
@EXIT_SUCCESS = global i32 0
@.str.1 = [5 x i8] c"exit\00"
@.str.2 = [29 x i8] c"mysh: unknow option for exit\00"
@EXIT_FAILURE = global i32 0
define i32 @launch_shell(i32 %0, i32 %1) {
%3 = alloca i32
%4 = icmp eq i32 %1, 1
%5 = bitcast i32* %3 to i8*
%6 = icmp eq i32 %1, 0
%7 = select i1 %6, i32 1, i32 2
br label %8
8: ; preds = %64, %2
%9 = phi i32 [ undef, %2 ], [ %66, %64 ]
br i1 %4, label %10, label %12
10: ; preds = %8
%11 = call i32 @write(i32 %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
br label %12
12: ; preds = %10, %8
%13 = call i8** @get_cmd_array(i32 %0)
%14 = icmp eq i8** %13, null
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i32, i32* @EXIT_SUCCESS
%17 = select i1 %6, i32 %16, i32 %9
br label %64
18: ; preds = %12, %18
%19 = phi i64 [ %23, %18 ], [ 0, %12 ]
%20 = getelementptr inbounds i8*, i8** %13, i64 %19
%21 = load i8*, i8** %20
%22 = icmp eq i8* %21, null
%23 = add i64 %19, 1
br i1 %22, label %24, label %18
24: ; preds = %18
%25 = load i8*, i8** %13
%26 = call i64 @strcmp(i8* %25, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %43
28: ; preds = %24
%29 = icmp eq i64 %19, 2
br i1 %29, label %30, label %35
30: ; preds = %28
%31 = getelementptr inbounds i8*, i8** %13, i64 1
%32 = load i8*, i8** %31
%33 = call i32 @atoi(i8* %32)
%34 = call i32 @free_cmd(i8** %13)
br label %68
35: ; preds = %28
%36 = call i32 @free_cmd(i8** %13)
%37 = icmp eq i64 %19, 1
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = load i32, i32* @EXIT_SUCCESS
br label %68
40: ; preds = %35
%41 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
br label %68
43: ; preds = %24
%44 = call i32 (...) @fork()
switch i32 %44, label %55 [
i32 0, label %45
i32 -1, label %52
]
45: ; preds = %43
%46 = load i8*, i8** %13
%47 = call i32 @execvp(i8* %46, i8** %13)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %62
49: ; preds = %45
%50 = call i32 @free_cmd(i8** %13)
%51 = load i32, i32* @EXIT_FAILURE
br label %68
52: ; preds = %43
%53 = call i32 @free_cmd(i8** %13)
%54 = load i32, i32* @EXIT_FAILURE
br label %68
55: ; preds = %43
%56 = call i32 @waitpid(i32 %44, i32* %3, i32 0)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %61
58: ; preds = %55
%59 = call i32 @free_cmd(i8** %13)
%60 = load i32, i32* @EXIT_FAILURE
br label %68
61: ; preds = %55
br label %62
62: ; preds = %61, %45
%63 = call i32 @free_cmd(i8** %13)
br label %64
64: ; preds = %15, %62
%65 = phi i32 [ 0, %62 ], [ %7, %15 ]
%66 = phi i32 [ %9, %62 ], [ %17, %15 ]
%67 = icmp eq i32 %65, 1
br i1 %67, label %68, label %8
68: ; preds = %64, %58, %52, %49, %40, %38, %30
%69 = phi i32 [ %60, %58 ], [ %54, %52 ], [ %51, %49 ], [ %42, %40 ], [ %39, %38 ], [ %33, %30 ], [ %66, %64 ]
ret i32 %69
}