#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 */
struct timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
char *realloc(char *, int);
int select(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
char *read_line(int fd, int multiline) {
  int size = 80;
  char errorbuf[100];
  char *s = malloc(size + 2);
  int result, errors = 0;
  int i = 0;
  result = read(fd, s + i, 1);
  while (1) {
    if (result < 0) {
      perror("read()");
      sprintf(errorbuf, "Error code: %d\n", errno);
      fprintf(stderr, "%s", errorbuf);
      errors++;
      if (errors > 5) {
        free(s);
        return NULL;
      } else {
        sleep(1);
      }
    } else if (result == 0) {
      break;
    } else if (multiline == 0 && s[i] == '\n') {
      break;
    } else {
      if (s[i] == '\n') {
        fd_set set;
        FD_ZERO(&set);
        FD_SET(fd, &set);
        struct timeval timeout;
        timeout.tv_sec = 3;
        timeout.tv_usec = 0;
        int ready = select(FD_SETSIZE, &set, NULL, NULL, &timeout);
        if (ready < 1) {
          break;
        }
      }
      i++;
      if (i == size) {
        size = size * 2;
        s = realloc(s, size + 2);
      }
    }
    result = read(fd, s + i, 1);
  }
  if (result == 0 && i == 0) {
    free(s);
    return NULL;
  }
  s[i] = '\n';
  s[i + 1] = '\0';
  return s;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
movl   $0x50,-0x14(%rbp)
mov    -0x14(%rbp),%edi
add    $0x2,%edi
call   1080 <malloc@plt>
mov    %rax,-0x88(%rbp)
movl   $0x0,-0x90(%rbp)
movl   $0x0,-0x94(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x88(%rbp),%rsi
movslq -0x94(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x8c(%rbp)
cmpl   $0x0,-0x8c(%rbp)
jge    12a1 <read_line+0xf1>
lea    0xdda(%rip),%rdi
call   10a0 <perror@plt>
lea    -0x80(%rbp),%rdi
mov    0x2d9a(%rip),%rax
mov    (%rax),%edx
lea    0xdc8(%rip),%rsi
call   10b0 <sprintf@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
lea    -0x80(%rbp),%rdx
lea    0xdbf(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x90(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x90(%rbp)
cmpl   $0x5,-0x90(%rbp)
jle    1292 <read_line+0xe2>
mov    -0x88(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1447 <read_line+0x297>
mov    $0x1,%edi
call   10d0 <sleep@plt>
jmp    13b7 <read_line+0x207>
cmpl   $0x0,-0x8c(%rbp)
jne    12b3 <read_line+0x103>
jmp    13e0 <read_line+0x230>
cmpl   $0x0,-0x10(%rbp)
jne    12dd <read_line+0x12d>
mov    -0x88(%rbp),%rax
movslq -0x94(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    12dd <read_line+0x12d>
jmp    13e0 <read_line+0x230>
mov    -0x88(%rbp),%rax
movslq -0x94(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1368 <read_line+0x1b8>
lea    -0x98(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0xc(%rbp),%edi
lea    -0x98(%rbp),%rsi
call   10c0 <FD_SET@plt>
movl   $0x3,-0xa8(%rbp)
movq   $0x0,-0xa0(%rbp)
mov    0x2ca9(%rip),%rax
mov    (%rax),%edi
lea    -0x98(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0xa8(%rbp),%r8
mov    %rcx,%rdx
call   1070 <select@plt>
mov    %eax,-0xac(%rbp)
cmpl   $0x1,-0xac(%rbp)
jge    1363 <read_line+0x1b3>
jmp    13e0 <read_line+0x230>
jmp    1368 <read_line+0x1b8>
mov    -0x94(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x94(%rbp)
mov    -0x94(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    13a8 <read_line+0x1f8>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x88(%rbp),%rdi
mov    -0x14(%rbp),%esi
add    $0x2,%esi
call   1090 <realloc@plt>
mov    %rax,-0x88(%rbp)
jmp    13ad <read_line+0x1fd>
jmp    13b2 <read_line+0x202>
jmp    13b7 <read_line+0x207>
mov    -0xc(%rbp),%edi
mov    -0x88(%rbp),%rsi
movslq -0x94(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x8c(%rbp)
jmp    1212 <read_line+0x62>
cmpl   $0x0,-0x8c(%rbp)
jne    1413 <read_line+0x263>
cmpl   $0x0,-0x94(%rbp)
jne    1413 <read_line+0x263>
mov    -0x88(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1447 <read_line+0x297>
mov    -0x88(%rbp),%rax
movslq -0x94(%rbp),%rcx
movb   $0xa,(%rax,%rcx,1)
mov    -0x88(%rbp),%rax
mov    -0x94(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x88(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %esi,0xc(%rsp)
mov    %edi,%r14d
mov    $0x52,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %r14d,%edi
mov    %rax,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
xor    %ecx,%ecx
mov    %rcx,0x10(%rsp)
mov    $0x50,%r15d
xor    %r13d,%r13d
jmp    122f <read_line+0x7f>
nopw   0x0(%rax,%rax,1)
lea    (%r15,%r15,1),%ebp
lea    (%r15,%r15,1),%esi
add    $0x2,%esi
mov    %rbx,%rdi
call   1090 <realloc@plt>
mov    %rax,%rbx
mov    %r15d,%r13d
movslq %r13d,%rsi
add    %rbx,%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %ebp,%r15d
test   %eax,%eax
js     12b9 <read_line+0x109>
movslq %r13d,%r12
je     131e <read_line+0x16e>
movzbl (%rbx,%r12,1),%eax
cmpl   $0x0,0xc(%rsp)
je     12a0 <read_line+0xf0>
cmp    $0xa,%al
jne    12a4 <read_line+0xf4>
lea    0x1c(%rsp),%rbp
mov    %rbp,%rdi
call   1060 <FD_ZERO@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
call   10c0 <FD_SET@plt>
movl   $0x3,0x20(%rsp)
movq   $0x0,0x28(%rsp)
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x20(%rsp),%r8
call   1070 <select@plt>
test   %eax,%eax
jg     12a4 <read_line+0xf4>
jmp    1323 <read_line+0x173>
nopl   0x0(%rax)
cmp    $0xa,%al
je     1323 <read_line+0x173>
add    $0x1,%r13d
cmp    %r15d,%r13d
je     1200 <read_line+0x50>
mov    %r15d,%ebp
jmp    1219 <read_line+0x69>
lea    0xd40(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d04(%rip),%rax
mov    (%rax),%edx
lea    0x30(%rsp),%rbp
mov    %rbp,%rdi
lea    0xd2a(%rip),%rsi
call   10b0 <sprintf@plt>
mov    0x2cf7(%rip),%rax
mov    (%rax),%edi
lea    0xd25(%rip),%rsi
mov    %rbp,%rdx
call   1050 <fprintf@plt>
mov    0x10(%rsp),%rax
cmp    $0x5,%eax
jge    132c <read_line+0x17c>
add    $0x1,%eax
mov    %rax,0x10(%rsp)
mov    $0x1,%edi
call   10d0 <sleep@plt>
mov    %r15d,%ebp
jmp    1219 <read_line+0x69>
test   %r13d,%r13d
je     132c <read_line+0x17c>
movw   $0xa,(%rbx,%r12,1)
jmp    1336 <read_line+0x186>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [7 x i8] c"read()\00"
@.str.1 = [16 x i8] c"Error code: %d\0A\00"
@errno = global i32 0
@stderr = global i32 0
@.str.2 = [3 x i8] c"%s\00"
@FD_SETSIZE = global i32 0
define i8* @read_line(i32 %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [100 x i8]
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.timeval
%14 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 80, i32* %6
%15 = load i32, i32* %6
%16 = add nsw i32 %15, 2
%17 = call i8* @malloc(i32 %16)
store i8* %17, i8** %8
store i32 0, i32* %10
store i32 0, i32* %11
%18 = load i32, i32* %4
%19 = load i8*, i8** %8
%20 = load i32, i32* %11
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = call i32 @read(i32 %18, i8* %22, i32 1)
store i32 %23, i32* %9
br label %24
24: ; preds = %2, %97
%25 = load i32, i32* %9
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %45
27: ; preds = %24
%28 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%30 = load i32, i32* @errno
%31 = call i32 @sprintf(i8* %29, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %30)
%32 = load i32, i32* @stderr
%33 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%34 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %33)
%35 = load i32, i32* %10
%36 = add nsw i32 %35, 1
store i32 %36, i32* %10
%37 = load i32, i32* %10
%38 = icmp sgt i32 %37, 5
br i1 %38, label %39, label %42
39: ; preds = %27
%40 = load i8*, i8** %8
%41 = call i32 @free(i8* %40)
store i8* null, i8** %3
br label %124
42: ; preds = %27
%43 = call i32 @sleep(i32 1)
br label %44
44: ; preds = %42
br label %97
45: ; preds = %24
%46 = load i32, i32* %9
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %45
br label %104
49: ; preds = %45
%50 = load i32, i32* %5
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %61
52: ; preds = %49
%53 = load i8*, i8** %8
%54 = load i32, i32* %11
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %53, i64 %55
%57 = load i8, i8* %56
%58 = sext i8 %57 to i32
%59 = icmp eq i32 %58, 10
br i1 %59, label %60, label %61
60: ; preds = %52
br label %104
61: ; preds = %52, %49
%62 = load i8*, i8** %8
%63 = load i32, i32* %11
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8, i8* %62, i64 %64
%66 = load i8, i8* %65
%67 = sext i8 %66 to i32
%68 = icmp eq i32 %67, 10
br i1 %68, label %69, label %81
69: ; preds = %61
%70 = call i32 @FD_ZERO(i32* %12)
%71 = load i32, i32* %4
%72 = call i32 @FD_SET(i32 %71, i32* %12)
%73 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
store i32 3, i32* %73
%74 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 1
store i64 0, i64* %74
%75 = load i32, i32* @FD_SETSIZE
%76 = call i32 @select(i32 %75, i32* %12, i32* null, i32* null, %struct.timeval* %13)
store i32 %76, i32* %14
%77 = load i32, i32* %14
%78 = icmp slt i32 %77, 1
br i1 %78, label %79, label %80
79: ; preds = %69
br label %104
80: ; preds = %69
br label %81
81: ; preds = %80, %61
%82 = load i32, i32* %11
%83 = add nsw i32 %82, 1
store i32 %83, i32* %11
%84 = load i32, i32* %11
%85 = load i32, i32* %6
%86 = icmp eq i32 %84, %85
br i1 %86, label %87, label %94
87: ; preds = %81
%88 = load i32, i32* %6
%89 = mul nsw i32 %88, 2
store i32 %89, i32* %6
%90 = load i8*, i8** %8
%91 = load i32, i32* %6
%92 = add nsw i32 %91, 2
%93 = call i8* @realloc(i8* %90, i32 %92)
store i8* %93, i8** %8
br label %94
94: ; preds = %87, %81
br label %95
95: ; preds = %94
br label %96
96: ; preds = %95
br label %97
97: ; preds = %96, %44
%98 = load i32, i32* %4
%99 = load i8*, i8** %8
%100 = load i32, i32* %11
%101 = sext i32 %100 to i64
%102 = getelementptr inbounds i8, i8* %99, i64 %101
%103 = call i32 @read(i32 %98, i8* %102, i32 1)
store i32 %103, i32* %9
br label %24
104: ; preds = %79, %60, %48
%105 = load i32, i32* %9
%106 = icmp eq i32 %105, 0
br i1 %106, label %107, label %113
107: ; preds = %104
%108 = load i32, i32* %11
%109 = icmp eq i32 %108, 0
br i1 %109, label %110, label %113
110: ; preds = %107
%111 = load i8*, i8** %8
%112 = call i32 @free(i8* %111)
store i8* null, i8** %3
br label %124
113: ; preds = %107, %104
%114 = load i8*, i8** %8
%115 = load i32, i32* %11
%116 = sext i32 %115 to i64
%117 = getelementptr inbounds i8, i8* %114, i64 %116
store i8 10, i8* %117
%118 = load i8*, i8** %8
%119 = load i32, i32* %11
%120 = add nsw i32 %119, 1
%121 = sext i32 %120 to i64
%122 = getelementptr inbounds i8, i8* %118, i64 %121
store i8 0, i8* %122
%123 = load i8*, i8** %8
store i8* %123, i8** %3
br label %124
124: ; preds = %113, %110, %39
%125 = load i8*, i8** %3
ret i8* %125
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [7 x i8] c"read()\00"
@.str.1 = [16 x i8] c"Error code: %d\0A\00"
@errno = global i32 0
@stderr = global i32 0
@.str.2 = [3 x i8] c"%s\00"
@FD_SETSIZE = global i32 0
define i8* @read_line(i32 %0, i32 %1) {
%3 = alloca [100 x i8]
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%7 = tail call dereferenceable_or_null(82) i8* @malloc(i32 82)
%8 = tail call i32 @read(i32 %0, i8* %7, i32 1)
%9 = icmp eq i32 %1, 0
%10 = bitcast i32* %4 to i8*
%11 = bitcast %struct.timeval* %5 to i8*
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
br label %14
14: ; preds = %55, %2
%15 = phi i32 [ 80, %2 ], [ %56, %55 ]
%16 = phi i8* [ %7, %2 ], [ %57, %55 ]
%17 = phi i32 [ %8, %2 ], [ %62, %55 ]
%18 = phi i32 [ 0, %2 ], [ %58, %55 ]
%19 = phi i32 [ 0, %2 ], [ %59, %55 ]
%20 = icmp slt i32 %17, 0
br i1 %20, label %21, label %33
21: ; preds = %14
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%23 = load i32, i32* @errno
%24 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %6)
%27 = icmp sgt i32 %18, 4
br i1 %27, label %28, label %30
28: ; preds = %21
%29 = call i32 @free(i8* %16)
br label %73
30: ; preds = %21
%31 = add nsw i32 %18, 1
%32 = call i32 @sleep(i32 1)
br label %55
33: ; preds = %14
%34 = icmp eq i32 %17, 0
br i1 %34, label %63, label %35
35: ; preds = %33
%36 = sext i32 %19 to i64
%37 = getelementptr inbounds i8, i8* %16, i64 %36
%38 = load i8, i8* %37
%39 = icmp eq i8 %38, 10
br i1 %9, label %40, label %41
40: ; preds = %35
br i1 %39, label %67, label %48
41: ; preds = %35
br i1 %39, label %42, label %48
42: ; preds = %41
%43 = call i32 @FD_ZERO(i32* %4)
%44 = call i32 @FD_SET(i32 %0, i32* %4)
store i32 3, i32* %12
store i64 0, i64* %13
%45 = load i32, i32* @FD_SETSIZE
%46 = call i32 @select(i32 %45, i32* %4, i32* null, i32* null, %struct.timeval* %5)
%47 = icmp slt i32 %46, 1
br i1 %47, label %67, label %48
48: ; preds = %40, %42, %41
%49 = add nsw i32 %19, 1
%50 = icmp eq i32 %49, %15
br i1 %50, label %51, label %55
51: ; preds = %48
%52 = shl nsw i32 %15, 1
%53 = add nsw i32 %52, 2
%54 = call i8* @realloc(i8* %16, i32 %53)
br label %55
55: ; preds = %48, %51, %30
%56 = phi i32 [ %15, %30 ], [ %52, %51 ], [ %15, %48 ]
%57 = phi i8* [ %16, %30 ], [ %54, %51 ], [ %16, %48 ]
%58 = phi i32 [ %31, %30 ], [ %18, %51 ], [ %18, %48 ]
%59 = phi i32 [ %19, %30 ], [ %15, %51 ], [ %49, %48 ]
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %57, i64 %60
%62 = call i32 @read(i32 %0, i8* %61, i32 1)
br label %14
63: ; preds = %33
%64 = icmp eq i32 %19, 0
br i1 %64, label %65, label %67
65: ; preds = %63
%66 = call i32 @free(i8* %16)
br label %73
67: ; preds = %40, %42, %63
%68 = sext i32 %19 to i64
%69 = getelementptr inbounds i8, i8* %16, i64 %68
store i8 10, i8* %69
%70 = add nsw i32 %19, 1
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8, i8* %16, i64 %71
store i8 0, i8* %72
br label %73
73: ; preds = %67, %65, %28
%74 = phi i8* [ null, %28 ], [ null, %65 ], [ %16, %67 ]
ret i8* %74
}