#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*/ buffer;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ isascii(char);
char *strdup(char *);
char *read_first_line(const char *path) {
  if (path == NULL)
    return NULL;
  FILE *fp = fopen(path, "rt");
  char *output = NULL;
  if (fp) {
    int i;
    char buffer[128];
    int len = fread(buffer, 1, sizeof(buffer), fp);
    fclose(fp);
    for (i = 0; i < len; i++) {
      if (!isascii(buffer[i])) {
        if (output) {
          free(output);
          output = NULL;
        }
        break;
      }
      if (output == NULL && buffer[i] == '\n') {
        buffer[i] = '\0';
        output = strdup(buffer);
      }
    }
  }
  return output;
}


x86 assembly (clang, -O0):
<read_first_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1187 <read_first_line+0x27>
movq   $0x0,-0x8(%rbp)
jmp    1284 <read_first_line+0x124>
mov    -0x10(%rbp),%rdi
lea    0xe6e(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     127c <read_first_line+0x11c>
lea    -0xb0(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x80,%edx
call   1040 <fread@plt>
mov    %eax,-0xb4(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xb4(%rbp),%eax
jge    1277 <read_first_line+0x117>
movslq -0x24(%rbp),%rax
movsbl -0xb0(%rbp,%rax,1),%edi
ff
call   1070 <isascii@plt>
cmp    $0x0,%eax
jne    1228 <read_first_line+0xc8>
cmpq   $0x0,-0x20(%rbp)
je     1223 <read_first_line+0xc3>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
jmp    1277 <read_first_line+0x117>
cmpq   $0x0,-0x20(%rbp)
jne    1264 <read_first_line+0x104>
movslq -0x24(%rbp),%rax
movsbl -0xb0(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    1264 <read_first_line+0x104>
movslq -0x24(%rbp),%rax
movb   $0x0,-0xb0(%rbp,%rax,1)
lea    -0xb0(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x20(%rbp)
jmp    1269 <read_first_line+0x109>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11de <read_first_line+0x7e>
jmp    127c <read_first_line+0x11c>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_first_line>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x80,%rsp
test   %rdi,%rdi
je     1205 <read_first_line+0xa5>
lea    0xe81(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1205 <read_first_line+0xa5>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x80,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1050 <fclose@plt>
test   %ebp,%ebp
jle    1205 <read_first_line+0xa5>
mov    %ebp,%r15d
xor    %ebp,%ebp
mov    %rsp,%r14
xor    %r12d,%r12d
jmp    11d9 <read_first_line+0x79>
movb   $0x0,(%rsp,%rbp,1)
mov    %r14,%rdi
call   1080 <strdup@plt>
mov    %rax,%r12
nop
add    $0x1,%rbp
cmp    %rbp,%r15
je     1208 <read_first_line+0xa8>
movzbl (%rsp,%rbp,1),%ebx
movsbl %bl,%edi
call   1070 <isascii@plt>
test   %eax,%eax
je     11f8 <read_first_line+0x98>
test   %r12,%r12
jne    11d0 <read_first_line+0x70>
cmp    $0xa,%bl
je     11c0 <read_first_line+0x60>
xor    %r12d,%r12d
jmp    11d0 <read_first_line+0x70>
test   %r12,%r12
je     1205 <read_first_line+0xa5>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x80,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rt\00"
define i8* @read_first_line(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [128 x i8]
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %12
11: ; preds = %1
store i8* null, i8** %2
br label %64
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %4
store i8* null, i8** %5
%15 = load i32*, i32** %4
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %62
17: ; preds = %12
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%19 = load i32*, i32** %4
%20 = call i32 @fread(i8* %18, i32 1, i32 128, i32* %19)
store i32 %20, i32* %8
%21 = load i32*, i32** %4
%22 = call i32 @fclose(i32* %21)
store i32 0, i32* %6
br label %23
23: ; preds = %58, %17
%24 = load i32, i32* %6
%25 = load i32, i32* %8
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %61
27: ; preds = %23
%28 = load i32, i32* %6
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 %29
%31 = load i8, i8* %30
%32 = call i32 @isascii(i8 signext %31)
%33 = icmp ne i32 %32, 0
br i1 %33, label %41, label %34
34: ; preds = %27
%35 = load i8*, i8** %5
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = load i8*, i8** %5
%39 = call i32 @free(i8* %38)
store i8* null, i8** %5
br label %40
40: ; preds = %37, %34
br label %61
41: ; preds = %27
%42 = load i8*, i8** %5
%43 = icmp eq i8* %42, null
br i1 %43, label %44, label %57
44: ; preds = %41
%45 = load i32, i32* %6
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 %46
%48 = load i8, i8* %47
%49 = sext i8 %48 to i32
%50 = icmp eq i32 %49, 10
br i1 %50, label %51, label %57
51: ; preds = %44
%52 = load i32, i32* %6
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 %53
store i8 0, i8* %54
%55 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%56 = call i8* @strdup(i8* %55)
store i8* %56, i8** %5
br label %57
57: ; preds = %51, %44, %41
br label %58
58: ; preds = %57
%59 = load i32, i32* %6
%60 = add nsw i32 %59, 1
store i32 %60, i32* %6
br label %23
61: ; preds = %40, %23
br label %62
62: ; preds = %61, %12
%63 = load i8*, i8** %5
store i8* %63, i8** %2
br label %64
64: ; preds = %62, %11
%65 = load i8*, i8** %2
ret i8* %65
}

LLVM IR (-O2):
@.str = [3 x i8] c"rt\00"
define i8* @read_first_line(i8* %0) {
%2 = alloca [128 x i8]
%3 = icmp eq i8* %0, null
br i1 %3, label %36, label %4
4: ; preds = %1
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %36, label %7
7: ; preds = %4
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%9 = call i32 @fread(i8* %8, i32 1, i32 128, i32* %5)
%10 = tail call i32 @fclose(i32* %5)
%11 = icmp sgt i32 %9, 0
br i1 %11, label %12, label %34
12: ; preds = %7
%13 = zext i32 %9 to i64
br label %14
14: ; preds = %12, %30
%15 = phi i64 [ 0, %12 ], [ %32, %30 ]
%16 = phi i8* [ null, %12 ], [ %31, %30 ]
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 %15
%18 = load i8, i8* %17
%19 = tail call i32 @isascii(i8 signext %18)
%20 = icmp eq i32 %19, 0
%21 = icmp eq i8* %16, null
br i1 %20, label %22, label %25
22: ; preds = %14
br i1 %21, label %34, label %23
23: ; preds = %22
%24 = tail call i32 @free(i8* %16)
br label %34
25: ; preds = %14
br i1 %21, label %26, label %30
26: ; preds = %25
%27 = icmp eq i8 %18, 10
br i1 %27, label %28, label %30
28: ; preds = %26
store i8 0, i8* %17
%29 = call i8* @strdup(i8* %8)
br label %30
30: ; preds = %25, %26, %28
%31 = phi i8* [ %29, %28 ], [ null, %26 ], [ %16, %25 ]
%32 = add nuw nsw i64 %15, 1
%33 = icmp eq i64 %32, %13
br i1 %33, label %34, label %14
34: ; preds = %30, %7, %22, %23
%35 = phi i8* [ null, %23 ], [ null, %22 ], [ null, %7 ], [ %31, %30 ]
br label %36
36: ; preds = %4, %34, %1
%37 = phi i8* [ null, %1 ], [ %35, %34 ], [ null, %4 ]
ret i8* %37
}