#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 BUFSIZ;
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
unsigned int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, unsigned int);
char *xrealloc(char *, unsigned int);
char *xstrdup(char *);
char *file_read_line_alloc(FILE *fp) {
  char buf[BUFSIZ];
  unsigned int buf_len;
  char *line = NULL;
  unsigned int line_size = 0;
  int got_nl = 0;
  buf[0] = '\0';
  while (fgets(buf, BUFSIZ, fp)) {
    buf_len = strlen(buf);
    if (buf[buf_len - 1] == '\n') {
      buf_len--;
      buf[buf_len] = '\0';
      got_nl = 1;
    }
    if (line) {
      line_size += buf_len;
      line = xrealloc(line, line_size + 1);
      strncat(line, buf, line_size);
    } else {
      line_size = buf_len + 1;
      line = xstrdup(buf);
    }
    if (got_nl)
      break;
  }
  return line;
}


x86 assembly (clang, -O0):
<file_read_line_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,%ecx
mov    %rsp,%rax
mov    %rax,-0x10(%rbp)
mov    %rcx,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rax
sub    %rdx,%rax
mov    %rax,-0x38(%rbp)
mov    %rax,%rsp
mov    %rcx,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movb   $0x0,(%rax)
mov    -0x38(%rbp),%rdi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%rdx
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     126b <file_read_line_alloc+0x11b>
mov    -0x38(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,-0x1c(%rbp)
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
mov    %ecx,%ecx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1205 <file_read_line_alloc+0xb5>
mov    -0x38(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x1c(%rbp)
mov    -0x1c(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
movl   $0x1,-0x30(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1241 <file_read_line_alloc+0xf1>
mov    -0x1c(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
add    $0x1,%esi
call   1040 <xrealloc@plt>
mov    -0x38(%rbp),%rsi
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%edx
call   1050 <strncat@plt>
jmp    1257 <file_read_line_alloc+0x107>
mov    -0x38(%rbp),%rdi
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
call   1060 <xstrdup@plt>
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x30(%rbp)
je     1266 <file_read_line_alloc+0x116>
jmp    126b <file_read_line_alloc+0x11b>
jmp    11a3 <file_read_line_alloc+0x53>
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_read_line_alloc>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rdx
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rax
mov    %rsi,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rax,%r12
sub    %rcx,%r12
mov    %r12,%rsp
neg    %rcx
movb   $0x0,(%rax,%rcx,1)
mov    %r12,%rdi
mov    %rdx,-0x30(%rbp)
call   1070 <fgets@plt>
xor    %r13d,%r13d
xor    %r15d,%r15d
test   %rax,%rax
je     1245 <file_read_line_alloc+0xf5>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebx
add    $0xffffffff,%eax
movzbl (%r12,%rax,1),%r14d
cmp    $0xa,%r14b
jne    11cf <file_read_line_alloc+0x7f>
movb   $0x0,(%r12,%rax,1)
mov    %eax,%ebx
test   %r15,%r15
je     1210 <file_read_line_alloc+0xc0>
mov    %r13,%rax
add    %ebx,%r13d
lea    (%rbx,%rax,1),%esi
add    $0x1,%esi
mov    %r15,%rdi
call   1040 <xrealloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r13d,%edx
call   1050 <strncat@plt>
cmp    $0xa,%r14b
jne    1227 <file_read_line_alloc+0xd7>
jmp    1245 <file_read_line_alloc+0xf5>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebx
mov    %r12,%rdi
call   1060 <xstrdup@plt>
mov    %rax,%r15
mov    %ebx,%r13d
cmp    $0xa,%r14b
je     1245 <file_read_line_alloc+0xf5>
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
mov    -0x30(%rbp),%rdx
call   1070 <fgets@plt>
test   %rax,%rax
jne    11b0 <file_read_line_alloc+0x60>
mov    %r15,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BUFSIZ = global i32 0
define i8* @file_read_line_alloc(i32* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %2
%9 = load i32, i32* @BUFSIZ
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %3
%12 = alloca i8, i64 %10
store i64 %10, i64* %4
store i8* null, i8** %6
store i32 0, i32* %7
store i32 0, i32* %8
%13 = getelementptr inbounds i8, i8* %12, i64 0
store i8 0, i8* %13
br label %14
14: ; preds = %56, %1
%15 = load i32, i32* @BUFSIZ
%16 = load i32*, i32** %2
%17 = call i64 @fgets(i8* %12, i32 %15, i32* %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %57
19: ; preds = %14
%20 = call i32 @strlen(i8* %12)
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = sub i32 %21, 1
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %12, i64 %23
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 10
br i1 %27, label %28, label %34
28: ; preds = %19
%29 = load i32, i32* %5
%30 = add i32 %29, -1
store i32 %30, i32* %5
%31 = load i32, i32* %5
%32 = zext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %12, i64 %32
store i8 0, i8* %33
store i32 1, i32* %8
br label %34
34: ; preds = %28, %19
%35 = load i8*, i8** %6
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %48
37: ; preds = %34
%38 = load i32, i32* %5
%39 = load i32, i32* %7
%40 = add i32 %39, %38
store i32 %40, i32* %7
%41 = load i8*, i8** %6
%42 = load i32, i32* %7
%43 = add i32 %42, 1
%44 = call i8* @xrealloc(i8* %41, i32 %43)
store i8* %44, i8** %6
%45 = load i8*, i8** %6
%46 = load i32, i32* %7
%47 = call i32 @strncat(i8* %45, i8* %12, i32 %46)
br label %52
48: ; preds = %34
%49 = load i32, i32* %5
%50 = add i32 %49, 1
store i32 %50, i32* %7
%51 = call i8* @xstrdup(i8* %12)
store i8* %51, i8** %6
br label %52
52: ; preds = %48, %37
%53 = load i32, i32* %8
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %52
br label %57
56: ; preds = %52
br label %14
57: ; preds = %55, %14
%58 = load i8*, i8** %6
%59 = load i8*, i8** %3
ret i8* %58
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
define i8* @file_read_line_alloc(i32* nocapture %0) {
%2 = load i32, i32* @BUFSIZ
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
store i8 0, i8* %4
%5 = call i64 @fgets(i8* %4, i32 %2, i32* %0)
%6 = icmp eq i64 %5, 0
br i1 %6, label %35, label %7
7: ; preds = %1, %31
%8 = phi i32 [ %30, %31 ], [ 0, %1 ]
%9 = phi i8* [ %29, %31 ], [ null, %1 ]
%10 = call i32 @strlen(i8* %4)
%11 = add i32 %10, -1
%12 = zext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %4, i64 %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 10
br i1 %15, label %16, label %17
16: ; preds = %7
store i8 0, i8* %13
br label %17
17: ; preds = %16, %7
%18 = phi i32 [ %11, %16 ], [ %10, %7 ]
%19 = icmp eq i8* %9, null
br i1 %19, label %25, label %20
20: ; preds = %17
%21 = add i32 %18, %8
%22 = add i32 %21, 1
%23 = call i8* @xrealloc(i8* %9, i32 %22)
%24 = call i32 @strncat(i8* %23, i8* %4, i32 %21)
br label %28
25: ; preds = %17
%26 = add i32 %18, 1
%27 = call i8* @xstrdup(i8* %4)
br label %28
28: ; preds = %25, %20
%29 = phi i8* [ %23, %20 ], [ %27, %25 ]
%30 = phi i32 [ %21, %20 ], [ %26, %25 ]
br i1 %15, label %35, label %31
31: ; preds = %28
%32 = load i32, i32* @BUFSIZ
%33 = call i64 @fgets(i8* %4, i32 %32, i32* %0)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %7
35: ; preds = %31, %28, %1
%36 = phi i8* [ null, %1 ], [ %29, %28 ], [ %29, %31 ]
ret i8* %36
}