#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 atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ perror(char *);
int read_num_from_file(const char *filename) {
  FILE *fp;
  char buffer[1024];
  int num;
  fp = fopen(filename, "r");
  if (fp == NULL) {
    perror("fopen");
    return -1;
  }
  fgets(buffer, 1024, fp);
  fclose(fp);
  num = atoi(buffer);
  return num;
}


x86 assembly (clang, -O0):
<read_num_from_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe96(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1196 <read_num_from_file+0x46>
lea    0xe7d(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cf <read_num_from_file+0x7f>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x400,%esi
call   1040 <fgets@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x420(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x424(%rbp)
mov    -0x424(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_num_from_file>:
push   %r14
push   %rbx
sub    $0x408,%rsp
lea    0xe9f(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     119c <read_num_from_file+0x4c>
mov    %rax,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %r14,%rdi
call   1070 <atoi@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe5f(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    1191 <read_num_from_file+0x41>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"fopen\00"
define i32 @read_num_from_file(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [1024 x i8]
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %4
%9 = load i32*, i32** %4
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %22
13: ; preds = %1
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%15 = load i32*, i32** %4
%16 = call i32 @fgets(i8* %14, i32 1024, i32* %15)
%17 = load i32*, i32** %4
%18 = call i32 @fclose(i32* %17)
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%20 = call i32 @atoi(i8* %19)
store i32 %20, i32* %6
%21 = load i32, i32* %6
store i32 %21, i32* %2
br label %22
22: ; preds = %13, %11
%23 = load i32, i32* %2
ret i32 %23
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"fopen\00"
define i32 @read_num_from_file(i8* nocapture %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %12
8: ; preds = %1
%9 = call i32 @fgets(i8* %3, i32 1024, i32* %4)
%10 = call i32 @fclose(i32* %4)
%11 = call i32 @atoi(i8* %3)
br label %12
12: ; preds = %8, %6
%13 = phi i32 [ -1, %6 ], [ %11, %8 ]
ret i32 %13
}