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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ bzero(char *, int);
int errno;
int /*<<< orphan*/ *fdopen(int, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  int fd = open("a.txt", O_RDONLY);
  if (fd == -1) {
    perror("open");
    return errno;
  }
  FILE *file = fdopen(fd, "r");
  if (file == NULL) {
    perror("fdopen");
    return errno;
  }
  char buf[16];
  bzero(buf, sizeof(buf));
  size_t i = fread(buf, 1, sizeof(buf) - 1, file);
  if (i == 0) {
    printf("none read\n");
    return 0;
  }
  printf("%s\n", buf);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11ae <main+0x4e>
lea    0xe72(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1249 <main+0xe9>
mov    -0x8(%rbp),%edi
lea    0xe4e(%rip),%rsi
call   1050 <fdopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11e9 <main+0x89>
lea    0xe35(%rip),%rdi
call   1070 <perror@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1249 <main+0xe9>
lea    -0x20(%rbp),%rdi
mov    $0x10,%esi
call   1080 <bzero@plt>
lea    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0xf,%edx
call   1030 <fread@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1237 <main+0xd7>
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1249 <main+0xe9>
lea    -0x20(%rbp),%rsi
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
lea    0xe89(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11dc <main+0x7c>
lea    0xe7e(%rip),%rsi
mov    %eax,%edi
call   1050 <fdopen@plt>
test   %rax,%rax
je     11e5 <main+0x85>
mov    %rax,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x10,%esi
call   1080 <bzero@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0xf,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     11c6 <main+0x66>
mov    %rsp,%rdi
jmp    11cd <main+0x6d>
lea    0xe42(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe27(%rip),%rdi
jmp    11ec <main+0x8c>
lea    0xe1c(%rip),%rdi
call   1070 <perror@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
jmp    11d4 <main+0x74>

LLVM IR (-O0):
@.str = [6 x i8] c"a.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@errno = global i32 0
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [7 x i8] c"fdopen\00"
@.str.4 = [11 x i8] c"none read\0A\00"
@.str.5 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = alloca [16 x i8]
%5 = alloca i64
store i32 0, i32* %1
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* @errno
store i32 %12, i32* %1
br label %34
13: ; preds = %0
%14 = load i32, i32* %2
%15 = call i32* @fdopen(i32 %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %15, i32** %3
%16 = load i32*, i32** %3
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%20 = load i32, i32* @errno
store i32 %20, i32* %1
br label %34
21: ; preds = %13
%22 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%23 = call i32 @bzero(i8* %22, i32 16)
%24 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%25 = load i32*, i32** %3
%26 = call i64 @fread(i8* %24, i32 1, i32 15, i32* %25)
store i64 %26, i64* %5
%27 = load i64, i64* %5
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %21
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %1
br label %34
31: ; preds = %21
%32 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %32)
br label %34
34: ; preds = %31, %29, %18, %10
%35 = load i32, i32* %1
ret i32 %35
}

LLVM IR (-O2):
@.str = [6 x i8] c"a.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@errno = global i32 0
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [7 x i8] c"fdopen\00"
@str = [10 x i8] c"none read\00"
define i32 @main() {
%1 = alloca [16 x i8]
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%7 = load i32, i32* @errno
br label %24
8: ; preds = %0
%9 = tail call i32* @fdopen(i32 %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%13 = load i32, i32* @errno
br label %24
14: ; preds = %8
%15 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%16 = call i32 @bzero(i8* %15, i32 16)
%17 = call i64 @fread(i8* %15, i32 1, i32 15, i32* %9)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
br label %23
21: ; preds = %14
%22 = call i32 @puts(i8* %15)
br label %23
23: ; preds = %21, %19
br label %24
24: ; preds = %11, %23, %5
%25 = phi i32 [ %7, %5 ], [ %13, %11 ], [ 0, %23 ]
ret i32 %25
}