#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*/ execvp(char *, char **);
scalar_t__ fork();
int /*<<< orphan*/ gets(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strcmp(char *, char *);
char *strtok(char *, char *);
int /*<<< orphan*/ wait();
int main(void) {
  int exit = 0;
  char *args[80 / 2 + 1];
  while (!exit) {
    char line[50];
    int background = 0;
    int i = 0;
    pid_t pid;
    printf("-> ");
    gets(line);
    args[0] = strtok(line, " ");
    while (args[i] != NULL) {
      args[++i] = strtok(NULL, " ");
      if (!strcmp(args[i - 1], "&")) {
        background = 1;
      }
    }
    pid = fork();
    if (pid == 0) {
      execvp(args[0], args);
      exit = 1;
    } else if (!background) {
      wait();
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1a0,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    119f <main+0x2f>
jmp    12b7 <main+0x147>
movl   $0x0,-0x194(%rbp)
movl   $0x0,-0x198(%rbp)
lea    0xe46(%rip),%rdi
call   1030 <printf@plt>
lea    -0x190(%rbp),%rdi
call   1050 <gets@plt>
lea    -0x190(%rbp),%rdi
lea    0xe29(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x150(%rbp)
movslq -0x198(%rbp),%rax
cmpq   $0x0,-0x150(%rbp,%rax,8)
je     125e <main+0xee>
xor    %eax,%eax
mov    %eax,%edi
lea    0xdfc(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,%rcx
mov    -0x198(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x198(%rbp)
cltq
mov    %rcx,-0x150(%rbp,%rax,8)
ff
mov    -0x198(%rbp),%eax
sub    $0x1,%eax
cltq
mov    -0x150(%rbp,%rax,8),%rdi
ff
lea    0xdc3(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    1259 <main+0xe9>
movl   $0x1,-0x194(%rbp)
jmp    11e5 <main+0x75>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x1a0(%rbp)
cmpq   $0x0,-0x1a0(%rbp)
jne    1299 <main+0x129>
mov    -0x150(%rbp),%rdi
lea    -0x150(%rbp),%rsi
call   1070 <execvp@plt>
movl   $0x1,-0x8(%rbp)
jmp    12b2 <main+0x142>
cmpl   $0x0,-0x194(%rbp)
jne    12ad <main+0x13d>
mov    $0x0,%al
call   1080 <wait@plt>
jmp    12b2 <main+0x142>
jmp    1189 <main+0x19>
xor    %eax,%eax
add    $0x1a0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x188,%rsp
lea    0xe7a(%rip),%r13
lea    0xe75(%rip),%r12
mov    $0x1,%ebp
jmp    11b7 <main+0x47>
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     1231 <main+0xc1>
xor    %eax,%eax
call   1080 <wait@plt>
lea    0xe42(%rip),%rdi
call   1030 <printf@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1050 <gets@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1060 <strtok@plt>
mov    %rax,0x40(%rsp)
test   %rax,%rax
je     11a0 <main+0x30>
xor    %r14d,%r14d
lea    0x48(%rsp),%r15
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
mov    %r13,%rsi
call   1060 <strtok@plt>
mov    %rax,%rbx
mov    %rax,(%r15)
mov    -0x8(%r15),%rdi
mov    %r12,%rsi
call   1040 <strcmp@plt>
test   %eax,%eax
cmove  %ebp,%r14d
add    $0x8,%r15
test   %rbx,%rbx
jne    11f0 <main+0x80>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     1231 <main+0xc1>
test   %r14d,%r14d
jne    11b7 <main+0x47>
jmp    11b0 <main+0x40>
mov    0x40(%rsp),%rdi
lea    0x40(%rsp),%rsi
call   1070 <execvp@plt>
xor    %eax,%eax
add    $0x188,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"-> \00"
@.str.1 = [2 x i8] c" \00"
@.str.2 = [2 x i8] c"&\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [41 x i8*]
%4 = alloca [50 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
store i32 0, i32* %1
store i32 0, i32* %2
br label %8
8: ; preds = %55, %0
%9 = load i32, i32* %2
%10 = icmp ne i32 %9, 0
%11 = xor i1 %10, true
br i1 %11, label %12, label %56
12: ; preds = %8
store i32 0, i32* %5
store i32 0, i32* %6
%13 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%14 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%15 = call i32 @gets(i8* %14)
%16 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%17 = call i8* @strtok(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%18 = getelementptr inbounds [41 x i8*], [41 x i8*]* %3, i64 0, i64 0
store i8* %17, i8** %18
br label %19
19: ; preds = %39, %12
%20 = load i32, i32* %6
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [41 x i8*], [41 x i8*]* %3, i64 0, i64 %21
%23 = load i8*, i8** %22
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %40
25: ; preds = %19
%26 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* %6
%28 = add nsw i32 %27, 1
store i32 %28, i32* %6
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [41 x i8*], [41 x i8*]* %3, i64 0, i64 %29
store i8* %26, i8** %30
%31 = load i32, i32* %6
%32 = sub nsw i32 %31, 1
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [41 x i8*], [41 x i8*]* %3, i64 0, i64 %33
%35 = load i8*, i8** %34
%36 = call i32 @strcmp(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%37 = icmp ne i32 %36, 0
br i1 %37, label %39, label %38
38: ; preds = %25
store i32 1, i32* %5
br label %39
39: ; preds = %38, %25
br label %19
40: ; preds = %19
%41 = call i64 (...) @fork()
store i64 %41, i64* %7
%42 = load i64, i64* %7
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %40
%45 = getelementptr inbounds [41 x i8*], [41 x i8*]* %3, i64 0, i64 0
%46 = load i8*, i8** %45
%47 = getelementptr inbounds [41 x i8*], [41 x i8*]* %3, i64 0, i64 0
%48 = call i32 @execvp(i8* %46, i8** %47)
store i32 1, i32* %2
br label %55
49: ; preds = %40
%50 = load i32, i32* %5
%51 = icmp ne i32 %50, 0
br i1 %51, label %54, label %52
52: ; preds = %49
%53 = call i32 (...) @wait()
br label %54
54: ; preds = %52, %49
br label %55
55: ; preds = %54, %44
br label %8
56: ; preds = %8
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"-> \00"
@.str.1 = [2 x i8] c" \00"
@.str.2 = [2 x i8] c"&\00"
define i32 @main() {
%1 = alloca [41 x i8*]
%2 = alloca [50 x i8]
%3 = bitcast [41 x i8*]* %1 to i8*
%4 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [41 x i8*], [41 x i8*]* %1, i64 0, i64 0
br label %6
6: ; preds = %33, %0
%7 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @gets(i8* %4)
%9 = call i8* @strtok(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %9, i8** %5
%10 = icmp eq i8* %9, null
br i1 %10, label %26, label %11
11: ; preds = %6, %11
%12 = phi i64 [ %16, %11 ], [ 0, %6 ]
%13 = phi i8** [ %17, %11 ], [ %5, %6 ]
%14 = phi i32 [ %21, %11 ], [ 0, %6 ]
%15 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%16 = add nuw nsw i64 %12, 1
%17 = getelementptr inbounds [41 x i8*], [41 x i8*]* %1, i64 0, i64 %16
store i8* %15, i8** %17
%18 = load i8*, i8** %13
%19 = call i32 @strcmp(i8* dereferenceable(1) %18, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp eq i32 %19, 0
%21 = select i1 %20, i32 1, i32 %14
%22 = icmp eq i8* %15, null
br i1 %22, label %23, label %11
23: ; preds = %11
%24 = call i64 (...) @fork()
%25 = icmp eq i64 %24, 0
br i1 %25, label %34, label %29
26: ; preds = %6
%27 = call i64 (...) @fork()
%28 = icmp eq i64 %27, 0
br i1 %28, label %34, label %31
29: ; preds = %23
%30 = icmp eq i32 %21, 0
br i1 %30, label %31, label %33
31: ; preds = %26, %29
%32 = call i32 (...) @wait()
br label %33
33: ; preds = %29, %31
br label %6
34: ; preds = %23, %26
%35 = load i8*, i8** %5
%36 = call i32 @execvp(i8* %35, i8** %5)
ret i32 0
}