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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ abort();
int execv(char const *, char *const *);
int execvp(char const *, char *const *);
char **malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(int /*<<< orphan*/ *);
int read(int, char *, int);
int main(int argc, char *argv[]) {
  int fd, i;
  char buf[3];
  char **nargv, **nap;
  int (*ef)(const char *path, char *const argv[]);
  if (argc < 2)
    abort();
  fd = open(argv[1], O_RDONLY);
  if (fd <= 0) {
    perror(NULL);
    return 1;
  }
  ssize_t got = read(fd, buf, sizeof(buf));
  if (got < 0) {
    perror(NULL);
    return 1;
  }
  nargv = malloc(sizeof(char *) * (argc + 1));
  if (!nargv)
    abort();
  nap = nargv;
  ef = execv;
  if (got >= 3 && buf[0] == '#' && buf[1] == '!' && buf[2] > ' ') {
    *nap++ = "perl";
    ef = execvp;
  }
  for (i = 1; i < argc; i++)
    *nap++ = argv[i];
  *nap++ = NULL;
  ef(nargv[0], nargv);
  perror(NULL);
  abort();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1177 <main+0x27>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     11af <main+0x5f>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e9 <main+0x199>
mov    -0x14(%rbp),%edi
lea    -0x1b(%rbp),%rsi
mov    $0x3,%edx
call   1040 <read@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    11e2 <main+0x92>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e9 <main+0x199>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    120b <main+0xbb>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2dde(%rip),%rax
mov    %rax,-0x38(%rbp)
cmpl   $0x3,-0x3c(%rbp)
jl     1273 <main+0x123>
movsbl -0x1b(%rbp),%eax
cmp    $0x23,%eax
jne    1273 <main+0x123>
movsbl -0x1a(%rbp),%eax
cmp    $0x21,%eax
jne    1273 <main+0x123>
movsbl -0x19(%rbp),%eax
cmp    $0x20,%eax
jle    1273 <main+0x123>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x30(%rbp)
lea    0xd9b(%rip),%rcx
mov    %rcx,(%rax)
mov    0x2d71(%rip),%rax
mov    %rax,-0x38(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12b2 <main+0x162>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x8,%rdx
mov    %rdx,-0x30(%rbp)
mov    %rcx,(%rax)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    127a <main+0x12a>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x30(%rbp)
movq   $0x0,(%rax)
mov    -0x38(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rcx),%rdi
mov    -0x28(%rbp),%rsi
call   *%rax
xor    %eax,%eax
mov    %eax,%edi
call   1070 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x1,%edi
jle    11d0 <main+0x70>
mov    %rsi,%rbx
mov    %edi,%r14d
mov    0x8(%rsi),%rdi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
jle    11a1 <main+0x41>
lea    0x5(%rsp),%rsi
mov    %eax,%edi
mov    $0x3,%edx
call   1040 <read@plt>
test   %eax,%eax
jns    11bc <main+0x5c>
xor    %edi,%edi
call   1080 <perror@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebp
lea    0x8(,%r14,8),%edi
call   1060 <malloc@plt>
test   %rax,%rax
jne    11d7 <main+0x77>
xor    %eax,%eax
call   1030 <abort@plt>
mov    %rax,%r15
add    $0x8,%rbx
mov    0x2e13(%rip),%r13
cmp    $0x3,%ebp
jb     121c <main+0xbc>
cmpb   $0x23,0x5(%rsp)
jne    121c <main+0xbc>
cmpb   $0x21,0x6(%rsp)
jne    121c <main+0xbc>
cmpb   $0x21,0x7(%rsp)
mov    %r15,%r12
jl     121f <main+0xbf>
mov    %r15,%r12
add    $0x8,%r12
lea    0xdf0(%rip),%rax
mov    %rax,(%r15)
mov    0x2dc6(%rip),%r13
jmp    121f <main+0xbf>
mov    %r15,%r12
lea    -0x1(%r14),%edx
shl    $0x3,%rdx
mov    %r12,%rdi
mov    %rbx,%rsi
call   1050 <memcpy@plt>
add    $0xfffffffe,%r14d
movq   $0x0,0x8(%r12,%r14,8)
mov    (%r15),%rdi
mov    %r15,%rsi
call   *%r13
xor    %edi,%edi
call   1080 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [5 x i8] c"perl\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [3 x i8]
%9 = alloca i8**
%10 = alloca i8**
%11 = alloca i32 (i8*, i8**)*
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 2
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 (...) @abort()
unreachable
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp sle i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %17
%26 = call i32 @perror(i32* null)
store i32 1, i32* %3
br label %95
27: ; preds = %17
%28 = load i32, i32* %6
%29 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 0
%30 = call i32 @read(i32 %28, i8* %29, i32 3)
store i32 %30, i32* %12
%31 = load i32, i32* %12
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %27
%34 = call i32 @perror(i32* null)
store i32 1, i32* %3
br label %95
35: ; preds = %27
%36 = load i32, i32* %4
%37 = add nsw i32 %36, 1
%38 = sext i32 %37 to i64
%39 = mul i64 8, %38
%40 = trunc i64 %39 to i32
%41 = call i8** @malloc(i32 %40)
store i8** %41, i8*** %9
%42 = load i8**, i8*** %9
%43 = icmp ne i8** %42, null
br i1 %43, label %46, label %44
44: ; preds = %35
%45 = call i32 (...) @abort()
unreachable
46: ; preds = %35
%47 = load i8**, i8*** %9
store i8** %47, i8*** %10
store i32 (i8*, i8**)* @execv, i32 (i8*, i8**)** %11
%48 = load i32, i32* %12
%49 = icmp sge i32 %48, 3
br i1 %49, label %50, label %68
50: ; preds = %46
%51 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 0
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = icmp eq i32 %53, 35
br i1 %54, label %55, label %68
55: ; preds = %50
%56 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 1
%57 = load i8, i8* %56
%58 = sext i8 %57 to i32
%59 = icmp eq i32 %58, 33
br i1 %59, label %60, label %68
60: ; preds = %55
%61 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 2
%62 = load i8, i8* %61
%63 = sext i8 %62 to i32
%64 = icmp sgt i32 %63, 32
br i1 %64, label %65, label %68
65: ; preds = %60
%66 = load i8**, i8*** %10
%67 = getelementptr inbounds i8*, i8** %66, i32 1
store i8** %67, i8*** %10
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %66
store i32 (i8*, i8**)* @execvp, i32 (i8*, i8**)** %11
br label %68
68: ; preds = %65, %60, %55, %50, %46
store i32 1, i32* %7
br label %69
69: ; preds = %81, %68
%70 = load i32, i32* %7
%71 = load i32, i32* %4
%72 = icmp slt i32 %70, %71
br i1 %72, label %73, label %84
73: ; preds = %69
%74 = load i8**, i8*** %5
%75 = load i32, i32* %7
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds i8*, i8** %74, i64 %76
%78 = load i8*, i8** %77
%79 = load i8**, i8*** %10
%80 = getelementptr inbounds i8*, i8** %79, i32 1
store i8** %80, i8*** %10
store i8* %78, i8** %79
br label %81
81: ; preds = %73
%82 = load i32, i32* %7
%83 = add nsw i32 %82, 1
store i32 %83, i32* %7
br label %69
84: ; preds = %69
%85 = load i8**, i8*** %10
%86 = getelementptr inbounds i8*, i8** %85, i32 1
store i8** %86, i8*** %10
store i8* null, i8** %85
%87 = load i32 (i8*, i8**)*, i32 (i8*, i8**)** %11
%88 = load i8**, i8*** %9
%89 = getelementptr inbounds i8*, i8** %88, i64 0
%90 = load i8*, i8** %89
%91 = load i8**, i8*** %9
%92 = call i32 %87(i8* %90, i8** %91)
%93 = call i32 @perror(i32* null)
%94 = call i32 (...) @abort()
unreachable
95: ; preds = %33, %25
%96 = load i32, i32* %3
ret i32 %96
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [5 x i8] c"perl\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [3 x i8]
%4 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 (...) @abort()
unreachable
8: ; preds = %2
%9 = getelementptr i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* %10, i32 %11)
%13 = icmp slt i32 %12, 1
br i1 %13, label %55, label %14
14: ; preds = %8
%15 = call i32 @read(i32 %12, i8* %4, i32 3)
%16 = icmp slt i32 %15, 0
br i1 %16, label %55, label %17
17: ; preds = %14
%18 = shl i32 %0, 3
%19 = add i32 %18, 8
%20 = tail call i8** @malloc(i32 %19)
%21 = icmp eq i8** %20, null
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = tail call i32 (...) @abort()
unreachable
24: ; preds = %17
%25 = icmp ugt i32 %15, 2
%26 = load i8, i8* %4
%27 = icmp eq i8 %26, 35
%28 = select i1 %25, i1 %27, i1 false
%29 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 1
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 33
%32 = select i1 %28, i1 %31, i1 false
%33 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 2
%34 = load i8, i8* %33
%35 = icmp sgt i8 %34, 32
%36 = select i1 %32, i1 %35, i1 false
br i1 %36, label %37, label %39
37: ; preds = %24
%38 = getelementptr inbounds i8*, i8** %20, i64 1
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %20
br label %39
39: ; preds = %37, %24
%40 = phi i8** [ %38, %37 ], [ %20, %24 ]
%41 = phi i32 (i8*, i8**)* [ @execvp, %37 ], [ @execv, %24 ]
%42 = bitcast i8** %40 to i8*
%43 = bitcast i8** %9 to i8*
%44 = add nsw i32 %0, -1
%45 = zext i32 %44 to i64
%46 = shl nuw nsw i64 %45, 3
%47 = add nsw i32 %0, -2
%48 = zext i32 %47 to i64
%49 = add nuw nsw i64 %48, 1
%50 = getelementptr i8*, i8** %40, i64 %49
store i8* null, i8** %50
%51 = load i8*, i8** %20
%52 = tail call i32 %41(i8* %51, i8** %20)
%53 = tail call i32 @perror(i32* null)
%54 = tail call i32 (...) @abort()
unreachable
55: ; preds = %14, %8
%56 = tail call i32 @perror(i32* null)
ret i32 1
}