#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read_line(int /*<<< orphan*/ *, char *);
scalar_t__ shell_repl(char *, int);
int run_batch(char *fname) {
  FILE *fstream;
  if (!(fstream = fopen(fname, "r"))) {
    perror("shell: Error");
    printf("Could not open batch file %s, aborting...\n", fname);
    return -1;
  }
  char buf[1024];
  int buf_len;
  do {
    if ((buf_len = read_line(fstream, buf)) <= 0) {
      fclose(fstream);
      return buf_len;
    }
    printf(buf);
  } while (shell_repl(buf, buf_len - 1));
  fclose(fstream);
  return 0;
}


x86 assembly (clang, -O0):
<run_batch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe91(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11b7 <run_batch+0x57>
lea    0xe6c(%rip),%rdi
call   1080 <perror@plt>
mov    -0x10(%rbp),%rsi
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122f <run_batch+0xcf>
jmp    11bc <run_batch+0x5c>
mov    -0x18(%rbp),%rdi
lea    -0x420(%rbp),%rsi
call   1060 <read_line@plt>
mov    %eax,-0x424(%rbp)
cmp    $0x0,%eax
jg     11f2 <run_batch+0x92>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x424(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    122f <run_batch+0xcf>
lea    -0x420(%rbp),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x420(%rbp),%rdi
mov    -0x424(%rbp),%esi
sub    $0x1,%esi
call   1050 <shell_repl@plt>
cmp    $0x0,%rax
jne    11bc <run_batch+0x5c>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_batch>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%rbx
lea    0xe94(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11d9 <run_batch+0x79>
mov    %rax,%r14
mov    %rsp,%r15
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <read_line@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11bf <run_batch+0x5f>
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0xffffffff,%ebp
mov    %r15,%rdi
mov    %ebp,%esi
call   1050 <shell_repl@plt>
test   %rax,%rax
jne    1190 <run_batch+0x30>
jmp    11c1 <run_batch+0x61>
mov    %ebp,%ebx
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %ebx,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe20(%rip),%rdi
call   1080 <perror@plt>
lea    0xe21(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%ebx
jmp    11c9 <run_batch+0x69>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [13 x i8] c"shell: Error\00"
@.str.2 = [43 x i8] c"Could not open batch file %s, aborting...\0A\00"
define i32 @run_batch(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [1024 x i8]
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %4
%9 = icmp ne i32* %8, null
br i1 %9, label %14, label %10
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%12 = load i8*, i8** %3
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %12)
store i32 -1, i32* %2
br label %36
14: ; preds = %1
br label %15
15: ; preds = %27, %14
%16 = load i32*, i32** %4
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%18 = call i32 @read_line(i32* %16, i8* %17)
store i32 %18, i32* %6
%19 = icmp sle i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %15
%21 = load i32*, i32** %4
%22 = call i32 @fclose(i32* %21)
%23 = load i32, i32* %6
store i32 %23, i32* %2
br label %36
24: ; preds = %15
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%26 = call i32 (i8*, ...) @printf(i8* %25)
br label %27
27: ; preds = %24
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%29 = load i32, i32* %6
%30 = sub nsw i32 %29, 1
%31 = call i64 @shell_repl(i8* %28, i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %15, label %33
33: ; preds = %27
%34 = load i32*, i32** %4
%35 = call i32 @fclose(i32* %34)
store i32 0, i32* %2
br label %36
36: ; preds = %33, %20, %10
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [13 x i8] c"shell: Error\00"
@.str.2 = [43 x i8] c"Could not open batch file %s, aborting...\0A\00"
define i32 @run_batch(i8* %0) {
%2 = alloca [1024 x i8]
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %21
8: ; preds = %1
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
br label %10
10: ; preds = %13, %8
%11 = call i32 @read_line(i32* %3, i8* %9)
%12 = icmp slt i32 %11, 1
br i1 %12, label %18, label %13
13: ; preds = %10
%14 = call i32 (i8*, ...) @printf(i8* %9)
%15 = add nsw i32 %11, -1
%16 = call i64 @shell_repl(i8* %9, i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %10
18: ; preds = %13, %10
%19 = phi i32 [ %11, %10 ], [ 0, %13 ]
%20 = call i32 @fclose(i32* %3)
br label %21
21: ; preds = %18, %5
%22 = phi i32 [ %19, %18 ], [ -1, %5 ]
ret i32 %22
}