#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 EXIT_SUCCESS;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ input(int /*<<< orphan*/ *, int, char, char);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int main(void) {
  FILE *fin = NULL;
  FILE *finb = NULL;
  fin = fopen("/proc/"
              "kputc_test",
              "r");
  finb = fopen("/proc/"
               "kputc_testb",
               "rb");
  if (fin == NULL || finb == NULL)
    goto exit_no_file;
  if (input(fin, 10000000, '0', '~'))
    goto exit_fail;
  if (fclose(fin)) {
    fin = NULL;
    goto exit_bad_fclose;
  }
  fin = NULL;
  fprintf(stderr, "- text mode done\n");
  if (input(finb, 10000000, '\1', '~'))
    goto exit_fail;
  if (fclose(finb)) {
    finb = NULL;
    goto exit_bad_fclose;
  }
  fprintf(stderr, "- binary mode done\n");
  return EXIT_SUCCESS;
exit_no_file:
  perror("error opening files");
  goto exit_fail;
exit_bad_fclose:
  perror("error closing files");
  goto exit_fail;
exit_fail:
  if (fin)
    fclose(fin);
  if (finb)
    fclose(finb);
  return EXIT_FAILURE;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    0xe8a(%rip),%rdi
lea    0xe94(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
lea    0xe86(%rip),%rdi
lea    0xe91(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11b3 <main+0x63>
cmpq   $0x0,-0x18(%rbp)
jne    11b8 <main+0x68>
jmp    1289 <main+0x139>
mov    -0x10(%rbp),%rdi
mov    $0x989680,%esi
mov    $0x30,%edx
mov    $0x7e,%ecx
call   1030 <input@plt>
cmp    $0x0,%rax
je     11df <main+0x8f>
jmp    12a6 <main+0x156>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     11ff <main+0xaf>
movq   $0x0,-0x10(%rbp)
jmp    129a <main+0x14a>
movq   $0x0,-0x10(%rbp)
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
lea    0xe11(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
mov    $0x989680,%esi
mov    $0x1,%edx
mov    $0x7e,%ecx
call   1030 <input@plt>
cmp    $0x0,%rax
je     1243 <main+0xf3>
jmp    12a6 <main+0x156>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     1263 <main+0x113>
movq   $0x0,-0x18(%rbp)
jmp    129a <main+0x14a>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
lea    0xdc7(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12da <main+0x18a>
lea    0xdbe(%rip),%rdi
call   1070 <perror@plt>
jmp    12a6 <main+0x156>
lea    0xdc1(%rip),%rdi
call   1070 <perror@plt>
cmpq   $0x0,-0x10(%rbp)
je     12ba <main+0x16a>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x18(%rbp)
je     12ce <main+0x17e>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2d03(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xea5(%rip),%rdi
lea    0xeaf(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
lea    0xea2(%rip),%rdi
lea    0xead(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r14
test   %rbx,%rbx
je     123c <main+0xec>
test   %r14,%r14
je     123c <main+0xec>
mov    %rbx,%rdi
mov    $0x989680,%esi
mov    $0x30,%edx
mov    $0x7e,%ecx
call   1030 <input@plt>
test   %rax,%rax
je     11bd <main+0x6d>
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %r14,%r14
jne    11ff <main+0xaf>
jmp    1207 <main+0xb7>
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %rax,%rax
jne    1259 <main+0x109>
mov    0x2e0b(%rip),%rbx
mov    (%rbx),%edi
lea    0xe4a(%rip),%rsi
call   1050 <fprintf@plt>
mov    %r14,%rdi
mov    $0x989680,%esi
mov    $0x1,%edx
mov    $0x7e,%ecx
call   1030 <input@plt>
test   %rax,%rax
je     1218 <main+0xc8>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %r14,%rdi
call   1040 <fclose@plt>
test   %rax,%rax
jne    1256 <main+0x106>
mov    (%rbx),%edi
lea    0xe0c(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d96(%rip),%rax
jmp    120e <main+0xbe>
lea    0xe0b(%rip),%rdi
call   1070 <perror@plt>
test   %rbx,%rbx
jne    11ae <main+0x5e>
jmp    11b6 <main+0x66>
xor    %r14d,%r14d
lea    0xe02(%rip),%rdi
call   1070 <perror@plt>
jmp    11b6 <main+0x66>

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/kputc_test\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [18 x i8] c"/proc/kputc_testb\00"
@.str.3 = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.4 = [18 x i8] c"- text mode done\0A\00"
@.str.5 = [20 x i8] c"- binary mode done\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.6 = [20 x i8] c"error opening files\00"
@.str.7 = [20 x i8] c"error closing files\00"
@EXIT_FAILURE = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
store i32 0, i32* %1
store i32* null, i32** %2
store i32* null, i32** %3
%4 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %2
%5 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %5, i32** %3
%6 = load i32*, i32** %2
%7 = icmp eq i32* %6, null
br i1 %7, label %11, label %8
8: ; preds = %0
%9 = load i32*, i32** %3
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %12
11: ; preds = %8, %0
br label %38
12: ; preds = %8
%13 = load i32*, i32** %2
%14 = call i64 @input(i32* %13, i32 10000000, i8 signext 48, i8 signext 126)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %12
br label %42
17: ; preds = %12
%18 = load i32*, i32** %2
%19 = call i64 @fclose(i32* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %17
store i32* null, i32** %2
br label %40
22: ; preds = %17
store i32* null, i32** %2
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%25 = load i32*, i32** %3
%26 = call i64 @input(i32* %25, i32 10000000, i8 signext 1, i8 signext 126)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %22
br label %42
29: ; preds = %22
%30 = load i32*, i32** %3
%31 = call i64 @fclose(i32* %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %29
store i32* null, i32** %3
br label %40
34: ; preds = %29
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
%37 = load i32, i32* @EXIT_SUCCESS
store i32 %37, i32* %1
br label %56
38: ; preds = %11
%39 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
br label %42
40: ; preds = %33, %21
%41 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0))
br label %42
42: ; preds = %40, %38, %28, %16
%43 = load i32*, i32** %2
%44 = icmp ne i32* %43, null
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i32*, i32** %2
%47 = call i64 @fclose(i32* %46)
br label %48
48: ; preds = %45, %42
%49 = load i32*, i32** %3
%50 = icmp ne i32* %49, null
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i32*, i32** %3
%53 = call i64 @fclose(i32* %52)
br label %54
54: ; preds = %51, %48
%55 = load i32, i32* @EXIT_FAILURE
store i32 %55, i32* %1
br label %56
56: ; preds = %54, %34
%57 = load i32, i32* %1
ret i32 %57
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/kputc_test\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [18 x i8] c"/proc/kputc_testb\00"
@.str.3 = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.4 = [18 x i8] c"- text mode done\0A\00"
@.str.5 = [20 x i8] c"- binary mode done\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.6 = [20 x i8] c"error opening files\00"
@.str.7 = [20 x i8] c"error closing files\00"
@EXIT_FAILURE = global i32 0
define i32 @main() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%3 = icmp eq i32* %1, null
%4 = icmp eq i32* %2, null
%5 = or i1 %3, %4
br i1 %5, label %26, label %6
6: ; preds = %0
%7 = tail call i64 @input(i32* %1, i32 10000000, i8 signext 48, i8 signext 126)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %28
9: ; preds = %6
%10 = tail call i64 @fclose(i32* %1)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %23
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%15 = tail call i64 @input(i32* %2, i32 10000000, i8 signext 1, i8 signext 126)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %33
17: ; preds = %12
%18 = tail call i64 @fclose(i32* %2)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %36
23: ; preds = %17, %9
%24 = phi i32* [ %2, %9 ], [ null, %17 ]
%25 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0))
br label %30
26: ; preds = %0
%27 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
br i1 %3, label %30, label %28
28: ; preds = %6, %26
%29 = tail call i64 @fclose(i32* %1)
br label %30
30: ; preds = %23, %28, %26
%31 = phi i32* [ %2, %28 ], [ %2, %26 ], [ %24, %23 ]
%32 = icmp eq i32* %31, null
br i1 %32, label %36, label %33
33: ; preds = %12, %30
%34 = phi i32* [ %31, %30 ], [ %2, %12 ]
%35 = tail call i64 @fclose(i32* %34)
br label %36
36: ; preds = %30, %33, %20
%37 = phi i32* [ @EXIT_SUCCESS, %20 ], [ @EXIT_FAILURE, %33 ], [ @EXIT_FAILURE, %30 ]
%38 = load i32, i32* %37
ret i32 %38
}