#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;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ EX_DATAERR;
int /*<<< orphan*/ EX_OK;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execlp(char *, char *, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
scalar_t__ getinput(char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int strlen(char *);
int waitpid(int, int *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  char buf[1024];
  pid_t pid;
  int status;
  while (getinput(buf, sizeof(buf))) {
    buf[strlen(buf) - 1] = '\0';
    if ((pid = fork()) == -1) {
      fprintf(stderr, "shell: can't fork: %s\n", strerror(errno));
      continue;
    } else if (pid == 0) {
      execlp(buf, buf, (char *)0);
      fprintf(stderr, "shell: couldn't exec %s: %s\n", buf, strerror(errno));
      exit(EX_DATAERR);
    }
    if ((pid = waitpid(pid, &status, 0)) < 0)
      fprintf(stderr, "shell: waitpid error: %s\n", strerror(errno));
  }
  exit(EX_OK);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x410(%rbp),%rdi
mov    $0x400,%esi
call   1050 <getinput@plt>
cmp    $0x0,%rax
je     12f8 <main+0x178>
lea    -0x410(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x410(%rbp,%rax,1)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x414(%rbp)
cmp    $0xffffffff,%eax
jne    121c <main+0x9c>
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x41c(%rbp)
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x41c(%rbp),%edi
mov    %rax,%rdx
lea    0xdf0(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1199 <main+0x19>
cmpl   $0x0,-0x414(%rbp)
jne    1297 <main+0x117>
lea    -0x410(%rbp),%rdi
lea    -0x410(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1080 <execlp@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x42c(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x428(%rbp)
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x42c(%rbp),%edi
mov    -0x428(%rbp),%rdx
mov    %rax,%rcx
lea    0xd95(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    129c <main+0x11c>
mov    -0x414(%rbp),%edi
lea    -0x418(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    %eax,-0x414(%rbp)
cmp    $0x0,%eax
jge    12f3 <main+0x173>
mov    0x2d12(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x430(%rbp)
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x430(%rbp),%edi
mov    %rax,%rdx
lea    0xd48(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1199 <main+0x19>
mov    0x2cc9(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0x400,%esi
call   1050 <getinput@plt>
test   %rax,%rax
jne    11b6 <main+0x36>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    0xe43(%rip),%r14
mov    0x2e14(%rip),%r12
mov    0x2e05(%rip),%r13
jmp    11fe <main+0x7e>
nopl   (%rax)
mov    (%r12),%ebp
mov    0x0(%r13),%edi
call   1090 <strerror@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %r15,%rdi
mov    $0x400,%esi
call   1050 <getinput@plt>
test   %rax,%rax
je     11a8 <main+0x28>
mov    %r15,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%r15,1)
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %r14,%rbx
cmp    $0xffffffff,%eax
je     11d0 <main+0x50>
test   %eax,%eax
je     123c <main+0xbc>
mov    %eax,%edi
lea    0xc(%rsp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
lea    0xdfe(%rip),%rbx
test   %eax,%eax
jns    11ec <main+0x6c>
jmp    11d0 <main+0x50>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
call   1080 <execlp@plt>
mov    (%r12),%ebp
mov    0x0(%r13),%edi
call   1090 <strerror@plt>
lea    0xdb5(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [23 x i8] c"shell: can't fork: %s\0A\00"
@errno = global i32 0
@.str.1 = [29 x i8] c"shell: couldn't exec %s: %s\0A\00"
@EX_DATAERR = global i32 0
@.str.2 = [26 x i8] c"shell: waitpid error: %s\0A\00"
@EX_OK = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1024 x i8]
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
br label %9
9: ; preds = %50, %21, %2
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%11 = call i64 @getinput(i8* %10, i32 1024)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %51
13: ; preds = %9
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%15 = call i32 @strlen(i8* %14)
%16 = sub nsw i32 %15, 1
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %17
store i8 0, i8* %18
%19 = call i32 (...) @fork()
store i32 %19, i32* %7
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %26
21: ; preds = %13
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %24)
br label %9
26: ; preds = %13
%27 = load i32, i32* %7
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %40
29: ; preds = %26
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%32 = call i32 @execlp(i8* %30, i8* %31, i8* null)
%33 = load i32, i32* @stderr
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %34, i8* %36)
%38 = load i32, i32* @EX_DATAERR
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %26
br label %41
41: ; preds = %40
%42 = load i32, i32* %7
%43 = call i32 @waitpid(i32 %42, i32* %8, i32 0)
store i32 %43, i32* %7
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %41
%46 = load i32, i32* @stderr
%47 = load i32, i32* @errno
%48 = call i8* @strerror(i32 %47)
%49 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %48)
br label %50
50: ; preds = %45, %41
br label %9
51: ; preds = %9
%52 = load i32, i32* @EX_OK
%53 = call i32 @exit(i32 %52)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [23 x i8] c"shell: can't fork: %s\0A\00"
@errno = global i32 0
@.str.1 = [29 x i8] c"shell: couldn't exec %s: %s\0A\00"
@EX_DATAERR = global i32 0
@.str.2 = [26 x i8] c"shell: waitpid error: %s\0A\00"
@EX_OK = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = bitcast i32* %4 to i8*
%7 = call i64 @getinput(i8* %5, i32 1024)
%8 = icmp eq i64 %7, 0
br i1 %8, label %35, label %9
9: ; preds = %2, %21
%10 = call i32 @strlen(i8* %5)
%11 = add nsw i32 %10, -1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %12
store i8 0, i8* %13
%14 = call i32 (...) @fork()
switch i32 %14, label %32 [
i32 -1, label %15
i32 0, label %24
]
15: ; preds = %9, %32
%16 = phi i8* [ getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), %32 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), %9 ]
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %17, i8* %16, i8* %19)
br label %21
21: ; preds = %15, %32
%22 = call i64 @getinput(i8* %5, i32 1024)
%23 = icmp eq i64 %22, 0
br i1 %23, label %35, label %9
24: ; preds = %9
%25 = call i32 @execlp(i8* %5, i8* %5, i8* null)
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %5, i8* %28)
%30 = load i32, i32* @EX_DATAERR
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %9
%33 = call i32 @waitpid(i32 %14, i32* %4, i32 0)
%34 = icmp slt i32 %33, 0
br i1 %34, label %15, label %21
35: ; preds = %21, %2
%36 = load i32, i32* @EX_OK
%37 = call i32 @exit(i32 %36)
unreachable
}