#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 */

/* Variables and functions */
int /*<<< orphan*/ free(char *);
int read(int, char *, int);
char *realloc(char *, int);
int /*<<< orphan*/ ui_fatal(char *);
int read_line(int fd, char **line) {
  char *ptr;
  char *alc_ptr = NULL;
  int len = 1;
  int ret;
  *line = NULL;
  do {
    alc_ptr = realloc(*line, len + 1);
    if (alc_ptr == NULL) {
      if (*line != NULL) {
        free(*line);
      }
      ui_fatal("Reallocation of input string failed!");
    }
    *line = alc_ptr;
    ptr = *line + len - 1;
    len++;
  } while ((ret = read(fd, ptr, 1)) > 0 && *ptr != '\n');
  if (ret <= 0) {
    free(*line);
    return ret;
  }
  *(ptr + 1) = '\0';
  return len - 1;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x24(%rbp),%esi
add    $0x1,%esi
call   1050 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b0 <read_line+0x70>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     11a4 <read_line+0x64>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
lea    0xe55(%rip),%rdi
call   1060 <ui_fatal@plt>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x24(%rbp),%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0x28(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x29(%rbp)
jle    120e <read_line+0xce>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1169 <read_line+0x29>
cmpl   $0x0,-0x28(%rbp)
jg     123a <read_line+0xfa>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    124b <read_line+0x10b>
mov    -0x18(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %edi,%r15d
movq   $0x0,(%rsi)
xor    %r14d,%r14d
xor    %edi,%edi
nopl   (%rax)
lea    0x2(%r14),%esi
call   1050 <realloc@plt>
mov    %rax,%r13
test   %rax,%rax
jne    118b <read_line+0x4b>
mov    (%r12),%rdi
test   %rdi,%rdi
je     117f <read_line+0x3f>
call   1030 <free@plt>
lea    0xe7a(%rip),%rdi
call   1060 <ui_fatal@plt>
mov    %r13,(%r12)
mov    %r13,%rbp
add    %r14,%rbp
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    11b9 <read_line+0x79>
cmpb   $0xa,0x0(%rbp)
je     11c6 <read_line+0x86>
mov    (%r12),%rdi
add    $0x1,%r14
jmp    1160 <read_line+0x20>
mov    %eax,%ebx
mov    (%r12),%rdi
call   1030 <free@plt>
jmp    11d0 <read_line+0x90>
lea    0x1(%r14),%ebx
movb   $0x0,0x1(%r13,%r14,1)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [37 x i8] c"Reallocation of input string failed!\00"
define i32 @read_line(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %7
store i32 1, i32* %8
%10 = load i8**, i8*** %5
store i8* null, i8** %10
br label %11
11: ; preds = %50, %2
%12 = load i8**, i8*** %5
%13 = load i8*, i8** %12
%14 = load i32, i32* %8
%15 = add nsw i32 %14, 1
%16 = call i8* @realloc(i8* %13, i32 %15)
store i8* %16, i8** %7
%17 = load i8*, i8** %7
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %29
19: ; preds = %11
%20 = load i8**, i8*** %5
%21 = load i8*, i8** %20
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = load i8**, i8*** %5
%25 = load i8*, i8** %24
%26 = call i32 @free(i8* %25)
br label %27
27: ; preds = %23, %19
%28 = call i32 @ui_fatal(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %27, %11
%30 = load i8*, i8** %7
%31 = load i8**, i8*** %5
store i8* %30, i8** %31
%32 = load i8**, i8*** %5
%33 = load i8*, i8** %32
%34 = load i32, i32* %8
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = getelementptr inbounds i8, i8* %36, i64 -1
store i8* %37, i8** %6
%38 = load i32, i32* %8
%39 = add nsw i32 %38, 1
store i32 %39, i32* %8
br label %40
40: ; preds = %29
%41 = load i32, i32* %4
%42 = load i8*, i8** %6
%43 = call i32 @read(i32 %41, i8* %42, i32 1)
store i32 %43, i32* %9
%44 = icmp sgt i32 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %40
%46 = load i8*, i8** %6
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp ne i32 %48, 10
br label %50
50: ; preds = %45, %40
%51 = phi i1 [ false, %40 ], [ %49, %45 ]
br i1 %51, label %11, label %52
52: ; preds = %50
%53 = load i32, i32* %9
%54 = icmp sle i32 %53, 0
br i1 %54, label %55, label %60
55: ; preds = %52
%56 = load i8**, i8*** %5
%57 = load i8*, i8** %56
%58 = call i32 @free(i8* %57)
%59 = load i32, i32* %9
store i32 %59, i32* %3
br label %65
60: ; preds = %52
%61 = load i8*, i8** %6
%62 = getelementptr inbounds i8, i8* %61, i64 1
store i8 0, i8* %62
%63 = load i32, i32* %8
%64 = sub nsw i32 %63, 1
store i32 %64, i32* %3
br label %65
65: ; preds = %60, %55
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
@.str = [37 x i8] c"Reallocation of input string failed!\00"
define i32 @read_line(i32 %0, i8** nocapture %1) {
store i8* null, i8** %1
br label %3
3: ; preds = %25, %2
%4 = phi i8* [ %26, %25 ], [ null, %2 ]
%5 = phi i64 [ %6, %25 ], [ 1, %2 ]
%6 = add nuw i64 %5, 1
%7 = trunc i64 %6 to i32
%8 = tail call i8* @realloc(i8* %4, i32 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %17
10: ; preds = %3
%11 = load i8*, i8** %1
%12 = icmp eq i8* %11, null
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = tail call i32 @free(i8* %11)
br label %15
15: ; preds = %13, %10
%16 = tail call i32 @ui_fatal(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %3
store i8* %8, i8** %1
%18 = getelementptr inbounds i8, i8* %8, i64 -1
%19 = getelementptr inbounds i8, i8* %18, i64 %5
%20 = tail call i32 @read(i32 %0, i8* %19, i32 1)
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %17
%23 = load i8, i8* %19
%24 = icmp eq i8 %23, 10
br i1 %24, label %30, label %25
25: ; preds = %22
%26 = load i8*, i8** %1
br label %3
27: ; preds = %17
%28 = load i8*, i8** %1
%29 = tail call i32 @free(i8* %28)
br label %33
30: ; preds = %22
%31 = trunc i64 %5 to i32
%32 = getelementptr inbounds i8, i8* %19, i64 1
store i8 0, i8* %32
br label %33
33: ; preds = %30, %27
%34 = phi i32 [ %20, %27 ], [ %31, %30 ]
ret i32 %34
}