#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 */
char *malloc(int);
char *realloc(char *, int);
int recv(int, char *, int, int /*<<< orphan*/);
char *read_line(int connection) {
  static int line_len = 255;
  static char *line = NULL;
  int size;
  char c;
  int pos = 0;
  if (!line) {
    line = malloc(line_len);
  }
  while ((size = recv(connection, &c, 1, 0)) > 0) {
    if ((c == '\n') && (line[pos - 1] == '\r')) {
      line[pos - 1] = '\0';
      break;
    }
    line[pos++] = c;
    if (pos > line_len) {
      line_len *= 2;
      line = realloc(line, line_len);
    }
  }
  return line;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x10(%rbp)
cmpq   $0x0,0x2efe(%rip)
jne    1162 <read_line+0x32>
mov    0x2ee2(%rip),%edi
call   1040 <malloc@plt>
mov    %rax,0x2ee6(%rip)
jmp    1167 <read_line+0x37>
mov    -0x4(%rbp),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jle    1220 <read_line+0xf0>
movsbl -0x9(%rbp),%eax
cmp    $0xa,%eax
jne    11c9 <read_line+0x99>
mov    0x2eae(%rip),%rax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
jne    11c9 <read_line+0x99>
mov    0x2e91(%rip),%rax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1220 <read_line+0xf0>
mov    -0x9(%rbp),%dl
mov    0x2e75(%rip),%rax
mov    -0x10(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x10(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
cmp    0x2e4b(%rip),%eax
jle    121b <read_line+0xeb>
mov    0x2e3f(%rip),%eax
shl    $0x1,%eax
mov    %eax,0x2e36(%rip)
mov    0x2e3f(%rip),%rdi
mov    0x2e29(%rip),%esi
call   1050 <realloc@plt>
mov    %rax,0x2e2d(%rip)
jmp    1167 <read_line+0x37>
mov    0x2e21(%rip),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
cmpq   $0x0,0x2f06(%rip)
jne    1156 <read_line+0x26>
mov    0x2eee(%rip),%edi
call   1040 <malloc@plt>
mov    %rax,0x2ef2(%rip)
lea    0xf(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    11da <read_line+0xaa>
xor    %ebx,%ebx
lea    0xf(%rsp),%r14
jmp    1199 <read_line+0x69>
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
add    $0x1,%rbx
test   %eax,%eax
jle    11da <read_line+0xaa>
movzbl 0xf(%rsp),%eax
mov    0x2ea3(%rip),%rdi
cmp    $0xa,%al
jne    11b0 <read_line+0x80>
cmpb   $0xd,-0x1(%rdi,%rbx,1)
je     11d5 <read_line+0xa5>
mov    %al,(%rdi,%rbx,1)
movslq 0x2e7e(%rip),%rsi
cmp    %rsi,%rbx
jl     1180 <read_line+0x50>
add    %esi,%esi
mov    %esi,0x2e71(%rip)
call   1050 <realloc@plt>
mov    %rax,0x2e75(%rip)
jmp    1180 <read_line+0x50>
movb   $0x0,-0x1(%rdi,%rbx,1)
mov    0x2e67(%rip),%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@read_line.line_len = internal unnamed_addr global i32 255
@read_line.line = internal unnamed_addr global i8* null
define i8* @read_line(i32 %0) {
%2 = alloca i8
%3 = load i8*, i8** @read_line.line
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = load i32, i32* @read_line.line_len
%7 = tail call i8* @malloc(i32 %6)
store i8* %7, i8** @read_line.line
br label %8
8: ; preds = %5, %1
%9 = call i32 @recv(i32 %0, i8* %2, i32 1, i32 0)
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %35
11: ; preds = %8, %32
%12 = phi i64 [ %24, %32 ], [ 0, %8 ]
%13 = load i8, i8* %2
%14 = icmp eq i8 %13, 10
%15 = load i8*, i8** @read_line.line
br i1 %14, label %16, label %23
16: ; preds = %11
%17 = add nsw i64 %12, -1
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 13
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = getelementptr inbounds i8, i8* %15, i64 %17
store i8 0, i8* %22
br label %35
23: ; preds = %11, %16
%24 = add nuw i64 %12, 1
%25 = getelementptr inbounds i8, i8* %15, i64 %12
store i8 %13, i8* %25
%26 = load i32, i32* @read_line.line_len
%27 = sext i32 %26 to i64
%28 = icmp slt i64 %12, %27
br i1 %28, label %32, label %29
29: ; preds = %23
%30 = shl nsw i32 %26, 1
store i32 %30, i32* @read_line.line_len
%31 = call i8* @realloc(i8* %15, i32 %30)
store i8* %31, i8** @read_line.line
br label %32
32: ; preds = %29, %23
%33 = call i32 @recv(i32 %0, i8* %2, i32 1, i32 0)
%34 = icmp sgt i32 %33, 0
br i1 %34, label %11, label %35
35: ; preds = %32, %8, %21
%36 = load i8*, i8** @read_line.line
ret i8* %36
}