#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 EXIT_FAILURE;
int /*<<< orphan*/ execute_line(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int run_in_batch_mode(const char *file) {
  char cmd_text[512];
  FILE *fp;
  fp = fopen(file, "r");
  if (!fp) {
    fprintf(stderr, "shell: Error opening file %s\n", file);
    return EXIT_FAILURE;
  }
  while (fgets(cmd_text, 512, fp)) {
    fprintf(stdout, "shell: execute: %s", cmd_text);
    execute_line(cmd_text);
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<run_in_batch_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe96(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x218(%rbp)
cmpq   $0x0,-0x218(%rbp)
jne    11ae <run_in_batch_mode+0x5e>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe6a(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1215 <run_in_batch_mode+0xc5>
jmp    11b3 <run_in_batch_mode+0x63>
lea    -0x210(%rbp),%rdi
mov    -0x218(%rbp),%rdx
mov    $0x200,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1202 <run_in_batch_mode+0xb2>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    -0x210(%rbp),%rdx
lea    0xe34(%rip),%rsi
call   1060 <fprintf@plt>
lea    -0x210(%rbp),%rdi
call   1040 <execute_line@plt>
jmp    11b3 <run_in_batch_mode+0x63>
mov    -0x218(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_in_batch_mode>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rdi,%rbx
lea    0xe98(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11d8 <run_in_batch_mode+0x88>
mov    %rax,%r15
mov    %rsp,%rdi
mov    $0x200,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11cc <run_in_batch_mode+0x7c>
mov    0x2e3f(%rip),%r12
lea    0xe88(%rip),%r14
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
mov    (%r12),%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <execute_line@plt>
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    11a0 <run_in_batch_mode+0x50>
mov    %r15,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    11f9 <run_in_batch_mode+0xa9>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
mov    %rbx,%rdx
call   1060 <fprintf@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"shell: Error opening file %s\0A\00"
@EXIT_FAILURE = global i32 0
@stdout = global i32 0
@.str.2 = [19 x i8] c"shell: execute: %s\00"
define i32 @run_in_batch_mode(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [512 x i8]
%5 = alloca i32*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %5
%8 = load i32*, i32** %5
%9 = icmp ne i32* %8, null
br i1 %9, label %15, label %10
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = load i8*, i8** %3
%13 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%14 = load i32, i32* @EXIT_FAILURE
store i32 %14, i32* %2
br label %30
15: ; preds = %1
br label %16
16: ; preds = %21, %15
%17 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%18 = load i32*, i32** %5
%19 = call i64 @fgets(i8* %17, i32 512, i32* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %16
%22 = load i32, i32* @stdout
%23 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%24 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%26 = call i32 @execute_line(i8* %25)
br label %16
27: ; preds = %16
%28 = load i32*, i32** %5
%29 = call i32 @fclose(i32* %28)
store i32 0, i32* %2
br label %30
30: ; preds = %27, %10
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"shell: Error opening file %s\0A\00"
@EXIT_FAILURE = global i32 0
@stdout = global i32 0
@.str.2 = [19 x i8] c"shell: execute: %s\00"
define i32 @run_in_batch_mode(i8* %0) {
%2 = alloca [512 x i8]
%3 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = call i64 @fgets(i8* %3, i32 512, i32* %4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %19, label %13
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%12 = load i32, i32* @EXIT_FAILURE
br label %21
13: ; preds = %6, %13
%14 = load i32, i32* @stdout
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %3)
%16 = call i32 @execute_line(i8* %3)
%17 = call i64 @fgets(i8* %3, i32 512, i32* %4)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %13
19: ; preds = %13, %6
%20 = call i32 @fclose(i32* %4)
br label %21
21: ; preds = %19, %9
%22 = phi i32 [ 0, %19 ], [ %12, %9 ]
ret i32 %22
}