#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int /*<<< orphan*/,
                           char *);
int getpid();
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char const *, int);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int /*<<< orphan*/ stderr;
int strlen(char *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int write(int, char *, int);
int proxyHTTPhandshake(int s, const char *server, int port) {
  int cc;
  int L;
  int code;
  char buf[128];
  sprintf(buf, "CONNECT %s:%d HTTP/1.1\n\n", server, port);
  L = strlen(buf);
  cc = write(s, buf, L);
  if (cc != L) {
    perror("write()");
    return -1;
  }
  cc = read(s, buf, sizeof(buf));
  if (cc <= 0) {
    perror("read()");
    return -1;
  }
  sscanf(buf, "%*s%d", &code);
  if (code != 200) {
    fprintf(stderr, "%d:%lu proxy handshake failed %s", getpid(), time(NULL),
            buf);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<proxyHTTPhandshake>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0xa0(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xe46(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0xa0(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     1207 <proxyHTTPhandshake+0x77>
lea    0xe23(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bd <proxyHTTPhandshake+0x12d>
mov    -0x8(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x80,%edx
call   1060 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     1240 <proxyHTTPhandshake+0xb0>
lea    0xdf2(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bd <proxyHTTPhandshake+0x12d>
lea    -0xa0(%rbp),%rdi
lea    0xdda(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1090 <sscanf@plt>
cmpl   $0xc8,-0x20(%rbp)
je     12b6 <proxyHTTPhandshake+0x126>
mov    0x2d75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xa8(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0xa4(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    -0xa8(%rbp),%edi
mov    -0xa4(%rbp),%edx
mov    %eax,%ecx
lea    -0xa0(%rbp),%r8
lea    0xd89(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bd <proxyHTTPhandshake+0x12d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<proxyHTTPhandshake>:
push   %rbp
push   %r14
push   %rbx
sub    $0x90,%rsp
mov    %edx,%ecx
mov    %rsi,%rdx
mov    %edi,%ebx
lea    0xe57(%rip),%rsi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   10b0 <sprintf@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
jne    1248 <proxyHTTPhandshake+0xb8>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x80,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    1256 <proxyHTTPhandshake+0xc6>
lea    0xe3c(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0xc(%rsp),%rdx
call   1090 <sscanf@plt>
xor    %eax,%eax
cmpl   $0xc8,0xc(%rsp)
je     123c <proxyHTTPhandshake+0xac>
mov    0x2dd2(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
xor    %edi,%edi
call   1080 <time@plt>
lea    0xe07(%rip),%rsi
lea    0x10(%rsp),%r8
mov    %ebx,%edi
mov    %ebp,%edx
mov    %eax,%ecx
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x90,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdca(%rip),%rdi
call   10a0 <perror@plt>
jmp    1237 <proxyHTTPhandshake+0xa7>
lea    0xdc4(%rip),%rdi
call   10a0 <perror@plt>
jmp    1237 <proxyHTTPhandshake+0xa7>

LLVM IR (-O0):
@.str = [25 x i8] c"CONNECT %s:%d HTTP/1.1\0A\0A\00"
@.str.1 = [8 x i8] c"write()\00"
@.str.2 = [7 x i8] c"read()\00"
@.str.3 = [6 x i8] c"%*s%d\00"
@stderr = global i32 0
@.str.4 = [33 x i8] c"%d:%lu proxy handshake failed %s\00"
define i32 @proxyHTTPhandshake(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [128 x i8]
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%13 = load i8*, i8** %6
%14 = load i32, i32* %7
%15 = call i32 @sprintf(i8* %12, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %13, i32 %14)
%16 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%17 = call i32 @strlen(i8* %16)
store i32 %17, i32* %9
%18 = load i32, i32* %5
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%20 = load i32, i32* %9
%21 = call i32 @write(i32 %18, i8* %19, i32 %20)
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = load i32, i32* %9
%24 = icmp ne i32 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %3
%26 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %47
27: ; preds = %3
%28 = load i32, i32* %5
%29 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%30 = call i32 @read(i32 %28, i8* %29, i32 128)
store i32 %30, i32* %8
%31 = load i32, i32* %8
%32 = icmp sle i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %27
%34 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %47
35: ; preds = %27
%36 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%37 = call i32 @sscanf(i8* %36, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32* %10)
%38 = load i32, i32* %10
%39 = icmp ne i32 %38, 200
br i1 %39, label %40, label %46
40: ; preds = %35
%41 = load i32, i32* @stderr
%42 = call i32 (...) @getpid()
%43 = call i32 @time(i32* null)
%44 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%45 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %42, i32 %43, i8* %44)
store i32 -1, i32* %4
br label %47
46: ; preds = %35
store i32 0, i32* %4
br label %47
47: ; preds = %46, %40, %33, %25
%48 = load i32, i32* %4
ret i32 %48
}

LLVM IR (-O2):
@.str = [25 x i8] c"CONNECT %s:%d HTTP/1.1\0A\0A\00"
@.str.1 = [8 x i8] c"write()\00"
@.str.2 = [7 x i8] c"read()\00"
@.str.3 = [6 x i8] c"%*s%d\00"
@stderr = global i32 0
@.str.4 = [33 x i8] c"%d:%lu proxy handshake failed %s\00"
define i32 @proxyHTTPhandshake(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca [128 x i8]
%6 = bitcast i32* %4 to i8*
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%8 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %1, i32 %2)
%9 = call i32 @strlen(i8* %7)
%10 = call i32 @write(i32 %0, i8* %7, i32 %9)
%11 = icmp eq i32 %10, %9
br i1 %11, label %14, label %12
12: ; preds = %3
%13 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %28
14: ; preds = %3
%15 = call i32 @read(i32 %0, i8* %7, i32 128)
%16 = icmp slt i32 %15, 1
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %28
19: ; preds = %14
%20 = call i32 @sscanf(i8* %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32* %4)
%21 = load i32, i32* %4
%22 = icmp eq i32 %21, 200
br i1 %22, label %28, label %23
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = call i32 (...) @getpid()
%26 = call i32 @time(i32* null)
%27 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %25, i32 %26, i8* %7)
br label %28
28: ; preds = %19, %23, %17, %12
%29 = phi i32 [ -1, %12 ], [ -1, %17 ], [ -1, %23 ], [ 0, %19 ]
ret i32 %29
}