#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*/ chop_newline(char *);
int /*<<< orphan*/ debug(char *, char *, ...);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int read_first_line(char *filename, char *buf, int size) {
  FILE *file;
  file = fopen(filename, "r");
  if (file == NULL) {
    debug("error opening '%s': %s", filename, strerror(errno));
    return 0;
  }
  int ok = (fgets(buf, size, file) != NULL);
  fclose(file);
  if (!ok) {
    debug("error or EOF reading from '%s'", filename);
    return 0;
  }
  chop_newline(buf);
  return 1;
}


x86 assembly (clang, -O0):
<read_first_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe82(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c8 <read_first_line+0x68>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1040 <debug@plt>
movl   $0x0,-0x4(%rbp)
jmp    1228 <read_first_line+0xc8>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rdx
call   1050 <fgets@plt>
cmp    $0x0,%rax
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x0,-0x2c(%rbp)
jne    1218 <read_first_line+0xb8>
mov    -0x10(%rbp),%rsi
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1040 <debug@plt>
movl   $0x0,-0x4(%rbp)
jmp    1228 <read_first_line+0xc8>
mov    -0x18(%rbp),%rdi
call   1060 <chop_newline@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_first_line>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe8a(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11af <read_first_line+0x4f>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %ebp,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %rbp,%rbp
je     11d4 <read_first_line+0x74>
mov    %r14,%rdi
call   1060 <chop_newline@plt>
mov    $0x1,%ebp
jmp    11e7 <read_first_line+0x87>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe3e(%rip),%rdi
xor    %ebp,%ebp
mov    %r15,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <debug@plt>
jmp    11e7 <read_first_line+0x87>
lea    0xe3e(%rip),%rdi
xor    %ebp,%ebp
mov    %r15,%rsi
xor    %eax,%eax
call   1040 <debug@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [23 x i8] c"error opening '%s': %s\00"
@errno = global i32 0
@.str.2 = [31 x i8] c"error or EOF reading from '%s'\00"
define i32 @read_first_line(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %8
%12 = load i32*, i32** %8
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %19
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = load i32, i32* @errno
%17 = call i32 @strerror(i32 %16)
%18 = call i32 (i8*, i8*, ...) @debug(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %15, i32 %17)
store i32 0, i32* %4
br label %36
19: ; preds = %3
%20 = load i8*, i8** %6
%21 = load i32, i32* %7
%22 = load i32*, i32** %8
%23 = call i32* @fgets(i8* %20, i32 %21, i32* %22)
%24 = icmp ne i32* %23, null
%25 = zext i1 %24 to i32
store i32 %25, i32* %9
%26 = load i32*, i32** %8
%27 = call i32 @fclose(i32* %26)
%28 = load i32, i32* %9
%29 = icmp ne i32 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %19
%31 = load i8*, i8** %5
%32 = call i32 (i8*, i8*, ...) @debug(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %31)
store i32 0, i32* %4
br label %36
33: ; preds = %19
%34 = load i8*, i8** %6
%35 = call i32 @chop_newline(i8* %34)
store i32 1, i32* %4
br label %36
36: ; preds = %33, %30, %14
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [23 x i8] c"error opening '%s': %s\00"
@errno = global i32 0
@.str.2 = [31 x i8] c"error or EOF reading from '%s'\00"
define i32 @read_first_line(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %10
6: ; preds = %3
%7 = load i32, i32* @errno
%8 = tail call i32 @strerror(i32 %7)
%9 = tail call i32 (i8*, i8*, ...) @debug(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %8)
br label %18
10: ; preds = %3
%11 = tail call i32* @fgets(i8* %1, i32 %2, i32* %4)
%12 = icmp eq i32* %11, null
%13 = tail call i32 @fclose(i32* %4)
br i1 %12, label %14, label %16
14: ; preds = %10
%15 = tail call i32 (i8*, i8*, ...) @debug(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %18
16: ; preds = %10
%17 = tail call i32 @chop_newline(i8* %1)
br label %18
18: ; preds = %14, %16, %6
%19 = phi i32 [ 0, %6 ], [ 1, %16 ], [ 0, %14 ]
ret i32 %19
}