#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 addrinfo {
  int dummy;
};

/* Variables and functions */
int connect_directly(struct addrinfo *);
scalar_t__ sendexactly(int, char **, size_t);
size_t snprintf(char *, int, char *, char const *);
int connect_via_proxy(struct addrinfo *ai_proxy, const char *dest) {
  int proxyfd = connect_directly(ai_proxy);
  if (proxyfd < 0)
    return proxyfd;
  char buf[128];
  size_t len = snprintf(buf, 128, "CONNECT %s HTTP/1.0\r\n\r\n", dest);
  if (sendexactly(proxyfd, &buf, len) < 0) {
    return -1;
  }
  return proxyfd;
}


x86 assembly (clang, -O0):
<connect_via_proxy>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <connect_directly@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1164 <connect_via_proxy+0x34>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b9 <connect_via_proxy+0x89>
lea    -0xa0(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x80,%esi
lea    0xe85(%rip),%rdx
call   1030 <snprintf@plt>
mov    %rax,-0xa8(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    -0xa8(%rbp),%rdx
call   1040 <sendexactly@plt>
cmp    $0x0,%rax
jge    11b3 <connect_via_proxy+0x83>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b9 <connect_via_proxy+0x89>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_via_proxy>:
push   %r15
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    %rsi,%r14
call   1050 <connect_directly@plt>
mov    %eax,%ebx
test   %eax,%eax
js     117c <connect_via_proxy+0x4c>
lea    0xeaf(%rip),%rdx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x80,%esi
mov    %r14,%rcx
call   1030 <snprintf@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %rax,%rdx
call   1040 <sendexactly@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovs  %eax,%ebx
mov    %ebx,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32 }
@.str = [24 x i8] c"CONNECT %s HTTP/1.0\0D\0A\0D\0A\00"
define i32 @connect_via_proxy(%struct.addrinfo* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.addrinfo*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [128 x i8]
%8 = alloca i64
store %struct.addrinfo* %0, %struct.addrinfo** %4
store i8* %1, i8** %5
%9 = load %struct.addrinfo*, %struct.addrinfo** %4
%10 = call i32 @connect_directly(%struct.addrinfo* %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = load i32, i32* %6
store i32 %14, i32* %3
br label %27
15: ; preds = %2
%16 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%17 = load i8*, i8** %5
%18 = call i64 @snprintf(i8* %16, i32 128, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %17)
store i64 %18, i64* %8
%19 = load i32, i32* %6
%20 = bitcast [128 x i8]* %7 to i8**
%21 = load i64, i64* %8
%22 = call i64 @sendexactly(i32 %19, i8** %20, i64 %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %15
store i32 -1, i32* %3
br label %27
25: ; preds = %15
%26 = load i32, i32* %6
store i32 %26, i32* %3
br label %27
27: ; preds = %25, %24, %13
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
%struct.addrinfo = type { i32 }
@.str = [24 x i8] c"CONNECT %s HTTP/1.0\0D\0A\0D\0A\00"
define i32 @connect_via_proxy(%struct.addrinfo* %0, i8* %1) {
%3 = alloca [128 x i8]
%4 = tail call i32 @connect_directly(%struct.addrinfo* %0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %13, label %6
6: ; preds = %2
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%8 = call i64 @snprintf(i8* %7, i32 128, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %1)
%9 = bitcast [128 x i8]* %3 to i8**
%10 = call i64 @sendexactly(i32 %4, i8** %9, i64 %8)
%11 = icmp slt i64 %10, 0
%12 = select i1 %11, i32 -1, i32 %4
br label %13
13: ; preds = %2, %6
%14 = phi i32 [ %12, %6 ], [ %4, %2 ]
ret i32 %14
}