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

/* Variables and functions */
int /*<<< orphan*/ chomp(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ process_input(char *, int);
void cmd_script(int argc, char *argv[]) {
  FILE *fd;
  char buff[2048];
  fd = fopen(argv[1], "r");
  if (fd == NULL) {
    perror("fopen");
    return;
  }
  printf("Executing script %s\n", argv[1]);
  while (fgets(buff, sizeof(buff), fd)) {
    chomp(buff);
    process_input(buff, sizeof(buff));
  }
  fclose(fd);
}


x86 assembly (clang, -O0):
<cmd_script>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe6f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b6 <cmd_script+0x46>
lea    0xe56(%rip),%rdi
call   1080 <perror@plt>
jmp    1214 <cmd_script+0xa4>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe43(%rip),%rdi
call   1040 <printf@plt>
lea    -0x820(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x800,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     120b <cmd_script+0x9b>
lea    -0x820(%rbp),%rdi
call   1060 <chomp@plt>
lea    -0x820(%rbp),%rdi
mov    $0x800,%esi
call   1090 <process_input@plt>
jmp    11ca <cmd_script+0x5a>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_script>:
push   %r14
push   %rbx
sub    $0x808,%rsp
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0xe78(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11fd <cmd_script+0x8d>
mov    %rax,%r14
mov    0x8(%rbx),%rsi
lea    0xe68(%rip),%rdi
call   1040 <printf@plt>
mov    %rsp,%rdi
mov    $0x800,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11ea <cmd_script+0x7a>
mov    %rsp,%rbx
nopl   (%rax)
mov    %rbx,%rdi
call   1060 <chomp@plt>
mov    %rbx,%rdi
mov    $0x800,%esi
call   1090 <process_input@plt>
mov    %rbx,%rdi
mov    $0x800,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    11c0 <cmd_script+0x50>
mov    %r14,%rdi
call   1030 <fclose@plt>
add    $0x808,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdfe(%rip),%rdi
add    $0x808,%rsp
pop    %rbx
pop    %r14
jmp    1080 <perror@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"fopen\00"
@.str.2 = [21 x i8] c"Executing script %s\0A\00"
define void @cmd_script(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32*
%6 = alloca [2048 x i8]
store i32 %0, i32* %3
store i8** %1, i8*** %4
%7 = load i8**, i8*** %4
%8 = getelementptr inbounds i8*, i8** %7, i64 1
%9 = load i8*, i8** %8
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %5
%11 = load i32*, i32** %5
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %33
15: ; preds = %2
%16 = load i8**, i8*** %4
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %25, %15
%21 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%22 = load i32*, i32** %5
%23 = call i64 @fgets(i8* %21, i32 2048, i32* %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %20
%26 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%27 = call i32 @chomp(i8* %26)
%28 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%29 = call i32 @process_input(i8* %28, i32 2048)
br label %20
30: ; preds = %20
%31 = load i32*, i32** %5
%32 = call i32 @fclose(i32* %31)
br label %33
33: ; preds = %30, %13
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"fopen\00"
@.str.2 = [21 x i8] c"Executing script %s\0A\00"
define void @cmd_script(i32 %0, i8** nocapture %1) {
%3 = alloca [2048 x i8]
%4 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %23
11: ; preds = %2
%12 = load i8*, i8** %5
%13 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %12)
%14 = call i64 @fgets(i8* %4, i32 2048, i32* %7)
%15 = icmp eq i64 %14, 0
br i1 %15, label %21, label %16
16: ; preds = %11, %16
%17 = call i32 @chomp(i8* %4)
%18 = call i32 @process_input(i8* %4, i32 2048)
%19 = call i64 @fgets(i8* %4, i32 2048, i32* %7)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %16
21: ; preds = %16, %11
%22 = call i32 @fclose(i32* %7)
br label %23
23: ; preds = %21, %9
ret void
}