#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 ssize_t;
typedef int pid_t;

/* Variables and functions */
int WEXITSTATUS(int);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ printf(char *, int, int);
scalar_t__ read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ strcmp(char *, char *);
char *strtok(char *, char *);
int wait(int *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  char buf[1000];
  char *args[1000];
  ssize_t len;
  write(1, "$ ", 2);
  while (len = read(0, buf, 1000) && *buf && *buf != '\n') {
    args[0] = strtok(buf, " \t\n");
    if (!strcmp(args[0], "exit") || !strcmp(args[0], "quit"))
      return 0;
    size_t i = 0;
    while (args[++i] = strtok(NULL, " \t\n"))
      ;
    args[i - 1] = NULL;
    bool background = false;
    if (*args[i - 2] == '&')
      background = true;
    if (!fork())
      execvp(args[0], args);
    else {
      int status;
      if (!background || background && !fork()) {
        pid_t cpid = wait(&status);
        printf("Child %d exited with status code %d.\n", cpid,
               WEXITSTATUS(status));
      }
    }
    write(1, "$ ", 2);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2370,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x1,%edi
lea    0xe4b(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
lea    -0x400(%rbp),%rsi
xor    %edi,%edi
mov    $0x3e8,%edx
call   1050 <read@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x235d(%rbp)
je     1212 <main+0x82>
movsbl -0x400(%rbp),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x235d(%rbp)
je     1212 <main+0x82>
movsbl -0x400(%rbp),%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x235d(%rbp)
mov    -0x235d(%rbp),%al
mov    %al,%cl
and    $0x1,%cl
movzbl %cl,%ecx
mov    %ecx,-0x2344(%rbp)
test   $0x1,%al
jne    1233 <main+0xa3>
jmp    13d1 <main+0x241>
lea    -0x400(%rbp),%rdi
lea    0xdc2(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x2340(%rbp)
mov    -0x2340(%rbp),%rdi
lea    0xdac(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
je     1285 <main+0xf5>
mov    -0x2340(%rbp),%rdi
lea    0xd95(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1291 <main+0x101>
movl   $0x0,-0x4(%rbp)
jmp    13d8 <main+0x248>
movq   $0x0,-0x2350(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd5c(%rip),%rsi
call   1080 <strtok@plt>
mov    -0x2350(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x2350(%rbp)
mov    %rax,-0x2338(%rbp,%rcx,8)
ff
cmp    $0x0,%rax
je     12d8 <main+0x148>
jmp    129c <main+0x10c>
mov    -0x2350(%rbp),%rax
sub    $0x1,%rax
movq   $0x0,-0x2340(%rbp,%rax,8)
movl   $0x0,-0x2354(%rbp)
mov    -0x2350(%rbp),%rax
sub    $0x2,%rax
mov    -0x2340(%rbp,%rax,8),%rax
ff
movsbl (%rax),%eax
cmp    $0x26,%eax
jne    1322 <main+0x192>
movl   $0x1,-0x2354(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
cmp    $0x0,%eax
jne    134a <main+0x1ba>
mov    -0x2340(%rbp),%rdi
lea    -0x2340(%rbp),%rsi
call   1090 <execvp@plt>
jmp    13b6 <main+0x226>
cmpl   $0x0,-0x2354(%rbp)
je     1374 <main+0x1e4>
cmpl   $0x0,-0x2354(%rbp)
je     13b1 <main+0x221>
mov    $0x0,%al
call   10b0 <fork@plt>
cmp    $0x0,%eax
jne    13b1 <main+0x221>
lea    -0x2358(%rbp),%rdi
call   10a0 <wait@plt>
mov    %eax,-0x235c(%rbp)
mov    -0x235c(%rbp),%eax
mov    %eax,-0x2364(%rbp)
mov    -0x2358(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    -0x2364(%rbp),%esi
mov    %eax,%edx
lea    0xc65(%rip),%rdi
call   1040 <printf@plt>
jmp    13b6 <main+0x226>
mov    $0x1,%edi
lea    0xc3e(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
jmp    11bf <main+0x2f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2370,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2348,%rsp
lea    0xe58(%rip),%rsi
mov    $0x1,%edi
mov    $0x2,%edx
call   1030 <write@plt>
lea    0x10(%rsp),%rsi
xor    %edi,%edi
mov    $0x3e8,%edx
call   1050 <read@plt>
test   %rax,%rax
je     130f <main+0x17f>
mov    0x10(%rsp),%al
test   %al,%al
je     130f <main+0x17f>
cmp    $0xa,%al
je     130f <main+0x17f>
lea    0xe17(%rip),%rbx
lea    0x10(%rsp),%r14
lea    0xe0f(%rip),%r15
lea    0xe0d(%rip),%r12
nop
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <strtok@plt>
mov    %rax,%r13
mov    %rax,0x400(%rsp)
mov    %rax,%rdi
mov    %r15,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     130f <main+0x17f>
mov    %r13,%rdi
mov    %r12,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     130f <main+0x17f>
lea    0x408(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %edi,%edi
mov    %rbx,%rsi
call   1080 <strtok@plt>
mov    %rax,0x0(%rbp)
add    $0x8,%rbp
test   %rax,%rax
jne    1250 <main+0xc0>
movq   $0x0,-0x10(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%bpl
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     12c0 <main+0x130>
cmp    $0x26,%bpl
jne    1292 <main+0x102>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
jne    12d5 <main+0x145>
lea    0xc(%rsp),%rdi
call   10a0 <wait@plt>
mov    %eax,%ebp
mov    0xc(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
lea    0xd63(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1040 <printf@plt>
jmp    12d5 <main+0x145>
nopl   0x0(%rax)
mov    0x400(%rsp),%rdi
lea    0x400(%rsp),%rsi
call   1090 <execvp@plt>
mov    $0x1,%edi
lea    0xd1f(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
xor    %edi,%edi
mov    %r14,%rsi
mov    $0x3e8,%edx
call   1050 <read@plt>
test   %rax,%rax
je     130f <main+0x17f>
mov    0x10(%rsp),%al
test   %al,%al
je     130f <main+0x17f>
cmp    $0xa,%al
jne    1200 <main+0x70>
xor    %eax,%eax
add    $0x2348,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"$ \00"
@.str.1 = [4 x i8] c" \09\0A\00"
@.str.2 = [5 x i8] c"exit\00"
@.str.3 = [5 x i8] c"quit\00"
@.str.4 = [38 x i8] c"Child %d exited with status code %d.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1000 x i8]
%7 = alloca [1000 x i8*]
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = call i32 @write(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
br label %14
14: ; preds = %89, %2
%15 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%16 = call i64 @read(i32 0, i8* %15, i32 1000)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %28
18: ; preds = %14
%19 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %18
%24 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp ne i32 %26, 10
br label %28
28: ; preds = %23, %18, %14
%29 = phi i1 [ false, %18 ], [ false, %14 ], [ %27, %23 ]
%30 = zext i1 %29 to i32
store i32 %30, i32* %8
br i1 %29, label %31, label %91
31: ; preds = %28
%32 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%33 = call i8* @strtok(i8* %32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%34 = getelementptr inbounds [1000 x i8*], [1000 x i8*]* %7, i64 0, i64 0
store i8* %33, i8** %34
%35 = getelementptr inbounds [1000 x i8*], [1000 x i8*]* %7, i64 0, i64 0
%36 = load i8*, i8** %35
%37 = call i32 @strcmp(i8* %36, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %31
%40 = getelementptr inbounds [1000 x i8*], [1000 x i8*]* %7, i64 0, i64 0
%41 = load i8*, i8** %40
%42 = call i32 @strcmp(i8* %41, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%43 = icmp ne i32 %42, 0
br i1 %43, label %45, label %44
44: ; preds = %39, %31
store i32 0, i32* %3
br label %92
45: ; preds = %39
store i64 0, i64* %9
br label %46
46: ; preds = %52, %45
%47 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%48 = load i64, i64* %9
%49 = add i64 %48, 1
store i64 %49, i64* %9
%50 = getelementptr inbounds [1000 x i8*], [1000 x i8*]* %7, i64 0, i64 %49
store i8* %47, i8** %50
%51 = icmp ne i8* %47, null
br i1 %51, label %52, label %53
52: ; preds = %46
br label %46
53: ; preds = %46
%54 = load i64, i64* %9
%55 = sub i64 %54, 1
%56 = getelementptr inbounds [1000 x i8*], [1000 x i8*]* %7, i64 0, i64 %55
store i8* null, i8** %56
store i32 0, i32* %10
%57 = load i64, i64* %9
%58 = sub i64 %57, 2
%59 = getelementptr inbounds [1000 x i8*], [1000 x i8*]* %7, i64 0, i64 %58
%60 = load i8*, i8** %59
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = icmp eq i32 %62, 38
br i1 %63, label %64, label %65
64: ; preds = %53
store i32 1, i32* %10
br label %65
65: ; preds = %64, %53
%66 = call i32 (...) @fork()
%67 = icmp ne i32 %66, 0
br i1 %67, label %73, label %68
68: ; preds = %65
%69 = getelementptr inbounds [1000 x i8*], [1000 x i8*]* %7, i64 0, i64 0
%70 = load i8*, i8** %69
%71 = getelementptr inbounds [1000 x i8*], [1000 x i8*]* %7, i64 0, i64 0
%72 = call i32 @execvp(i8* %70, i8** %71)
br label %89
73: ; preds = %65
%74 = load i32, i32* %10
%75 = icmp ne i32 %74, 0
br i1 %75, label %76, label %82
76: ; preds = %73
%77 = load i32, i32* %10
%78 = icmp ne i32 %77, 0
br i1 %78, label %79, label %88
79: ; preds = %76
%80 = call i32 (...) @fork()
%81 = icmp ne i32 %80, 0
br i1 %81, label %88, label %82
82: ; preds = %79, %73
%83 = call i32 @wait(i32* %11)
store i32 %83, i32* %12
%84 = load i32, i32* %12
%85 = load i32, i32* %11
%86 = call i32 @WEXITSTATUS(i32 %85)
%87 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i32 %84, i32 %86)
br label %88
88: ; preds = %82, %79, %76
br label %89
89: ; preds = %88, %68
%90 = call i32 @write(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
br label %14
91: ; preds = %28
store i32 0, i32* %3
br label %92
92: ; preds = %91, %44
%93 = load i32, i32* %3
ret i32 %93
}

LLVM IR (-O2):
@.str = [3 x i8] c"$ \00"
@.str.1 = [4 x i8] c" \09\0A\00"
@.str.2 = [5 x i8] c"exit\00"
@.str.3 = [5 x i8] c"quit\00"
@.str.4 = [38 x i8] c"Child %d exited with status code %d.\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [1000 x i8]
%4 = alloca [1000 x i8*]
%5 = alloca i32
%6 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 0
%7 = bitcast [1000 x i8*]* %4 to i8*
%8 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%9 = call i64 @read(i32 0, i8* %6, i32 1000)
%10 = icmp ne i64 %9, 0
%11 = load i8, i8* %6
%12 = icmp ne i8 %11, 0
%13 = select i1 %10, i1 %12, i1 false
%14 = icmp ne i8 %11, 10
%15 = select i1 %13, i1 %14, i1 false
br i1 %15, label %16, label %63
16: ; preds = %2
%17 = getelementptr inbounds [1000 x i8*], [1000 x i8*]* %4, i64 0, i64 0
%18 = bitcast i32* %5 to i8*
br label %19
19: ; preds = %16, %54
%20 = call i8* @strtok(i8* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i8* %20, i8** %17
%21 = call i32 @strcmp(i8* dereferenceable(1) %20, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp eq i32 %21, 0
br i1 %22, label %63, label %23
23: ; preds = %19
%24 = call i32 @strcmp(i8* dereferenceable(1) %20, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%25 = icmp eq i32 %24, 0
br i1 %25, label %63, label %26
26: ; preds = %23, %26
%27 = phi i64 [ %29, %26 ], [ 0, %23 ]
%28 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%29 = add i64 %27, 1
%30 = getelementptr inbounds [1000 x i8*], [1000 x i8*]* %4, i64 0, i64 %29
store i8* %28, i8** %30
%31 = icmp eq i8* %28, null
br i1 %31, label %32, label %26
32: ; preds = %26
%33 = getelementptr inbounds [1000 x i8*], [1000 x i8*]* %4, i64 0, i64 %27
store i8* null, i8** %33
%34 = add i64 %27, -1
%35 = getelementptr inbounds [1000 x i8*], [1000 x i8*]* %4, i64 0, i64 %34
%36 = load i8*, i8** %35
%37 = load i8, i8* %36
%38 = call i32 (...) @fork()
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %32
%41 = load i8*, i8** %17
%42 = call i32 @execvp(i8* %41, i8** %17)
br label %54
43: ; preds = %32
%44 = icmp eq i8 %37, 38
br i1 %44, label %45, label %48
45: ; preds = %43
%46 = call i32 (...) @fork()
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %53
48: ; preds = %45, %43
%49 = call i32 @wait(i32* %5)
%50 = load i32, i32* %5
%51 = call i32 @WEXITSTATUS(i32 %50)
%52 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i32 %49, i32 %51)
br label %53
53: ; preds = %48, %45
br label %54
54: ; preds = %53, %40
%55 = call i32 @write(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%56 = call i64 @read(i32 0, i8* %6, i32 1000)
%57 = icmp ne i64 %56, 0
%58 = load i8, i8* %6
%59 = icmp ne i8 %58, 0
%60 = select i1 %57, i1 %59, i1 false
%61 = icmp ne i8 %58, 10
%62 = select i1 %60, i1 %61, i1 false
br i1 %62, label %19, label %63
63: ; preds = %23, %19, %54, %2
ret i32 0
}