#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ execv(char *, char **);
int /*<<< orphan*/ *fopen(char *, char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
void *malloc(int);
char *parent_dir(char *);
int /*<<< orphan*/ realpath(char *, char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int main(int argc, char **argv) {
  char *script = malloc(2048);
  realpath(argv[1], script);
  char *script_parent = parent_dir(script);
  FILE *script_fp = fopen(script, "r");
  char *line = NULL;
  size_t line_len = 0;
  char *accum;
  int accum_len = 0;
  char *prev_accum = "";
  int is_first_line = 1;
  while (getline(&line, &line_len, script_fp) != -1) {
    if (!is_first_line) {
      accum = malloc(accum_len + line_len);
      strcpy(accum, prev_accum);
      strcat(accum, line);
      accum_len += line_len;
      prev_accum = accum;
    } else {
      is_first_line = 0;
    }
  }
  char *script_src = accum;
  char **script_args = malloc(255);
  script_args[0] = "bash";
  script_args[1] = "-c";
  script_args[2] = script_src;
  script_args[3] = script_parent;
  int script_arg_count = 4;
  for (int i = 0; i < argc; i++) {
    script_args[i + 4] = argv[i + 2];
    script_arg_count += 1;
  }
  script_args[script_arg_count] = (char *)NULL;
  execv("/bin/bash", script_args);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x800,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <realpath@plt>
mov    -0x18(%rbp),%rdi
call   1040 <parent_dir@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe33(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0x0,-0x44(%rbp)
lea    0xe10(%rip),%rax
mov    %rax,-0x50(%rbp)
movl   $0x1,-0x54(%rbp)
mov    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1080 <getline@plt>
cmp    $0xffffffff,%eax
je     1277 <main+0xf7>
cmpl   $0x0,-0x54(%rbp)
jne    126b <main+0xeb>
movslq -0x44(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x50(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1090 <strcat@plt>
mov    -0x38(%rbp),%rcx
movslq -0x44(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
jmp    1272 <main+0xf2>
movl   $0x0,-0x54(%rbp)
jmp    11ff <main+0x7f>
mov    -0x40(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    $0xff,%edi
call   1060 <malloc@plt>
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rax
lea    0xd72(%rip),%rcx
mov    %rcx,(%rax)
mov    -0x68(%rbp),%rax
lea    0xd5c(%rip),%rcx
mov    %rcx,0x8(%rax)
mov    -0x60(%rbp),%rcx
mov    -0x68(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    -0x20(%rbp),%rcx
mov    -0x68(%rbp),%rax
mov    %rcx,0x18(%rax)
movl   $0x4,-0x6c(%rbp)
movl   $0x0,-0x70(%rbp)
mov    -0x70(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1315 <main+0x195>
mov    -0x10(%rbp),%rax
mov    -0x70(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdx
mov    -0x68(%rbp),%rax
mov    -0x70(%rbp),%ecx
add    $0x4,%ecx
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x6c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x6c(%rbp)
mov    -0x70(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x70(%rbp)
jmp    12d0 <main+0x150>
mov    -0x68(%rbp),%rax
movslq -0x6c(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x68(%rbp),%rsi
lea    0xcd5(%rip),%rdi
call   10a0 <execv@plt>
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbp
mov    %edi,%r14d
mov    $0x800,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    0x8(%rbp),%rdi
mov    %rax,%rsi
call   1050 <realpath@plt>
mov    %rbx,%rdi
call   1040 <parent_dir@plt>
mov    %rax,%r15
lea    0xe31(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
mov    %rax,%r13
movq   $0x0,0x8(%rsp)
movq   $0x0,(%rsp)
lea    0x8(%rsp),%rdi
mov    %rsp,%rsi
mov    %rax,%rdx
call   1090 <getline@plt>
cmp    $0xffffffff,%eax
mov    %r14,0x20(%rsp)
mov    %rbp,0x10(%rsp)
mov    %r15,0x18(%rsp)
je     1282 <main+0xf2>
lea    0x8(%rsp),%rdi
mov    %rsp,%rsi
mov    %r13,%rdx
call   1090 <getline@plt>
cmp    $0xffffffff,%eax
je     1282 <main+0xf2>
lea    0xdd9(%rip),%r14
xor    %r12d,%r12d
lea    0x8(%rsp),%rbp
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
mov    (%rsp),%edi
add    %r12d,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   10a0 <strcat@plt>
add    (%rsp),%r12d
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
call   1090 <getline@plt>
mov    %rbx,%r14
cmp    $0xffffffff,%eax
jne    1240 <main+0xb0>
jmp    1282 <main+0xf2>
mov    $0xff,%edi
call   1070 <malloc@plt>
mov    %rax,%rbp
lea    0xd74(%rip),%rax
mov    %rax,0x0(%rbp)
lea    0xd61(%rip),%rax
mov    %rax,0x8(%rbp)
mov    %rbx,0x10(%rbp)
mov    0x18(%rsp),%rax
mov    %rax,0x18(%rbp)
mov    0x20(%rsp),%rbx
test   %ebx,%ebx
jle    12db <main+0x14b>
mov    %rbp,%rdi
add    $0x20,%rdi
mov    0x10(%rsp),%rsi
add    $0x10,%rsi
mov    %ebx,%edx
shl    $0x3,%rdx
call   1060 <memcpy@plt>
add    $0x4,%ebx
jmp    12e0 <main+0x150>
mov    $0x4,%ebx
movq   $0x0,0x0(%rbp,%rbx,8)
lea    0xd15(%rip),%rdi
mov    %rbp,%rsi
call   10b0 <execv@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [5 x i8] c"bash\00"
@.str.3 = [3 x i8] c"-c\00"
@.str.4 = [10 x i8] c"/bin/bash\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = tail call dereferenceable_or_null(2048) i8* @malloc(i32 2048)
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = tail call i32 @realpath(i8* %7, i8* %5)
%9 = tail call i8* @parent_dir(i8* %5)
%10 = tail call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%12 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%13 = call i32 @getline(i8** %3, i64* %4, i32* %10)
%14 = icmp eq i32 %13, -1
br i1 %14, label %33, label %15
15: ; preds = %2
%16 = call i32 @getline(i8** %3, i64* %4, i32* %10)
%17 = icmp eq i32 %16, -1
br i1 %17, label %33, label %18
18: ; preds = %15, %18
%19 = phi i32 [ %30, %18 ], [ 0, %15 ]
%20 = phi i8* [ %24, %18 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), %15 ]
%21 = load i64, i64* %4
%22 = trunc i64 %21 to i32
%23 = add i32 %19, %22
%24 = call i8* @malloc(i32 %23)
%25 = call i32 @strcpy(i8* %24, i8* %20)
%26 = load i8*, i8** %3
%27 = call i32 @strcat(i8* %24, i8* %26)
%28 = load i64, i64* %4
%29 = trunc i64 %28 to i32
%30 = add i32 %19, %29
%31 = call i32 @getline(i8** %3, i64* %4, i32* %10)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %18
33: ; preds = %18, %15, %2
%34 = phi i8* [ undef, %2 ], [ undef, %15 ], [ %24, %18 ]
%35 = call dereferenceable_or_null(255) i8* @malloc(i32 255)
%36 = bitcast i8* %35 to i8**
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8** %36
%37 = getelementptr inbounds i8*, i8** %36, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8** %37
%38 = getelementptr inbounds i8*, i8** %36, i64 2
store i8* %34, i8** %38
%39 = getelementptr inbounds i8*, i8** %36, i64 3
store i8* %9, i8** %39
%40 = icmp sgt i32 %0, 0
br i1 %40, label %41, label %49
41: ; preds = %33
%42 = getelementptr i8, i8* %35, i64 32
%43 = getelementptr i8*, i8** %1, i64 2
%44 = bitcast i8** %43 to i8*
%45 = zext i32 %0 to i64
%46 = shl nuw nsw i64 %45, 3
%47 = add nuw i32 %0, 4
%48 = zext i32 %47 to i64
br label %49
49: ; preds = %41, %33
%50 = phi i64 [ 4, %33 ], [ %48, %41 ]
%51 = getelementptr inbounds i8*, i8** %36, i64 %50
store i8* null, i8** %51
%52 = call i32 @execv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8** %36)
ret i32 0
}