#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*/ exit(int);
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int main() {
  char *file_name;
  FILE *f_ptr;
  char *buffer;
  size_t result;
  f_ptr = fopen("1.txt", "r");
  if (f_ptr == NULL) {
    printf("File not found\n");
  }
  printf("Now sleeping for a while\n");
  sleep(5);
  buffer = malloc(20);
  if (buffer != NULL) {
    result = fread(buffer, 1, 22, f_ptr);
    printf("Result is: %s\n", buffer);
  } else {
    printf("malloc failed\n");
  }
  free(buffer);
  if (fclose(f_ptr) != 0) {
    exit(1);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe6a(%rip),%rdi
lea    0xe69(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11bf <main+0x3f>
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x5,%edi
call   10a0 <sleep@plt>
mov    $0x14,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1222 <main+0xa2>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x16,%edx
call   1040 <fread@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1230 <main+0xb0>
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
cmp    $0x0,%rax
je     1256 <main+0xd6>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xe65(%rip),%rdi
lea    0xe64(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11bb <main+0x2b>
lea    0xe88(%rip),%rdi
call   1040 <puts@plt>
lea    0xe55(%rip),%rdi
call   1040 <puts@plt>
mov    $0x5,%edi
call   10b0 <sleep@plt>
mov    $0x14,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     120b <main+0x7b>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x16,%edx
mov    %r14,%rcx
call   1050 <fread@plt>
lea    0xe09(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    1217 <main+0x87>
lea    0xe1e(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1060 <fclose@plt>
test   %rax,%rax
jne    1236 <main+0xa6>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"1.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [16 x i8] c"File not found\0A\00"
@.str.3 = [26 x i8] c"Now sleeping for a while\0A\00"
@.str.4 = [15 x i8] c"Result is: %s\0A\00"
@.str.5 = [15 x i8] c"malloc failed\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i64
store i32 0, i32* %1
%6 = call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %3
%7 = load i32*, i32** %3
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %11
11: ; preds = %9, %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%13 = call i32 @sleep(i32 5)
%14 = call i8* @malloc(i32 20)
store i8* %14, i8** %4
%15 = load i8*, i8** %4
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %23
17: ; preds = %11
%18 = load i8*, i8** %4
%19 = load i32*, i32** %3
%20 = call i64 @fread(i8* %18, i32 1, i32 22, i32* %19)
store i64 %20, i64* %5
%21 = load i8*, i8** %4
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %21)
br label %25
23: ; preds = %11
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
br label %25
25: ; preds = %23, %17
%26 = load i8*, i8** %4
%27 = call i32 @free(i8* %26)
%28 = load i32*, i32** %3
%29 = call i64 @fclose(i32* %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %25
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %25
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
@.str = [6 x i8] c"1.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.4 = [15 x i8] c"Result is: %s\0A\00"
@str = [25 x i8] c"Now sleeping for a while\00"
@str.6 = [14 x i8] c"malloc failed\00"
@str.7 = [15 x i8] c"File not found\00"
define i32 @main() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.7, i64 0, i64 0))
br label %5
5: ; preds = %3, %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%7 = tail call i32 @sleep(i32 5)
%8 = tail call dereferenceable_or_null(20) i8* @malloc(i32 20)
%9 = icmp eq i8* %8, null
br i1 %9, label %13, label %10
10: ; preds = %5
%11 = tail call i64 @fread(i8* %8, i32 1, i32 22, i32* %1)
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %8)
br label %15
13: ; preds = %5
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.6, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
%16 = tail call i32 @free(i8* %8)
%17 = tail call i64 @fclose(i32* %1)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %15
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %15
ret i32 0
}