#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 */
scalar_t__ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long const, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *malloc(int);
char *realloc(char *, int);
char *restrict fs_first_line(FILE *file_stream) {
  const long int origin = ftell(file_stream);
  char char_buffer[1];
  char *line_buffer = malloc(1);
  int current_position = 0;
  while (fread(char_buffer, 1, 1, file_stream) > 0 && *char_buffer != '\n') {
    line_buffer = realloc(line_buffer, ++current_position);
    line_buffer[current_position - 1] = *char_buffer;
  }
  fseek(file_stream, origin, 0);
  return line_buffer;
}


x86 assembly (clang, -O0):
<fs_first_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <ftell@plt>
mov    %rax,-0x10(%rbp)
mov    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
lea    -0x11(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x25(%rbp)
jle    11b1 <fs_first_line+0x61>
movsbl -0x11(%rbp),%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    11c1 <fs_first_line+0x71>
jmp    11ef <fs_first_line+0x9f>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
add    $0x1,%esi
mov    %esi,-0x24(%rbp)
call   1070 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x11(%rbp),%dl
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    117e <fs_first_line+0x2e>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1060 <fseek@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fs_first_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r15
call   1040 <ftell@plt>
mov    %rax,%rbp
mov    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,%r12
lea    0xf(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
test   %rax,%rax
mov    %rbp,0x10(%rsp)
jle    11ec <fs_first_line+0x9c>
mov    0xf(%rsp),%bl
cmp    $0xa,%bl
je     11ec <fs_first_line+0x9c>
xor    %r14d,%r14d
lea    0xf(%rsp),%r13
nopl   0x0(%rax,%rax,1)
mov    %r14,%rbp
add    $0x1,%r14
mov    %r12,%rdi
mov    %r14d,%esi
call   1070 <realloc@plt>
mov    %rax,%r12
mov    %bl,(%rax,%rbp,1)
mov    %r13,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
test   %rax,%rax
jle    11ec <fs_first_line+0x9c>
movzbl 0xf(%rsp),%ebx
cmp    $0xa,%bl
jne    11b0 <fs_first_line+0x60>
mov    %r15,%rdi
mov    0x10(%rsp),%rsi
xor    %edx,%edx
call   1060 <fseek@plt>
mov    %r12,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @fs_first_line(i32* nocapture %0) {
%2 = alloca [1 x i8]
%3 = tail call i64 @ftell(i32* %0)
%4 = getelementptr inbounds [1 x i8], [1 x i8]* %2, i64 0, i64 0
%5 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
%6 = call i64 @fread(i8* %4, i32 1, i32 1, i32* %0)
%7 = icmp sgt i64 %6, 0
%8 = load i8, i8* %4
%9 = icmp ne i8 %8, 10
%10 = select i1 %7, i1 %9, i1 false
br i1 %10, label %11, label %24
11: ; preds = %1, %11
%12 = phi i64 [ %15, %11 ], [ 0, %1 ]
%13 = phi i8 [ %21, %11 ], [ %8, %1 ]
%14 = phi i8* [ %17, %11 ], [ %5, %1 ]
%15 = add nuw i64 %12, 1
%16 = trunc i64 %15 to i32
%17 = tail call i8* @realloc(i8* %14, i32 %16)
%18 = getelementptr inbounds i8, i8* %17, i64 %12
store i8 %13, i8* %18
%19 = call i64 @fread(i8* %4, i32 1, i32 1, i32* %0)
%20 = icmp sgt i64 %19, 0
%21 = load i8, i8* %4
%22 = icmp ne i8 %21, 10
%23 = select i1 %20, i1 %22, i1 false
br i1 %23, label %11, label %24
24: ; preds = %11, %1
%25 = phi i8* [ %5, %1 ], [ %17, %11 ]
%26 = tail call i32 @fseek(i32* %0, i64 %3, i32 0)
ret i8* %25
}