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

/* Variables and functions */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ free(char *);
char *realloc(char *, int);
int recv(int, char *, int, int /*<<< orphan*/);
int recv_first_line(int sockfd, char **result) {
  int buff_len = 512, llen = 0, i;
  ssize_t bs_read;
  char *line = NULL, *buff = (char *)calloc(buff_len, sizeof(char));
  if (!buff)
    goto recv_first_line_err;
  for (;;) {
    bs_read = recv(sockfd, buff, sizeof(char) * buff_len, 0);
    if (bs_read <= 0)
      goto recv_first_line_err;
    line = realloc(line, sizeof(char) * (llen + bs_read));
    if (!line)
      goto recv_first_line_err;
    for (i = 0; i < bs_read; i++) {
      if (buff[i] == '\n' || buff[i] == '\r') {
        line[llen + i] = '\0';
        goto recv_first_line_done;
      }
      line[llen + i] = buff[i];
    }
    llen += bs_read;
  }
recv_first_line_done:
  free(buff);
  *result = line;
  return 0;
recv_first_line_err:
  free(buff);
  free(line);
  return 1;
}


x86 assembly (clang, -O0):
<recv_first_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x200,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1186 <recv_first_line+0x46>
jmp    128a <recv_first_line+0x14a>
jmp    118b <recv_first_line+0x4b>
mov    -0x8(%rbp),%edi
mov    -0x30(%rbp),%rsi
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jg     11b5 <recv_first_line+0x75>
jmp    128a <recv_first_line+0x14a>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%eax
add    -0x20(%rbp),%eax
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e0 <recv_first_line+0xa0>
jmp    128a <recv_first_line+0x14a>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    125c <recv_first_line+0x11c>
mov    -0x30(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     121d <recv_first_line+0xdd>
mov    -0x30(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
jne    1233 <recv_first_line+0xf3>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    -0x1c(%rbp),%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    126a <recv_first_line+0x12a>
mov    -0x30(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    -0x1c(%rbp),%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11e7 <recv_first_line+0xa7>
mov    -0x20(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    118b <recv_first_line+0x4b>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    12a3 <recv_first_line+0x163>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_first_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %edi,%ebp
mov    $0x1,%ebx
mov    $0x200,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%r12
test   %rax,%rax
je     11ff <recv_first_line+0xbf>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
test   %eax,%eax
jle    11ff <recv_first_line+0xbf>
mov    %eax,%r14d
mov    %ebp,0xc(%rsp)
mov    %r15,0x10(%rsp)
xor    %ebp,%ebp
xor    %r15d,%r15d
lea    (%r14,%r15,1),%r13d
mov    %rbp,%rdi
mov    %r13d,%esi
call   1060 <realloc@plt>
test   %rax,%rax
je     11ff <recv_first_line+0xbf>
mov    %rax,%rbp
movslq %r15d,%rax
mov    %r14d,%ecx
add    %rbp,%rax
xor    %edx,%edx
nopl   0x0(%rax,%rax,1)
movzbl (%r12,%rdx,1),%ebx
cmp    $0xd,%bl
je     1213 <recv_first_line+0xd3>
cmp    $0xa,%bl
je     1213 <recv_first_line+0xd3>
mov    %bl,(%rax,%rdx,1)
add    $0x1,%rdx
cmp    %rdx,%rcx
jne    11c0 <recv_first_line+0x80>
mov    0xc(%rsp),%edi
mov    %r12,%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %eax,%r14d
mov    %r13d,%r15d
test   %eax,%eax
mov    $0x1,%ebx
jg     1199 <recv_first_line+0x59>
jmp    1201 <recv_first_line+0xc1>
xor    %ebp,%ebp
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
jmp    1230 <recv_first_line+0xf0>
add    %edx,%r15d
movslq %r15d,%rax
movb   $0x0,0x0(%rbp,%rax,1)
mov    %r12,%rdi
call   1030 <free@plt>
mov    0x10(%rsp),%rax
mov    %rbp,(%rax)
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @recv_first_line(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 512, i32* %6
store i32 0, i32* %7
store i8* null, i8** %10
%12 = load i32, i32* %6
%13 = call i64 @calloc(i32 %12, i32 1)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %11
%15 = load i8*, i8** %11
%16 = icmp ne i8* %15, null
br i1 %16, label %18, label %17
17: ; preds = %2
br label %94
18: ; preds = %2
br label %19
19: ; preds = %85, %18
%20 = load i32, i32* %4
%21 = load i8*, i8** %11
%22 = load i32, i32* %6
%23 = sext i32 %22 to i64
%24 = mul i64 1, %23
%25 = trunc i64 %24 to i32
%26 = call i32 @recv(i32 %20, i8* %21, i32 %25, i32 0)
store i32 %26, i32* %9
%27 = load i32, i32* %9
%28 = icmp sle i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %19
br label %94
30: ; preds = %19
%31 = load i8*, i8** %10
%32 = load i32, i32* %7
%33 = load i32, i32* %9
%34 = add nsw i32 %32, %33
%35 = sext i32 %34 to i64
%36 = mul i64 1, %35
%37 = trunc i64 %36 to i32
%38 = call i8* @realloc(i8* %31, i32 %37)
store i8* %38, i8** %10
%39 = load i8*, i8** %10
%40 = icmp ne i8* %39, null
br i1 %40, label %42, label %41
41: ; preds = %30
br label %94
42: ; preds = %30
store i32 0, i32* %8
br label %43
43: ; preds = %82, %42
%44 = load i32, i32* %8
%45 = load i32, i32* %9
%46 = icmp slt i32 %44, %45
br i1 %46, label %47, label %85
47: ; preds = %43
%48 = load i8*, i8** %11
%49 = load i32, i32* %8
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = icmp eq i32 %53, 10
br i1 %54, label %63, label %55
55: ; preds = %47
%56 = load i8*, i8** %11
%57 = load i32, i32* %8
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %56, i64 %58
%60 = load i8, i8* %59
%61 = sext i8 %60 to i32
%62 = icmp eq i32 %61, 13
br i1 %62, label %63, label %70
63: ; preds = %55, %47
%64 = load i8*, i8** %10
%65 = load i32, i32* %7
%66 = load i32, i32* %8
%67 = add nsw i32 %65, %66
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8, i8* %64, i64 %68
store i8 0, i8* %69
br label %89
70: ; preds = %55
%71 = load i8*, i8** %11
%72 = load i32, i32* %8
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i8, i8* %71, i64 %73
%75 = load i8, i8* %74
%76 = load i8*, i8** %10
%77 = load i32, i32* %7
%78 = load i32, i32* %8
%79 = add nsw i32 %77, %78
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds i8, i8* %76, i64 %80
store i8 %75, i8* %81
br label %82
82: ; preds = %70
%83 = load i32, i32* %8
%84 = add nsw i32 %83, 1
store i32 %84, i32* %8
br label %43
85: ; preds = %43
%86 = load i32, i32* %9
%87 = load i32, i32* %7
%88 = add nsw i32 %87, %86
store i32 %88, i32* %7
br label %19
89: ; preds = %63
%90 = load i8*, i8** %11
%91 = call i32 @free(i8* %90)
%92 = load i8*, i8** %10
%93 = load i8**, i8*** %5
store i8* %92, i8** %93
store i32 0, i32* %3
br label %99
94: ; preds = %41, %29, %17
%95 = load i8*, i8** %11
%96 = call i32 @free(i8* %95)
%97 = load i8*, i8** %10
%98 = call i32 @free(i8* %97)
store i32 1, i32* %3
br label %99
99: ; preds = %94, %89
%100 = load i32, i32* %3
ret i32 %100
}

LLVM IR (-O2):
define i32 @recv_first_line(i32 %0, i8** nocapture writeonly %1) {
%3 = tail call i64 @calloc(i32 512, i32 1)
%4 = inttoptr i64 %3 to i8*
%5 = icmp eq i64 %3, 0
br i1 %5, label %37, label %6
6: ; preds = %2
%7 = tail call i32 @recv(i32 %0, i8* %4, i32 512, i32 0)
%8 = icmp slt i32 %7, 1
br i1 %8, label %37, label %12
9: ; preds = %32
%10 = tail call i32 @recv(i32 %0, i8* %4, i32 512, i32 0)
%11 = icmp slt i32 %10, 1
br i1 %11, label %37, label %12
12: ; preds = %6, %9
%13 = phi i32 [ %10, %9 ], [ %7, %6 ]
%14 = phi i8* [ %17, %9 ], [ null, %6 ]
%15 = phi i32 [ %16, %9 ], [ 0, %6 ]
%16 = add nsw i32 %13, %15
%17 = tail call i8* @realloc(i8* %14, i32 %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %37, label %19
19: ; preds = %12
%20 = sext i32 %15 to i64
%21 = zext i32 %13 to i64
br label %22
22: ; preds = %19, %32
%23 = phi i64 [ 0, %19 ], [ %35, %32 ]
%24 = getelementptr inbounds i8, i8* %4, i64 %23
%25 = load i8, i8* %24
switch i8 %25, label %32 [
i8 10, label %26
i8 13, label %26
]
26: ; preds = %22, %22
%27 = trunc i64 %23 to i32
%28 = add nsw i32 %15, %27
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %17, i64 %29
store i8 0, i8* %30
%31 = tail call i32 @free(i8* %4)
store i8* %17, i8** %1
br label %41
32: ; preds = %22
%33 = add nsw i64 %23, %20
%34 = getelementptr inbounds i8, i8* %17, i64 %33
store i8 %25, i8* %34
%35 = add nuw nsw i64 %23, 1
%36 = icmp eq i64 %35, %21
br i1 %36, label %9, label %22
37: ; preds = %9, %12, %6, %2
%38 = phi i8* [ null, %2 ], [ null, %6 ], [ %17, %9 ], [ null, %12 ]
%39 = tail call i32 @free(i8* %4)
%40 = tail call i32 @free(i8* %38)
br label %41
41: ; preds = %37, %26
%42 = phi i32 [ 1, %37 ], [ 0, %26 ]
ret i32 %42
}