#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 */
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};

/* Variables and functions */
char *inet_ntoa(int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int snprintf(char *, int, char *, char *, int);
char *strstr(char *, char *);
int write(int, char *, int);
int connectHttps(int s, struct sockaddr_in *addr, int c) {
  char buf[512];
  char *headerend = NULL;
  int header_length;
  int len = snprintf(buf, 512, "CONNECT %s:%d HTTP/1.0\r\n\r\n",
                     inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
  if (write(s, buf, len) != len) {
    perror("Error writing to proxy");
    return -1;
  }
  while (headerend == NULL) {
    len = read(s, buf, 512);
    if (len <= 0) {
      perror("Error reading from proxy");
      return -1;
    }
    headerend = strstr(buf, "\r\n\r\n");
  }
  header_length = headerend + 4 - buf;
  if (header_length < len) {
    if (write(c, headerend + 4, len - header_length) != (len - header_length)) {
      perror("Error writing to client");
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<connectHttps>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movq   $0x0,-0x228(%rbp)
lea    -0x220(%rbp),%rax
mov    %rax,-0x240(%rbp)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x238(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1070 <ntohs@plt>
mov    -0x240(%rbp),%rdi
mov    -0x238(%rbp),%rcx
mov    %eax,%r8d
mov    $0x200,%esi
lea    0xe27(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x230(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    -0x230(%rbp),%edx
call   1030 <write@plt>
cmp    -0x230(%rbp),%eax
je     121d <connectHttps+0xad>
lea    0xe0f(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1312 <connectHttps+0x1a2>
jmp    1222 <connectHttps+0xb2>
cmpq   $0x0,-0x228(%rbp)
jne    128e <connectHttps+0x11e>
mov    -0x8(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1060 <read@plt>
mov    %eax,-0x230(%rbp)
cmpl   $0x0,-0x230(%rbp)
jg     126f <connectHttps+0xff>
lea    0xdd4(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1312 <connectHttps+0x1a2>
lea    -0x220(%rbp),%rdi
lea    0xd99(%rip),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x228(%rbp)
jmp    1222 <connectHttps+0xb2>
mov    -0x228(%rbp),%rax
add    $0x4,%rax
lea    -0x220(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x22c(%rbp)
mov    -0x22c(%rbp),%eax
cmp    -0x230(%rbp),%eax
jge    130b <connectHttps+0x19b>
mov    -0x14(%rbp),%edi
mov    -0x228(%rbp),%rsi
add    $0x4,%rsi
mov    -0x230(%rbp),%edx
sub    -0x22c(%rbp),%edx
call   1030 <write@plt>
mov    -0x230(%rbp),%ecx
sub    -0x22c(%rbp),%ecx
cmp    %ecx,%eax
je     1306 <connectHttps+0x196>
lea    0xd56(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1312 <connectHttps+0x1a2>
jmp    130b <connectHttps+0x19b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connectHttps>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%r12d
mov    0x4(%rsi),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rbp
mov    (%rbx),%edi
call   1070 <ntohs@plt>
lea    0xe5d(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %rbp,%rcx
mov    %eax,%r8d
call   1050 <snprintf@plt>
mov    %eax,%ebp
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
jne    1231 <connectHttps+0xc1>
mov    %rsp,%rbp
lea    0xe5c(%rip),%r15
lea    0xe39(%rip),%r13
nopl   (%rax)
mov    %r12d,%edi
mov    %rbp,%rsi
mov    $0x200,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    1238 <connectHttps+0xc8>
mov    %eax,%ebx
mov    %rbp,%rdi
mov    %r13,%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     11e0 <connectHttps+0x70>
add    $0x4,%rax
mov    %rsp,%rcx
mov    %eax,%edx
sub    %ecx,%edx
xor    %ebp,%ebp
sub    %edx,%ebx
jle    1245 <connectHttps+0xd5>
mov    %r14d,%edi
mov    %rax,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
je     1245 <connectHttps+0xd5>
lea    0xe1c(%rip),%r15
jmp    1238 <connectHttps+0xc8>
lea    0xde3(%rip),%r15
mov    %r15,%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
@.str = [27 x i8] c"CONNECT %s:%d HTTP/1.0\0D\0A\0D\0A\00"
@.str.1 = [23 x i8] c"Error writing to proxy\00"
@.str.2 = [25 x i8] c"Error reading from proxy\00"
@.str.3 = [5 x i8] c"\0D\0A\0D\0A\00"
@.str.4 = [24 x i8] c"Error writing to client\00"
define i32 @connectHttps(i32 %0, %struct.sockaddr_in* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in*
%7 = alloca i32
%8 = alloca [512 x i8]
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %6
store i32 %2, i32* %7
store i8* null, i8** %9
%12 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%13 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 1
%15 = load i32, i32* %14
%16 = call i8* @inet_ntoa(i32 %15)
%17 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %17, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @ntohs(i32 %19)
%21 = call i32 @snprintf(i8* %12, i32 512, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %16, i32 %20)
store i32 %21, i32* %11
%22 = load i32, i32* %5
%23 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%24 = load i32, i32* %11
%25 = call i32 @write(i32 %22, i8* %23, i32 %24)
%26 = load i32, i32* %11
%27 = icmp ne i32 %25, %26
br i1 %27, label %28, label %30
28: ; preds = %3
%29 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %72
30: ; preds = %3
br label %31
31: ; preds = %42, %30
%32 = load i8*, i8** %9
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %45
34: ; preds = %31
%35 = load i32, i32* %5
%36 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%37 = call i32 @read(i32 %35, i8* %36, i32 512)
store i32 %37, i32* %11
%38 = load i32, i32* %11
%39 = icmp sle i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %34
%41 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %72
42: ; preds = %34
%43 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%44 = call i8* @strstr(i8* %43, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i8* %44, i8** %9
br label %31
45: ; preds = %31
%46 = load i8*, i8** %9
%47 = getelementptr inbounds i8, i8* %46, i64 4
%48 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%49 = ptrtoint i8* %47 to i64
%50 = ptrtoint i8* %48 to i64
%51 = sub i64 %49, %50
%52 = trunc i64 %51 to i32
store i32 %52, i32* %10
%53 = load i32, i32* %10
%54 = load i32, i32* %11
%55 = icmp slt i32 %53, %54
br i1 %55, label %56, label %71
56: ; preds = %45
%57 = load i32, i32* %7
%58 = load i8*, i8** %9
%59 = getelementptr inbounds i8, i8* %58, i64 4
%60 = load i32, i32* %11
%61 = load i32, i32* %10
%62 = sub nsw i32 %60, %61
%63 = call i32 @write(i32 %57, i8* %59, i32 %62)
%64 = load i32, i32* %11
%65 = load i32, i32* %10
%66 = sub nsw i32 %64, %65
%67 = icmp ne i32 %63, %66
br i1 %67, label %68, label %70
68: ; preds = %56
%69 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %4
br label %72
70: ; preds = %56
br label %71
71: ; preds = %70, %45
store i32 0, i32* %4
br label %72
72: ; preds = %71, %68, %40, %28
%73 = load i32, i32* %4
ret i32 %73
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
@.str = [27 x i8] c"CONNECT %s:%d HTTP/1.0\0D\0A\0D\0A\00"
@.str.1 = [23 x i8] c"Error writing to proxy\00"
@.str.2 = [25 x i8] c"Error reading from proxy\00"
@.str.3 = [5 x i8] c"\0D\0A\0D\0A\00"
@.str.4 = [24 x i8] c"Error writing to client\00"
define i32 @connectHttps(i32 %0, %struct.sockaddr_in* nocapture %1, i32 %2) {
%4 = alloca [512 x i8]
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
%7 = load i32, i32* %6
%8 = tail call i8* @inet_ntoa(i32 %7)
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%10 = load i32, i32* %9
%11 = tail call i32 @ntohs(i32 %10)
%12 = call i32 @snprintf(i8* %5, i32 512, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %8, i32 %11)
%13 = call i32 @write(i32 %0, i8* %5, i32 %12)
%14 = icmp eq i32 %13, %12
br i1 %14, label %15, label %32
15: ; preds = %3, %18
%16 = call i32 @read(i32 %0, i8* %5, i32 512)
%17 = icmp slt i32 %16, 1
br i1 %17, label %32, label %18
18: ; preds = %15
%19 = call i8* @strstr(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%20 = icmp eq i8* %19, null
br i1 %20, label %15, label %21
21: ; preds = %18
%22 = getelementptr inbounds i8, i8* %19, i64 4
%23 = ptrtoint i8* %22 to i64
%24 = ptrtoint [512 x i8]* %4 to i64
%25 = sub i64 %23, %24
%26 = trunc i64 %25 to i32
%27 = icmp sgt i32 %16, %26
br i1 %27, label %28, label %35
28: ; preds = %21
%29 = sub nsw i32 %16, %26
%30 = call i32 @write(i32 %2, i8* %22, i32 %29)
%31 = icmp eq i32 %30, %29
br i1 %31, label %35, label %32
32: ; preds = %15, %28, %3
%33 = phi i8* [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), %3 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), %28 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), %15 ]
%34 = call i32 @perror(i8* %33)
br label %35
35: ; preds = %32, %21, %28
%36 = phi i32 [ 0, %28 ], [ 0, %21 ], [ -1, %32 ]
ret i32 %36
}