#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 *);
scalar_t__ output(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main(void) {
  FILE *fout = NULL;
  srand(time(NULL));
  fout = fopen("/proc/"
               "kscanf_test",
               "w");
  if (fout == NULL)
    goto exit_no_file;
  if (output(fout))
    goto exit_fail;
  if (fclose(fout)) {
    fout = NULL;
    goto exit_bad_fclose;
  }
  return EXIT_SUCCESS;
exit_no_file:
  perror("error opening file");
  goto exit_fail;
exit_bad_fclose:
  perror("error closing file");
  goto exit_fail;
exit_fail:
  if (fout)
    fclose(fout);
  return EXIT_FAILURE;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %eax,%edi
call   1050 <srand@plt>
lea    0xe72(%rip),%rdi
lea    0xe7d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ae <main+0x4e>
jmp    11f7 <main+0x97>
mov    -0x10(%rbp),%rdi
call   1030 <output@plt>
cmp    $0x0,%rax
je     11c6 <main+0x66>
jmp    1214 <main+0xb4>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     11e6 <main+0x86>
movq   $0x0,-0x10(%rbp)
jmp    1208 <main+0xa8>
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1234 <main+0xd4>
lea    0xe16(%rip),%rdi
call   1080 <perror@plt>
jmp    1214 <main+0xb4>
lea    0xe18(%rip),%rdi
call   1080 <perror@plt>
cmpq   $0x0,-0x10(%rbp)
je     1228 <main+0xc8>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2db1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %edi,%edi
call   1060 <time@plt>
mov    %eax,%edi
call   1050 <srand@plt>
lea    0xe87(%rip),%rdi
lea    0xe92(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11c4 <main+0x64>
mov    %rax,%rbx
mov    %rax,%rdi
call   1030 <output@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %r14,%r14
je     11ae <main+0x4e>
mov    0x2e34(%rip),%rax
jmp    11ba <main+0x5a>
test   %rax,%rax
jne    11cd <main+0x6d>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe49(%rip),%rdi
jmp    11d4 <main+0x74>
lea    0xe53(%rip),%rdi
call   1080 <perror@plt>
jmp    11a5 <main+0x45>

LLVM IR (-O0):
@.str = [18 x i8] c"/proc/kscanf_test\00"
@.str.1 = [2 x i8] c"w\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [19 x i8] c"error opening file\00"
@.str.3 = [19 x i8] c"error closing file\00"
@EXIT_FAILURE = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
store i32 0, i32* %1
store i32* null, i32** %2
%3 = call i32 @time(i32* null)
%4 = call i32 @srand(i32 %3)
%5 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %2
%6 = load i32*, i32** %2
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %9
8: ; preds = %0
br label %21
9: ; preds = %0
%10 = load i32*, i32** %2
%11 = call i64 @output(i32* %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %9
br label %25
14: ; preds = %9
%15 = load i32*, i32** %2
%16 = call i64 @fclose(i32* %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %14
store i32* null, i32** %2
br label %23
19: ; preds = %14
%20 = load i32, i32* @EXIT_SUCCESS
store i32 %20, i32* %1
br label %33
21: ; preds = %8
%22 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %25
23: ; preds = %18
%24 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %23, %21, %13
%26 = load i32*, i32** %2
%27 = icmp ne i32* %26, null
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i32*, i32** %2
%30 = call i64 @fclose(i32* %29)
br label %31
31: ; preds = %28, %25
%32 = load i32, i32* @EXIT_FAILURE
store i32 %32, i32* %1
br label %33
33: ; preds = %31, %19
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
@.str = [18 x i8] c"/proc/kscanf_test\00"
@.str.1 = [2 x i8] c"w\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [19 x i8] c"error opening file\00"
@.str.3 = [19 x i8] c"error closing file\00"
@EXIT_FAILURE = global i32 0
define i32 @main() {
%1 = tail call i32 @time(i32* null)
%2 = tail call i32 @srand(i32 %1)
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %11, label %5
5: ; preds = %0
%6 = tail call i64 @output(i32* %3)
%7 = icmp eq i64 %6, 0
%8 = tail call i64 @fclose(i32* %3)
br i1 %7, label %9, label %14
9: ; preds = %5
%10 = icmp eq i64 %8, 0
br i1 %10, label %14, label %11
11: ; preds = %0, %9
%12 = phi i8* [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), %9 ], [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), %0 ]
%13 = tail call i32 @perror(i8* %12)
br label %14
14: ; preds = %11, %5, %9
%15 = phi i32* [ @EXIT_SUCCESS, %9 ], [ @EXIT_FAILURE, %5 ], [ @EXIT_FAILURE, %11 ]
%16 = load i32, i32* %15
ret i32 %16
}