#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 */
int EOF;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(size_t);
scalar_t__ realloc(char *, size_t);
scalar_t__ recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int proxy_recv_line(int sock, char **resultp) {
  int c;
  char *result;
  size_t input_index = 0;
  size_t result_size = 80;
  result = (char *)malloc(result_size);
  while (1) {
    char ch;
    if (recv(sock, &ch, 1, 0) < 0)
      fprintf(stderr, "recv() error from  proxy server\n");
    c = ch;
    if (c == EOF) {
      free(result);
      fprintf(stderr, "end of file from  server\n");
    }
    if (c == '\012')
      break;
    result[input_index++] = c;
    while (input_index + 1 >= result_size) {
      result_size *= 2;
      result = (char *)realloc(result, result_size);
    }
  }
  if (resultp)
    *resultp = result;
  result[input_index] = '\0';
  if (resultp == NULL)
    free(result);
  return input_index;
}


x86 assembly (clang, -O0):
<proxy_recv_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x50,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x31(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
cmp    $0x0,%rax
jge    11ae <proxy_recv_line+0x5e>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
lea    0xe57(%rip),%rsi
call   1050 <fprintf@plt>
movsbl -0x31(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e29(%rip),%rcx
cmp    (%rcx),%eax
jne    11e5 <proxy_recv_line+0x95>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
call   1050 <fprintf@plt>
cmpl   $0xa,-0x14(%rbp)
jne    11f4 <proxy_recv_line+0xa4>
jmp    1248 <proxy_recv_line+0xf8>
mov    -0x14(%rbp),%eax
mov    %al,%dl
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x28(%rbp)
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
cmp    -0x30(%rbp),%rax
jb     1243 <proxy_recv_line+0xf3>
mov    -0x30(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1070 <realloc@plt>
mov    %rax,-0x20(%rbp)
jmp    120f <proxy_recv_line+0xbf>
jmp    117c <proxy_recv_line+0x2c>
cmpq   $0x0,-0x10(%rbp)
je     125e <proxy_recv_line+0x10e>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpq   $0x0,-0x10(%rbp)
jne    127e <proxy_recv_line+0x12e>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<proxy_recv_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,0x10(%rsp)
mov    %edi,%r15d
mov    $0x50,%ebx
mov    $0x50,%edi
call   1060 <malloc@plt>
mov    %rax,%rbp
xor    %r12d,%r12d
mov    0x2e66(%rip),%r14
jmp    1194 <proxy_recv_line+0x44>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%r12
mov    %r15d,%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
test   %rax,%rax
jns    11c2 <proxy_recv_line+0x72>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    0xe43(%rip),%rsi
call   1050 <fprintf@plt>
movsbl 0xf(%rsp),%r13d
cmp    %r13d,(%r14)
jne    11ea <proxy_recv_line+0x9a>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
call   1050 <fprintf@plt>
cmp    $0xa,%r13b
je     121b <proxy_recv_line+0xcb>
mov    %r13b,0x0(%rbp,%r12,1)
lea    0x2(%r12),%r13
cmp    %rbx,%r13
jb     1190 <proxy_recv_line+0x40>
nop
add    %rbx,%rbx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1070 <realloc@plt>
mov    %rax,%rbp
cmp    %rbx,%r13
jae    1200 <proxy_recv_line+0xb0>
jmp    1190 <proxy_recv_line+0x40>
mov    0x10(%rsp),%rax
test   %rax,%rax
je     1230 <proxy_recv_line+0xe0>
mov    %rbp,(%rax)
movb   $0x0,0x0(%rbp,%r12,1)
jmp    123e <proxy_recv_line+0xee>
movb   $0x0,0x0(%rbp,%r12,1)
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [33 x i8] c"recv() error from proxy server\0A\00"
@EOF = global i32 0
@.str.1 = [26 x i8] c"end of file from server\0A\00"
define i32 @proxy_recv_line(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8
store i32 %0, i32* %3
store i8** %1, i8*** %4
store i64 0, i64* %7
store i64 80, i64* %8
%10 = load i64, i64* %8
%11 = call i64 @malloc(i64 %10)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %6
br label %13
13: ; preds = %2, %54
%14 = load i32, i32* %3
%15 = call i64 @recv(i32 %14, i8* %9, i32 1, i32 0)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %17, %13
%21 = load i8, i8* %9
%22 = sext i8 %21 to i32
store i32 %22, i32* %5
%23 = load i32, i32* %5
%24 = load i32, i32* @EOF
%25 = icmp eq i32 %23, %24
br i1 %25, label %26, label %31
26: ; preds = %20
%27 = load i8*, i8** %6
%28 = call i32 @free(i8* %27)
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %26, %20
%32 = load i32, i32* %5
%33 = icmp eq i32 %32, 10
br i1 %33, label %34, label %35
34: ; preds = %31
br label %55
35: ; preds = %31
%36 = load i32, i32* %5
%37 = trunc i32 %36 to i8
%38 = load i8*, i8** %6
%39 = load i64, i64* %7
%40 = add i64 %39, 1
store i64 %40, i64* %7
%41 = getelementptr inbounds i8, i8* %38, i64 %39
store i8 %37, i8* %41
br label %42
42: ; preds = %47, %35
%43 = load i64, i64* %7
%44 = add i64 %43, 1
%45 = load i64, i64* %8
%46 = icmp uge i64 %44, %45
br i1 %46, label %47, label %54
47: ; preds = %42
%48 = load i64, i64* %8
%49 = mul i64 %48, 2
store i64 %49, i64* %8
%50 = load i8*, i8** %6
%51 = load i64, i64* %8
%52 = call i64 @realloc(i8* %50, i64 %51)
%53 = inttoptr i64 %52 to i8*
store i8* %53, i8** %6
br label %42
54: ; preds = %42
br label %13
55: ; preds = %34
%56 = load i8**, i8*** %4
%57 = icmp ne i8** %56, null
br i1 %57, label %58, label %61
58: ; preds = %55
%59 = load i8*, i8** %6
%60 = load i8**, i8*** %4
store i8* %59, i8** %60
br label %61
61: ; preds = %58, %55
%62 = load i8*, i8** %6
%63 = load i64, i64* %7
%64 = getelementptr inbounds i8, i8* %62, i64 %63
store i8 0, i8* %64
%65 = load i8**, i8*** %4
%66 = icmp eq i8** %65, null
br i1 %66, label %67, label %70
67: ; preds = %61
%68 = load i8*, i8** %6
%69 = call i32 @free(i8* %68)
br label %70
70: ; preds = %67, %61
%71 = load i64, i64* %7
%72 = trunc i64 %71 to i32
ret i32 %72
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [33 x i8] c"recv() error from proxy server\0A\00"
@EOF = global i32 0
@.str.1 = [26 x i8] c"end of file from server\0A\00"
define i32 @proxy_recv_line(i32 %0, i8** writeonly %1) {
%3 = alloca i8
%4 = tail call i64 @malloc(i64 80)
%5 = inttoptr i64 %4 to i8*
br label %6
6: ; preds = %40, %2
%7 = phi i8* [ %5, %2 ], [ %41, %40 ]
%8 = phi i64 [ 0, %2 ], [ %27, %40 ]
%9 = phi i64 [ 80, %2 ], [ %42, %40 ]
%10 = call i64 @recv(i32 %0, i8* %3, i32 1, i32 0)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %6
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %12, %6
%16 = load i8, i8* %3
%17 = sext i8 %16 to i32
%18 = load i32, i32* @EOF
%19 = icmp eq i32 %18, %17
br i1 %19, label %20, label %24
20: ; preds = %15
%21 = call i32 @free(i8* %7)
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %20, %15
%25 = icmp eq i8 %16, 10
br i1 %25, label %43, label %26
26: ; preds = %24
%27 = add i64 %8, 1
%28 = getelementptr inbounds i8, i8* %7, i64 %8
store i8 %16, i8* %28
%29 = add i64 %8, 2
%30 = icmp ult i64 %29, %9
br i1 %30, label %40, label %31
31: ; preds = %26, %31
%32 = phi i64 [ %34, %31 ], [ %9, %26 ]
%33 = phi i8* [ %36, %31 ], [ %7, %26 ]
%34 = shl i64 %32, 1
%35 = call i64 @realloc(i8* %33, i64 %34)
%36 = inttoptr i64 %35 to i8*
%37 = icmp ult i64 %29, %34
br i1 %37, label %38, label %31
38: ; preds = %31
%39 = inttoptr i64 %35 to i8*
br label %40
40: ; preds = %38, %26
%41 = phi i8* [ %7, %26 ], [ %39, %38 ]
%42 = phi i64 [ %9, %26 ], [ %34, %38 ]
br label %6
43: ; preds = %24
%44 = icmp eq i8** %1, null
br i1 %44, label %47, label %45
45: ; preds = %43
store i8* %7, i8** %1
%46 = getelementptr inbounds i8, i8* %7, i64 %8
store i8 0, i8* %46
br label %50
47: ; preds = %43
%48 = getelementptr inbounds i8, i8* %7, i64 %8
store i8 0, i8* %48
%49 = call i32 @free(i8* %7)
br label %50
50: ; preds = %45, %47
%51 = trunc i64 %8 to i32
ret i32 %51
}