#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*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int sh(int, int);
int /*<<< orphan*/ writestr(int, char *);
int main(int argc, char **argv) {
  int ret, f;
  if (argc == 1)
    return sh(0, 1);
  if ((f = open(argv[1], O_RDONLY)) == -1) {
    writestr(2, "sh: ");
    writestr(2, argv[1]);
    writestr(2, ": Can't open\n");
    return 1;
  }
  ret = sh(f, 0);
  close(f);
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    1174 <main+0x34>
xor    %edi,%edi
mov    $0x1,%esi
call   1030 <sh@plt>
mov    %eax,-0x4(%rbp)
jmp    11f1 <main+0xb1>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    11d6 <main+0x96>
mov    $0x2,%edi
lea    0xe5e(%rip),%rsi
call   1050 <writestr@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    $0x2,%edi
call   1050 <writestr@plt>
mov    $0x2,%edi
lea    0xe40(%rip),%rsi
call   1050 <writestr@plt>
movl   $0x1,-0x4(%rbp)
jmp    11f1 <main+0xb1>
mov    -0x18(%rbp),%edi
xor    %esi,%esi
call   1030 <sh@plt>
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x1,%edi
jne    115a <main+0x1a>
xor    %edi,%edi
mov    $0x1,%esi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <sh@plt>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     118c <main+0x4c>
mov    %eax,%ebp
mov    %eax,%edi
xor    %esi,%esi
call   1030 <sh@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
jmp    11c1 <main+0x81>
lea    0xe6d(%rip),%rsi
mov    $0x2,%edi
call   1050 <writestr@plt>
mov    0x8(%rbx),%rsi
mov    $0x2,%edi
call   1050 <writestr@plt>
lea    0xe53(%rip),%rsi
mov    $0x2,%edi
call   1050 <writestr@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [5 x i8] c"sh: \00"
@.str.1 = [14 x i8] c": Can't open\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @sh(i32 0, i32 1)
store i32 %11, i32* %3
br label %32
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %7
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %26
19: ; preds = %12
%20 = call i32 @writestr(i32 2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 @writestr(i32 2, i8* %23)
%25 = call i32 @writestr(i32 2, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %32
26: ; preds = %12
%27 = load i32, i32* %7
%28 = call i32 @sh(i32 %27, i32 0)
store i32 %28, i32* %6
%29 = load i32, i32* %7
%30 = call i32 @close(i32 %29)
%31 = load i32, i32* %6
store i32 %31, i32* %3
br label %32
32: ; preds = %26, %19, %10
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [5 x i8] c"sh: \00"
@.str.1 = [14 x i8] c": Can't open\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 1
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @sh(i32 0, i32 1)
br label %20
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* %8, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %17
12: ; preds = %6
%13 = tail call i32 @writestr(i32 2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%14 = load i8*, i8** %7
%15 = tail call i32 @writestr(i32 2, i8* %14)
%16 = tail call i32 @writestr(i32 2, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %20
17: ; preds = %6
%18 = tail call i32 @sh(i32 %10, i32 0)
%19 = tail call i32 @close(i32 %10)
br label %20
20: ; preds = %17, %12, %4
%21 = phi i32 [ %5, %4 ], [ 1, %12 ], [ %18, %17 ]
ret i32 %21
}