#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 open(char *, int /*<<< orphan*/);
int read(int, char *, size_t);
int read_line(char *line, size_t line_size) {
  int fd;
  int len;
  fd = open(line, O_RDONLY);
  if (fd == 0)
    return -1;
  len = read(fd, line, line_size - 1);
  close(fd);
  if (len <= 0)
    return -1;
  line[len] = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e9d(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    116b <read_line+0x3b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b3 <read_line+0x83>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
call   1040 <read@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
cmpl   $0x0,-0x20(%rbp)
jg     11a0 <read_line+0x70>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b3 <read_line+0x83>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbp
mov    %rdi,%r15
mov    0x2ea4(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
je     117f <read_line+0x4f>
mov    %eax,%ebx
add    $0xffffffffffffffff,%rbp
mov    %eax,%edi
mov    %r15,%rsi
mov    %rbp,%rdx
call   1040 <read@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1030 <close@plt>
test   %ebp,%ebp
jle    117f <read_line+0x4f>
mov    %ebp,%eax
movb   $0x0,(%r15,%rax,1)
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i32 @read_line(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = load i8*, i8** %4
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %30
14: ; preds = %2
%15 = load i32, i32* %6
%16 = load i8*, i8** %4
%17 = load i64, i64* %5
%18 = sub i64 %17, 1
%19 = call i32 @read(i32 %15, i8* %16, i64 %18)
store i32 %19, i32* %7
%20 = load i32, i32* %6
%21 = call i32 @close(i32 %20)
%22 = load i32, i32* %7
%23 = icmp sle i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %14
store i32 -1, i32* %3
br label %30
25: ; preds = %14
%26 = load i8*, i8** %4
%27 = load i32, i32* %7
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 0, i8* %29
store i32 0, i32* %3
br label %30
30: ; preds = %25, %24, %13
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i32 @read_line(i8* nocapture %0, i64 %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp eq i32 %4, 0
br i1 %5, label %14, label %6
6: ; preds = %2
%7 = add i64 %1, -1
%8 = tail call i32 @read(i32 %4, i8* %0, i64 %7)
%9 = tail call i32 @close(i32 %4)
%10 = icmp slt i32 %8, 1
br i1 %10, label %14, label %11
11: ; preds = %6
%12 = zext i32 %8 to i64
%13 = getelementptr inbounds i8, i8* %0, i64 %12
store i8 0, i8* %13
br label %14
14: ; preds = %6, %2, %11
%15 = phi i32 [ 0, %11 ], [ -1, %2 ], [ -1, %6 ]
ret i32 %15
}