#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 /*<<< orphan*/ fatal(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fread(void *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int readdim_file(char *fn) {
  FILE *fd = fopen(fn, "r");
  if (!fd) {
    fprintf(stderr, "readdim_file: fn = %s\n", fn);
    fatal("open failed");
  }
  if (fd == NULL)
    fprintf(stderr, "File does not exist: %s\n", fn);
  int D = 0;
  fread((void *)&D, sizeof(int), 1, fd);
  fclose(fd);
  return D;
}


x86 assembly (clang, -O0):
<readdim_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a0 <readdim_file+0x50>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe73(%rip),%rsi
call   1050 <fprintf@plt>
lea    0xe7e(%rip),%rdi
call   1070 <fatal@plt>
cmpq   $0x0,-0x10(%rbp)
jne    11c4 <readdim_file+0x74>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe66(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x14(%rbp)
lea    -0x14(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readdim_file>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
lea    0xe9d(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11a7 <readdim_file+0x57>
mov    0x2e69(%rip),%r15
mov    (%r15),%edi
lea    0xe81(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
lea    0xe89(%rip),%rdi
call   1070 <fatal@plt>
mov    (%r15),%edi
lea    0xe86(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
movl   $0x0,0xc(%rsp)
lea    0xc(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0xc(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"readdim_file: fn = %s\0A\00"
@.str.2 = [12 x i8] c"open failed\00"
@.str.3 = [25 x i8] c"File does not exist: %s\0A\00"
define i32 @readdim_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %6, i32** %3
%7 = load i32*, i32** %3
%8 = icmp ne i32* %7, null
br i1 %8, label %14, label %9
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = load i8*, i8** %2
%12 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%13 = call i32 @fatal(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %14
14: ; preds = %9, %1
%15 = load i32*, i32** %3
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %2
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %19)
br label %21
21: ; preds = %17, %14
store i32 0, i32* %4
%22 = bitcast i32* %4 to i8*
%23 = load i32*, i32** %3
%24 = call i32 @fread(i8* %22, i32 4, i32 1, i32* %23)
%25 = load i32*, i32** %3
%26 = call i32 @fclose(i32* %25)
%27 = load i32, i32* %4
ret i32 %27
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"readdim_file: fn = %s\0A\00"
@.str.2 = [12 x i8] c"open failed\00"
@.str.3 = [25 x i8] c"File does not exist: %s\0A\00"
define i32 @readdim_file(i8* %0) {
%2 = alloca i32
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %11
5: ; preds = %1
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%8 = tail call i32 @fatal(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %11
11: ; preds = %1, %5
%12 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%13 = call i32 @fread(i8* %12, i32 4, i32 1, i32* %3)
%14 = tail call i32 @fclose(i32* %3)
%15 = load i32, i32* %2
ret i32 %15
}