#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*/ base64_encode(char *, int, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int strlen(char *);
int http_get(int connection, const char *path, const char *host,
             const char *proxy_host, char *proxy_user, char *proxy_password) {
  static char get_command[255];
  if (proxy_host) {
    sprintf(get_command, "GET http://%s/%s HTTP/1.1\r\n", host, path);
  } else {
    sprintf(get_command, "GET /%s HTTP/1.1\r\n", path);
  }
  if (send(connection, get_command, strlen(get_command), 0) == -1) {
    return 1;
  }
  sprintf(get_command, "Host: %s\r\n", host);
  if (send(connection, get_command, strlen(get_command), 0) == -1) {
    return 2;
  }
  if (proxy_user) {
    int credentials_len = strlen(proxy_user) + strlen(proxy_password) + 1;
    char *proxy_credentials = malloc(credentials_len);
    char *auth_string = malloc(((credentials_len * 4) / 3) + 1);
    sprintf(proxy_credentials, "%s:%s", proxy_user, proxy_password);
    base64_encode(proxy_credentials, credentials_len, auth_string);
    sprintf(get_command, "Proxy-Authorization: BASIC %s\r\n", auth_string);
    if (send(connection, get_command, strlen(get_command), 0) == -1) {
      free(proxy_credentials);
      free(auth_string);
      return 4;
    }
    free(proxy_credentials);
    free(auth_string);
  }
  sprintf(get_command, "Connection: Close\r\n\r\n");
  if (send(connection, get_command, strlen(get_command), 0) == -1) {
    return 3;
  }
  return 0;
}


x86 assembly (clang, -O0):
<http_get>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11ac <http_get+0x4c>
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0x2ec7(%rip),%rdi
lea    0xe60(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
jmp    11c5 <http_get+0x65>
mov    -0x10(%rbp),%rdx
lea    0x2ea9(%rip),%rdi
lea    0xe5e(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4c(%rbp)
lea    0x2e8e(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x4c(%rbp),%edi
mov    %eax,%edx
lea    0x2e7d(%rip),%rsi
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0xffffffff,%eax
jne    11ff <http_get+0x9f>
movl   $0x1,-0x4(%rbp)
jmp    139e <http_get+0x23e>
mov    -0x18(%rbp),%rdx
lea    0x2e56(%rip),%rdi
lea    0xe1e(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x50(%rbp)
lea    0x2e3b(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x50(%rbp),%edi
mov    %eax,%edx
lea    0x2e2a(%rip),%rsi
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0xffffffff,%eax
jne    1252 <http_get+0xf2>
movl   $0x2,-0x4(%rbp)
jmp    139e <http_get+0x23e>
cmpq   $0x0,-0x28(%rbp)
je     1348 <http_get+0x1e8>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x58(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x58(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x34(%rbp),%eax
shl    $0x2,%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rcx
lea    0xd80(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
mov    -0x40(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    -0x48(%rbp),%rdx
call   1070 <base64_encode@plt>
mov    -0x48(%rbp),%rdx
lea    0x2d84(%rip),%rdi
lea    0xd5d(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x54(%rbp)
lea    0x2d69(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x54(%rbp),%edi
mov    %eax,%edx
lea    0x2d58(%rip),%rsi
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0xffffffff,%eax
jne    1336 <http_get+0x1d6>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
movl   $0x4,-0x4(%rbp)
jmp    139e <http_get+0x23e>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
lea    0x2d11(%rip),%rdi
lea    0xd0a(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x5c(%rbp)
lea    0x2cf6(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x5c(%rbp),%edi
mov    %eax,%edx
lea    0x2ce5(%rip),%rsi
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0xffffffff,%eax
jne    1397 <http_get+0x237>
movl   $0x3,-0x4(%rbp)
jmp    139e <http_get+0x23e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<http_get>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9,%r12
mov    %r8,%r15
mov    %rdx,%rbx
mov    %rsi,%rax
mov    %edi,%r14d
lea    0x2edf(%rip),%rdi
test   %rcx,%rcx
je     119c <http_get+0x3c>
lea    0xe73(%rip),%rsi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1080 <sprintf@plt>
jmp    11ad <http_get+0x4d>
lea    0xe79(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1080 <sprintf@plt>
lea    0x2eac(%rip),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0xffffffff,%eax
je     1327 <http_get+0x1c7>
lea    0x2e85(%rip),%rbp
lea    0xe4d(%rip),%rsi
mov    %rbp,%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   1080 <sprintf@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0xffffffff,%eax
je     132e <http_get+0x1ce>
test   %r15,%r15
je     12dd <http_get+0x17d>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %ebx,%ebp
add    %eax,%ebp
lea    (%rbx,%rax,1),%r13d
add    $0x1,%r13d
mov    %r13d,%edi
call   1060 <malloc@plt>
mov    %r12,(%rsp)
mov    %rax,%r12
lea    0x4(,%rbp,4),%eax
cltq
imul   $0x55555556,%rax,%rax
mov    %rax,%rcx
shr    $0x20,%rcx
shr    $0x3f,%rax
lea    (%rcx,%rax,1),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
lea    0xdc5(%rip),%rsi
mov    %r12,%rdi
mov    %r15,%rdx
mov    (%rsp),%rcx
xor    %eax,%eax
call   1080 <sprintf@plt>
mov    %r12,%rdi
mov    %r13d,%esi
mov    %rbx,%rdx
call   1070 <base64_encode@plt>
lea    0x2dc5(%rip),%rbp
lea    0xd9e(%rip),%rsi
mov    %rbp,%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   1080 <sprintf@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %eax,%ebp
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
cmp    $0xffffffff,%ebp
je     1335 <http_get+0x1d5>
movups 0xd7c(%rip),%xmm0
movaps %xmm0,0x2d75(%rip)
movabs $0xa0d0a0d65736f,%rax
mov    %rax,0x2d72(%rip)
lea    0x2d5d(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
xor    %ecx,%ecx
cmp    $0xffffffff,%eax
sete   %cl
lea    (%rcx,%rcx,2),%eax
jmp    133a <http_get+0x1da>
mov    $0x1,%eax
jmp    133a <http_get+0x1da>
mov    $0x2,%eax
jmp    133a <http_get+0x1da>
mov    $0x4,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@http_get.get_command = internal global [255 x i8] zeroinitializer
@.str = [28 x i8] c"GET http://%s/%s HTTP/1.1\0D\0A\00"
@.str.1 = [19 x i8] c"GET /%s HTTP/1.1\0D\0A\00"
@.str.2 = [11 x i8] c"Host: %s\0D\0A\00"
@.str.3 = [6 x i8] c"%s:%s\00"
@.str.4 = [32 x i8] c"Proxy-Authorization: BASIC %s\0D\0A\00"
@.str.5 = [22 x i8] c"Connection: Close\0D\0A\0D\0A\00"
define i32 @http_get(i32 %0, i8* %1, i8* %2, i8* %3, i8* %4, i8* %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i8*
%16 = alloca i8*
store i32 %0, i32* %8
store i8* %1, i8** %9
store i8* %2, i8** %10
store i8* %3, i8** %11
store i8* %4, i8** %12
store i8* %5, i8** %13
%17 = load i8*, i8** %11
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %23
19: ; preds = %6
%20 = load i8*, i8** %10
%21 = load i8*, i8** %9
%22 = call i32 (i8*, i8*, ...) @sprintf(i8* getelementptr inbounds ([255 x i8], [255 x i8]* @http_get.get_command, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %20, i8* %21)
br label %26
23: ; preds = %6
%24 = load i8*, i8** %9
%25 = call i32 (i8*, i8*, ...) @sprintf(i8* getelementptr inbounds ([255 x i8], [255 x i8]* @http_get.get_command, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %24)
br label %26
26: ; preds = %23, %19
%27 = load i32, i32* %8
%28 = call i32 @strlen(i8* getelementptr inbounds ([255 x i8], [255 x i8]* @http_get.get_command, i64 0, i64 0))
%29 = call i32 @send(i32 %27, i8* getelementptr inbounds ([255 x i8], [255 x i8]* @http_get.get_command, i64 0, i64 0), i32 %28, i32 0)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %26
store i32 1, i32* %7
br label %89
32: ; preds = %26
%33 = load i8*, i8** %10
%34 = call i32 (i8*, i8*, ...) @sprintf(i8* getelementptr inbounds ([255 x i8], [255 x i8]* @http_get.get_command, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %33)
%35 = load i32, i32* %8
%36 = call i32 @strlen(i8* getelementptr inbounds ([255 x i8], [255 x i8]* @http_get.get_command, i64 0, i64 0))
%37 = call i32 @send(i32 %35, i8* getelementptr inbounds ([255 x i8], [255 x i8]* @http_get.get_command, i64 0, i64 0), i32 %36, i32 0)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %40
39: ; preds = %32
store i32 2, i32* %7
br label %89
40: ; preds = %32
%41 = load i8*, i8** %12
%42 = icmp ne i8* %41, null
br i1 %42, label %43, label %81
43: ; preds = %40
%44 = load i8*, i8** %12
%45 = call i32 @strlen(i8* %44)
%46 = load i8*, i8** %13
%47 = call i32 @strlen(i8* %46)
%48 = add nsw i32 %45, %47
%49 = add nsw i32 %48, 1
store i32 %49, i32* %14
%50 = load i32, i32* %14
%51 = call i8* @malloc(i32 %50)
store i8* %51, i8** %15
%52 = load i32, i32* %14
%53 = mul nsw i32 %52, 4
%54 = sdiv i32 %53, 3
%55 = add nsw i32 %54, 1
%56 = call i8* @malloc(i32 %55)
store i8* %56, i8** %16
%57 = load i8*, i8** %15
%58 = load i8*, i8** %12
%59 = load i8*, i8** %13
%60 = call i32 (i8*, i8*, ...) @sprintf(i8* %57, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %58, i8* %59)
%61 = load i8*, i8** %15
%62 = load i32, i32* %14
%63 = load i8*, i8** %16
%64 = call i32 @base64_encode(i8* %61, i32 %62, i8* %63)
%65 = load i8*, i8** %16
%66 = call i32 (i8*, i8*, ...) @sprintf(i8* getelementptr inbounds ([255 x i8], [255 x i8]* @http_get.get_command, i64 0, i64 0), i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %65)
%67 = load i32, i32* %8
%68 = call i32 @strlen(i8* getelementptr inbounds ([255 x i8], [255 x i8]* @http_get.get_command, i64 0, i64 0))
%69 = call i32 @send(i32 %67, i8* getelementptr inbounds ([255 x i8], [255 x i8]* @http_get.get_command, i64 0, i64 0), i32 %68, i32 0)
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %76
71: ; preds = %43
%72 = load i8*, i8** %15
%73 = call i32 @free(i8* %72)
%74 = load i8*, i8** %16
%75 = call i32 @free(i8* %74)
store i32 4, i32* %7
br label %89
76: ; preds = %43
%77 = load i8*, i8** %15
%78 = call i32 @free(i8* %77)
%79 = load i8*, i8** %16
%80 = call i32 @free(i8* %79)
br label %81
81: ; preds = %76, %40
%82 = call i32 (i8*, i8*, ...) @sprintf(i8* getelementptr inbounds ([255 x i8], [255 x i8]* @http_get.get_command, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%83 = load i32, i32* %8
%84 = call i32 @strlen(i8* getelementptr inbounds ([255 x i8], [255 x i8]* @http_get.get_command, i64 0, i64 0))
%85 = call i32 @send(i32 %83, i8* getelementptr inbounds ([255 x i8], [255 x i8]* @http_get.get_command, i64 0, i64 0), i32 %84, i32 0)
%86 = icmp eq i32 %85, -1
br i1 %86, label %87, label %88
87: ; preds = %81
store i32 3, i32* %7
br label %89
88: ; preds = %81
store i32 0, i32* %7
br label %89
89: ; preds = %88, %87, %71, %39, %31
%90 = load i32, i32* %7
ret i32 %90
}

LLVM IR (-O2):
@http_get.get_command = internal global [255 x i8] zeroinitializer
@.str = [28 x i8] c"GET http://%s/%s HTTP/1.1\0D\0A\00"
@.str.1 = [19 x i8] c"GET /%s HTTP/1.1\0D\0A\00"
@.str.2 = [11 x i8] c"Host: %s\0D\0A\00"
@.str.3 = [6 x i8] c"%s:%s\00"
@.str.4 = [32 x i8] c"Proxy-Authorization: BASIC %s\0D\0A\00"
@.str.5 = [22 x i8] c"Connection: Close\0D\0A\0D\0A\00"
define i32 @http_get(i32 %0, i8* %1, i8* %2, i8* readnone %3, i8* %4, i8* %5) {
%7 = icmp eq i8* %3, null
br i1 %7, label %10, label %8
8: ; preds = %6
%9 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) getelementptr inbounds ([255 x i8], [255 x i8]* @http_get.get_command, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %2, i8* %1)
br label %12
10: ; preds = %6
%11 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) getelementptr inbounds ([255 x i8], [255 x i8]* @http_get.get_command, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %12
12: ; preds = %10, %8
%13 = tail call i32 @strlen(i8* getelementptr inbounds ([255 x i8], [255 x i8]* @http_get.get_command, i64 0, i64 0))
%14 = tail call i32 @send(i32 %0, i8* getelementptr inbounds ([255 x i8], [255 x i8]* @http_get.get_command, i64 0, i64 0), i32 %13, i32 0)
%15 = icmp eq i32 %14, -1
br i1 %15, label %46, label %16
16: ; preds = %12
%17 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) getelementptr inbounds ([255 x i8], [255 x i8]* @http_get.get_command, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%18 = tail call i32 @strlen(i8* getelementptr inbounds ([255 x i8], [255 x i8]* @http_get.get_command, i64 0, i64 0))
%19 = tail call i32 @send(i32 %0, i8* getelementptr inbounds ([255 x i8], [255 x i8]* @http_get.get_command, i64 0, i64 0), i32 %18, i32 0)
%20 = icmp eq i32 %19, -1
br i1 %20, label %46, label %21
21: ; preds = %16
%22 = icmp eq i8* %4, null
br i1 %22, label %41, label %23
23: ; preds = %21
%24 = tail call i32 @strlen(i8* %4)
%25 = tail call i32 @strlen(i8* %5)
%26 = add i32 %24, 1
%27 = add i32 %26, %25
%28 = tail call i8* @malloc(i32 %27)
%29 = shl nsw i32 %27, 2
%30 = sdiv i32 %29, 3
%31 = add nsw i32 %30, 1
%32 = tail call i8* @malloc(i32 %31)
%33 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %28, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %4, i8* %5)
%34 = tail call i32 @base64_encode(i8* %28, i32 %27, i8* %32)
%35 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) getelementptr inbounds ([255 x i8], [255 x i8]* @http_get.get_command, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %32)
%36 = tail call i32 @strlen(i8* getelementptr inbounds ([255 x i8], [255 x i8]* @http_get.get_command, i64 0, i64 0))
%37 = tail call i32 @send(i32 %0, i8* getelementptr inbounds ([255 x i8], [255 x i8]* @http_get.get_command, i64 0, i64 0), i32 %36, i32 0)
%38 = icmp eq i32 %37, -1
%39 = tail call i32 @free(i8* %28)
%40 = tail call i32 @free(i8* %32)
br i1 %38, label %46, label %41
41: ; preds = %23, %21
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 16 dereferenceable(22) getelementptr inbounds ([255 x i8], [255 x i8]* @http_get.get_command, i64 0, i64 0), i8* align 1 dereferenceable(22) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i64 22, i1 false)
%42 = tail call i32 @strlen(i8* getelementptr inbounds ([255 x i8], [255 x i8]* @http_get.get_command, i64 0, i64 0))
%43 = tail call i32 @send(i32 %0, i8* getelementptr inbounds ([255 x i8], [255 x i8]* @http_get.get_command, i64 0, i64 0), i32 %42, i32 0)
%44 = icmp eq i32 %43, -1
%45 = select i1 %44, i32 3, i32 0
br label %46
46: ; preds = %23, %41, %16, %12
%47 = phi i32 [ 1, %12 ], [ 2, %16 ], [ %45, %41 ], [ 4, %23 ]
ret i32 %47
}