#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 size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char const *, char *);
size_t read(int, char *, size_t);
char *strerror(int /*<<< orphan*/);
void read_file(const char *filename, char *buf, size_t size) {
  int fd;
  ssize_t ret;
  fd = open(filename, O_RDONLY);
  if (fd < 0) {
    printf("error: "
           "failed to open %s"
           ": %s"
           "\n",
           filename, strerror(errno));
    exit(-1);
  };
  ret = read(fd, buf, size - 1);
  if (ret < 0) {
    printf("error: "
           "failed to read %s"
           ": %s"
           "\n",
           filename, strerror(errno));
    exit(-1);
  };
  buf[ret] = 0;
  while (buf[ret - 1] == '\n')
    buf[--ret] = 0;
  close(fd);
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11c6 <read_file+0x66>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
lea    0xe49(%rip),%rdi
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
call   1050 <read@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jae    121c <read_file+0xbc>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
lea    0xe11(%rip),%rdi
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    125e <read_file+0xfe>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x28(%rbp)
movb   $0x0,-0x1(%rax,%rcx,1)
jmp    1228 <read_file+0xc8>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     11cf <read_file+0x6f>
mov    %eax,%r15d
add    $0xffffffffffffffff,%rbp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1050 <read@plt>
movb   $0x0,(%rbx,%rax,1)
cmpb   $0xa,-0x1(%rbx,%rax,1)
jne    11bd <read_file+0x5d>
add    %rbx,%rax
add    $0xffffffffffffffff,%rax
nopl   0x0(%rax,%rax,1)
movb   $0x0,(%rax)
cmpb   $0xa,-0x1(%rax)
lea    -0x1(%rax),%rax
je     11b0 <read_file+0x50>
mov    %r15d,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <close@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe1c(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [30 x i8] c"error: failed to open %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [30 x i8] c"error: failed to read %s: %s\0A\00"
define void @read_file(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %3
%15 = load i8*, i8** %4
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %17)
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %3
%21 = load i32, i32* %7
%22 = load i8*, i8** %5
%23 = load i64, i64* %6
%24 = sub i64 %23, 1
%25 = call i64 @read(i32 %21, i8* %22, i64 %24)
store i64 %25, i64* %8
%26 = load i64, i64* %8
%27 = icmp ult i64 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %20
%29 = load i8*, i8** %4
%30 = load i32, i32* @errno
%31 = call i8* @strerror(i32 %30)
%32 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %29, i8* %31)
%33 = call i32 @exit(i32 -1)
unreachable
34: ; preds = %20
%35 = load i8*, i8** %5
%36 = load i64, i64* %8
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8 0, i8* %37
br label %38
38: ; preds = %46, %34
%39 = load i8*, i8** %5
%40 = load i64, i64* %8
%41 = sub i64 %40, 1
%42 = getelementptr inbounds i8, i8* %39, i64 %41
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp eq i32 %44, 10
br i1 %45, label %46, label %51
46: ; preds = %38
%47 = load i8*, i8** %5
%48 = load i64, i64* %8
%49 = add i64 %48, -1
store i64 %49, i64* %8
%50 = getelementptr inbounds i8, i8* %47, i64 %49
store i8 0, i8* %50
br label %38
51: ; preds = %38
%52 = load i32, i32* %7
%53 = call i32 @close(i32 %52)
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [30 x i8] c"error: failed to open %s: %s\0A\00"
@errno = global i32 0
define void @read_file(i8* %0, i8* nocapture %1, i64 %2) {
%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 %12
7: ; preds = %3
%8 = load i32, i32* @errno
%9 = tail call i8* @strerror(i32 %8)
%10 = tail call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %9)
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %3
%13 = add i64 %2, -1
%14 = tail call i64 @read(i32 %5, i8* %1, i64 %13)
%15 = getelementptr inbounds i8, i8* %1, i64 %14
store i8 0, i8* %15
%16 = add i64 %14, -1
%17 = getelementptr inbounds i8, i8* %1, i64 %16
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 10
br i1 %19, label %20, label %27
20: ; preds = %12, %20
%21 = phi i8* [ %24, %20 ], [ %17, %12 ]
%22 = phi i64 [ %23, %20 ], [ %16, %12 ]
store i8 0, i8* %21
%23 = add i64 %22, -1
%24 = getelementptr inbounds i8, i8* %1, i64 %23
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 10
br i1 %26, label %20, label %27
27: ; preds = %20, %12
%28 = tail call i32 @close(i32 %5)
ret void
}