#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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(unsigned char *);
int makeSocket(char *, long);
unsigned char *malloc(long);
int /*<<< orphan*/ memcpy(unsigned char *, char *, long);
int /*<<< orphan*/ printf(char *, char *);
long readline(int, char *, int);
unsigned char *realloc(unsigned char *, long);
long recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
long http_get(char *host, long port, char *path, unsigned char **resptr) {
  long resalloc = 1024;
  long reslen = 0;
  char linebuf[2048];
  long len;
  int sock;
  unsigned char *res;
  *resptr = NULL;
  res = NULL;
  sock = makeSocket(host, port);
  if (sock < 0) {
    printf("makeSocket() err: %s\n", strerror(errno));
    return (-1);
  }
  snprintf(linebuf, 2048 - 1,
           "GET %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n", path,
           host);
  send(sock, linebuf, strlen(linebuf), 0);
  for (;;) {
    len = readline(sock, linebuf, 2048);
    if (len == 0)
      break;
  }
  res = malloc(resalloc);
  if (res == NULL) {
    close(sock);
    return (-1);
  }
  for (;;) {
    len = recv(sock, linebuf, 2048, 0);
    if (len == 0)
      break;
    if (len < 0) {
      printf("Err: %s\n", strerror(errno));
      free(res);
      close(sock);
      return (-1);
    }
    if (reslen + len + 1 > resalloc) {
      resalloc *= 2;
      res = realloc(res, resalloc);
      if (res == NULL) {
        close(sock);
        return (-1);
      }
    }
    memcpy(&(res[reslen]), linebuf, len);
    reslen += len;
  }
  res[reslen] = 0;
  *resptr = res;
  return (reslen);
}


x86 assembly (clang, -O0):
<http_get>:
push   %rbp
mov    %rsp,%rbp
sub    $0x870,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x400,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x28(%rbp),%rax
movq   $0x0,(%rax)
movq   $0x0,-0x858(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <makeSocket@plt>
mov    %eax,-0x84c(%rbp)
cmpl   $0x0,-0x84c(%rbp)
jge    125b <http_get+0x8b>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    %rax,%rsi
lea    0xdb7(%rip),%rdi
call   1070 <printf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1468 <http_get+0x298>
lea    -0x840(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    $0x7ff,%esi
lea    0xda0(%rip),%rdx
call   1090 <snprintf@plt>
mov    -0x84c(%rbp),%eax
mov    %eax,-0x864(%rbp)
lea    -0x840(%rbp),%rax
mov    %rax,-0x860(%rbp)
lea    -0x840(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x864(%rbp),%edi
mov    -0x860(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    -0x84c(%rbp),%edi
lea    -0x840(%rbp),%rsi
mov    $0x800,%edx
call   10a0 <readline@plt>
mov    %rax,-0x848(%rbp)
cmpq   $0x0,-0x848(%rbp)
jne    12e8 <http_get+0x118>
jmp    12ed <http_get+0x11d>
jmp    12b7 <http_get+0xe7>
mov    -0x30(%rbp),%rdi
call   10d0 <malloc@plt>
mov    %rax,-0x858(%rbp)
cmpq   $0x0,-0x858(%rbp)
jne    1323 <http_get+0x153>
mov    -0x84c(%rbp),%edi
call   10b0 <close@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1468 <http_get+0x298>
jmp    1328 <http_get+0x158>
mov    -0x84c(%rbp),%edi
lea    -0x840(%rbp),%rsi
mov    $0x800,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %rax,-0x848(%rbp)
cmpq   $0x0,-0x848(%rbp)
jne    135b <http_get+0x18b>
jmp    1443 <http_get+0x273>
cmpq   $0x0,-0x848(%rbp)
jge    13aa <http_get+0x1da>
mov    0x2c70(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    %rax,%rsi
lea    0xcc6(%rip),%rdi
call   1070 <printf@plt>
mov    -0x858(%rbp),%rdi
call   1030 <free@plt>
mov    -0x84c(%rbp),%edi
call   10b0 <close@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1468 <http_get+0x298>
mov    -0x38(%rbp),%rax
add    -0x848(%rbp),%rax
add    $0x1,%rax
cmp    -0x30(%rbp),%rax
jle    1411 <http_get+0x241>
mov    -0x30(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x858(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   10e0 <realloc@plt>
mov    %rax,-0x858(%rbp)
cmpq   $0x0,-0x858(%rbp)
jne    140c <http_get+0x23c>
mov    -0x84c(%rbp),%edi
call   10b0 <close@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1468 <http_get+0x298>
jmp    1411 <http_get+0x241>
mov    -0x858(%rbp),%rdi
add    -0x38(%rbp),%rdi
lea    -0x840(%rbp),%rsi
mov    -0x848(%rbp),%rdx
call   10c0 <memcpy@plt>
mov    -0x848(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1328 <http_get+0x158>
mov    -0x858(%rbp),%rax
mov    -0x38(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x858(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x870,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<http_get>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
mov    %rcx,%r12
mov    %rdx,%r14
mov    %rdi,%rbp
movq   $0x0,(%rcx)
call   1080 <makeSocket@plt>
test   %eax,%eax
js     1302 <http_get+0x132>
mov    %eax,%r15d
lea    0xe0e(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x7ff,%esi
mov    %r14,%rcx
mov    %rbp,%r8
call   1090 <snprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
nopw   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x800,%edx
call   10a0 <readline@plt>
test   %rax,%rax
jne    1240 <http_get+0x70>
mov    $0x400,%edi
call   10d0 <malloc@plt>
test   %rax,%rax
je     1355 <http_get+0x185>
mov    %rax,%rbp
mov    %r12,0x8(%rsp)
lea    0x10(%rsp),%rsi
xor    %ebx,%ebx
mov    %r15d,%edi
mov    $0x800,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
test   %rax,%rax
je     1321 <http_get+0x151>
mov    %rax,%r14
mov    $0x400,%r13d
xor    %r12d,%r12d
jmp    12dc <http_get+0x10c>
nopl   (%rax)
add    %rbp,%r12
mov    %r12,%rdi
mov    %rbx,%r12
mov    %rbp,%rbx
lea    0x10(%rsp),%rbp
mov    %rbp,%rsi
mov    %r14,%rdx
call   10c0 <memcpy@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rbp
mov    %r12,%rbx
mov    $0x800,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %rax,%r14
test   %rax,%rax
je     1321 <http_get+0x151>
test   %r14,%r14
js     1330 <http_get+0x160>
lea    (%r14,%r12,1),%rbx
cmp    %r13,%rbx
jl     12a0 <http_get+0xd0>
add    %r13,%r13
mov    %rbp,%rdi
mov    %r13,%rsi
call   10e0 <realloc@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    12a0 <http_get+0xd0>
jmp    1355 <http_get+0x185>
mov    0x2cd7(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
lea    0xce9(%rip),%rdi
mov    %rax,%rsi
call   1070 <printf@plt>
jmp    135d <http_get+0x18d>
movb   $0x0,0x0(%rbp,%rbx,1)
mov    0x8(%rsp),%rax
mov    %rbp,(%rax)
jmp    1364 <http_get+0x194>
mov    0x2ca9(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
lea    0xd02(%rip),%rdi
mov    %rax,%rsi
call   1070 <printf@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r15d,%edi
call   10b0 <close@plt>
mov    $0xffffffffffffffff,%rbx
mov    %rbx,%rax
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"makeSocket() err: %s\0A\00"
@errno = global i32 0
@.str.1 = [49 x i8] c"GET %s HTTP/1.1\0D\0AHost: %s\0D\0AConnection: close\0D\0A\0D\0A\00"
@.str.2 = [9 x i8] c"Err: %s\0A\00"
define i64 @http_get(i8* %0, i64 %1, i8* %2, i8** %3) {
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8**
%10 = alloca i64
%11 = alloca i64
%12 = alloca [2048 x i8]
%13 = alloca i64
%14 = alloca i32
%15 = alloca i8*
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i8** %3, i8*** %9
store i64 1024, i64* %10
store i64 0, i64* %11
%16 = load i8**, i8*** %9
store i8* null, i8** %16
store i8* null, i8** %15
%17 = load i8*, i8** %6
%18 = load i64, i64* %7
%19 = call i32 @makeSocket(i8* %17, i64 %18)
store i32 %19, i32* %14
%20 = load i32, i32* %14
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %4
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %24)
store i64 -1, i64* %5
br label %107
26: ; preds = %4
%27 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 0
%28 = load i8*, i8** %8
%29 = load i8*, i8** %6
%30 = call i32 @snprintf(i8* %27, i32 2047, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i8* %28, i8* %29)
%31 = load i32, i32* %14
%32 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 0
%33 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 0
%34 = call i32 @strlen(i8* %33)
%35 = call i32 @send(i32 %31, i8* %32, i32 %34, i32 0)
br label %36
36: ; preds = %43, %26
%37 = load i32, i32* %14
%38 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 0
%39 = call i64 @readline(i32 %37, i8* %38, i32 2048)
store i64 %39, i64* %13
%40 = load i64, i64* %13
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %36
br label %44
43: ; preds = %36
br label %36
44: ; preds = %42
%45 = load i64, i64* %10
%46 = call i8* @malloc(i64 %45)
store i8* %46, i8** %15
%47 = load i8*, i8** %15
%48 = icmp eq i8* %47, null
br i1 %48, label %49, label %52
49: ; preds = %44
%50 = load i32, i32* %14
%51 = call i32 @close(i32 %50)
store i64 -1, i64* %5
br label %107
52: ; preds = %44
br label %53
53: ; preds = %90, %52
%54 = load i32, i32* %14
%55 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 0
%56 = call i64 @recv(i32 %54, i8* %55, i32 2048, i32 0)
store i64 %56, i64* %13
%57 = load i64, i64* %13
%58 = icmp eq i64 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %53
br label %100
60: ; preds = %53
%61 = load i64, i64* %13
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %71
63: ; preds = %60
%64 = load i32, i32* @errno
%65 = call i8* @strerror(i32 %64)
%66 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %65)
%67 = load i8*, i8** %15
%68 = call i32 @free(i8* %67)
%69 = load i32, i32* %14
%70 = call i32 @close(i32 %69)
store i64 -1, i64* %5
br label %107
71: ; preds = %60
%72 = load i64, i64* %11
%73 = load i64, i64* %13
%74 = add nsw i64 %72, %73
%75 = add nsw i64 %74, 1
%76 = load i64, i64* %10
%77 = icmp sgt i64 %75, %76
br i1 %77, label %78, label %90
78: ; preds = %71
%79 = load i64, i64* %10
%80 = mul nsw i64 %79, 2
store i64 %80, i64* %10
%81 = load i8*, i8** %15
%82 = load i64, i64* %10
%83 = call i8* @realloc(i8* %81, i64 %82)
store i8* %83, i8** %15
%84 = load i8*, i8** %15
%85 = icmp eq i8* %84, null
br i1 %85, label %86, label %89
86: ; preds = %78
%87 = load i32, i32* %14
%88 = call i32 @close(i32 %87)
store i64 -1, i64* %5
br label %107
89: ; preds = %78
br label %90
90: ; preds = %89, %71
%91 = load i8*, i8** %15
%92 = load i64, i64* %11
%93 = getelementptr inbounds i8, i8* %91, i64 %92
%94 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 0
%95 = load i64, i64* %13
%96 = call i32 @memcpy(i8* %93, i8* %94, i64 %95)
%97 = load i64, i64* %13
%98 = load i64, i64* %11
%99 = add nsw i64 %98, %97
store i64 %99, i64* %11
br label %53
100: ; preds = %59
%101 = load i8*, i8** %15
%102 = load i64, i64* %11
%103 = getelementptr inbounds i8, i8* %101, i64 %102
store i8 0, i8* %103
%104 = load i8*, i8** %15
%105 = load i8**, i8*** %9
store i8* %104, i8** %105
%106 = load i64, i64* %11
store i64 %106, i64* %5
br label %107
107: ; preds = %100, %86, %63, %49, %22
%108 = load i64, i64* %5
ret i64 %108
}

LLVM IR (-O2):
@.str = [22 x i8] c"makeSocket() err: %s\0A\00"
@errno = global i32 0
@.str.1 = [49 x i8] c"GET %s HTTP/1.1\0D\0AHost: %s\0D\0AConnection: close\0D\0A\0D\0A\00"
@.str.2 = [9 x i8] c"Err: %s\0A\00"
define i64 @http_get(i8* %0, i64 %1, i8* %2, i8** nocapture writeonly %3) {
%5 = alloca [2048 x i8]
%6 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
store i8* null, i8** %3
%7 = tail call i32 @makeSocket(i8* %0, i64 %1)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %4
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %11)
br label %60
13: ; preds = %4
%14 = call i32 @snprintf(i8* %6, i32 2047, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i8* %2, i8* %0)
%15 = call i32 @strlen(i8* %6)
%16 = call i32 @send(i32 %7, i8* %6, i32 %15, i32 0)
br label %17
17: ; preds = %17, %13
%18 = call i64 @readline(i32 %7, i8* %6, i32 2048)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %17
20: ; preds = %17
%21 = call dereferenceable_or_null(1024) i8* @malloc(i64 1024)
%22 = icmp eq i8* %21, null
br i1 %22, label %26, label %23
23: ; preds = %20
%24 = call i64 @recv(i32 %7, i8* %6, i32 2048, i32 0)
%25 = icmp eq i64 %24, 0
br i1 %25, label %56, label %28
26: ; preds = %20
%27 = call i32 @close(i32 %7)
br label %60
28: ; preds = %23, %49
%29 = phi i64 [ %54, %49 ], [ %24, %23 ]
%30 = phi i8* [ %51, %49 ], [ %21, %23 ]
%31 = phi i64 [ %41, %49 ], [ 0, %23 ]
%32 = phi i64 [ %50, %49 ], [ 1024, %23 ]
%33 = icmp slt i64 %29, 0
br i1 %33, label %34, label %40
34: ; preds = %28
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %36)
%38 = call i32 @free(i8* %30)
%39 = call i32 @close(i32 %7)
br label %60
40: ; preds = %28
%41 = add nsw i64 %29, %31
%42 = icmp slt i64 %41, %32
br i1 %42, label %49, label %43
43: ; preds = %40
%44 = shl nsw i64 %32, 1
%45 = call i8* @realloc(i8* %30, i64 %44)
%46 = icmp eq i8* %45, null
br i1 %46, label %47, label %49
47: ; preds = %43
%48 = call i32 @close(i32 %7)
br label %60
49: ; preds = %43, %40
%50 = phi i64 [ %44, %43 ], [ %32, %40 ]
%51 = phi i8* [ %45, %43 ], [ %30, %40 ]
%52 = getelementptr inbounds i8, i8* %51, i64 %31
%53 = call i32 @memcpy(i8* %52, i8* %6, i64 %29)
%54 = call i64 @recv(i32 %7, i8* %6, i32 2048, i32 0)
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %28
56: ; preds = %49, %23
%57 = phi i64 [ 0, %23 ], [ %41, %49 ]
%58 = phi i8* [ %21, %23 ], [ %51, %49 ]
%59 = getelementptr inbounds i8, i8* %58, i64 %57
store i8 0, i8* %59
store i8* %58, i8** %3
br label %60
60: ; preds = %56, %47, %34, %26, %9
%61 = phi i64 [ -1, %9 ], [ -1, %26 ], [ %57, %56 ], [ -1, %34 ], [ -1, %47 ]
ret i64 %61
}