#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 */

/* Variables and functions */
int /*<<< orphan*/ freopen(char const *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char const *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ *strstr(char *, char *);
int /*<<< orphan*/ testScanf(int);
int main(int argc, char **argv) {
  int standardText = 0;
  char *path;
  const char *defaultFile = "/boot/etc/test_input.txt";
  if (argc > 1) {
    if (strstr(argv[1], "stdin") != NULL) {
      path = "stdin";
    } else {
      if (!freopen(argv[1], "r", stdin)) {
        printf("File: \"%s\" could not be opened.\r\n", argv[1]);
        return -1;
      }
      path = argv[1];
    }
  } else {
    standardText = 1;
    if (!freopen(defaultFile, "r", stdin)) {
      printf("File: \"%s\" could not be opened.\r\n", defaultFile);
      return -1;
    }
    path = "/boot/etc/test_input.txt";
  }
  printf("A test of \"fscanf\" from file: \"%s\".\r\n\n", path);
  testScanf(standardText);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    0xe9c(%rip),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11f7 <main+0xb7>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe98(%rip),%rsi
call   1060 <strstr@plt>
cmp    $0x0,%rax
je     11a0 <main+0x60>
lea    0xe82(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    11f2 <main+0xb2>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
lea    0xe67(%rip),%rsi
call   1050 <freopen@plt>
cmp    $0x0,%eax
jne    11e6 <main+0xa6>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe4c(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1266 <main+0x126>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    1247 <main+0x107>
movl   $0x1,-0x14(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2ddf(%rip),%rax
mov    (%rax),%edx
lea    0xe0d(%rip),%rsi
call   1050 <freopen@plt>
cmp    $0x0,%eax
jne    123c <main+0xfc>
mov    -0x28(%rbp),%rsi
lea    0xdf6(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1266 <main+0x126>
lea    0xdbd(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xdf1(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1030 <testScanf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jl     1199 <main+0x59>
mov    %rsi,%rbp
mov    0x8(%rsi),%r15
lea    0xebf(%rip),%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1060 <strstr@plt>
xor    %r14d,%r14d
test   %rax,%rax
jne    11c2 <main+0x82>
mov    0x2e74(%rip),%rax
mov    (%rax),%edx
lea    0xea2(%rip),%rsi
mov    %r15,%rdi
call   1050 <freopen@plt>
mov    0x8(%rbp),%rbx
test   %eax,%eax
jne    11c2 <main+0x82>
lea    0xe8d(%rip),%rdi
mov    %rbx,%rsi
jmp    11eb <main+0xab>
mov    0x2e48(%rip),%rax
mov    (%rax),%edx
lea    0xe57(%rip),%rbx
lea    0xe6f(%rip),%rsi
mov    %rbx,%rdi
call   1050 <freopen@plt>
mov    $0x1,%r14d
test   %eax,%eax
je     11dd <main+0x9d>
lea    0xe7a(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %r14d,%edi
call   1030 <testScanf@plt>
xor    %eax,%eax
jmp    11f5 <main+0xb5>
lea    0xe3d(%rip),%rdi
lea    0xe15(%rip),%rsi
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"/boot/etc/test_input.txt\00"
@.str.1 = [6 x i8] c"stdin\00"
@.str.2 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.3 = [34 x i8] c"File: \22%s\22 could not be opened.\0D\0A\00"
@.str.4 = [39 x i8] c"A test of \22fscanf\22 from file: \22%s\22.\0D\0A\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8** %8
%9 = load i32, i32* %4
%10 = icmp sgt i32 %9, 1
br i1 %10, label %11, label %35
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call i32* @strstr(i8* %14, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %18
17: ; preds = %11
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8** %7
br label %34
18: ; preds = %11
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = load i32, i32* @stdin
%23 = call i32 @freopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %22)
%24 = icmp ne i32 %23, 0
br i1 %24, label %30, label %25
25: ; preds = %18
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %28)
store i32 -1, i32* %3
br label %49
30: ; preds = %18
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
store i8* %33, i8** %7
br label %34
34: ; preds = %30, %17
br label %44
35: ; preds = %2
store i32 1, i32* %6
%36 = load i8*, i8** %8
%37 = load i32, i32* @stdin
%38 = call i32 @freopen(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %37)
%39 = icmp ne i32 %38, 0
br i1 %39, label %43, label %40
40: ; preds = %35
%41 = load i8*, i8** %8
%42 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %41)
store i32 -1, i32* %3
br label %49
43: ; preds = %35
store i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8** %7
br label %44
44: ; preds = %43, %34
%45 = load i8*, i8** %7
%46 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i8* %45)
%47 = load i32, i32* %6
%48 = call i32 @testScanf(i32 %47)
store i32 0, i32* %3
br label %49
49: ; preds = %44, %40, %25
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
@.str = [25 x i8] c"/boot/etc/test_input.txt\00"
@.str.1 = [6 x i8] c"stdin\00"
@.str.2 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.3 = [34 x i8] c"File: \22%s\22 could not be opened.\0D\0A\00"
@.str.4 = [39 x i8] c"A test of \22fscanf\22 from file: \22%s\22.\0D\0A\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %16
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32* @strstr(i8* dereferenceable(1) %6, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %22
9: ; preds = %4
%10 = load i32, i32* @stdin
%11 = tail call i32 @freopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %10)
%12 = icmp eq i32 %11, 0
%13 = load i8*, i8** %5
br i1 %12, label %14, label %22
14: ; preds = %9
%15 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %13)
br label %27
16: ; preds = %2
%17 = load i32, i32* @stdin
%18 = tail call i32 @freopen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %27
22: ; preds = %9, %16, %4
%23 = phi i32 [ 0, %4 ], [ 1, %16 ], [ 0, %9 ]
%24 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), %4 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), %16 ], [ %13, %9 ]
%25 = tail call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i8* %24)
%26 = tail call i32 @testScanf(i32 %23)
br label %27
27: ; preds = %22, %20, %14
%28 = phi i32 [ 0, %22 ], [ -1, %14 ], [ -1, %20 ]
ret i32 %28
}