#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*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int line_from_file(char *filename, char *linebuf) {
  char *s;
  int i;
  FILE *fp = fopen(filename, "r");
  if (!fp)
    return -1;
  s = fgets(linebuf, 64, fp);
  fclose(fp);
  if (!s)
    return -2;
  for (i = 0; (*s) && (i < 64); i++) {
    if (*s == '\n')
      *s = 0;
    s++;
  }
  return 0;
}


x86 assembly (clang, -O0):
<line_from_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb5(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    116b <line_from_file+0x3b>
movl   $0xffffffff,-0x4(%rbp)
jmp    120f <line_from_file+0xdf>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    $0x40,%esi
call   1040 <fgets@plt>
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
cmpq   $0x0,-0x20(%rbp)
jne    11a1 <line_from_file+0x71>
movl   $0xfffffffe,-0x4(%rbp)
jmp    120f <line_from_file+0xdf>
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x31(%rbp)
je     11c7 <line_from_file+0x97>
cmpl   $0x40,-0x24(%rbp)
setl   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    11d7 <line_from_file+0xa7>
jmp    1208 <line_from_file+0xd8>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    11ee <line_from_file+0xbe>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11a8 <line_from_file+0x78>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<line_from_file>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
lea    0xec2(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11a0 <line_from_file+0x70>
mov    %rax,%r14
mov    %rbx,%rdi
mov    $0x40,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <fclose@plt>
test   %rbx,%rbx
je     11a7 <line_from_file+0x77>
mov    (%rbx),%dl
test   %dl,%dl
je     11ae <line_from_file+0x7e>
xor    %ecx,%ecx
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nop
cmp    $0xa,%dl
jne    1189 <line_from_file+0x59>
movb   $0x0,(%rbx,%rcx,1)
movzbl 0x1(%rbx,%rcx,1),%edx
test   %dl,%dl
je     11b0 <line_from_file+0x80>
lea    0x1(%rcx),%rsi
cmp    $0x3f,%ecx
mov    %rsi,%rcx
jb     1180 <line_from_file+0x50>
jmp    11b0 <line_from_file+0x80>
mov    $0xffffffff,%eax
jmp    11b0 <line_from_file+0x80>
mov    $0xfffffffe,%eax
jmp    11b0 <line_from_file+0x80>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i32 @line_from_file(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %8
%11 = load i32*, i32** %8
%12 = icmp ne i32* %11, null
br i1 %12, label %14, label %13
13: ; preds = %2
store i32 -1, i32* %3
br label %48
14: ; preds = %2
%15 = load i8*, i8** %5
%16 = load i32*, i32** %8
%17 = call i8* @fgets(i8* %15, i32 64, i32* %16)
store i8* %17, i8** %6
%18 = load i32*, i32** %8
%19 = call i32 @fclose(i32* %18)
%20 = load i8*, i8** %6
%21 = icmp ne i8* %20, null
br i1 %21, label %23, label %22
22: ; preds = %14
store i32 -2, i32* %3
br label %48
23: ; preds = %14
store i32 0, i32* %7
br label %24
24: ; preds = %44, %23
%25 = load i8*, i8** %6
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = load i32, i32* %7
%31 = icmp slt i32 %30, 64
br label %32
32: ; preds = %29, %24
%33 = phi i1 [ false, %24 ], [ %31, %29 ]
br i1 %33, label %34, label %47
34: ; preds = %32
%35 = load i8*, i8** %6
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 10
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = load i8*, i8** %6
store i8 0, i8* %40
br label %41
41: ; preds = %39, %34
%42 = load i8*, i8** %6
%43 = getelementptr inbounds i8, i8* %42, i32 1
store i8* %43, i8** %6
br label %44
44: ; preds = %41
%45 = load i32, i32* %7
%46 = add nsw i32 %45, 1
store i32 %46, i32* %7
br label %24
47: ; preds = %32
store i32 0, i32* %3
br label %48
48: ; preds = %47, %22, %13
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i32 @line_from_file(i8* nocapture %0, i8* %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %25, label %5
5: ; preds = %2
%6 = tail call i8* @fgets(i8* %1, i32 64, i32* %3)
%7 = tail call i32 @fclose(i32* %3)
%8 = icmp eq i8* %6, null
br i1 %8, label %25, label %9
9: ; preds = %5
%10 = load i8, i8* %6
%11 = icmp eq i8 %10, 0
br i1 %11, label %25, label %12
12: ; preds = %9, %18
%13 = phi i8 [ %21, %18 ], [ %10, %9 ]
%14 = phi i32 [ %20, %18 ], [ 0, %9 ]
%15 = phi i8* [ %19, %18 ], [ %6, %9 ]
%16 = icmp eq i8 %13, 10
br i1 %16, label %17, label %18
17: ; preds = %12
store i8 0, i8* %15
br label %18
18: ; preds = %17, %12
%19 = getelementptr inbounds i8, i8* %15, i64 1
%20 = add nuw nsw i32 %14, 1
%21 = load i8, i8* %19
%22 = icmp ne i8 %21, 0
%23 = icmp ult i32 %14, 63
%24 = select i1 %22, i1 %23, i1 false
br i1 %24, label %12, label %25
25: ; preds = %18, %9, %5, %2
%26 = phi i32 [ -1, %2 ], [ -2, %5 ], [ 0, %9 ], [ 0, %18 ]
ret i32 %26
}