#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 */

/* Variables and functions */
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ *gets(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ splitarg(char *, char **);
int wait(int *);
int main() {
  char cmd[1024];
  char *args[64];
  int pid, state;
  while (1) {
    printf("gkas> ");
    if (gets(cmd) == NULL) {
      printf("\n");
      exit(0);
    }
    splitarg(cmd, args);
    if ((pid = fork()) < 0) {
      perror("fork");
      exit(1);
    }
    if (pid == 0) {
      execvp(*args, args);
      perror(*args);
      exit(1);
    }
    while (wait(&state) != pid)
      ;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x620,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe67(%rip),%rdi
call   1030 <printf@plt>
lea    -0x410(%rbp),%rdi
call   1040 <gets@plt>
cmp    $0x0,%rax
jne    11c7 <main+0x47>
lea    0xe4c(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
lea    -0x410(%rbp),%rdi
lea    -0x610(%rbp),%rsi
call   1090 <splitarg@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x614(%rbp)
cmp    $0x0,%eax
jge    1206 <main+0x86>
lea    0xe12(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x614(%rbp)
jne    123c <main+0xbc>
mov    -0x610(%rbp),%rdi
lea    -0x610(%rbp),%rsi
call   1060 <execvp@plt>
mov    -0x610(%rbp),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    1241 <main+0xc1>
lea    -0x618(%rbp),%rdi
call   1080 <wait@plt>
cmp    -0x614(%rbp),%eax
je     125e <main+0xde>
jmp    1241 <main+0xc1>
jmp    1192 <main+0x12>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x610,%rsp
lea    0xe5a(%rip),%rdi
call   1040 <printf@plt>
lea    0x210(%rsp),%rdi
call   1050 <gets@plt>
test   %rax,%rax
je     1221 <main+0x91>
lea    0x210(%rsp),%r15
lea    0x10(%rsp),%r14
lea    0xc(%rsp),%rbx
lea    0xe2a(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r14,%rsi
call   10a0 <splitarg@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
js     1232 <main+0xa2>
mov    %eax,%ebp
je     123b <main+0xab>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1090 <wait@plt>
cmp    %ebp,%eax
jne    1200 <main+0x70>
mov    %r12,%rdi
call   1040 <printf@plt>
mov    %r15,%rdi
call   1050 <gets@plt>
test   %rax,%rax
jne    11e0 <main+0x50>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %edi,%edi
call   1080 <exit@plt>
lea    0xdce(%rip),%rdi
jmp    124f <main+0xbf>
mov    0x10(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1070 <execvp@plt>
mov    0x10(%rsp),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"gkas> \00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [5 x i8] c"fork\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [1024 x i8]
%3 = alloca [64 x i8*]
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
br label %6
6: ; preds = %0, %41
%7 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%9 = call i32* @gets(i8* %8)
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %6
%12 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 0)
unreachable
14: ; preds = %6
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%16 = getelementptr inbounds [64 x i8*], [64 x i8*]* %3, i64 0, i64 0
%17 = call i32 @splitarg(i8* %15, i8** %16)
%18 = call i32 (...) @fork()
store i32 %18, i32* %4
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %14
%24 = load i32, i32* %4
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %35
26: ; preds = %23
%27 = getelementptr inbounds [64 x i8*], [64 x i8*]* %3, i64 0, i64 0
%28 = load i8*, i8** %27
%29 = getelementptr inbounds [64 x i8*], [64 x i8*]* %3, i64 0, i64 0
%30 = call i32 @execvp(i8* %28, i8** %29)
%31 = getelementptr inbounds [64 x i8*], [64 x i8*]* %3, i64 0, i64 0
%32 = load i8*, i8** %31
%33 = call i32 @perror(i8* %32)
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %23
br label %36
36: ; preds = %40, %35
%37 = call i32 @wait(i32* %5)
%38 = load i32, i32* %4
%39 = icmp ne i32 %37, %38
br i1 %39, label %40, label %41
40: ; preds = %36
br label %36
41: ; preds = %36
br label %6
}

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