#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*/ EXIT_SUCCESS;
int /*<<< orphan*/ EX_OSERR;
int /*<<< orphan*/ _IONBF;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ execlp(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ setvbuf(int /*<<< orphan*/, int /*<<< orphan*/ *,
                           int /*<<< orphan*/, size_t);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int strlen(char *);
scalar_t__ vfork();
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int /*<<< orphan*/ warn(char *, ...);
int main(void) {
  char buf[640];
  pid_t pid;
  int status;
  setvbuf(stdout, NULL, _IONBF, (size_t)0);
  printf("shs%% ");
  while (fgets(buf, 640, stdin) != NULL) {
    buf[strlen(buf) - 1] = '\0';
    pid = vfork();
    if (pid == 0) {
      execlp(buf, buf, NULL);
      warn("could not execlp '%s'", buf);
      _exit(EX_OSERR);
    } else if (pid > 0) {
      if ((pid = waitpid(pid, &status, 0)) < 0)
        warn("waitpid error");
    } else {
      warn("vfork error");
    }
    printf("shs%% ");
  }
  putchar('\n');
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2a0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rsi
call   1090 <setvbuf@plt>
lea    0xe19(%rip),%rdi
call   1070 <printf@plt>
lea    -0x290(%rbp),%rdi
mov    0x2dee(%rip),%rax
mov    (%rax),%edx
mov    $0x280,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     12ea <main+0x13a>
lea    -0x290(%rbp),%rdi
call   1060 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x290(%rbp,%rax,1)
mov    $0x0,%al
call   10b0 <vfork@plt>
mov    %rax,-0x298(%rbp)
cmpq   $0x0,-0x298(%rbp)
jne    127f <main+0xcf>
lea    -0x290(%rbp),%rdi
lea    -0x290(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   10d0 <execlp@plt>
lea    -0x290(%rbp),%rsi
lea    0xd9d(%rip),%rdi
mov    $0x0,%al
call   1050 <warn@plt>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
call   1040 <_exit@plt>
cmpq   $0x0,-0x298(%rbp)
jle    12c6 <main+0x116>
mov    -0x298(%rbp),%rdi
lea    -0x29c(%rbp),%rsi
xor    %edx,%edx
call   10a0 <waitpid@plt>
mov    %rax,-0x298(%rbp)
cmp    $0x0,%rax
jge    12c1 <main+0x111>
lea    0xd63(%rip),%rdi
mov    $0x0,%al
call   1050 <warn@plt>
jmp    12d4 <main+0x124>
lea    0xd5e(%rip),%rdi
mov    $0x0,%al
call   1050 <warn@plt>
jmp    12d9 <main+0x129>
lea    0xd20(%rip),%rdi
call   1070 <printf@plt>
jmp    11ec <main+0x3c>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x2ccd(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x298,%rsp
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
mov    0x2e0f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
xor    %ecx,%ecx
call   1090 <setvbuf@plt>
lea    0xe1d(%rip),%rdi
call   1070 <printf@plt>
mov    0x2df9(%rip),%r12
mov    (%r12),%edx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x280,%esi
call   1080 <fgets@plt>
test   %rax,%rax
jne    1222 <main+0x72>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    0xe02(%rip),%r13
lea    0xc(%rsp),%r14
lea    0xde8(%rip),%rbp
lea    0xdc4(%rip),%r15
jmp    1265 <main+0xb5>
xchg   %ax,%ax
xor    %eax,%eax
call   1050 <warn@plt>
mov    %r15,%rdi
call   1070 <printf@plt>
mov    (%r12),%edx
mov    %rbx,%rdi
mov    $0x280,%esi
call   1080 <fgets@plt>
test   %rax,%rax
je     120a <main+0x5a>
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
xor    %eax,%eax
call   10b0 <vfork@plt>
test   %rax,%rax
je     129c <main+0xec>
mov    %r13,%rdi
jle    1240 <main+0x90>
mov    %rax,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   10a0 <waitpid@plt>
mov    %rbp,%rdi
test   %rax,%rax
jns    1247 <main+0x97>
jmp    1240 <main+0x90>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
call   10d0 <execlp@plt>
lea    0xd52(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <warn@plt>
mov    0x2cfa(%rip),%rax
mov    (%rax),%edi
call   1040 <_exit@plt>

LLVM IR (-O0):
@stdout = global i32 0
@_IONBF = global i32 0
@.str = [7 x i8] c"shs%% \00"
@stdin = global i32 0
@.str.1 = [22 x i8] c"could not execlp '%s'\00"
@EX_OSERR = global i32 0
@.str.2 = [14 x i8] c"waitpid error\00"
@.str.3 = [12 x i8] c"vfork error\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca [640 x i8]
%3 = alloca i64
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @stdout
%6 = load i32, i32* @_IONBF
%7 = call i32 @setvbuf(i32 %5, i32* null, i32 %6, i64 0)
%8 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %44, %0
%10 = getelementptr inbounds [640 x i8], [640 x i8]* %2, i64 0, i64 0
%11 = load i32, i32* @stdin
%12 = call i32* @fgets(i8* %10, i32 640, i32 %11)
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %46
14: ; preds = %9
%15 = getelementptr inbounds [640 x i8], [640 x i8]* %2, i64 0, i64 0
%16 = call i32 @strlen(i8* %15)
%17 = sub nsw i32 %16, 1
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [640 x i8], [640 x i8]* %2, i64 0, i64 %18
store i8 0, i8* %19
%20 = call i64 (...) @vfork()
store i64 %20, i64* %3
%21 = load i64, i64* %3
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %31
23: ; preds = %14
%24 = getelementptr inbounds [640 x i8], [640 x i8]* %2, i64 0, i64 0
%25 = getelementptr inbounds [640 x i8], [640 x i8]* %2, i64 0, i64 0
%26 = call i32 @execlp(i8* %24, i8* %25, i32* null)
%27 = getelementptr inbounds [640 x i8], [640 x i8]* %2, i64 0, i64 0
%28 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %27)
%29 = load i32, i32* @EX_OSERR
%30 = call i32 @_exit(i32 %29)
unreachable
31: ; preds = %14
%32 = load i64, i64* %3
%33 = icmp sgt i64 %32, 0
br i1 %33, label %34, label %41
34: ; preds = %31
%35 = load i64, i64* %3
%36 = call i64 @waitpid(i64 %35, i32* %4, i32 0)
store i64 %36, i64* %3
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %38, %34
br label %43
41: ; preds = %31
%42 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %41, %40
br label %44
44: ; preds = %43
%45 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %9
46: ; preds = %9
%47 = call i32 @putchar(i8 signext 10)
%48 = load i32, i32* @EXIT_SUCCESS
%49 = call i32 @exit(i32 %48)
unreachable
}

LLVM IR (-O2):
@stdout = global i32 0
@_IONBF = global i32 0
@.str = [7 x i8] c"shs%% \00"
@stdin = global i32 0
@.str.1 = [22 x i8] c"could not execlp '%s'\00"
@EX_OSERR = global i32 0
@.str.2 = [14 x i8] c"waitpid error\00"
@.str.3 = [12 x i8] c"vfork error\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca [640 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [640 x i8], [640 x i8]* %1, i64 0, i64 0
%4 = bitcast i32* %2 to i8*
%5 = load i32, i32* @stdout
%6 = load i32, i32* @_IONBF
%7 = tail call i32 @setvbuf(i32 %5, i32* null, i32 %6, i64 0)
%8 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @stdin
%10 = call i32* @fgets(i8* %3, i32 640, i32 %9)
%11 = icmp eq i32* %10, null
br i1 %11, label %37, label %12
12: ; preds = %0, %32
%13 = call i32 @strlen(i8* %3)
%14 = add nsw i32 %13, -1
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [640 x i8], [640 x i8]* %1, i64 0, i64 %15
store i8 0, i8* %16
%17 = call i64 (...) @vfork()
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %12
%20 = call i32 @execlp(i8* %3, i8* %3, i32* null)
%21 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%22 = load i32, i32* @EX_OSERR
%23 = call i32 @_exit(i32 %22)
unreachable
24: ; preds = %12
%25 = icmp sgt i64 %17, 0
br i1 %25, label %26, label %29
26: ; preds = %24
%27 = call i64 @waitpid(i64 %17, i32* %2, i32 0)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %24, %26
%30 = phi i8* [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), %26 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), %24 ]
%31 = call i32 (i8*, ...) @warn(i8* %30)
br label %32
32: ; preds = %29, %26
%33 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%34 = load i32, i32* @stdin
%35 = call i32* @fgets(i8* %3, i32 640, i32 %34)
%36 = icmp eq i32* %35, null
br i1 %36, label %37, label %12
37: ; preds = %32, %0
%38 = call i32 @putchar(i8 signext 10)
%39 = load i32, i32* @EXIT_SUCCESS
%40 = call i32 @exit(i32 %39)
unreachable
}