#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*/ SIGCHLD;
int /*<<< orphan*/ SIGINT;
int /*<<< orphan*/ SIGQUIT;
int /*<<< orphan*/ SIG_DFL;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ dup(int);
int /*<<< orphan*/ execl(char *, char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int wait(int *);
int shell(void) {
  int rc, status, unixpid;
  if ((unixpid = fork()) == 0) {
    close(0);
    dup(2);
    execl("/bin/sh", "sh", "-t", 0);
    exit(255);
  } else if (unixpid == -1)
    return (0);
  else {
    signal(SIGCHLD, SIG_DFL);
    signal(SIGINT, SIG_IGN);
    signal(SIGQUIT, SIG_IGN);
    while ((rc = wait(&status)) != unixpid && rc != -1)
      ;
    signal(SIGINT, SIG_DFL);
    signal(SIGQUIT, SIG_DFL);
    return (1);
  }
}


x86 assembly (clang, -O0):
<shell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jne    11c2 <shell+0x52>
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1030 <dup@plt>
lea    0xe5d(%rip),%rdi
lea    0xe5b(%rip),%rsi
lea    0xe57(%rip),%rdx
xor    %ecx,%ecx
call   1070 <execl@plt>
mov    $0xff,%edi
call   1060 <exit@plt>
cmpl   $0xffffffff,-0x10(%rbp)
jne    11d8 <shell+0x68>
movl   $0x0,-0x4(%rbp)
jmp    128d <shell+0x11d>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
lea    -0xc(%rbp),%rdi
call   1080 <wait@plt>
mov    %eax,%ecx
mov    %ecx,-0x8(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%ecx
mov    %al,-0x11(%rbp)
je     1243 <shell+0xd3>
cmpl   $0xffffffff,-0x8(%rbp)
setne  %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    1253 <shell+0xe3>
jmp    1258 <shell+0xe8>
jmp    121d <shell+0xad>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
mov    0x2d58(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
mov    0x2d41(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shell>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %ebp,%ebp
xor    %eax,%eax
call   1090 <fork@plt>
cmp    $0xffffffff,%eax
je     121f <shell+0xaf>
mov    %eax,%ebx
test   %eax,%eax
je     1230 <shell+0xc0>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
mov    0x2e19(%rip),%r13
mov    0x0(%r13),%esi
call   1050 <signal@plt>
mov    0x2e31(%rip),%r15
mov    (%r15),%edi
mov    0x2e0f(%rip),%rbp
mov    0x0(%rbp),%esi
call   1050 <signal@plt>
mov    0x2e10(%rip),%r12
mov    (%r12),%edi
mov    0x0(%rbp),%esi
call   1050 <signal@plt>
lea    0x4(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1080 <wait@plt>
cmp    %ebx,%eax
je     1201 <shell+0x91>
cmp    $0xffffffff,%eax
jne    11f0 <shell+0x80>
mov    (%r15),%edi
mov    0x0(%r13),%esi
call   1050 <signal@plt>
mov    (%r12),%edi
mov    0x0(%r13),%esi
call   1050 <signal@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1030 <dup@plt>
lea    0xdb8(%rip),%rdi
lea    0xdb6(%rip),%rsi
lea    0xdb2(%rip),%rdx
xor    %ecx,%ecx
call   1070 <execl@plt>
mov    $0xff,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"sh\00"
@.str.2 = [3 x i8] c"-t\00"
@SIGCHLD = global i32 0
@SIG_DFL = global i32 0
@SIGINT = global i32 0
@SIG_IGN = global i32 0
@SIGQUIT = global i32 0
define i32 @shell() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = call i32 (...) @fork()
store i32 %5, i32* %4
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %0
%8 = call i32 @close(i32 0)
%9 = call i32 @dup(i32 2)
%10 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 0)
%11 = call i32 @exit(i32 255)
unreachable
12: ; preds = %0
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %12
store i32 0, i32* %1
br label %43
16: ; preds = %12
%17 = load i32, i32* @SIGCHLD
%18 = load i32, i32* @SIG_DFL
%19 = call i32 @signal(i32 %17, i32 %18)
%20 = load i32, i32* @SIGINT
%21 = load i32, i32* @SIG_IGN
%22 = call i32 @signal(i32 %20, i32 %21)
%23 = load i32, i32* @SIGQUIT
%24 = load i32, i32* @SIG_IGN
%25 = call i32 @signal(i32 %23, i32 %24)
br label %26
26: ; preds = %35, %16
%27 = call i32 @wait(i32* %3)
store i32 %27, i32* %2
%28 = load i32, i32* %4
%29 = icmp ne i32 %27, %28
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = load i32, i32* %2
%32 = icmp ne i32 %31, -1
br label %33
33: ; preds = %30, %26
%34 = phi i1 [ false, %26 ], [ %32, %30 ]
br i1 %34, label %35, label %36
35: ; preds = %33
br label %26
36: ; preds = %33
%37 = load i32, i32* @SIGINT
%38 = load i32, i32* @SIG_DFL
%39 = call i32 @signal(i32 %37, i32 %38)
%40 = load i32, i32* @SIGQUIT
%41 = load i32, i32* @SIG_DFL
%42 = call i32 @signal(i32 %40, i32 %41)
store i32 1, i32* %1
br label %43
43: ; preds = %36, %15
%44 = load i32, i32* %1
ret i32 %44
}

LLVM IR (-O2):
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"sh\00"
@.str.2 = [3 x i8] c"-t\00"
@SIGCHLD = global i32 0
@SIG_DFL = global i32 0
@SIGINT = global i32 0
@SIG_IGN = global i32 0
@SIGQUIT = global i32 0
define i32 @shell() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (...) @fork()
switch i32 %3, label %9 [
i32 0, label %4
i32 -1, label %31
]
4: ; preds = %0
%5 = tail call i32 @close(i32 0)
%6 = tail call i32 @dup(i32 2)
%7 = tail call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 0)
%8 = tail call i32 @exit(i32 255)
unreachable
9: ; preds = %0
%10 = load i32, i32* @SIGCHLD
%11 = load i32, i32* @SIG_DFL
%12 = tail call i32 @signal(i32 %10, i32 %11)
%13 = load i32, i32* @SIGINT
%14 = load i32, i32* @SIG_IGN
%15 = tail call i32 @signal(i32 %13, i32 %14)
%16 = load i32, i32* @SIGQUIT
%17 = load i32, i32* @SIG_IGN
%18 = tail call i32 @signal(i32 %16, i32 %17)
br label %19
19: ; preds = %19, %9
%20 = call i32 @wait(i32* %1)
%21 = icmp ne i32 %20, %3
%22 = icmp ne i32 %20, -1
%23 = and i1 %21, %22
br i1 %23, label %19, label %24
24: ; preds = %19
%25 = load i32, i32* @SIGINT
%26 = load i32, i32* @SIG_DFL
%27 = call i32 @signal(i32 %25, i32 %26)
%28 = load i32, i32* @SIGQUIT
%29 = load i32, i32* @SIG_DFL
%30 = call i32 @signal(i32 %28, i32 %29)
br label %31
31: ; preds = %0, %24
%32 = phi i32 [ 1, %24 ], [ 0, %0 ]
ret i32 %32
}