#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 */
char EOF;
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
char *malloc(int);
char *realloc(char *, int);
char *vnstat_read_line(FILE *stream) {
  long size = 1;
  char c, *line = malloc(sizeof(char));
  while (!feof(stream) && (c = fgetc(stream)) != '\n' && c != EOF) {
    line[size - 1] = c;
    line = realloc(line, sizeof(char) * ++size);
  }
  line[size - 1] = '\0';
  return line;
}


x86 assembly (clang, -O0):
<vnstat_read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x1,-0x10(%rbp)
mov    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <feof@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x21(%rbp)
jne    11ae <vnstat_read_line+0x6e>
mov    -0x8(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %al,-0x11(%rbp)
movsbl %al,%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x21(%rbp)
je     11ae <vnstat_read_line+0x6e>
movsbl -0x11(%rbp),%eax
mov    0x2e45(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11be <vnstat_read_line+0x7e>
jmp    11f4 <vnstat_read_line+0xb4>
mov    -0x11(%rbp),%dl
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
shl    $0x0,%rax
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
jmp    1162 <vnstat_read_line+0x22>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vnstat_read_line>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
mov    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    %r15,%rdi
call   1040 <feof@plt>
mov    $0x1,%ebx
test   %eax,%eax
jne    11b4 <vnstat_read_line+0x74>
mov    $0x1,%ebx
mov    0x2e73(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
call   1030 <fgetc@plt>
cmp    $0xa,%al
je     11b4 <vnstat_read_line+0x74>
cmp    (%r12),%al
je     11b4 <vnstat_read_line+0x74>
mov    %al,-0x1(%r14,%rbx,1)
add    $0x1,%rbx
mov    %r14,%rdi
mov    %ebx,%esi
call   1060 <realloc@plt>
mov    %rax,%r14
mov    %r15,%rdi
call   1040 <feof@plt>
test   %eax,%eax
je     1180 <vnstat_read_line+0x40>
movb   $0x0,-0x1(%rbx,%r14,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@EOF = global i8 0
define i8* @vnstat_read_line(i32* %0) {
%2 = alloca i32*
%3 = alloca i64
%4 = alloca i8
%5 = alloca i8*
store i32* %0, i32** %2
store i64 1, i64* %3
%6 = call i8* @malloc(i32 1)
store i8* %6, i8** %5
br label %7
7: ; preds = %24, %1
%8 = load i32*, i32** %2
%9 = call i32 @feof(i32* %8)
%10 = icmp ne i32 %9, 0
br i1 %10, label %22, label %11
11: ; preds = %7
%12 = load i32*, i32** %2
%13 = call signext i8 @fgetc(i32* %12)
store i8 %13, i8* %4
%14 = sext i8 %13 to i32
%15 = icmp ne i32 %14, 10
br i1 %15, label %16, label %22
16: ; preds = %11
%17 = load i8, i8* %4
%18 = sext i8 %17 to i32
%19 = load i8, i8* @EOF
%20 = sext i8 %19 to i32
%21 = icmp ne i32 %18, %20
br label %22
22: ; preds = %16, %11, %7
%23 = phi i1 [ false, %11 ], [ false, %7 ], [ %21, %16 ]
br i1 %23, label %24, label %36
24: ; preds = %22
%25 = load i8, i8* %4
%26 = load i8*, i8** %5
%27 = load i64, i64* %3
%28 = sub nsw i64 %27, 1
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 %25, i8* %29
%30 = load i8*, i8** %5
%31 = load i64, i64* %3
%32 = add nsw i64 %31, 1
store i64 %32, i64* %3
%33 = mul i64 1, %32
%34 = trunc i64 %33 to i32
%35 = call i8* @realloc(i8* %30, i32 %34)
store i8* %35, i8** %5
br label %7
36: ; preds = %22
%37 = load i8*, i8** %5
%38 = load i64, i64* %3
%39 = sub nsw i64 %38, 1
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 0, i8* %40
%41 = load i8*, i8** %5
ret i8* %41
}

LLVM IR (-O2):
@EOF = global i8 0
define i8* @vnstat_read_line(i32* nocapture %0) {
%2 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
%3 = tail call i32 @feof(i32* %0)
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %21
5: ; preds = %1, %13
%6 = phi i8* [ %18, %13 ], [ %2, %1 ]
%7 = phi i64 [ %16, %13 ], [ 1, %1 ]
%8 = tail call signext i8 @fgetc(i32* %0)
%9 = icmp eq i8 %8, 10
%10 = load i8, i8* @EOF
%11 = icmp eq i8 %8, %10
%12 = select i1 %9, i1 true, i1 %11
br i1 %12, label %21, label %13
13: ; preds = %5
%14 = add nsw i64 %7, -1
%15 = getelementptr inbounds i8, i8* %6, i64 %14
store i8 %8, i8* %15
%16 = add nuw nsw i64 %7, 1
%17 = trunc i64 %16 to i32
%18 = tail call i8* @realloc(i8* %6, i32 %17)
%19 = tail call i32 @feof(i32* %0)
%20 = icmp eq i32 %19, 0
br i1 %20, label %5, label %21
21: ; preds = %5, %13, %1
%22 = phi i64 [ 1, %1 ], [ %16, %13 ], [ %7, %5 ]
%23 = phi i8* [ %2, %1 ], [ %18, %13 ], [ %6, %5 ]
%24 = add nsw i64 %22, -1
%25 = getelementptr inbounds i8, i8* %23, i64 %24
store i8 0, i8* %25
ret i8* %23
}