#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  char text[256];
  FILE *fp;
  fp = fopen("test.txt", "r");
  if (fp == NULL) {
    printf("Can't open file!\n");
    return -1;
  }
  fgets(text, 256, fp);
  printf("%s\n", text);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xea7(%rip),%rdi
lea    0xea9(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x118(%rbp)
cmpq   $0x0,-0x118(%rbp)
jne    1194 <main+0x54>
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d4 <main+0x94>
lea    -0x110(%rbp),%rdi
mov    -0x118(%rbp),%rdx
mov    $0x100,%esi
call   1050 <fgets@plt>
lea    -0x110(%rbp),%rsi
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x118(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x108,%rsp
lea    0xeaf(%rip),%rdi
lea    0xeb1(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     118c <main+0x4c>
mov    %rax,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    119d <main+0x5d>
lea    0xe78(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [18 x i8] c"Can't open file!\0A\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca i32*
store i32 0, i32* %1
%4 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %3
%5 = load i32*, i32** %3
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %17
9: ; preds = %0
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%11 = load i32*, i32** %3
%12 = call i32 @fgets(i8* %10, i32 256, i32* %11)
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %13)
%15 = load i32*, i32** %3
%16 = call i32 @fclose(i32* %15)
store i32 0, i32* %1
br label %17
17: ; preds = %9, %7
%18 = load i32, i32* %1
ret i32 %18
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [2 x i8] c"r\00"
@str = [17 x i8] c"Can't open file!\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 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 %7
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %11
7: ; preds = %0
%8 = call i32 @fgets(i8* %2, i32 256, i32* %3)
%9 = call i32 @puts(i8* %2)
%10 = call i32 @fclose(i32* %3)
br label %11
11: ; preds = %7, %5
%12 = phi i32 [ -1, %5 ], [ 0, %7 ]
ret i32 %12
}