#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int f_read(const char *filename) {
  int fd, ret;
  char buf[512 + 1] = {0};
  fd = open(filename, O_RDONLY);
  if (fd < 0) {
    printf("File %s open failure, errno=%d\n", filename, errno);
    return -1;
  }
  printf("Before read function\n");
  ret = read(fd, buf, 512);
  printf("After read function\n");
  if (ret < 0) {
    printf("File %s read failure, errno=%d\n", filename, errno);
  } else if (ret == 0) {
    printf("No data in file %s\n", filename);
  } else {
    buf[ret] = '\0';
    printf("Read %d(bytes): %s\n", ret, buf);
  }
  close(fd);
  return ret < 0 ? -1 : 0;
}


x86 assembly (clang, -O0):
<f_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x201,%edx
call   1040 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11b8 <f_read+0x68>
mov    -0x10(%rbp),%rsi
mov    0x2e3c(%rip),%rax
mov    (%rax),%edx
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127a <f_read+0x12a>
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1060 <read@plt>
mov    %eax,-0x18(%rbp)
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0x18(%rbp)
jge    1215 <f_read+0xc5>
mov    -0x10(%rbp),%rsi
mov    0x2dd8(%rip),%rax
mov    (%rax),%edx
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    125f <f_read+0x10f>
cmpl   $0x0,-0x18(%rbp)
jne    1236 <f_read+0xe6>
mov    -0x10(%rbp),%rsi
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    125a <f_read+0x10a>
movslq -0x18(%rbp),%rax
movb   $0x0,-0x220(%rbp,%rax,1)
mov    -0x18(%rbp),%esi
lea    -0x220(%rbp),%rdx
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    125f <f_read+0x10f>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edx
xor    %eax,%eax
mov    $0xffffffff,%ecx
cmp    $0x0,%edx
cmovl  %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<f_read>:
push   %rbp
push   %r14
push   %rbx
sub    $0x210,%rsp
mov    %rdi,%r14
mov    %rsp,%rdi
mov    $0x201,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11de <f_read+0x7e>
mov    %eax,%ebx
lea    0xecd(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x200,%edx
call   1070 <read@plt>
mov    %eax,%ebp
lea    0xec5(%rip),%rdi
call   1030 <puts@plt>
test   %ebp,%ebp
js     11ff <f_read+0x9f>
je     121b <f_read+0xbb>
mov    %ebp,%eax
movb   $0x0,(%rsp,%rax,1)
lea    0xe84(%rip),%rdi
mov    %rsp,%rdx
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    122c <f_read+0xcc>
mov    0x2df3(%rip),%rax
mov    (%rax),%edx
lea    0xe12(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%ebp
jmp    1236 <f_read+0xd6>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
lea    0xe11(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    122c <f_read+0xcc>
lea    0xe1e(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
sar    $0x1f,%ebp
mov    %ebp,%eax
add    $0x210,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [32 x i8] c"File %s open failure, errno=%d\0A\00"
@errno = global i32 0
@.str.1 = [22 x i8] c"Before read function\0A\00"
@.str.2 = [21 x i8] c"After read function\0A\00"
@.str.3 = [32 x i8] c"File %s read failure, errno=%d\0A\00"
@.str.4 = [20 x i8] c"No data in file %s\0A\00"
@.str.5 = [20 x i8] c"Read %d(bytes): %s\0A\00"
define i32 @f_read(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [513 x i8]
store i8* %0, i8** %3
%7 = bitcast [513 x i8]* %6 to i8*
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %1
%14 = load i8*, i8** %3
%15 = load i32, i32* @errno
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %14, i32 %15)
store i32 -1, i32* %2
br label %50
17: ; preds = %1
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* %4
%20 = getelementptr inbounds [513 x i8], [513 x i8]* %6, i64 0, i64 0
%21 = call i32 @read(i32 %19, i8* %20, i32 512)
store i32 %21, i32* %5
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%23 = load i32, i32* %5
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %17
%26 = load i8*, i8** %3
%27 = load i32, i32* @errno
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %26, i32 %27)
br label %43
29: ; preds = %17
%30 = load i32, i32* %5
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i8*, i8** %3
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %33)
br label %42
35: ; preds = %29
%36 = load i32, i32* %5
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [513 x i8], [513 x i8]* %6, i64 0, i64 %37
store i8 0, i8* %38
%39 = load i32, i32* %5
%40 = getelementptr inbounds [513 x i8], [513 x i8]* %6, i64 0, i64 0
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %39, i8* %40)
br label %42
42: ; preds = %35, %32
br label %43
43: ; preds = %42, %25
%44 = load i32, i32* %4
%45 = call i32 @close(i32 %44)
%46 = load i32, i32* %5
%47 = icmp slt i32 %46, 0
%48 = zext i1 %47 to i64
%49 = select i1 %47, i32 -1, i32 0
store i32 %49, i32* %2
br label %50
50: ; preds = %43, %13
%51 = load i32, i32* %2
ret i32 %51
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [32 x i8] c"File %s open failure, errno=%d\0A\00"
@errno = global i32 0
@.str.3 = [32 x i8] c"File %s read failure, errno=%d\0A\00"
@.str.4 = [20 x i8] c"No data in file %s\0A\00"
@.str.5 = [20 x i8] c"Read %d(bytes): %s\0A\00"
@str = [21 x i8] c"Before read function\00"
@str.6 = [20 x i8] c"After read function\00"
define i32 @f_read(i8* %0) {
%2 = alloca [513 x i8]
%3 = getelementptr inbounds [513 x i8], [513 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @errno
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %8)
br label %29
10: ; preds = %1
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%12 = call i32 @read(i32 %5, i8* %3, i32 512)
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.6, i64 0, i64 0))
%14 = icmp slt i32 %12, 0
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = load i32, i32* @errno
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %0, i32 %16)
br label %26
18: ; preds = %10
%19 = icmp eq i32 %12, 0
br i1 %19, label %20, label %22
20: ; preds = %18
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %26
22: ; preds = %18
%23 = zext i32 %12 to i64
%24 = getelementptr inbounds [513 x i8], [513 x i8]* %2, i64 0, i64 %23
store i8 0, i8* %24
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %12, i8* %3)
br label %26
26: ; preds = %20, %22, %15
%27 = call i32 @close(i32 %5)
%28 = ashr i32 %12, 31
br label %29
29: ; preds = %26, %7
%30 = phi i32 [ -1, %7 ], [ %28, %26 ]
ret i32 %30
}