#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 int32_t;

/* Variables and functions */
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ malloc(int);
int read(int const, char *, int);
int /*<<< orphan*/ read_int(int const, int *, int *);
int /*<<< orphan*/ stderr;
size_t strnlen(char *, int);
bool read_line(const int fd, char **line, size_t *line_len,
               int32_t *line_size) {
  int32_t msg_size;
  size_t msg_len = 0;
  int32_t bytes_read;
  int32_t total_read_so_far = 0;
  char *buffer;
  if (!read_int(fd, &msg_size, &bytes_read)) {
    {
      fprintf(stderr, "Error reading line."
                      "\n");
      fflush(stderr);
    };
    *line = NULL;
    *line_len = 0;
    *line_size = 0;
    return false;
  }
  if (msg_size == 0) {
    *line = NULL;
    *line_len = 0;
    *line_size = 0;
    return true;
  }
  buffer = (char *)malloc(sizeof(char) * msg_size);
  if (buffer == NULL) {
    *line = NULL;
    *line_len = 0;
    *line_size = 0;
    return false;
  }
  while ((bytes_read = read(fd, buffer + total_read_so_far,
                            ((msg_size) < (1024) ? msg_size : 1024))) > 0 &&
         (total_read_so_far += bytes_read) < msg_size)
    ;
  msg_len = strnlen(buffer, msg_size);
  *line = buffer;
  *line_len = msg_len;
  *line_size = msg_size;
  return true;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   1080 <read_int@plt>
cmp    $0x0,%eax
jne    11ee <read_line+0x8e>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
lea    0xe51(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    132c <read_line+0x1cc>
cmpl   $0x0,-0x24(%rbp)
jne    1224 <read_line+0xc4>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    132c <read_line+0x1cc>
movslq -0x24(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    126e <read_line+0x10e>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    132c <read_line+0x1cc>
jmp    1273 <read_line+0x113>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x40(%rbp),%rax
movslq -0x38(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
cmpl   $0x400,-0x24(%rbp)
jge    12a0 <read_line+0x140>
mov    -0x24(%rbp),%eax
mov    %eax,-0x50(%rbp)
jmp    12ad <read_line+0x14d>
mov    $0x400,%eax
mov    %eax,-0x50(%rbp)
jmp    12ad <read_line+0x14d>
mov    -0x48(%rbp),%rsi
mov    -0x4c(%rbp),%edi
mov    -0x50(%rbp),%edx
call   1040 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0x34(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x51(%rbp)
jle    12e1 <read_line+0x181>
mov    -0x34(%rbp),%eax
add    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
cmp    -0x24(%rbp),%eax
setl   %al
mov    %al,-0x51(%rbp)
mov    -0x51(%rbp),%al
test   $0x1,%al
jne    12f1 <read_line+0x191>
jmp    12f6 <read_line+0x196>
jmp    1273 <read_line+0x113>
mov    -0x40(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1030 <strnlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x40(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%rbx
mov    %rdx,%r15
mov    %rsi,%r12
mov    %edi,%ebp
lea    0x8(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1080 <read_int@plt>
test   %eax,%eax
je     11eb <read_line+0x8b>
mov    0x8(%rsp),%edi
test   %edi,%edi
je     121e <read_line+0xbe>
call   1060 <malloc@plt>
test   %rax,%rax
je     1209 <read_line+0xa9>
mov    %rax,%r13
mov    %rbx,0x10(%rsp)
xor    %ebx,%ebx
mov    0x8(%rsp),%edx
mov    $0x400,%r14d
cs nopw 0x0(%rax,%rax,1)
movslq %ebx,%rsi
add    %r13,%rsi
cmp    $0x400,%edx
cmovge %r14d,%edx
mov    %ebp,%edi
call   1040 <read@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
jle    1236 <read_line+0xd6>
add    %eax,%ebx
mov    0x8(%rsp),%edx
cmp    %edx,%ebx
jl     11c0 <read_line+0x60>
jmp    123a <read_line+0xda>
mov    0x2dee(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0xe04(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x0(%rbp),%edi
call   1070 <fflush@plt>
movq   $0x0,(%r12)
movq   $0x0,(%r15)
xor    %ecx,%ecx
xor    %eax,%eax
jmp    1259 <read_line+0xf9>
movq   $0x0,(%r12)
movq   $0x0,(%r15)
mov    $0x1,%eax
xor    %ecx,%ecx
jmp    1259 <read_line+0xf9>
mov    0x8(%rsp),%edx
mov    %r13,%rdi
mov    %edx,%esi
call   1030 <strnlen@plt>
mov    %r13,(%r12)
mov    %rax,(%r15)
mov    $0x1,%eax
mov    0x8(%rsp),%ecx
mov    0x10(%rsp),%rbx
mov    %ecx,(%rbx)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [21 x i8] c"Error reading line.\0A\00"
define i32 @read_line(i32 %0, i8** nocapture writeonly %1, i64* nocapture writeonly %2, i32* nocapture writeonly %3) {
%5 = alloca i32
%6 = alloca i32
%7 = bitcast i32* %5 to i8*
%8 = bitcast i32* %6 to i8*
%9 = call i32 @read_int(i32 %0, i32* %5, i32* %6)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %4
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @stderr
%15 = call i32 @fflush(i32 %14)
store i8* null, i8** %1
store i64 0, i64* %2
br label %46
16: ; preds = %4
%17 = load i32, i32* %5
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %16
store i8* null, i8** %1
store i64 0, i64* %2
br label %46
20: ; preds = %16
%21 = call i64 @malloc(i32 %17)
%22 = inttoptr i64 %21 to i8*
%23 = icmp eq i64 %21, 0
br i1 %23, label %26, label %24
24: ; preds = %20
%25 = load i32, i32* %5
br label %27
26: ; preds = %20
store i8* null, i8** %1
store i64 0, i64* %2
br label %46
27: ; preds = %24, %38
%28 = phi i32 [ %40, %38 ], [ %25, %24 ]
%29 = phi i32 [ %39, %38 ], [ 0, %24 ]
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %22, i64 %30
%32 = icmp slt i32 %28, 1024
%33 = select i1 %32, i32 %28, i32 1024
%34 = call i32 @read(i32 %0, i8* %31, i32 %33)
store i32 %34, i32* %6
%35 = icmp sgt i32 %34, 0
br i1 %35, label %38, label %36
36: ; preds = %27
%37 = load i32, i32* %5
br label %42
38: ; preds = %27
%39 = add nsw i32 %34, %29
%40 = load i32, i32* %5
%41 = icmp slt i32 %39, %40
br i1 %41, label %27, label %42
42: ; preds = %38, %36
%43 = phi i32 [ %37, %36 ], [ %40, %38 ]
%44 = call i64 @strnlen(i8* %22, i32 %43)
store i8* %22, i8** %1
store i64 %44, i64* %2
%45 = load i32, i32* %5
br label %46
46: ; preds = %42, %26, %19, %11
%47 = phi i32 [ %45, %42 ], [ 0, %26 ], [ 0, %19 ], [ 0, %11 ]
%48 = phi i32 [ 1, %42 ], [ 0, %26 ], [ 1, %19 ], [ 0, %11 ]
store i32 %47, i32* %3
ret i32 %48
}