#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__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
char *realloc(char *, int);
int strlen(char *);
char *read_line(FILE *fin) {
  char *buffer;
  char *tmp;
  int read_chars = 0;
  int bufsize = 512;
  char *line = malloc(bufsize);
  if (!line) {
    return NULL;
  }
  buffer = line;
  while (fgets(buffer, bufsize - read_chars, fin)) {
    read_chars = strlen(line);
    if (line[read_chars - 1] == '\n') {
      line[read_chars - 1] = '\0';
      return line;
    } else {
      bufsize = 2 * bufsize;
      tmp = realloc(line, bufsize);
      if (tmp) {
        line = tmp;
        buffer = line + read_chars;
      } else {
        free(line);
        return NULL;
      }
    }
  }
  return NULL;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x200,-0x28(%rbp)
mov    -0x28(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    118e <read_line+0x3e>
movq   $0x0,-0x8(%rbp)
jmp    125f <read_line+0x10f>
mov    -0x30(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%esi
sub    -0x24(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1257 <read_line+0x107>
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11f7 <read_line+0xa7>
mov    -0x30(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    125f <read_line+0x10f>
mov    -0x28(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1070 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1237 <read_line+0xe7>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    124d <read_line+0xfd>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    125f <read_line+0x10f>
jmp    1252 <read_line+0x102>
jmp    1196 <read_line+0x46>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
mov    $0x200,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11e6 <read_line+0x96>
mov    %rax,%r15
mov    $0x200,%r12d
mov    %rax,%rdi
mov    $0x200,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11e6 <read_line+0x96>
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1040 <strlen@plt>
movslq %eax,%rbp
cmpb   $0xa,-0x1(%rbp,%r15,1)
je     11d6 <read_line+0x86>
add    %r12d,%r12d
mov    %r15,%rdi
mov    %r12d,%esi
call   1070 <realloc@plt>
test   %rax,%rax
je     11de <read_line+0x8e>
mov    %rax,%rbx
mov    %rax,%rdi
add    %rbp,%rdi
mov    %r12d,%esi
sub    %ebp,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
mov    %rbx,%r15
test   %rax,%rax
jne    1190 <read_line+0x40>
jmp    11e6 <read_line+0x96>
movb   $0x0,-0x1(%r15,%rbp,1)
jmp    11e9 <read_line+0x99>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @read_line(i32* nocapture %0) {
%2 = tail call dereferenceable_or_null(512) i8* @malloc(i32 512)
%3 = icmp eq i8* %2, null
br i1 %3, label %30, label %4
4: ; preds = %1
%5 = tail call i64 @fgets(i8* %2, i32 512, i32* %0)
%6 = icmp eq i64 %5, 0
br i1 %6, label %30, label %7
7: ; preds = %4, %22
%8 = phi i8* [ %20, %22 ], [ %2, %4 ]
%9 = phi i32 [ %19, %22 ], [ 512, %4 ]
%10 = tail call i32 @strlen(i8* %8)
%11 = add nsw i32 %10, -1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %8, i64 %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 10
br i1 %15, label %16, label %18
16: ; preds = %7
%17 = getelementptr inbounds i8, i8* %8, i64 %12
store i8 0, i8* %17
br label %30
18: ; preds = %7
%19 = shl nsw i32 %9, 1
%20 = tail call i8* @realloc(i8* %8, i32 %19)
%21 = icmp eq i8* %20, null
br i1 %21, label %28, label %22
22: ; preds = %18
%23 = sext i32 %10 to i64
%24 = getelementptr inbounds i8, i8* %20, i64 %23
%25 = sub nsw i32 %19, %10
%26 = tail call i64 @fgets(i8* %24, i32 %25, i32* %0)
%27 = icmp eq i64 %26, 0
br i1 %27, label %30, label %7
28: ; preds = %18
%29 = tail call i32 @free(i8* %8)
br label %30
30: ; preds = %22, %4, %1, %28, %16
%31 = phi i8* [ %8, %16 ], [ null, %28 ], [ null, %1 ], [ null, %4 ], [ null, %22 ]
ret i8* %31
}