#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 */
scalar_t__ EINTR;
scalar_t__ EPIPE;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int read(int, char *, int);
char *realloc(char *, int);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
char *read_line(int fd) {
  static char *buf;
  int buflen = 64;
  int i = 0;
  int r;
  int searching = 1;
  while (searching) {
    buf = realloc(buf, buflen);
    if (!buf) {
      fprintf(stderr, "ERR: malloc(%d): %s\n", buflen, strerror(errno));
      return NULL;
    }
    memset(buf + i, 0, buflen - i);
    while (i < buflen) {
      r = read(fd, buf + i, 1);
      if (r < 0 && errno != EINTR) {
        fprintf(stderr, "ERR: read(): %s\n", strerror(errno));
        return NULL;
      } else if (r == 0) {
        errno = EPIPE;
        return NULL;
      } else if (r == 1) {
        if (buf[i] == '\n') {
          searching = 0;
          buf[i] = '\0';
          break;
        }
        i++;
      }
    }
    if (buflen >= 1024) {
      break;
    }
    buflen *= 2;
  }
  return buf;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
movl   $0x40,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x1,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1328 <read_line+0x1d8>
mov    0x2ecf(%rip),%rdi
mov    -0x10(%rbp),%esi
call   1060 <realloc@plt>
mov    %rax,0x2ec0(%rip)
cmpq   $0x0,0x2eb8(%rip)
jne    11e3 <read_line+0x93>
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%edx
mov    %rax,%rcx
lea    0xe31(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1333 <read_line+0x1e3>
mov    0x2e66(%rip),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%edx
sub    -0x14(%rbp),%edx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x14(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1308 <read_line+0x1b8>
mov    -0xc(%rbp),%edi
mov    0x2e3c(%rip),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1288 <read_line+0x138>
mov    0x2d97(%rip),%rax
mov    (%rax),%rax
mov    0x2d85(%rip),%rcx
cmp    (%rcx),%rax
je     1288 <read_line+0x138>
mov    0x2d8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d71(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xda1(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1333 <read_line+0x1e3>
cmpl   $0x0,-0x18(%rbp)
jne    12b3 <read_line+0x163>
mov    0x2d3f(%rip),%rax
mov    (%rax),%rcx
mov    0x2d2d(%rip),%rax
mov    %rcx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1333 <read_line+0x1e3>
cmpl   $0x1,-0x18(%rbp)
jne    12f9 <read_line+0x1a9>
mov    0x2d8c(%rip),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    12f0 <read_line+0x1a0>
movl   $0x0,-0x1c(%rbp)
mov    0x2d6d(%rip),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1308 <read_line+0x1b8>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    12fe <read_line+0x1ae>
jmp    1303 <read_line+0x1b3>
jmp    11fe <read_line+0xae>
cmpl   $0x400,-0x10(%rbp)
jl     131a <read_line+0x1ca>
jmp    1328 <read_line+0x1d8>
mov    -0x10(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1170 <read_line+0x20>
mov    0x2d21(%rip),%rax
mov    %rax,-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   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r14d
mov    $0x40,%r13d
xor    %ebp,%ebp
mov    0x2e63(%rip),%r15
mov    0x2e54(%rip),%r12
mov    0x2ed5(%rip),%rdi
mov    %r13d,%esi
call   1060 <realloc@plt>
mov    %rax,0x2ec6(%rip)
test   %rax,%rax
je     124f <read_line+0xff>
movslq %ebp,%rbx
add    %rbx,%rax
mov    %r13d,%edx
sub    %ebx,%edx
mov    %rax,%rdi
xor    %esi,%esi
call   1030 <memset@plt>
cmp    %ebx,%r13d
jg     11c9 <read_line+0x79>
lea    0x0(%r13,%r13,1),%eax
cmp    $0x400,%r13d
mov    %eax,%r13d
jb     1174 <read_line+0x24>
jmp    1221 <read_line+0xd1>
test   %eax,%eax
je     120c <read_line+0xbc>
cmp    %r13d,%ebp
jge    11ad <read_line+0x5d>
movslq %ebp,%rbx
mov    0x2e7d(%rip),%rsi
add    %rbx,%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
jns    11f0 <read_line+0xa0>
mov    (%r15),%rdi
cmp    (%r12),%rdi
jne    122a <read_line+0xda>
cmp    $0x1,%eax
jne    11c0 <read_line+0x70>
mov    0x2e54(%rip),%rax
cmpb   $0xa,(%rax,%rbx,1)
je     121d <read_line+0xcd>
add    $0x1,%ebp
cmp    %r13d,%ebp
jl     11c9 <read_line+0x79>
jmp    11ad <read_line+0x5d>
mov    0x2dc5(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r15)
xor    %ebp,%ebp
jmp    127a <read_line+0x12a>
movb   $0x0,(%rax,%rbx,1)
mov    0x2e28(%rip),%rbp
jmp    127a <read_line+0x12a>
mov    0x2daf(%rip),%rax
mov    (%rax),%ebx
call   1070 <strerror@plt>
lea    0xdd6(%rip),%rsi
xor    %ebp,%ebp
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    127a <read_line+0x12a>
mov    0x2d8a(%rip),%rax
mov    (%rax),%r14d
mov    (%r15),%rdi
call   1070 <strerror@plt>
lea    0xd98(%rip),%rsi
xor    %ebp,%ebp
mov    %r14d,%edi
mov    %r13d,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@read_line.buf = internal global i8* null
@stderr = global i32 0
@.str = [21 x i8] c"ERR: malloc(%d): %s\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [17 x i8] c"ERR: read(): %s\0A\00"
@EPIPE = global i64 0
define i8* @read_line(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i32 64, i32* %4
store i32 0, i32* %5
store i32 1, i32* %7
br label %8
8: ; preds = %85, %1
%9 = load i32, i32* %7
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %88
11: ; preds = %8
%12 = load i8*, i8** @read_line.buf
%13 = load i32, i32* %4
%14 = call i8* @realloc(i8* %12, i32 %13)
store i8* %14, i8** @read_line.buf
%15 = load i8*, i8** @read_line.buf
%16 = icmp ne i8* %15, null
br i1 %16, label %23, label %17
17: ; preds = %11
%18 = load i32, i32* @stderr
%19 = load i32, i32* %4
%20 = load i64, i64* @errno
%21 = call i8* @strerror(i64 %20)
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %19, i8* %21)
store i8* null, i8** %2
br label %90
23: ; preds = %11
%24 = load i8*, i8** @read_line.buf
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i32, i32* %4
%29 = load i32, i32* %5
%30 = sub nsw i32 %28, %29
%31 = call i32 @memset(i8* %27, i32 0, i32 %30)
br label %32
32: ; preds = %80, %23
%33 = load i32, i32* %5
%34 = load i32, i32* %4
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %81
36: ; preds = %32
%37 = load i32, i32* %3
%38 = load i8*, i8** @read_line.buf
%39 = load i32, i32* %5
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
%42 = call i32 @read(i32 %37, i8* %41, i32 1)
store i32 %42, i32* %6
%43 = load i32, i32* %6
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %54
45: ; preds = %36
%46 = load i64, i64* @errno
%47 = load i64, i64* @EINTR
%48 = icmp ne i64 %46, %47
br i1 %48, label %49, label %54
49: ; preds = %45
%50 = load i32, i32* @stderr
%51 = load i64, i64* @errno
%52 = call i8* @strerror(i64 %51)
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %52)
store i8* null, i8** %2
br label %90
54: ; preds = %45, %36
%55 = load i32, i32* %6
%56 = icmp eq i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %54
%58 = load i64, i64* @EPIPE
store i64 %58, i64* @errno
store i8* null, i8** %2
br label %90
59: ; preds = %54
%60 = load i32, i32* %6
%61 = icmp eq i32 %60, 1
br i1 %61, label %62, label %78
62: ; preds = %59
%63 = load i8*, i8** @read_line.buf
%64 = load i32, i32* %5
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %63, i64 %65
%67 = load i8, i8* %66
%68 = sext i8 %67 to i32
%69 = icmp eq i32 %68, 10
br i1 %69, label %70, label %75
70: ; preds = %62
store i32 0, i32* %7
%71 = load i8*, i8** @read_line.buf
%72 = load i32, i32* %5
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i8, i8* %71, i64 %73
store i8 0, i8* %74
br label %81
75: ; preds = %62
%76 = load i32, i32* %5
%77 = add nsw i32 %76, 1
store i32 %77, i32* %5
br label %78
78: ; preds = %75, %59
br label %79
79: ; preds = %78
br label %80
80: ; preds = %79
br label %32
81: ; preds = %70, %32
%82 = load i32, i32* %4
%83 = icmp sge i32 %82, 1024
br i1 %83, label %84, label %85
84: ; preds = %81
br label %88
85: ; preds = %81
%86 = load i32, i32* %4
%87 = mul nsw i32 %86, 2
store i32 %87, i32* %4
br label %8
88: ; preds = %84, %8
%89 = load i8*, i8** @read_line.buf
store i8* %89, i8** %2
br label %90
90: ; preds = %88, %57, %49, %17
%91 = load i8*, i8** %2
ret i8* %91
}

LLVM IR (-O2):
@read_line.buf = internal unnamed_addr global i8* null
@stderr = global i32 0
@.str = [21 x i8] c"ERR: malloc(%d): %s\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [17 x i8] c"ERR: read(): %s\0A\00"
@EPIPE = global i64 0
define i8* @read_line(i32 %0) {
br label %2
2: ; preds = %49, %1
%3 = phi i32 [ 0, %1 ], [ %50, %49 ]
%4 = phi i32 [ 64, %1 ], [ %52, %49 ]
%5 = load i8*, i8** @read_line.buf
%6 = tail call i8* @realloc(i8* %5, i32 %4)
store i8* %6, i8** @read_line.buf
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i64, i64* @errno
%11 = tail call i8* @strerror(i64 %10)
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %4, i8* %11)
br label %55
13: ; preds = %2
%14 = sext i32 %3 to i64
%15 = getelementptr inbounds i8, i8* %6, i64 %14
%16 = sub nsw i32 %4, %3
%17 = tail call i32 @memset(i8* %15, i32 0, i32 %16)
%18 = icmp sgt i32 %4, %3
br i1 %18, label %19, label %49
19: ; preds = %13, %46
%20 = phi i32 [ %47, %46 ], [ %3, %13 ]
%21 = load i8*, i8** @read_line.buf
%22 = sext i32 %20 to i64
%23 = getelementptr inbounds i8, i8* %21, i64 %22
%24 = tail call i32 @read(i32 %0, i8* %23, i32 1)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %34
26: ; preds = %19
%27 = load i64, i64* @errno
%28 = load i64, i64* @EINTR
%29 = icmp eq i64 %27, %28
br i1 %29, label %34, label %30
30: ; preds = %26
%31 = load i32, i32* @stderr
%32 = tail call i8* @strerror(i64 %27)
%33 = tail call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %32)
br label %55
34: ; preds = %26, %19
switch i32 %24, label %46 [
i32 0, label %35
i32 1, label %37
]
35: ; preds = %34
%36 = load i64, i64* @EPIPE
store i64 %36, i64* @errno
br label %55
37: ; preds = %34
%38 = load i8*, i8** @read_line.buf
%39 = getelementptr inbounds i8, i8* %38, i64 %22
%40 = load i8, i8* %39
%41 = icmp eq i8 %40, 10
br i1 %41, label %42, label %44
42: ; preds = %37
%43 = getelementptr inbounds i8, i8* %38, i64 %22
store i8 0, i8* %43
br label %53
44: ; preds = %37
%45 = add nsw i32 %20, 1
br label %46
46: ; preds = %34, %44
%47 = phi i32 [ %45, %44 ], [ %20, %34 ]
%48 = icmp slt i32 %47, %4
br i1 %48, label %19, label %49
49: ; preds = %46, %13
%50 = phi i32 [ %3, %13 ], [ %47, %46 ]
%51 = icmp ugt i32 %4, 1023
%52 = shl nuw nsw i32 %4, 1
br i1 %51, label %53, label %2
53: ; preds = %49, %42
%54 = load i8*, i8** @read_line.buf
br label %55
55: ; preds = %53, %35, %30, %8
%56 = phi i8* [ null, %30 ], [ null, %35 ], [ %54, %53 ], [ null, %8 ]
ret i8* %56
}