#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 */
struct sigaction {
  int /*<<< orphan*/ sa_handler;
  scalar_t__ sa_flags;
};
typedef int pid_t;

/* Variables and functions */
int EXIT_FAILURE;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ SIGINT;
int /*<<< orphan*/ SIG_DFL;
int /*<<< orphan*/ SIG_IGN;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ append_bg_pid(int);
int /*<<< orphan*/ change_dir(char *);
int dup2(int, int);
int execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ free(char **);
char *getenv(char *);
int /*<<< orphan*/ lookup_all_bg_process();
char **malloc(int);
int /*<<< orphan*/ memset(char *, char, int);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sigaction(int /*<<< orphan*/, struct sigaction *,
                             int /*<<< orphan*/ *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
scalar_t__ strcmp(char *, char *);
int strlen(char *);
char *strtok(char *, char *);
int /*<<< orphan*/ term_shell();
int waitpid(int, int *, int /*<<< orphan*/);
void main_shell() {
  int foreground_exit_stat = 0;
  while (true) {
    int arg_count = 0, word_count = 0;
    char **arguments = malloc(512 * sizeof(char *));
    char *words[512 + 1];
    char *command = NULL, *input_file = NULL, *output_file = NULL;
    char input[2048 + 1];
    memset(input, '\0', 2048);
    bool background_mode = false, redir_input = false, redir_output = false;
    struct sigaction act;
    act.sa_handler = SIG_IGN;
    sigaction(SIGINT, &act, NULL);
    lookup_all_bg_process();
    printf(": ");
    fflush(stdout);
    fflush(stdin);
    fgets(input, 2048, stdin);
    if (strlen(input) > 1) {
      if (input[0] == '#') {
        continue;
      }
      input[strlen(input) - 1] = '\0';
    }
    if ((command = strtok(input, " \n"))) {
      arguments[arg_count++] = command;
      while ((words[word_count] = strtok(NULL, " "))) {
        char *word = words[word_count];
        if (word[0] == '#') {
          word[0] = '\0';
          break;
        }
        if (strcmp(word, "<") == 0) {
          words[++word_count] = strtok(NULL, " ");
          input_file = words[word_count];
          redir_input = true;
        } else if (strcmp(word, ">") == 0) {
          words[++word_count] = strtok(NULL, " ");
          output_file = words[word_count];
          redir_output = true;
        } else if (strcmp(word, "&") == 0) {
          background_mode = true;
          break;
        } else {
          arguments[arg_count++] = words[word_count++];
        }
      }
      arguments[arg_count] = NULL;
      if (strcmp(command, "exit") == 0) {
        free(arguments);
        term_shell();
      } else if (strcmp(command, "cd") == 0) {
        if (arg_count == 1) {
          change_dir(getenv("HOME"));
        } else if (arg_count == 2) {
          change_dir(arguments[1]);
        } else {
          printf("smallsh: cd: usage: cd [directory]\n");
        }
      } else if (strcmp(command, "status") == 0) {
        printf("exit value %d\n", foreground_exit_stat);
      } else {
        pid_t pid = fork();
        int fd_in, fd_out, fd_in2, fd_out2;
        if (pid == 0) {
          if (!redir_input && background_mode)
            input_file = "/dev/null";
          if (!redir_output && background_mode)
            output_file = "/dev/null";
          if (input_file) {
            fd_in = open(input_file, O_RDONLY);
            if (fd_in == -1) {
              perror("open");
              exit(1);
            }
            fd_in2 = dup2(fd_in, 0);
            if (fd_in2 == -1) {
              perror("dup2");
              exit(2);
            }
          }
          if (output_file) {
            fd_out = open(output_file, O_WRONLY | O_TRUNC | O_CREAT, 0644);
            if (fd_out == -1) {
              perror("open");
              exit(1);
            }
            fd_out2 = dup2(fd_out, 1);
            if (fd_out2 == -1) {
              perror("dup2");
              exit(2);
            }
          }
          if (!background_mode) {
            act.sa_flags = 0;
            act.sa_handler = SIG_DFL;
            sigaction(SIGINT, &act, NULL);
          }
          foreground_exit_stat = execvp(command, arguments);
          if (foreground_exit_stat == -1) {
            perror(command);
            foreground_exit_stat = 1;
          }
          exit(EXIT_FAILURE);
        } else if (pid == -1) {
          perror("fork()");
        } else {
          if (background_mode) {
            append_bg_pid(pid);
            printf("background pid is %d\n", pid);
          } else {
            pid = waitpid(pid, &foreground_exit_stat, 0);
            if (pid != -1 && pid != 0) {
              if (WIFSIGNALED(foreground_exit_stat)) {
                foreground_exit_stat = WTERMSIG(foreground_exit_stat);
                printf("pid %d is done: terminated by signal %d\n", pid,
                       foreground_exit_stat);
              } else if (WIFEXITED(foreground_exit_stat)) {
                foreground_exit_stat = WEXITSTATUS(foreground_exit_stat);
              }
            }
          }
        }
      }
    }
    if (arguments)
      free(arguments);
  }
}


x86 assembly (clang, -O0):
<main_shell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1880,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    $0x1000,%edi
call   1100 <malloc@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x1028(%rbp)
movq   $0x0,-0x1030(%rbp)
movq   $0x0,-0x1038(%rbp)
lea    -0x1840(%rbp),%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   10b0 <memset@plt>
movl   $0x0,-0x1844(%rbp)
movl   $0x0,-0x1848(%rbp)
movl   $0x0,-0x184c(%rbp)
mov    0x2c99(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1860(%rbp)
mov    0x2ca2(%rip),%rax
mov    (%rax),%edi
lea    -0x1860(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <sigaction@plt>
mov    $0x0,%al
call   1060 <lookup_all_bg_process@plt>
lea    0xcaa(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
mov    0x2c3c(%rip),%rax
mov    (%rax),%edi
call   1110 <fflush@plt>
mov    0x2c56(%rip),%rax
mov    (%rax),%edi
call   1110 <fflush@plt>
lea    -0x1840(%rbp),%rdi
mov    0x2c41(%rip),%rax
mov    (%rax),%edx
mov    $0x800,%esi
call   10c0 <fgets@plt>
lea    -0x1840(%rbp),%rdi
call   1070 <strlen@plt>
cmp    $0x1,%eax
jle    13d6 <main_shell+0x136>
movsbl -0x1840(%rbp),%eax
cmp    $0x23,%eax
jne    13bd <main_shell+0x11d>
jmp    12b2 <main_shell+0x12>
lea    -0x1840(%rbp),%rdi
call   1070 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x1840(%rbp,%rax,1)
lea    -0x1840(%rbp),%rdi
lea    0xc1f(%rip),%rsi
call   1180 <strtok@plt>
mov    %rax,-0x1028(%rbp)
cmp    $0x0,%rax
je     19a2 <main_shell+0x702>
mov    -0x1028(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x8(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
xor    %eax,%eax
mov    %eax,%edi
lea    0xbdf(%rip),%rsi
call   1180 <strtok@plt>
movslq -0xc(%rbp),%rcx
mov    %rax,-0x1020(%rbp,%rcx,8)
ff
cmp    $0x0,%rax
je     15a2 <main_shell+0x302>
movslq -0xc(%rbp),%rax
mov    -0x1020(%rbp,%rax,8),%rax
ff
mov    %rax,-0x1868(%rbp)
mov    -0x1868(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x23,%eax
jne    1472 <main_shell+0x1d2>
mov    -0x1868(%rbp),%rax
movb   $0x0,(%rax)
jmp    15a2 <main_shell+0x302>
mov    -0x1868(%rbp),%rdi
lea    0xb86(%rip),%rsi
call   10e0 <strcmp@plt>
cmp    $0x0,%rax
jne    14d7 <main_shell+0x237>
xor    %eax,%eax
mov    %eax,%edi
lea    0xb67(%rip),%rsi
call   1180 <strtok@plt>
mov    %rax,%rcx
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
cltq
mov    %rcx,-0x1020(%rbp,%rax,8)
ff
movslq -0xc(%rbp),%rax
mov    -0x1020(%rbp,%rax,8),%rax
ff
mov    %rax,-0x1030(%rbp)
movl   $0x1,-0x1848(%rbp)
jmp    159d <main_shell+0x2fd>
mov    -0x1868(%rbp),%rdi
lea    0xb23(%rip),%rsi
call   10e0 <strcmp@plt>
cmp    $0x0,%rax
jne    153c <main_shell+0x29c>
xor    %eax,%eax
mov    %eax,%edi
lea    0xb02(%rip),%rsi
call   1180 <strtok@plt>
mov    %rax,%rcx
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
cltq
mov    %rcx,-0x1020(%rbp,%rax,8)
ff
movslq -0xc(%rbp),%rax
mov    -0x1020(%rbp,%rax,8),%rax
ff
mov    %rax,-0x1038(%rbp)
movl   $0x1,-0x184c(%rbp)
jmp    1598 <main_shell+0x2f8>
mov    -0x1868(%rbp),%rdi
lea    0xac0(%rip),%rsi
call   10e0 <strcmp@plt>
cmp    $0x0,%rax
jne    1568 <main_shell+0x2c8>
movl   $0x1,-0x1844(%rbp)
jmp    15a2 <main_shell+0x302>
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0xc(%rbp)
cltq
mov    -0x1020(%rbp,%rax,8),%rdx
ff
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x8(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
jmp    1598 <main_shell+0x2f8>
jmp    159d <main_shell+0x2fd>
jmp    1417 <main_shell+0x177>
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x1028(%rbp),%rdi
lea    0xa4c(%rip),%rsi
call   10e0 <strcmp@plt>
cmp    $0x0,%rax
jne    15e4 <main_shell+0x344>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
mov    $0x0,%al
call   1080 <term_shell@plt>
jmp    199d <main_shell+0x6fd>
mov    -0x1028(%rbp),%rdi
lea    0xa1f(%rip),%rsi
call   10e0 <strcmp@plt>
cmp    $0x0,%rax
jne    1658 <main_shell+0x3b8>
cmpl   $0x1,-0x8(%rbp)
jne    1624 <main_shell+0x384>
lea    0xa02(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   1160 <change_dir@plt>
jmp    1653 <main_shell+0x3b3>
cmpl   $0x2,-0x8(%rbp)
jne    1640 <main_shell+0x3a0>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdi
call   1160 <change_dir@plt>
jmp    164e <main_shell+0x3ae>
lea    0x9d2(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
jmp    1653 <main_shell+0x3b3>
jmp    1998 <main_shell+0x6f8>
mov    -0x1028(%rbp),%rdi
lea    0x9d7(%rip),%rsi
call   10e0 <strcmp@plt>
cmp    $0x0,%rax
jne    168b <main_shell+0x3eb>
mov    -0x4(%rbp),%esi
lea    0x9c5(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
jmp    1993 <main_shell+0x6f3>
mov    $0x0,%al
call   11c0 <fork@plt>
mov    %eax,-0x186c(%rbp)
cmpl   $0x0,-0x186c(%rbp)
jne    189f <main_shell+0x5ff>
cmpl   $0x0,-0x1848(%rbp)
jne    16cd <main_shell+0x42d>
cmpl   $0x0,-0x1844(%rbp)
je     16cd <main_shell+0x42d>
lea    0x98d(%rip),%rax
mov    %rax,-0x1030(%rbp)
cmpl   $0x0,-0x184c(%rbp)
jne    16f5 <main_shell+0x455>
cmpl   $0x0,-0x1844(%rbp)
je     16f5 <main_shell+0x455>
lea    0x965(%rip),%rax
mov    %rax,-0x1038(%rbp)
cmpq   $0x0,-0x1030(%rbp)
je     177e <main_shell+0x4de>
mov    -0x1030(%rbp),%rdi
mov    0x28bf(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1150 <open@plt>
mov    %eax,-0x1870(%rbp)
cmpl   $0xffffffff,-0x1870(%rbp)
jne    1743 <main_shell+0x4a3>
lea    0x929(%rip),%rdi
call   1170 <perror@plt>
mov    $0x1,%edi
call   11b0 <exit@plt>
mov    -0x1870(%rbp),%edi
xor    %esi,%esi
call   1090 <dup2@plt>
mov    %eax,-0x1878(%rbp)
cmpl   $0xffffffff,-0x1878(%rbp)
jne    1779 <main_shell+0x4d9>
lea    0x8f8(%rip),%rdi
call   1170 <perror@plt>
mov    $0x2,%edi
call   11b0 <exit@plt>
jmp    177e <main_shell+0x4de>
cmpq   $0x0,-0x1038(%rbp)
je     1821 <main_shell+0x581>
mov    -0x1038(%rbp),%rdi
mov    0x27f6(%rip),%rax
mov    (%rax),%esi
mov    0x284d(%rip),%rax
or     (%rax),%esi
mov    0x284c(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1150 <open@plt>
mov    %eax,-0x1874(%rbp)
cmpl   $0xffffffff,-0x1874(%rbp)
jne    17e3 <main_shell+0x543>
lea    0x889(%rip),%rdi
call   1170 <perror@plt>
mov    $0x1,%edi
call   11b0 <exit@plt>
mov    -0x1874(%rbp),%edi
mov    $0x1,%esi
call   1090 <dup2@plt>
mov    %eax,-0x187c(%rbp)
cmpl   $0xffffffff,-0x187c(%rbp)
jne    181c <main_shell+0x57c>
lea    0x855(%rip),%rdi
call   1170 <perror@plt>
mov    $0x2,%edi
call   11b0 <exit@plt>
jmp    1821 <main_shell+0x581>
cmpl   $0x0,-0x1844(%rbp)
jne    1861 <main_shell+0x5c1>
movq   $0x0,-0x1858(%rbp)
mov    0x2768(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1860(%rbp)
mov    0x2789(%rip),%rax
mov    (%rax),%edi
lea    -0x1860(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <sigaction@plt>
mov    -0x1028(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1190 <execvp@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    1891 <main_shell+0x5f1>
mov    -0x1028(%rbp),%rdi
call   1170 <perror@plt>
movl   $0x1,-0x4(%rbp)
mov    0x2718(%rip),%rax
mov    (%rax),%edi
call   11b0 <exit@plt>
cmpl   $0xffffffff,-0x186c(%rbp)
jne    18bd <main_shell+0x61d>
lea    0x7b4(%rip),%rdi
call   1170 <perror@plt>
jmp    1989 <main_shell+0x6e9>
cmpl   $0x0,-0x1844(%rbp)
je     18ee <main_shell+0x64e>
mov    -0x186c(%rbp),%edi
call   10f0 <append_bg_pid@plt>
mov    -0x186c(%rbp),%esi
lea    0x78c(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
jmp    1984 <main_shell+0x6e4>
mov    -0x186c(%rbp),%edi
lea    -0x4(%rbp),%rsi
xor    %edx,%edx
call   1140 <waitpid@plt>
mov    %eax,-0x186c(%rbp)
cmpl   $0xffffffff,-0x186c(%rbp)
je     197f <main_shell+0x6df>
cmpl   $0x0,-0x186c(%rbp)
je     197f <main_shell+0x6df>
mov    -0x4(%rbp),%edi
call   10d0 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1958 <main_shell+0x6b8>
mov    -0x4(%rbp),%edi
call   1120 <WTERMSIG@plt>
mov    %eax,-0x4(%rbp)
mov    -0x186c(%rbp),%esi
mov    -0x4(%rbp),%edx
lea    0x738(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
jmp    197a <main_shell+0x6da>
mov    -0x4(%rbp),%edi
call   11a0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1975 <main_shell+0x6d5>
mov    -0x4(%rbp),%edi
call   1130 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
jmp    197a <main_shell+0x6da>
jmp    197f <main_shell+0x6df>
jmp    1984 <main_shell+0x6e4>
jmp    1989 <main_shell+0x6e9>
jmp    198e <main_shell+0x6ee>
jmp    1993 <main_shell+0x6f3>
jmp    1998 <main_shell+0x6f8>
jmp    199d <main_shell+0x6fd>
jmp    19a2 <main_shell+0x702>
cmpq   $0x0,-0x18(%rbp)
je     19b6 <main_shell+0x716>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
jmp    12b2 <main_shell+0x12>

x86 assembly (clang, -O2):
<main_shell>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x848,%rsp
movl   $0x0,0x4(%rsp)
lea    0x40(%rsp),%rbx
mov    0x2ceb(%rip),%r15
mov    0x2cfc(%rip),%r14
lea    0x20(%rsp),%r12
lea    0xd18(%rip),%r13
jmp    1322 <main_shell+0x72>
mov    %r14,%rdi
call   1040 <free@plt>
xor    %eax,%eax
call   1090 <term_shell@plt>
mov    0x2cd8(%rip),%r14
lea    0x40(%rsp),%rbx
mov    0x2cb4(%rip),%r15
lea    0x20(%rsp),%r12
lea    0xce8(%rip),%r13
mov    0x18(%rsp),%rdi
call   1040 <free@plt>
mov    $0x1000,%edi
call   1110 <malloc@plt>
mov    %rax,0x18(%rsp)
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   10c0 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x20(%rsp)
mov    (%r14),%edi
mov    %r12,%rsi
xor    %edx,%edx
call   1060 <sigaction@plt>
xor    %eax,%eax
call   1070 <lookup_all_bg_process@plt>
mov    %r13,%rdi
xor    %eax,%eax
call   10b0 <printf@plt>
mov    0x2c34(%rip),%rax
mov    (%rax),%edi
call   1120 <fflush@plt>
mov    0x2c4e(%rip),%rbp
mov    0x0(%rbp),%edi
call   1120 <fflush@plt>
mov    0x0(%rbp),%edx
mov    %rbx,%rdi
mov    $0x800,%esi
call   10d0 <fgets@plt>
mov    %rbx,%rdi
call   1080 <strlen@plt>
cmp    $0x2,%eax
jl     13b9 <main_shell+0x109>
cmpb   $0x23,0x40(%rsp)
je     1322 <main_shell+0x72>
mov    %rbx,%rdi
call   1080 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %rbx,%rdi
lea    0xc40(%rip),%rsi
call   1190 <strtok@plt>
test   %rax,%rax
je     1500 <main_shell+0x250>
mov    0x18(%rsp),%rcx
mov    %rax,0x10(%rsp)
mov    %rcx,%r15
mov    %rax,(%rcx)
xor    %r13d,%r13d
xor    %edi,%edi
lea    0xc14(%rip),%rsi
call   1190 <strtok@plt>
test   %rax,%rax
je     1511 <main_shell+0x261>
mov    %rax,%r14
mov    $0x1,%al
mov    %eax,0x8(%rsp)
xor    %eax,%eax
mov    %rax,0x38(%rsp)
mov    $0x1,%ebp
xor    %eax,%eax
mov    %rax,0x30(%rsp)
movl   $0x0,0xc(%rsp)
xor    %r13d,%r13d
jmp    1465 <main_shell+0x1b5>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %edi,%edi
lea    0xbc8(%rip),%rbx
mov    %rbx,%rsi
call   1190 <strtok@plt>
mov    %rax,0x38(%rsp)
mov    $0x1,%r13d
xor    %r12d,%r12d
xor    %edi,%edi
mov    %rbx,%rsi
call   1190 <strtok@plt>
mov    %rax,%r14
test   %rax,%rax
je     1550 <main_shell+0x2a0>
cmpb   $0x23,(%r14)
je     1537 <main_shell+0x287>
mov    %r14,%rdi
lea    0xb8d(%rip),%rsi
call   10f0 <strcmp@plt>
test   %rax,%rax
je     1430 <main_shell+0x180>
mov    %r14,%rdi
lea    0xb7b(%rip),%rsi
call   10f0 <strcmp@plt>
test   %rax,%rax
je     14d0 <main_shell+0x220>
mov    %r14,%rdi
lea    0xb69(%rip),%rsi
call   10f0 <strcmp@plt>
test   %rax,%rax
je     1540 <main_shell+0x290>
movslq %ebp,%rax
add    $0x1,%ebp
mov    %r14,(%r15,%rax,8)
lea    0xb41(%rip),%rbx
jmp    144c <main_shell+0x19c>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %edi,%edi
lea    0xb28(%rip),%rbx
mov    %rbx,%rsi
call   1190 <strtok@plt>
mov    %rax,0x30(%rsp)
movl   $0x1,0xc(%rsp)
jmp    144c <main_shell+0x19c>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmpq   $0x0,0x18(%rsp)
jne    1318 <main_shell+0x68>
jmp    1322 <main_shell+0x72>
mov    $0x1,%al
mov    %eax,0x8(%rsp)
mov    $0x1,%ebp
movl   $0x0,0xc(%rsp)
xor    %eax,%eax
mov    %rax,0x30(%rsp)
xor    %eax,%eax
mov    %rax,0x38(%rsp)
xor    %r12d,%r12d
jmp    1550 <main_shell+0x2a0>
movb   $0x0,(%r14)
xor    %r12d,%r12d
jmp    1550 <main_shell+0x2a0>
mov    $0x1,%r12b
movl   $0x0,0x8(%rsp)
nopl   0x0(%rax,%rax,1)
mov    0x10(%rsp),%rbx
mov    %r15,%r14
movslq %ebp,%rax
movq   $0x0,(%r15,%rax,8)
mov    %rbx,%rdi
lea    0xa9f(%rip),%rsi
call   10f0 <strcmp@plt>
test   %rax,%rax
je     12ea <main_shell+0x3a>
mov    %rbx,%rdi
lea    0xa8c(%rip),%rsi
call   10f0 <strcmp@plt>
test   %rax,%rax
je     1641 <main_shell+0x391>
mov    %rbx,%rdi
lea    0xa7c(%rip),%rsi
call   10f0 <strcmp@plt>
test   %rax,%rax
je     1674 <main_shell+0x3c4>
xor    %eax,%eax
call   11d0 <fork@plt>
cmp    $0xffffffff,%eax
je     1700 <main_shell+0x450>
mov    %eax,%ebp
test   %eax,%eax
je     1711 <main_shell+0x461>
cmpb   $0x0,0x8(%rsp)
lea    0x40(%rsp),%rbx
mov    0x29ea(%rip),%r15
lea    0x20(%rsp),%r12
lea    0xa1e(%rip),%r13
je     168b <main_shell+0x3db>
mov    %ebp,%edi
lea    0x4(%rsp),%rsi
xor    %edx,%edx
call   1150 <waitpid@plt>
mov    %eax,%ebp
lea    -0x1(%rbp),%ecx
cmp    $0xfffffffd,%ecx
mov    0x29d3(%rip),%r14
ja     1318 <main_shell+0x68>
mov    0x4(%rsp),%edi
call   10e0 <WIFSIGNALED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
je     16e0 <main_shell+0x430>
call   1130 <WTERMSIG@plt>
mov    %eax,0x4(%rsp)
lea    0xa2f(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   10b0 <printf@plt>
jmp    1318 <main_shell+0x68>
cmp    $0x2,%ebp
lea    0x40(%rsp),%rbx
mov    0x2970(%rip),%r15
lea    0x20(%rsp),%r12
lea    0x9a4(%rip),%r13
je     16ae <main_shell+0x3fe>
cmp    $0x1,%ebp
jne    16c8 <main_shell+0x418>
lea    0x9aa(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
jmp    16b7 <main_shell+0x407>
mov    0x4(%rsp),%esi
lea    0x9a1(%rip),%rdi
xor    %eax,%eax
call   10b0 <printf@plt>
jmp    12f9 <main_shell+0x49>
mov    %ebp,%edi
call   1100 <append_bg_pid@plt>
lea    0x9b1(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   10b0 <printf@plt>
mov    0x292f(%rip),%r14
jmp    1318 <main_shell+0x68>
mov    0x18(%rsp),%rax
mov    0x8(%rax),%rdi
call   1170 <change_dir@plt>
mov    0x2915(%rip),%r14
jmp    1318 <main_shell+0x68>
lea    0x9ba(%rip),%rdi
call   1050 <puts@plt>
mov    0x28fd(%rip),%r14
jmp    1318 <main_shell+0x68>
call   11b0 <WIFEXITED@plt>
test   %rax,%rax
je     1318 <main_shell+0x68>
mov    0x4(%rsp),%edi
call   1140 <WEXITSTATUS@plt>
mov    %eax,0x4(%rsp)
jmp    1318 <main_shell+0x68>
lea    0x93c(%rip),%rdi
call   1180 <perror@plt>
jmp    12f9 <main_shell+0x49>
lea    0x917(%rip),%rbp
test   %r12b,%r12b
mov    0x38(%rsp),%r15
mov    %r15,%rdi
cmovne %rbp,%rdi
mov    0x30(%rsp),%rax
cmove  %rax,%rbp
test   %r13d,%r13d
cmovne %r15,%rdi
cmpl   $0x0,0xc(%rsp)
cmovne %rax,%rbp
test   %rdi,%rdi
jne    17f8 <main_shell+0x548>
test   %rbp,%rbp
je     1796 <main_shell+0x4e6>
mov    0x283b(%rip),%rax
mov    0x2894(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2891(%rip),%rax
or     (%rax),%esi
mov    %rbp,%rdi
mov    $0x1a4,%edx
xor    %eax,%eax
call   1160 <open@plt>
cmp    $0xffffffff,%eax
je     180d <main_shell+0x55d>
mov    %eax,%edi
mov    $0x1,%esi
call   10a0 <dup2@plt>
cmp    $0xffffffff,%eax
je     1835 <main_shell+0x585>
test   %r12b,%r12b
jne    17c6 <main_shell+0x516>
movq   $0x0,0x28(%rsp)
mov    0x27fd(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2820(%rip),%rax
mov    (%rax),%edi
lea    0x20(%rsp),%rsi
xor    %edx,%edx
call   1060 <sigaction@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   11a0 <execvp@plt>
mov    %eax,0x4(%rsp)
cmp    $0xffffffff,%eax
jne    17ea <main_shell+0x53a>
mov    %rbx,%rdi
call   1180 <perror@plt>
movl   $0x1,0x4(%rsp)
mov    0x27bf(%rip),%rax
mov    (%rax),%edi
call   11c0 <exit@plt>
mov    0x27d1(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1160 <open@plt>
cmp    $0xffffffff,%eax
jne    1823 <main_shell+0x573>
lea    0x825(%rip),%rdi
call   1180 <perror@plt>
mov    $0x1,%edi
call   11c0 <exit@plt>
mov    %eax,%edi
xor    %esi,%esi
call   10a0 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1749 <main_shell+0x499>
lea    0x802(%rip),%rdi
call   1180 <perror@plt>
mov    $0x2,%edi
call   11c0 <exit@plt>

LLVM IR (-O0):
%struct.sigaction = type { i32, i64 }
@SIG_IGN = global i32 0
@SIGINT = global i32 0
@.str = [3 x i8] c": \00"
@stdout = global i32 0
@stdin = global i32 0
@.str.1 = [3 x i8] c" \0A\00"
@.str.2 = [2 x i8] c" \00"
@.str.3 = [2 x i8] c"<\00"
@.str.4 = [2 x i8] c">\00"
@.str.5 = [2 x i8] c"&\00"
@.str.6 = [5 x i8] c"exit\00"
@.str.7 = [3 x i8] c"cd\00"
@.str.8 = [5 x i8] c"HOME\00"
@.str.9 = [36 x i8] c"smallsh: cd: usage: cd [directory]\0A\00"
@.str.10 = [7 x i8] c"status\00"
@.str.11 = [15 x i8] c"exit value %d\0A\00"
@.str.12 = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@.str.13 = [5 x i8] c"open\00"
@.str.14 = [5 x i8] c"dup2\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@SIG_DFL = global i32 0
@EXIT_FAILURE = global i32 0
@.str.15 = [7 x i8] c"fork()\00"
@.str.16 = [22 x i8] c"background pid is %d\0A\00"
@.str.17 = [41 x i8] c"pid %d is done: terminated by signal %d\0A\00"
define void @main_shell() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8**
%5 = alloca [513 x i8*]
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca [2049 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.sigaction
%14 = alloca i8*
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
store i32 0, i32* %1
br label %20
20: ; preds = %0, %45, %308
store i32 0, i32* %2
store i32 0, i32* %3
%21 = call i8** @malloc(i32 4096)
store i8** %21, i8*** %4
store i8* null, i8** %6
store i8* null, i8** %7
store i8* null, i8** %8
%22 = getelementptr inbounds [2049 x i8], [2049 x i8]* %9, i64 0, i64 0
%23 = call i32 @memset(i8* %22, i8 signext 0, i32 2048)
store i32 0, i32* %10
store i32 0, i32* %11
store i32 0, i32* %12
%24 = load i32, i32* @SIG_IGN
%25 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %13, i32 0, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* @SIGINT
%27 = call i32 @sigaction(i32 %26, %struct.sigaction* %13, i32* null)
%28 = call i32 (...) @lookup_all_bg_process()
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%30 = load i32, i32* @stdout
%31 = call i32 @fflush(i32 %30)
%32 = load i32, i32* @stdin
%33 = call i32 @fflush(i32 %32)
%34 = getelementptr inbounds [2049 x i8], [2049 x i8]* %9, i64 0, i64 0
%35 = load i32, i32* @stdin
%36 = call i32 @fgets(i8* %34, i32 2048, i32 %35)
%37 = getelementptr inbounds [2049 x i8], [2049 x i8]* %9, i64 0, i64 0
%38 = call i32 @strlen(i8* %37)
%39 = icmp sgt i32 %38, 1
br i1 %39, label %40, label %52
40: ; preds = %20
%41 = getelementptr inbounds [2049 x i8], [2049 x i8]* %9, i64 0, i64 0
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp eq i32 %43, 35
br i1 %44, label %45, label %46
45: ; preds = %40
br label %20
46: ; preds = %40
%47 = getelementptr inbounds [2049 x i8], [2049 x i8]* %9, i64 0, i64 0
%48 = call i32 @strlen(i8* %47)
%49 = sub nsw i32 %48, 1
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [2049 x i8], [2049 x i8]* %9, i64 0, i64 %50
store i8 0, i8* %51
br label %52
52: ; preds = %46, %20
%53 = getelementptr inbounds [2049 x i8], [2049 x i8]* %9, i64 0, i64 0
%54 = call i8* @strtok(i8* %53, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i8* %54, i8** %6
%55 = icmp ne i8* %54, null
br i1 %55, label %56, label %302
56: ; preds = %52
%57 = load i8*, i8** %6
%58 = load i8**, i8*** %4
%59 = load i32, i32* %2
%60 = add nsw i32 %59, 1
store i32 %60, i32* %2
%61 = sext i32 %59 to i64
%62 = getelementptr inbounds i8*, i8** %58, i64 %61
store i8* %57, i8** %62
br label %63
63: ; preds = %128, %56
%64 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%65 = load i32, i32* %3
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds [513 x i8*], [513 x i8*]* %5, i64 0, i64 %66
store i8* %64, i8** %67
%68 = icmp ne i8* %64, null
br i1 %68, label %69, label %129
69: ; preds = %63
%70 = load i32, i32* %3
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds [513 x i8*], [513 x i8*]* %5, i64 0, i64 %71
%73 = load i8*, i8** %72
store i8* %73, i8** %14
%74 = load i8*, i8** %14
%75 = getelementptr inbounds i8, i8* %74, i64 0
%76 = load i8, i8* %75
%77 = sext i8 %76 to i32
%78 = icmp eq i32 %77, 35
br i1 %78, label %79, label %82
79: ; preds = %69
%80 = load i8*, i8** %14
%81 = getelementptr inbounds i8, i8* %80, i64 0
store i8 0, i8* %81
br label %129
82: ; preds = %69
%83 = load i8*, i8** %14
%84 = call i64 @strcmp(i8* %83, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%85 = icmp eq i64 %84, 0
br i1 %85, label %86, label %96
86: ; preds = %82
%87 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%88 = load i32, i32* %3
%89 = add nsw i32 %88, 1
store i32 %89, i32* %3
%90 = sext i32 %89 to i64
%91 = getelementptr inbounds [513 x i8*], [513 x i8*]* %5, i64 0, i64 %90
store i8* %87, i8** %91
%92 = load i32, i32* %3
%93 = sext i32 %92 to i64
%94 = getelementptr inbounds [513 x i8*], [513 x i8*]* %5, i64 0, i64 %93
%95 = load i8*, i8** %94
store i8* %95, i8** %7
store i32 1, i32* %11
br label %128
96: ; preds = %82
%97 = load i8*, i8** %14
%98 = call i64 @strcmp(i8* %97, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%99 = icmp eq i64 %98, 0
br i1 %99, label %100, label %110
100: ; preds = %96
%101 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%102 = load i32, i32* %3
%103 = add nsw i32 %102, 1
store i32 %103, i32* %3
%104 = sext i32 %103 to i64
%105 = getelementptr inbounds [513 x i8*], [513 x i8*]* %5, i64 0, i64 %104
store i8* %101, i8** %105
%106 = load i32, i32* %3
%107 = sext i32 %106 to i64
%108 = getelementptr inbounds [513 x i8*], [513 x i8*]* %5, i64 0, i64 %107
%109 = load i8*, i8** %108
store i8* %109, i8** %8
store i32 1, i32* %12
br label %127
110: ; preds = %96
%111 = load i8*, i8** %14
%112 = call i64 @strcmp(i8* %111, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%113 = icmp eq i64 %112, 0
br i1 %113, label %114, label %115
114: ; preds = %110
store i32 1, i32* %10
br label %129
115: ; preds = %110
%116 = load i32, i32* %3
%117 = add nsw i32 %116, 1
store i32 %117, i32* %3
%118 = sext i32 %116 to i64
%119 = getelementptr inbounds [513 x i8*], [513 x i8*]* %5, i64 0, i64 %118
%120 = load i8*, i8** %119
%121 = load i8**, i8*** %4
%122 = load i32, i32* %2
%123 = add nsw i32 %122, 1
store i32 %123, i32* %2
%124 = sext i32 %122 to i64
%125 = getelementptr inbounds i8*, i8** %121, i64 %124
store i8* %120, i8** %125
br label %126
126: ; preds = %115
br label %127
127: ; preds = %126, %100
br label %128
128: ; preds = %127, %86
br label %63
129: ; preds = %114, %79, %63
%130 = load i8**, i8*** %4
%131 = load i32, i32* %2
%132 = sext i32 %131 to i64
%133 = getelementptr inbounds i8*, i8** %130, i64 %132
store i8* null, i8** %133
%134 = load i8*, i8** %6
%135 = call i64 @strcmp(i8* %134, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%136 = icmp eq i64 %135, 0
br i1 %136, label %137, label %141
137: ; preds = %129
%138 = load i8**, i8*** %4
%139 = call i32 @free(i8** %138)
%140 = call i32 (...) @term_shell()
br label %301
141: ; preds = %129
%142 = load i8*, i8** %6
%143 = call i64 @strcmp(i8* %142, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
%144 = icmp eq i64 %143, 0
br i1 %144, label %145, label %163
145: ; preds = %141
%146 = load i32, i32* %2
%147 = icmp eq i32 %146, 1
br i1 %147, label %148, label %151
148: ; preds = %145
%149 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%150 = call i32 @change_dir(i8* %149)
br label %162
151: ; preds = %145
%152 = load i32, i32* %2
%153 = icmp eq i32 %152, 2
br i1 %153, label %154, label %159
154: ; preds = %151
%155 = load i8**, i8*** %4
%156 = getelementptr inbounds i8*, i8** %155, i64 1
%157 = load i8*, i8** %156
%158 = call i32 @change_dir(i8* %157)
br label %161
159: ; preds = %151
%160 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.9, i64 0, i64 0))
br label %161
161: ; preds = %159, %154
br label %162
162: ; preds = %161, %148
br label %300
163: ; preds = %141
%164 = load i8*, i8** %6
%165 = call i64 @strcmp(i8* %164, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.10, i64 0, i64 0))
%166 = icmp eq i64 %165, 0
br i1 %166, label %167, label %170
167: ; preds = %163
%168 = load i32, i32* %1
%169 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.11, i64 0, i64 0), i32 %168)
br label %299
170: ; preds = %163
%171 = call i32 (...) @fork()
store i32 %171, i32* %15
%172 = load i32, i32* %15
%173 = icmp eq i32 %172, 0
br i1 %173, label %174, label %255
174: ; preds = %170
%175 = load i32, i32* %11
%176 = icmp ne i32 %175, 0
br i1 %176, label %181, label %177
177: ; preds = %174
%178 = load i32, i32* %10
%179 = icmp ne i32 %178, 0
br i1 %179, label %180, label %181
180: ; preds = %177
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.12, i64 0, i64 0), i8** %7
br label %181
181: ; preds = %180, %177, %174
%182 = load i32, i32* %12
%183 = icmp ne i32 %182, 0
br i1 %183, label %188, label %184
184: ; preds = %181
%185 = load i32, i32* %10
%186 = icmp ne i32 %185, 0
br i1 %186, label %187, label %188
187: ; preds = %184
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.12, i64 0, i64 0), i8** %8
br label %188
188: ; preds = %187, %184, %181
%189 = load i8*, i8** %7
%190 = icmp ne i8* %189, null
br i1 %190, label %191, label %209
191: ; preds = %188
%192 = load i8*, i8** %7
%193 = load i32, i32* @O_RDONLY
%194 = call i32 (i8*, i32, ...) @open(i8* %192, i32 %193)
store i32 %194, i32* %16
%195 = load i32, i32* %16
%196 = icmp eq i32 %195, -1
br i1 %196, label %197, label %200
197: ; preds = %191
%198 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.13, i64 0, i64 0))
%199 = call i32 @exit(i32 1)
unreachable
200: ; preds = %191
%201 = load i32, i32* %16
%202 = call i32 @dup2(i32 %201, i32 0)
store i32 %202, i32* %18
%203 = load i32, i32* %18
%204 = icmp eq i32 %203, -1
br i1 %204, label %205, label %208
205: ; preds = %200
%206 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.14, i64 0, i64 0))
%207 = call i32 @exit(i32 2)
unreachable
208: ; preds = %200
br label %209
209: ; preds = %208, %188
%210 = load i8*, i8** %8
%211 = icmp ne i8* %210, null
br i1 %211, label %212, label %234
212: ; preds = %209
%213 = load i8*, i8** %8
%214 = load i32, i32* @O_WRONLY
%215 = load i32, i32* @O_TRUNC
%216 = or i32 %214, %215
%217 = load i32, i32* @O_CREAT
%218 = or i32 %216, %217
%219 = call i32 (i8*, i32, ...) @open(i8* %213, i32 %218, i32 420)
store i32 %219, i32* %17
%220 = load i32, i32* %17
%221 = icmp eq i32 %220, -1
br i1 %221, label %222, label %225
222: ; preds = %212
%223 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.13, i64 0, i64 0))
%224 = call i32 @exit(i32 1)
unreachable
225: ; preds = %212
%226 = load i32, i32* %17
%227 = call i32 @dup2(i32 %226, i32 1)
store i32 %227, i32* %19
%228 = load i32, i32* %19
%229 = icmp eq i32 %228, -1
br i1 %229, label %230, label %233
230: ; preds = %225
%231 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.14, i64 0, i64 0))
%232 = call i32 @exit(i32 2)
unreachable
233: ; preds = %225
br label %234
234: ; preds = %233, %209
%235 = load i32, i32* %10
%236 = icmp ne i32 %235, 0
br i1 %236, label %243, label %237
237: ; preds = %234
%238 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %13, i32 0, i32 1
store i64 0, i64* %238
%239 = load i32, i32* @SIG_DFL
%240 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %13, i32 0, i32 0
store i32 %239, i32* %240
%241 = load i32, i32* @SIGINT
%242 = call i32 @sigaction(i32 %241, %struct.sigaction* %13, i32* null)
br label %243
243: ; preds = %237, %234
%244 = load i8*, i8** %6
%245 = load i8**, i8*** %4
%246 = call i32 @execvp(i8* %244, i8** %245)
store i32 %246, i32* %1
%247 = load i32, i32* %1
%248 = icmp eq i32 %247, -1
br i1 %248, label %249, label %252
249: ; preds = %243
%250 = load i8*, i8** %6
%251 = call i32 @perror(i8* %250)
store i32 1, i32* %1
br label %252
252: ; preds = %249, %243
%253 = load i32, i32* @EXIT_FAILURE
%254 = call i32 @exit(i32 %253)
unreachable
255: ; preds = %170
%256 = load i32, i32* %15
%257 = icmp eq i32 %256, -1
br i1 %257, label %258, label %260
258: ; preds = %255
%259 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.15, i64 0, i64 0))
br label %297
260: ; preds = %255
%261 = load i32, i32* %10
%262 = icmp ne i32 %261, 0
br i1 %262, label %263, label %268
263: ; preds = %260
%264 = load i32, i32* %15
%265 = call i32 @append_bg_pid(i32 %264)
%266 = load i32, i32* %15
%267 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.16, i64 0, i64 0), i32 %266)
br label %296
268: ; preds = %260
%269 = load i32, i32* %15
%270 = call i32 @waitpid(i32 %269, i32* %1, i32 0)
store i32 %270, i32* %15
%271 = load i32, i32* %15
%272 = icmp ne i32 %271, -1
br i1 %272, label %273, label %295
273: ; preds = %268
%274 = load i32, i32* %15
%275 = icmp ne i32 %274, 0
br i1 %275, label %276, label %295
276: ; preds = %273
%277 = load i32, i32* %1
%278 = call i64 @WIFSIGNALED(i32 %277)
%279 = icmp ne i64 %278, 0
br i1 %279, label %280, label %286
280: ; preds = %276
%281 = load i32, i32* %1
%282 = call i32 @WTERMSIG(i32 %281)
store i32 %282, i32* %1
%283 = load i32, i32* %15
%284 = load i32, i32* %1
%285 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.17, i64 0, i64 0), i32 %283, i32 %284)
br label %294
286: ; preds = %276
%287 = load i32, i32* %1
%288 = call i64 @WIFEXITED(i32 %287)
%289 = icmp ne i64 %288, 0
br i1 %289, label %290, label %293
290: ; preds = %286
%291 = load i32, i32* %1
%292 = call i32 @WEXITSTATUS(i32 %291)
store i32 %292, i32* %1
br label %293
293: ; preds = %290, %286
br label %294
294: ; preds = %293, %280
br label %295
295: ; preds = %294, %273, %268
br label %296
296: ; preds = %295, %263
br label %297
297: ; preds = %296, %258
br label %298
298: ; preds = %297
br label %299
299: ; preds = %298, %167
br label %300
300: ; preds = %299, %162
br label %301
301: ; preds = %300, %137
br label %302
302: ; preds = %301, %52
%303 = load i8**, i8*** %4
%304 = icmp ne i8** %303, null
br i1 %304, label %305, label %308
305: ; preds = %302
%306 = load i8**, i8*** %4
%307 = call i32 @free(i8** %306)
br label %308
308: ; preds = %305, %302
br label %20
}

LLVM IR (-O2):
%struct.sigaction = type { i32, i64 }
@SIG_IGN = global i32 0
@SIGINT = global i32 0
@.str = [3 x i8] c": \00"
@stdout = global i32 0
@stdin = global i32 0
@.str.1 = [3 x i8] c" \0A\00"
@.str.2 = [2 x i8] c" \00"
@.str.3 = [2 x i8] c"<\00"
@.str.4 = [2 x i8] c">\00"
@.str.5 = [2 x i8] c"&\00"
@.str.6 = [5 x i8] c"exit\00"
@.str.7 = [3 x i8] c"cd\00"
@.str.8 = [5 x i8] c"HOME\00"
@.str.10 = [7 x i8] c"status\00"
@.str.11 = [15 x i8] c"exit value %d\0A\00"
@.str.12 = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@.str.13 = [5 x i8] c"open\00"
@.str.14 = [5 x i8] c"dup2\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@SIG_DFL = global i32 0
@EXIT_FAILURE = global i32 0
@.str.15 = [7 x i8] c"fork()\00"
@.str.16 = [22 x i8] c"background pid is %d\0A\00"
@.str.17 = [41 x i8] c"pid %d is done: terminated by signal %d\0A\00"
@str = [35 x i8] c"smallsh: cd: usage: cd [directory]\00"
define void @main_shell() {
%1 = alloca i32
%2 = alloca [2049 x i8]
%3 = alloca %struct.sigaction
%4 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%5 = getelementptr inbounds [2049 x i8], [2049 x i8]* %2, i64 0, i64 0
%6 = bitcast %struct.sigaction* %3 to i8*
%7 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 0
br label %8
8: ; preds = %191, %0
%9 = call i8** @malloc(i32 4096)
%10 = call i32 @memset(i8* %5, i8 signext 0, i32 2048)
%11 = load i32, i32* @SIG_IGN
store i32 %11, i32* %7
%12 = load i32, i32* @SIGINT
%13 = call i32 @sigaction(i32 %12, %struct.sigaction* %3, i32* null)
%14 = call i32 (...) @lookup_all_bg_process()
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @stdout
%17 = call i32 @fflush(i32 %16)
%18 = load i32, i32* @stdin
%19 = call i32 @fflush(i32 %18)
%20 = load i32, i32* @stdin
%21 = call i32 @fgets(i8* %5, i32 2048, i32 %20)
%22 = call i32 @strlen(i8* %5)
%23 = icmp sgt i32 %22, 1
br i1 %23, label %24, label %32
24: ; preds = %8
%25 = load i8, i8* %5
%26 = icmp eq i8 %25, 35
br i1 %26, label %191, label %27
27: ; preds = %24
%28 = call i32 @strlen(i8* %5)
%29 = add nsw i32 %28, -1
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [2049 x i8], [2049 x i8]* %2, i64 0, i64 %30
store i8 0, i8* %31
br label %32
32: ; preds = %27, %8
%33 = call i8* @strtok(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%34 = icmp eq i8* %33, null
br i1 %34, label %187, label %35
35: ; preds = %32
store i8* %33, i8** %9
%36 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%37 = icmp eq i8* %36, null
br i1 %37, label %73, label %38
38: ; preds = %35, %65
%39 = phi i8* [ %71, %65 ], [ %36, %35 ]
%40 = phi i32 [ %70, %65 ], [ 1, %35 ]
%41 = phi i8* [ %69, %65 ], [ null, %35 ]
%42 = phi i8* [ %68, %65 ], [ null, %35 ]
%43 = phi i32 [ %67, %65 ], [ 0, %35 ]
%44 = phi i32 [ %66, %65 ], [ 0, %35 ]
%45 = load i8, i8* %39
%46 = icmp eq i8 %45, 35
br i1 %46, label %47, label %48
47: ; preds = %38
store i8 0, i8* %39
br label %73
48: ; preds = %38
%49 = call i64 @strcmp(i8* %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %48
%52 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %65
53: ; preds = %48
%54 = call i64 @strcmp(i8* %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %53
%57 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %65
58: ; preds = %53
%59 = call i64 @strcmp(i8* %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%60 = icmp eq i64 %59, 0
br i1 %60, label %73, label %61
61: ; preds = %58
%62 = add nsw i32 %40, 1
%63 = sext i32 %40 to i64
%64 = getelementptr inbounds i8*, i8** %9, i64 %63
store i8* %39, i8** %64
br label %65
65: ; preds = %51, %61, %56
%66 = phi i32 [ 1, %51 ], [ %44, %56 ], [ %44, %61 ]
%67 = phi i32 [ %43, %51 ], [ 1, %56 ], [ %43, %61 ]
%68 = phi i8* [ %42, %51 ], [ %57, %56 ], [ %42, %61 ]
%69 = phi i8* [ %52, %51 ], [ %41, %56 ], [ %41, %61 ]
%70 = phi i32 [ %40, %51 ], [ %40, %56 ], [ %62, %61 ]
%71 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%72 = icmp eq i8* %71, null
br i1 %72, label %73, label %38
73: ; preds = %65, %58, %35, %47
%74 = phi i32 [ %44, %47 ], [ 0, %35 ], [ %66, %65 ], [ %44, %58 ]
%75 = phi i32 [ %43, %47 ], [ 0, %35 ], [ %67, %65 ], [ %43, %58 ]
%76 = phi i8* [ %42, %47 ], [ null, %35 ], [ %68, %65 ], [ %42, %58 ]
%77 = phi i8* [ %41, %47 ], [ null, %35 ], [ %69, %65 ], [ %41, %58 ]
%78 = phi i32 [ %40, %47 ], [ 1, %35 ], [ %70, %65 ], [ %40, %58 ]
%79 = phi i1 [ false, %47 ], [ false, %35 ], [ false, %65 ], [ true, %58 ]
%80 = phi i1 [ true, %47 ], [ true, %35 ], [ true, %65 ], [ false, %58 ]
%81 = sext i32 %78 to i64
%82 = getelementptr inbounds i8*, i8** %9, i64 %81
store i8* null, i8** %82
%83 = call i64 @strcmp(i8* %33, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%84 = icmp eq i64 %83, 0
br i1 %84, label %85, label %88
85: ; preds = %73
%86 = call i32 @free(i8** %9)
%87 = call i32 (...) @term_shell()
br label %189
88: ; preds = %73
%89 = call i64 @strcmp(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
%90 = icmp eq i64 %89, 0
br i1 %90, label %91, label %101
91: ; preds = %88
switch i32 %78, label %99 [
i32 1, label %92
i32 2, label %95
]
92: ; preds = %91
%93 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%94 = call i32 @change_dir(i8* %93)
br label %189
95: ; preds = %91
%96 = getelementptr inbounds i8*, i8** %9, i64 1
%97 = load i8*, i8** %96
%98 = call i32 @change_dir(i8* %97)
br label %189
99: ; preds = %91
%100 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
br label %189
101: ; preds = %88
%102 = call i64 @strcmp(i8* %33, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.10, i64 0, i64 0))
%103 = icmp eq i64 %102, 0
br i1 %103, label %104, label %107
104: ; preds = %101
%105 = load i32, i32* %1
%106 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.11, i64 0, i64 0), i32 %105)
br label %189
107: ; preds = %101
%108 = call i32 (...) @fork()
switch i32 %108, label %165 [
i32 0, label %109
i32 -1, label %163
]
109: ; preds = %107
%110 = icmp eq i32 %74, 0
%111 = and i1 %110, %79
%112 = select i1 %111, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.12, i64 0, i64 0), i8* %77
%113 = icmp eq i32 %75, 0
%114 = and i1 %113, %79
%115 = select i1 %114, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.12, i64 0, i64 0), i8* %76
%116 = icmp eq i8* %112, null
br i1 %116, label %130, label %117
117: ; preds = %109
%118 = load i32, i32* @O_RDONLY
%119 = call i32 (i8*, i32, ...) @open(i8* %112, i32 %118)
%120 = icmp eq i32 %119, -1
br i1 %120, label %121, label %124
121: ; preds = %117
%122 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.13, i64 0, i64 0))
%123 = call i32 @exit(i32 1)
unreachable
124: ; preds = %117
%125 = call i32 @dup2(i32 %119, i32 0)
%126 = icmp eq i32 %125, -1
br i1 %126, label %127, label %130
127: ; preds = %124
%128 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.14, i64 0, i64 0))
%129 = call i32 @exit(i32 2)
unreachable
130: ; preds = %124, %109
%131 = icmp eq i8* %115, null
br i1 %131, label %149, label %132
132: ; preds = %130
%133 = load i32, i32* @O_WRONLY
%134 = load i32, i32* @O_TRUNC
%135 = or i32 %134, %133
%136 = load i32, i32* @O_CREAT
%137 = or i32 %135, %136
%138 = call i32 (i8*, i32, ...) @open(i8* %115, i32 %137, i32 420)
%139 = icmp eq i32 %138, -1
br i1 %139, label %140, label %143
140: ; preds = %132
%141 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.13, i64 0, i64 0))
%142 = call i32 @exit(i32 1)
unreachable
143: ; preds = %132
%144 = call i32 @dup2(i32 %138, i32 1)
%145 = icmp eq i32 %144, -1
br i1 %145, label %146, label %149
146: ; preds = %143
%147 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.14, i64 0, i64 0))
%148 = call i32 @exit(i32 2)
unreachable
149: ; preds = %143, %130
br i1 %79, label %155, label %150
150: ; preds = %149
%151 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 1
store i64 0, i64* %151
%152 = load i32, i32* @SIG_DFL
store i32 %152, i32* %7
%153 = load i32, i32* @SIGINT
%154 = call i32 @sigaction(i32 %153, %struct.sigaction* %3, i32* null)
br label %155
155: ; preds = %150, %149
%156 = call i32 @execvp(i8* %33, i8** %9)
store i32 %156, i32* %1
%157 = icmp eq i32 %156, -1
br i1 %157, label %158, label %160
158: ; preds = %155
%159 = call i32 @perror(i8* %33)
store i32 1, i32* %1
br label %160
160: ; preds = %158, %155
%161 = load i32, i32* @EXIT_FAILURE
%162 = call i32 @exit(i32 %161)
unreachable
163: ; preds = %107
%164 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.15, i64 0, i64 0))
br label %189
165: ; preds = %107
br i1 %80, label %169, label %166
166: ; preds = %165
%167 = call i32 @append_bg_pid(i32 %108)
%168 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.16, i64 0, i64 0), i32 %108)
br label %189
169: ; preds = %165
%170 = call i32 @waitpid(i32 %108, i32* %1, i32 0)
%171 = add i32 %170, -1
%172 = icmp ult i32 %171, -2
br i1 %172, label %173, label %189
173: ; preds = %169
%174 = load i32, i32* %1
%175 = call i64 @WIFSIGNALED(i32 %174)
%176 = icmp eq i64 %175, 0
%177 = load i32, i32* %1
br i1 %176, label %181, label %178
178: ; preds = %173
%179 = call i32 @WTERMSIG(i32 %177)
store i32 %179, i32* %1
%180 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.17, i64 0, i64 0), i32 %170, i32 %179)
br label %189
181: ; preds = %173
%182 = call i64 @WIFEXITED(i32 %177)
%183 = icmp eq i64 %182, 0
br i1 %183, label %189, label %184
184: ; preds = %181
%185 = load i32, i32* %1
%186 = call i32 @WEXITSTATUS(i32 %185)
store i32 %186, i32* %1
br label %189
187: ; preds = %32
%188 = icmp eq i8** %9, null
br i1 %188, label %191, label %189
189: ; preds = %95, %99, %92, %104, %85, %163, %169, %181, %184, %178, %166, %187
%190 = call i32 @free(i8** %9)
br label %191
191: ; preds = %187, %189, %24
br label %8
}