#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*/ ENOMEM;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
char *malloc(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
char *first_line_from_file(char *filename) {
  char c, *out;
  int fd, len;
  fd = open(filename, O_RDONLY);
  if (fd < 0) {
    fprintf(stderr, "open: %s: %s", filename, strerror(errno));
    return NULL;
  }
  for (len = 0; ((read(fd, &c, 1) == 1) && (c != '\n')); len++)
    ;
  lseek(fd, 0, SEEK_SET);
  out = malloc(len + 1);
  if (!out) {
    errno = ENOMEM;
    fprintf(stderr, "malloc: %s", strerror(errno));
    goto out;
  }
  len = read(fd, out, len);
  if (len < 0) {
    fprintf(stderr, "read: %s", strerror(errno));
    free(out);
    out = NULL;
    goto out;
  }
  out[len] = 0;
out:
  close(fd);
  return out;
}


x86 assembly (clang, -O0):
<first_line_from_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11f2 <first_line_from_file+0x72>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xe22(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1338 <first_line_from_file+0x1b8>
movl   $0x0,-0x28(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x1,%ecx
mov    %al,-0x35(%rbp)
jne    1227 <first_line_from_file+0xa7>
movsbl -0x11(%rbp),%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x35(%rbp)
mov    -0x35(%rbp),%al
test   $0x1,%al
jne    1237 <first_line_from_file+0xb7>
jmp    124a <first_line_from_file+0xca>
jmp    123c <first_line_from_file+0xbc>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11f9 <first_line_from_file+0x79>
mov    -0x24(%rbp),%edi
mov    0x2d6c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    -0x28(%rbp),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12bc <first_line_from_file+0x13c>
mov    0x2d5a(%rip),%rax
mov    (%rax),%ecx
mov    0x2d41(%rip),%rax
mov    %ecx,(%rax)
mov    0x2d40(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2d2c(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    %rax,%rdx
lea    0xd5d(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1328 <first_line_from_file+0x1a8>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1060 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    131c <first_line_from_file+0x19c>
mov    0x2cf1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2cdd(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x40(%rbp),%edi
mov    %rax,%rdx
lea    0xd19(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
jmp    1328 <first_line_from_file+0x1a8>
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<first_line_from_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
js     120f <first_line_from_file+0x8f>
mov    %eax,%r15d
mov    $0xffffffff,%ebx
lea    0x7(%rsp),%rbp
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
add    $0x1,%ebx
cmp    $0x1,%eax
jne    11cf <first_line_from_file+0x4f>
cmpb   $0xa,0x7(%rsp)
jne    11b0 <first_line_from_file+0x30>
mov    0x2dea(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
lea    0x1(%rbx),%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1241 <first_line_from_file+0xc1>
mov    %rax,%r14
mov    %r15d,%edi
mov    %rax,%rsi
mov    %ebx,%edx
call   1060 <read@plt>
test   %eax,%eax
js     1279 <first_line_from_file+0xf9>
mov    %eax,%eax
movb   $0x0,(%r14,%rax,1)
jmp    12ae <first_line_from_file+0x12e>
mov    0x2dba(%rip),%rax
mov    (%rax),%ebp
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdd3(%rip),%rsi
xor    %r14d,%r14d
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    12b6 <first_line_from_file+0x136>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
mov    0x2d77(%rip),%rax
mov    %edi,(%rax)
mov    0x2d76(%rip),%rax
mov    (%rax),%ebx
call   10a0 <strerror@plt>
lea    0xda5(%rip),%rsi
xor    %r14d,%r14d
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    12ae <first_line_from_file+0x12e>
mov    0x2d50(%rip),%rax
mov    (%rax),%ebx
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd81(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r15d,%edi
call   1050 <close@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [13 x i8] c"open: %s: %s\00"
@errno = global i32 0
@SEEK_SET = global i32 0
@ENOMEM = global i32 0
@.str.1 = [11 x i8] c"malloc: %s\00"
@.str.2 = [9 x i8] c"read: %s\00"
define i8* @first_line_from_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%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 slt i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %3
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %17)
store i8* null, i8** %2
br label %72
19: ; preds = %1
store i32 0, i32* %7
br label %20
20: ; preds = %31, %19
%21 = load i32, i32* %6
%22 = call i32 @read(i32 %21, i8* %4, i32 1)
%23 = icmp eq i32 %22, 1
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = load i8, i8* %4
%26 = sext i8 %25 to i32
%27 = icmp ne i32 %26, 10
br label %28
28: ; preds = %24, %20
%29 = phi i1 [ false, %20 ], [ %27, %24 ]
br i1 %29, label %30, label %34
30: ; preds = %28
br label %31
31: ; preds = %30
%32 = load i32, i32* %7
%33 = add nsw i32 %32, 1
store i32 %33, i32* %7
br label %20
34: ; preds = %28
%35 = load i32, i32* %6
%36 = load i32, i32* @SEEK_SET
%37 = call i32 @lseek(i32 %35, i32 0, i32 %36)
%38 = load i32, i32* %7
%39 = add nsw i32 %38, 1
%40 = call i8* @malloc(i32 %39)
store i8* %40, i8** %5
%41 = load i8*, i8** %5
%42 = icmp ne i8* %41, null
br i1 %42, label %49, label %43
43: ; preds = %34
%44 = load i32, i32* @ENOMEM
store i32 %44, i32* @errno
%45 = load i32, i32* @stderr
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %47)
br label %68
49: ; preds = %34
%50 = load i32, i32* %6
%51 = load i8*, i8** %5
%52 = load i32, i32* %7
%53 = call i32 @read(i32 %50, i8* %51, i32 %52)
store i32 %53, i32* %7
%54 = load i32, i32* %7
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %63
56: ; preds = %49
%57 = load i32, i32* @stderr
%58 = load i32, i32* @errno
%59 = call i8* @strerror(i32 %58)
%60 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %59)
%61 = load i8*, i8** %5
%62 = call i32 @free(i8* %61)
store i8* null, i8** %5
br label %68
63: ; preds = %49
%64 = load i8*, i8** %5
%65 = load i32, i32* %7
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i8, i8* %64, i64 %66
store i8 0, i8* %67
br label %68
68: ; preds = %63, %56, %43
%69 = load i32, i32* %6
%70 = call i32 @close(i32 %69)
%71 = load i8*, i8** %5
store i8* %71, i8** %2
br label %72
72: ; preds = %68, %13
%73 = load i8*, i8** %2
ret i8* %73
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [13 x i8] c"open: %s: %s\00"
@errno = global i32 0
@SEEK_SET = global i32 0
@ENOMEM = global i32 0
@.str.1 = [11 x i8] c"malloc: %s\00"
@.str.2 = [9 x i8] c"read: %s\00"
define noalias i8* @first_line_from_file(i8* %0) {
%2 = alloca i8
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %11
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = load i32, i32* @errno
%9 = tail call i8* @strerror(i32 %8)
%10 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %9)
br label %44
11: ; preds = %1, %11
%12 = phi i32 [ %18, %11 ], [ 0, %1 ]
%13 = call i32 @read(i32 %4, i8* %2, i32 1)
%14 = icmp eq i32 %13, 1
%15 = load i8, i8* %2
%16 = icmp ne i8 %15, 10
%17 = select i1 %14, i1 %16, i1 false
%18 = add nuw nsw i32 %12, 1
br i1 %17, label %11, label %19
19: ; preds = %11
%20 = load i32, i32* @SEEK_SET
%21 = tail call i32 @lseek(i32 %4, i32 0, i32 %20)
%22 = tail call i8* @malloc(i32 %18)
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %29
24: ; preds = %19
%25 = load i32, i32* @ENOMEM
store i32 %25, i32* @errno
%26 = load i32, i32* @stderr
%27 = tail call i8* @strerror(i32 %25)
%28 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %27)
br label %41
29: ; preds = %19
%30 = tail call i32 @read(i32 %4, i8* %22, i32 %12)
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %38
32: ; preds = %29
%33 = load i32, i32* @stderr
%34 = load i32, i32* @errno
%35 = tail call i8* @strerror(i32 %34)
%36 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %35)
%37 = tail call i32 @free(i8* %22)
br label %41
38: ; preds = %29
%39 = zext i32 %30 to i64
%40 = getelementptr inbounds i8, i8* %22, i64 %39
store i8 0, i8* %40
br label %41
41: ; preds = %38, %32, %24
%42 = phi i8* [ null, %32 ], [ %22, %38 ], [ null, %24 ]
%43 = tail call i32 @close(i32 %4)
br label %44
44: ; preds = %41, %6
%45 = phi i8* [ null, %6 ], [ %42, %41 ]
ret i8* %45
}