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

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
int get_buf_sz(void) {
  char line[32];
  int ret;
  FILE *file = fopen("/sys/module/em5_module/parameters/mem", "r");
  if (!file) {
    fprintf(stderr,
            "Error: "
            "Can't open %s"
            "\n",
            "/sys/module/em5_module/parameters/mem");
    return 0;
  }
  if (fgets(line, sizeof(line), file)) {
    ret = atoi(line);
  } else {
    fprintf(stderr,
            "Error: "
            "Can't read %s"
            "\n",
            "/sys/module/em5_module/parameters/mem");
    return 0;
  }
  fclose(file);
  return ret;
}


x86 assembly (clang, -O0):
<get_buf_sz>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    0xea1(%rip),%rdi
lea    0xec0(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11a2 <get_buf_sz+0x52>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
lea    0xe9e(%rip),%rsi
lea    0xe6f(%rip),%rdx
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1206 <get_buf_sz+0xb6>
lea    -0x30(%rbp),%rdi
mov    -0x40(%rbp),%rdx
mov    $0x20,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     11cf <get_buf_sz+0x7f>
lea    -0x30(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x34(%rbp)
jmp    11f7 <get_buf_sz+0xa7>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xe5f(%rip),%rsi
lea    0xe1a(%rip),%rdx
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1206 <get_buf_sz+0xb6>
mov    -0x40(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_buf_sz>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
lea    0xea3(%rip),%rdi
lea    0xec2(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     119a <get_buf_sz+0x4a>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x20,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     11ac <get_buf_sz+0x5c>
mov    %rsp,%rdi
call   1070 <atoi@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    11ca <get_buf_sz+0x7a>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
lea    0xe7e(%rip),%rsi
jmp    11bc <get_buf_sz+0x6c>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
lea    0xe82(%rip),%rsi
lea    0xe3d(%rip),%rdx
call   1050 <fprintf@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [38 x i8] c"/sys/module/em5_module/parameters/mem\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [22 x i8] c"Error: Can't open %s\0A\00"
@.str.3 = [22 x i8] c"Error: Can't read %s\0A\00"
define i32 @get_buf_sz() {
%1 = alloca i32
%2 = alloca [32 x i8]
%3 = alloca i32
%4 = alloca i32*
%5 = call i32* @fopen(i8* getelementptr inbounds ([38 x i8], [38 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** %4
%6 = load i32*, i32** %4
%7 = icmp ne i32* %6, null
br i1 %7, label %11, label %8
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %26
11: ; preds = %0
%12 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%13 = load i32*, i32** %4
%14 = call i64 @fgets(i8* %12, i32 32, i32* %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%18 = call i32 @atoi(i8* %17)
store i32 %18, i32* %3
br label %22
19: ; preds = %11
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %26
22: ; preds = %16
%23 = load i32*, i32** %4
%24 = call i32 @fclose(i32* %23)
%25 = load i32, i32* %3
store i32 %25, i32* %1
br label %26
26: ; preds = %22, %19, %8
%27 = load i32, i32* %1
ret i32 %27
}

LLVM IR (-O2):
@.str = [38 x i8] c"/sys/module/em5_module/parameters/mem\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [22 x i8] c"Error: Can't open %s\0A\00"
@.str.3 = [22 x i8] c"Error: Can't read %s\0A\00"
define i32 @get_buf_sz() {
%1 = alloca [32 x i8]
%2 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([38 x i8], [38 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 %5, label %8
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
br label %17
8: ; preds = %0
%9 = call i64 @fgets(i8* %2, i32 32, i32* %3)
%10 = icmp eq i64 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %8
%12 = call i32 @atoi(i8* %2)
%13 = call i32 @fclose(i32* %3)
br label %17
14: ; preds = %8
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %11, %14, %5
%18 = phi i32 [ %12, %11 ], [ 0, %14 ], [ 0, %5 ]
ret i32 %18
}