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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ getline(char **, size_t *, int /*<<< orphan*/ *);
size_t strlen(char *);
__attribute__((unused)) char *read_line(const char *filename) {
  FILE *f = fopen(filename, "r");
  if (!f)
    return NULL;
  char *line = NULL;
  size_t len = 0;
  ssize_t r = getline(&line, &len, f);
  fclose(f);
  if (r >= 0) {
    len = strlen(line);
    if (len && line[len - 1] == '\n')
      line[len - 1] = 0;
  } else {
    free(line);
    line = NULL;
  }
  return line;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1188 <read_line+0x38>
movq   $0x0,-0x8(%rbp)
jmp    1220 <read_line+0xd0>
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1070 <getline@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x30(%rbp)
jl     1207 <read_line+0xb7>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1202 <read_line+0xb2>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1202 <read_line+0xb2>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1218 <read_line+0xc8>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0xea2(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11d1 <read_line+0x81>
mov    %rax,%rbx
movq   $0x0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    %rax,%rdx
call   1070 <getline@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x8(%rsp),%rbx
test   %r14,%r14
js     11c9 <read_line+0x79>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rax,0x10(%rsp)
test   %rax,%rax
je     11d3 <read_line+0x83>
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    11d3 <read_line+0x83>
movb   $0x0,-0x1(%rbx,%rax,1)
mov    0x8(%rsp),%rbx
jmp    11d3 <read_line+0x83>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i8* @read_line(i8* nocapture %0) {
%2 = alloca i8*
%3 = alloca i64
%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 %27, label %6
6: ; preds = %1
%7 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%8 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%9 = call i64 @getline(i8** %2, i64* %3, i32* %4)
%10 = call i32 @fclose(i32* %4)
%11 = icmp sgt i64 %9, -1
%12 = load i8*, i8** %2
br i1 %11, label %13, label %23
13: ; preds = %6
%14 = call i64 @strlen(i8* dereferenceable(1) %12)
store i64 %14, i64* %3
%15 = icmp eq i64 %14, 0
br i1 %15, label %25, label %16
16: ; preds = %13
%17 = add i64 %14, -1
%18 = getelementptr inbounds i8, i8* %12, i64 %17
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 10
br i1 %20, label %21, label %25
21: ; preds = %16
store i8 0, i8* %18
%22 = load i8*, i8** %2
br label %25
23: ; preds = %6
%24 = call i32 @free(i8* %12)
br label %25
25: ; preds = %13, %16, %21, %23
%26 = phi i8* [ %12, %13 ], [ %12, %16 ], [ %22, %21 ], [ null, %23 ]
br label %27
27: ; preds = %1, %25
%28 = phi i8* [ %26, %25 ], [ null, %1 ]
ret i8* %28
}