#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*/ AF_INET;
int /*<<< orphan*/ AF_INET6;
int TCP_Connect(char *, char *);
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ free(char *);
short htons(int /*<<< orphan*/);
scalar_t__ inet_pton(int /*<<< orphan*/, char *, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
char strlen(char *);
int /*<<< orphan*/ write(int, ...);
int socks5(int socket, char *host, char *port, char *user, char *pass) {
  if (socket < 0)
    return TCP_Connect(host, port);
  static char auth[3] = {0x05, 0x01};
  auth[2] = user && pass ? 0x02 : 0x00;
  write(socket, auth, 3);
  static char res[2];
  read(socket, res, 2);
  if (res[1])
    return -res[1];
  if (user && pass) {
    char tmp;
    tmp = 0x05;
    write(socket, &tmp, 1);
    tmp = strlen(user);
    write(socket, &tmp, 1);
    write(socket, &user, tmp);
    tmp = strlen(pass);
    write(socket, &tmp, 1);
    write(socket, &pass, tmp);
    read(socket, &tmp, 1);
    read(socket, &tmp, 1);
    if (tmp)
      return -tmp;
  }
  char IPgreeting[22] = {0x05, 0x01, 0x00};
  char *greeting = IPgreeting;
  char tofree = 0;
  int offset;
  if (inet_pton(AF_INET, host, greeting + 4)) {
    greeting[3] = 0x01;
    offset = 8;
  } else if (inet_pton(AF_INET6, host, greeting + 4)) {
    greeting[3] = 0x04;
    offset = 20;
  } else {
    char len = strlen(host);
    greeting = (char *)malloc(7 + len);
    greeting[0] = 0x05;
    greeting[1] = 0x01;
    greeting[3] = 0x03;
    greeting[4] = len;
    memcpy(greeting + 4, &len, 1);
    memcpy(greeting + 5, host, len);
    offset = len + 5;
    tofree = 1;
  }
  short ns_port = htons(atoi(port));
  memcpy(greeting + offset, &ns_port, 2);
  write(socket, greeting, offset + 2);
  read(socket, greeting, offset + 2);
  char rep = greeting[1];
  if (tofree)
    free(greeting);
  return rep ? -rep : socket;
}


x86 assembly (clang, -O0):
<socks5>:
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)
cmpl   $0x0,-0x8(%rbp)
jge    11da <socks5+0x3a>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10b0 <TCP_Connect@plt>
mov    %eax,-0x4(%rbp)
jmp    14e5 <socks5+0x345>
xor    %eax,%eax
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x56(%rbp)
je     11f5 <socks5+0x55>
cmpq   $0x0,-0x28(%rbp)
setne  %al
mov    %al,-0x56(%rbp)
mov    -0x56(%rbp),%dl
xor    %eax,%eax
mov    $0x2,%ecx
test   $0x1,%dl
cmovne %ecx,%eax
mov    %al,0x2e67(%rip)
mov    -0x8(%rbp),%edi
lea    0x2e5b(%rip),%rsi
mov    $0x3,%edx
mov    $0x0,%al
call   1040 <write@plt>
mov    -0x8(%rbp),%edi
lea    0x2e4d(%rip),%rsi
mov    $0x2,%edx
call   1070 <read@plt>
cmpb   $0x0,0x2e3d(%rip)
je     1255 <socks5+0xb5>
movsbl 0x2e30(%rip),%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    14e5 <socks5+0x345>
cmpq   $0x0,-0x20(%rbp)
je     1325 <socks5+0x185>
cmpq   $0x0,-0x28(%rbp)
je     1325 <socks5+0x185>
movb   $0x5,-0x29(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1040 <write@plt>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %al,-0x29(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1040 <write@plt>
mov    -0x8(%rbp),%edi
movsbl -0x29(%rbp),%edx
lea    -0x20(%rbp),%rsi
mov    $0x0,%al
call   1040 <write@plt>
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    %al,-0x29(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1040 <write@plt>
mov    -0x8(%rbp),%edi
movsbl -0x29(%rbp),%edx
lea    -0x28(%rbp),%rsi
mov    $0x0,%al
call   1040 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    -0x8(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmpb   $0x0,-0x29(%rbp)
je     1320 <socks5+0x180>
movsbl -0x29(%rbp),%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    14e5 <socks5+0x345>
jmp    1325 <socks5+0x185>
mov    0xcd4(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xcd1(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xccf(%rip),%eax
mov    %eax,-0x30(%rbp)
mov    0xcc9(%rip),%ax
mov    %ax,-0x2c(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
movb   $0x0,-0x49(%rbp)
mov    0x2c86(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    -0x48(%rbp),%rdx
add    $0x4,%rdx
call   1090 <inet_pton@plt>
cmp    $0x0,%rax
je     1393 <socks5+0x1f3>
mov    -0x48(%rbp),%rax
movb   $0x1,0x3(%rax)
movl   $0x8,-0x50(%rbp)
jmp    144a <socks5+0x2aa>
mov    0x2c46(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    -0x48(%rbp),%rdx
add    $0x4,%rdx
call   1090 <inet_pton@plt>
cmp    $0x0,%rax
je     13cb <socks5+0x22b>
mov    -0x48(%rbp),%rax
movb   $0x4,0x3(%rax)
movl   $0x14,-0x50(%rbp)
jmp    1445 <socks5+0x2a5>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %al,-0x51(%rbp)
movsbl -0x51(%rbp),%edi
add    $0x7,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
movb   $0x5,(%rax)
mov    -0x48(%rbp),%rax
movb   $0x1,0x1(%rax)
mov    -0x48(%rbp),%rax
movb   $0x3,0x3(%rax)
mov    -0x51(%rbp),%cl
mov    -0x48(%rbp),%rax
mov    %cl,0x4(%rax)
mov    -0x48(%rbp),%rdi
add    $0x4,%rdi
xor    %eax,%eax
lea    -0x51(%rbp),%rsi
mov    $0x1,%edx
call   1080 <memcpy@plt>
mov    -0x48(%rbp),%rdi
add    $0x5,%rdi
mov    -0x10(%rbp),%rsi
movsbl -0x51(%rbp),%edx
xor    %eax,%eax
call   1080 <memcpy@plt>
movsbl -0x51(%rbp),%eax
add    $0x5,%eax
mov    %eax,-0x50(%rbp)
movb   $0x1,-0x49(%rbp)
jmp    144a <socks5+0x2aa>
mov    -0x18(%rbp),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
call   1060 <htons@plt>
mov    %ax,-0x54(%rbp)
mov    -0x48(%rbp),%rdi
movslq -0x50(%rbp),%rax
add    %rax,%rdi
xor    %eax,%eax
lea    -0x54(%rbp),%rsi
mov    $0x2,%edx
call   1080 <memcpy@plt>
mov    -0x8(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    -0x50(%rbp),%edx
add    $0x2,%edx
mov    $0x0,%al
call   1040 <write@plt>
mov    -0x8(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    -0x50(%rbp),%edx
add    $0x2,%edx
call   1070 <read@plt>
mov    -0x48(%rbp),%rax
mov    0x1(%rax),%al
mov    %al,-0x55(%rbp)
cmpb   $0x0,-0x49(%rbp)
je     14bc <socks5+0x31c>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
movsbl -0x55(%rbp),%eax
cmp    $0x0,%eax
je     14d9 <socks5+0x339>
movsbl -0x55(%rbp),%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x5c(%rbp)
jmp    14df <socks5+0x33f>
mov    -0x8(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socks5>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rcx,0x30(%rsp)
mov    %r8,0x28(%rsp)
test   %edi,%edi
js     121a <socks5+0x7a>
mov    %r8,%rbx
mov    %rcx,%rbp
mov    %edi,%r13d
test   %rcx,%rcx
setne  %al
test   %r8,%r8
setne  %cl
and    %al,%cl
add    %cl,%cl
mov    %cl,0x2e91(%rip)
lea    0x2e88(%rip),%rsi
mov    $0x3,%edx
xor    %eax,%eax
call   1040 <write@plt>
lea    0x2e7d(%rip),%rsi
mov    %r13d,%edi
mov    $0x2,%edx
call   1070 <read@plt>
movsbl 0x2e6a(%rip),%eax
test   %eax,%eax
je     1233 <socks5+0x93>
neg    %eax
jmp    1436 <socks5+0x296>
mov    %r15,%rdi
mov    %r14,%rsi
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10b0 <TCP_Connect@plt>
test   %rbp,%rbp
je     12f2 <socks5+0x152>
test   %rbx,%rbx
je     12f2 <socks5+0x152>
movb   $0x5,0x10(%rsp)
lea    0x10(%rsp),%r12
mov    %r13d,%edi
mov    %r12,%rsi
mov    $0x1,%edx
xor    %eax,%eax
call   1040 <write@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %al,0x10(%rsp)
mov    %r13d,%edi
mov    %r12,%rsi
mov    $0x1,%edx
xor    %eax,%eax
call   1040 <write@plt>
movsbl 0x10(%rsp),%edx
lea    0x30(%rsp),%rsi
mov    %r13d,%edi
xor    %eax,%eax
call   1040 <write@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %al,0x10(%rsp)
mov    %r13d,%edi
mov    %r12,%rsi
mov    $0x1,%edx
xor    %eax,%eax
call   1040 <write@plt>
movsbl 0x10(%rsp),%edx
lea    0x28(%rsp),%rsi
mov    %r13d,%edi
xor    %eax,%eax
call   1040 <write@plt>
mov    %r13d,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %r13d,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
movsbl 0x10(%rsp),%eax
test   %eax,%eax
jne    1213 <socks5+0x73>
mov    0xd15(%rip),%rax
mov    %rax,0x1e(%rsp)
movaps 0xcfb(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
mov    0x2cd7(%rip),%rax
mov    (%rax),%edi
lea    0x14(%rsp),%rbx
mov    %r15,%rsi
mov    %rbx,%rdx
call   1090 <inet_pton@plt>
test   %rax,%rax
je     133f <socks5+0x19f>
lea    0x10(%rsp),%r12
movb   $0x1,0x13(%rsp)
mov    $0x8,%ebx
mov    $0x1,%bpl
jmp    13ce <socks5+0x22e>
mov    0x2c9a(%rip),%rax
mov    (%rax),%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1090 <inet_pton@plt>
test   %rax,%rax
je     136c <socks5+0x1cc>
lea    0x10(%rsp),%r12
movb   $0x4,0x13(%rsp)
mov    $0x14,%ebx
mov    $0x1,%bpl
jmp    13ce <socks5+0x22e>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %al,0xe(%rsp)
movsbl %al,%edi
add    $0x7,%edi
call   10a0 <malloc@plt>
mov    %rax,%r12
movw   $0x105,(%rax)
movb   $0x3,0x3(%rax)
mov    0xe(%rsp),%al
lea    0x4(%r12),%rdi
mov    %al,0x4(%r12)
xor    %ebp,%ebp
lea    0xe(%rsp),%rsi
mov    $0x1,%edx
xor    %eax,%eax
call   1080 <memcpy@plt>
mov    %r12,%rdi
add    $0x5,%rdi
movsbl 0xe(%rsp),%edx
mov    %r15,%rsi
xor    %eax,%eax
call   1080 <memcpy@plt>
movsbl 0xe(%rsp),%ebx
add    $0x5,%ebx
mov    %r14,%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
call   1060 <htons@plt>
mov    %ax,0xe(%rsp)
movslq %ebx,%rbx
lea    (%r12,%rbx,1),%rdi
lea    0xe(%rsp),%rsi
mov    $0x2,%edx
xor    %eax,%eax
call   1080 <memcpy@plt>
add    $0x2,%ebx
mov    %r13d,%edi
mov    %r12,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <write@plt>
mov    %r13d,%edi
mov    %r12,%rsi
mov    %ebx,%edx
call   1070 <read@plt>
movsbl 0x1(%r12),%ebx
test   %bpl,%bpl
jne    142c <socks5+0x28c>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
neg    %eax
test   %bl,%bl
cmove  %r13d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@socks5.auth = internal global [3 x i8] c"\05\01\00"
@socks5.res = internal global [2 x i8] zeroinitializer
@__const.socks5.IPgreeting = <{ i8, i8, [20 x i8] }> <{ i8 5, i8 1, [20 x i8] zeroinitializer }>
@AF_INET = global i32 0
@AF_INET6 = global i32 0
define i32 @socks5(i32 %0, i8* %1, i8* %2, i8* %3, i8* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8
%13 = alloca [22 x i8]
%14 = alloca i8*
%15 = alloca i8
%16 = alloca i32
%17 = alloca i8
%18 = alloca i16
%19 = alloca i8
store i32 %0, i32* %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i8* %4, i8** %11
%20 = load i32, i32* %7
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %5
%23 = load i8*, i8** %8
%24 = load i8*, i8** %9
%25 = call i32 @TCP_Connect(i8* %23, i8* %24)
store i32 %25, i32* %6
br label %174
26: ; preds = %5
%27 = load i8*, i8** %10
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i8*, i8** %11
%31 = icmp ne i8* %30, null
br label %32
32: ; preds = %29, %26
%33 = phi i1 [ false, %26 ], [ %31, %29 ]
%34 = zext i1 %33 to i64
%35 = select i1 %33, i32 2, i32 0
%36 = trunc i32 %35 to i8
store i8 %36, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @socks5.auth, i64 0, i64 2)
%37 = load i32, i32* %7
%38 = call i32 (i32, ...) @write(i32 %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @socks5.auth, i64 0, i64 0), i32 3)
%39 = load i32, i32* %7
%40 = call i32 @read(i32 %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @socks5.res, i64 0, i64 0), i32 2)
%41 = load i8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @socks5.res, i64 0, i64 1)
%42 = icmp ne i8 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %32
%44 = load i8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @socks5.res, i64 0, i64 1)
%45 = sext i8 %44 to i32
%46 = sub nsw i32 0, %45
store i32 %46, i32* %6
br label %174
47: ; preds = %32
%48 = load i8*, i8** %10
%49 = icmp ne i8* %48, null
br i1 %49, label %50, label %83
50: ; preds = %47
%51 = load i8*, i8** %11
%52 = icmp ne i8* %51, null
br i1 %52, label %53, label %83
53: ; preds = %50
store i8 5, i8* %12
%54 = load i32, i32* %7
%55 = call i32 (i32, ...) @write(i32 %54, i8* %12, i32 1)
%56 = load i8*, i8** %10
%57 = call signext i8 @strlen(i8* %56)
store i8 %57, i8* %12
%58 = load i32, i32* %7
%59 = call i32 (i32, ...) @write(i32 %58, i8* %12, i32 1)
%60 = load i32, i32* %7
%61 = load i8, i8* %12
%62 = sext i8 %61 to i32
%63 = call i32 (i32, ...) @write(i32 %60, i8** %10, i32 %62)
%64 = load i8*, i8** %11
%65 = call signext i8 @strlen(i8* %64)
store i8 %65, i8* %12
%66 = load i32, i32* %7
%67 = call i32 (i32, ...) @write(i32 %66, i8* %12, i32 1)
%68 = load i32, i32* %7
%69 = load i8, i8* %12
%70 = sext i8 %69 to i32
%71 = call i32 (i32, ...) @write(i32 %68, i8** %11, i32 %70)
%72 = load i32, i32* %7
%73 = call i32 @read(i32 %72, i8* %12, i32 1)
%74 = load i32, i32* %7
%75 = call i32 @read(i32 %74, i8* %12, i32 1)
%76 = load i8, i8* %12
%77 = icmp ne i8 %76, 0
br i1 %77, label %78, label %82
78: ; preds = %53
%79 = load i8, i8* %12
%80 = sext i8 %79 to i32
%81 = sub nsw i32 0, %80
store i32 %81, i32* %6
br label %174
82: ; preds = %53
br label %83
83: ; preds = %82, %50, %47
%84 = bitcast [22 x i8]* %13 to i8*
%85 = getelementptr inbounds [22 x i8], [22 x i8]* %13, i64 0, i64 0
store i8* %85, i8** %14
store i8 0, i8* %15
%86 = load i32, i32* @AF_INET
%87 = load i8*, i8** %8
%88 = load i8*, i8** %14
%89 = getelementptr inbounds i8, i8* %88, i64 4
%90 = call i64 @inet_pton(i32 %86, i8* %87, i8* %89)
%91 = icmp ne i64 %90, 0
br i1 %91, label %92, label %95
92: ; preds = %83
%93 = load i8*, i8** %14
%94 = getelementptr inbounds i8, i8* %93, i64 3
store i8 1, i8* %94
store i32 8, i32* %16
br label %135
95: ; preds = %83
%96 = load i32, i32* @AF_INET6
%97 = load i8*, i8** %8
%98 = load i8*, i8** %14
%99 = getelementptr inbounds i8, i8* %98, i64 4
%100 = call i64 @inet_pton(i32 %96, i8* %97, i8* %99)
%101 = icmp ne i64 %100, 0
br i1 %101, label %102, label %105
102: ; preds = %95
%103 = load i8*, i8** %14
%104 = getelementptr inbounds i8, i8* %103, i64 3
store i8 4, i8* %104
store i32 20, i32* %16
br label %134
105: ; preds = %95
%106 = load i8*, i8** %8
%107 = call signext i8 @strlen(i8* %106)
store i8 %107, i8* %17
%108 = load i8, i8* %17
%109 = sext i8 %108 to i32
%110 = add nsw i32 7, %109
%111 = call i64 @malloc(i32 %110)
%112 = inttoptr i64 %111 to i8*
store i8* %112, i8** %14
%113 = load i8*, i8** %14
%114 = getelementptr inbounds i8, i8* %113, i64 0
store i8 5, i8* %114
%115 = load i8*, i8** %14
%116 = getelementptr inbounds i8, i8* %115, i64 1
store i8 1, i8* %116
%117 = load i8*, i8** %14
%118 = getelementptr inbounds i8, i8* %117, i64 3
store i8 3, i8* %118
%119 = load i8, i8* %17
%120 = load i8*, i8** %14
%121 = getelementptr inbounds i8, i8* %120, i64 4
store i8 %119, i8* %121
%122 = load i8*, i8** %14
%123 = getelementptr inbounds i8, i8* %122, i64 4
%124 = call i32 (i8*, ...) @memcpy(i8* %123, i8* %17, i32 1)
%125 = load i8*, i8** %14
%126 = getelementptr inbounds i8, i8* %125, i64 5
%127 = load i8*, i8** %8
%128 = load i8, i8* %17
%129 = sext i8 %128 to i32
%130 = call i32 (i8*, ...) @memcpy(i8* %126, i8* %127, i32 %129)
%131 = load i8, i8* %17
%132 = sext i8 %131 to i32
%133 = add nsw i32 %132, 5
store i32 %133, i32* %16
store i8 1, i8* %15
br label %134
134: ; preds = %105, %102
br label %135
135: ; preds = %134, %92
%136 = load i8*, i8** %9
%137 = call i32 @atoi(i8* %136)
%138 = call signext i16 @htons(i32 %137)
store i16 %138, i16* %18
%139 = load i8*, i8** %14
%140 = load i32, i32* %16
%141 = sext i32 %140 to i64
%142 = getelementptr inbounds i8, i8* %139, i64 %141
%143 = call i32 (i8*, ...) @memcpy(i8* %142, i16* %18, i32 2)
%144 = load i32, i32* %7
%145 = load i8*, i8** %14
%146 = load i32, i32* %16
%147 = add nsw i32 %146, 2
%148 = call i32 (i32, ...) @write(i32 %144, i8* %145, i32 %147)
%149 = load i32, i32* %7
%150 = load i8*, i8** %14
%151 = load i32, i32* %16
%152 = add nsw i32 %151, 2
%153 = call i32 @read(i32 %149, i8* %150, i32 %152)
%154 = load i8*, i8** %14
%155 = getelementptr inbounds i8, i8* %154, i64 1
%156 = load i8, i8* %155
store i8 %156, i8* %19
%157 = load i8, i8* %15
%158 = icmp ne i8 %157, 0
br i1 %158, label %159, label %162
159: ; preds = %135
%160 = load i8*, i8** %14
%161 = call i32 @free(i8* %160)
br label %162
162: ; preds = %159, %135
%163 = load i8, i8* %19
%164 = sext i8 %163 to i32
%165 = icmp ne i32 %164, 0
br i1 %165, label %166, label %170
166: ; preds = %162
%167 = load i8, i8* %19
%168 = sext i8 %167 to i32
%169 = sub nsw i32 0, %168
br label %172
170: ; preds = %162
%171 = load i32, i32* %7
br label %172
172: ; preds = %170, %166
%173 = phi i32 [ %169, %166 ], [ %171, %170 ]
store i32 %173, i32* %6
br label %174
174: ; preds = %172, %78, %43, %22
%175 = load i32, i32* %6
ret i32 %175
}

LLVM IR (-O2):
@socks5.auth = internal global [3 x i8] c"\05\01\00"
@socks5.res = internal global [2 x i8] zeroinitializer
@__const.socks5.IPgreeting = <{ i8, i8, [20 x i8] }> <{ i8 5, i8 1, [20 x i8] zeroinitializer }>
@AF_INET = global i32 0
@AF_INET6 = global i32 0
define i32 @socks5(i32 %0, i8* %1, i8* %2, i8* %3, i8* %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8
%9 = alloca [22 x i8]
%10 = alloca i8
%11 = alloca i16
store i8* %3, i8** %6
store i8* %4, i8** %7
%12 = icmp slt i32 %0, 0
br i1 %12, label %13, label %15
13: ; preds = %5
%14 = tail call i32 @TCP_Connect(i8* %1, i8* %2)
br label %101
15: ; preds = %5
%16 = icmp ne i8* %3, null
%17 = icmp ne i8* %4, null
%18 = and i1 %16, %17
%19 = select i1 %18, i8 2, i8 0
store i8 %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @socks5.auth, i64 0, i64 2)
%20 = tail call i32 (i32, ...) @write(i32 %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @socks5.auth, i64 0, i64 0), i32 3)
%21 = tail call i32 @read(i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @socks5.res, i64 0, i64 0), i32 2)
%22 = load i8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @socks5.res, i64 0, i64 1)
%23 = icmp eq i8 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %15
%25 = sext i8 %22 to i32
%26 = sub nsw i32 0, %25
br label %101
27: ; preds = %15
%28 = and i1 %16, %17
br i1 %28, label %29, label %47
29: ; preds = %27
store i8 5, i8* %8
%30 = call i32 (i32, ...) @write(i32 %0, i8* %8, i32 1)
%31 = call signext i8 @strlen(i8* %3)
store i8 %31, i8* %8
%32 = call i32 (i32, ...) @write(i32 %0, i8* %8, i32 1)
%33 = load i8, i8* %8
%34 = sext i8 %33 to i32
%35 = call i32 (i32, ...) @write(i32 %0, i8** %6, i32 %34)
%36 = call signext i8 @strlen(i8* %4)
store i8 %36, i8* %8
%37 = call i32 (i32, ...) @write(i32 %0, i8* %8, i32 1)
%38 = load i8, i8* %8
%39 = sext i8 %38 to i32
%40 = call i32 (i32, ...) @write(i32 %0, i8** %7, i32 %39)
%41 = call i32 @read(i32 %0, i8* %8, i32 1)
%42 = call i32 @read(i32 %0, i8* %8, i32 1)
%43 = load i8, i8* %8
%44 = icmp eq i8 %43, 0
%45 = sext i8 %43 to i32
%46 = sub nsw i32 0, %45
br i1 %44, label %47, label %101
47: ; preds = %29, %27
%48 = getelementptr inbounds [22 x i8], [22 x i8]* %9, i64 0, i64 0
%49 = load i32, i32* @AF_INET
%50 = getelementptr inbounds [22 x i8], [22 x i8]* %9, i64 0, i64 4
%51 = call i64 @inet_pton(i32 %49, i8* %1, i8* %50)
%52 = icmp eq i64 %51, 0
br i1 %52, label %55, label %53
53: ; preds = %47
%54 = getelementptr inbounds [22 x i8], [22 x i8]* %9, i64 0, i64 3
store i8 1, i8* %54
br label %79
55: ; preds = %47
%56 = load i32, i32* @AF_INET6
%57 = call i64 @inet_pton(i32 %56, i8* %1, i8* %50)
%58 = icmp eq i64 %57, 0
br i1 %58, label %61, label %59
59: ; preds = %55
%60 = getelementptr inbounds [22 x i8], [22 x i8]* %9, i64 0, i64 3
store i8 4, i8* %60
br label %79
61: ; preds = %55
%62 = call signext i8 @strlen(i8* %1)
store i8 %62, i8* %10
%63 = sext i8 %62 to i32
%64 = add nsw i32 %63, 7
%65 = call i64 @malloc(i32 %64)
%66 = inttoptr i64 %65 to i8*
store i8 5, i8* %66
%67 = getelementptr inbounds i8, i8* %66, i64 1
store i8 1, i8* %67
%68 = getelementptr inbounds i8, i8* %66, i64 3
store i8 3, i8* %68
%69 = load i8, i8* %10
%70 = getelementptr inbounds i8, i8* %66, i64 4
store i8 %69, i8* %70
%71 = call i32 (i8*, ...) @memcpy(i8* %70, i8* %10, i32 1)
%72 = getelementptr inbounds i8, i8* %66, i64 5
%73 = load i8, i8* %10
%74 = sext i8 %73 to i32
%75 = call i32 (i8*, ...) @memcpy(i8* %72, i8* %1, i32 %74)
%76 = load i8, i8* %10
%77 = sext i8 %76 to i32
%78 = add nsw i32 %77, 5
br label %79
79: ; preds = %59, %61, %53
%80 = phi i8* [ %48, %53 ], [ %48, %59 ], [ %66, %61 ]
%81 = phi i1 [ true, %53 ], [ true, %59 ], [ false, %61 ]
%82 = phi i32 [ 8, %53 ], [ 20, %59 ], [ %78, %61 ]
%83 = bitcast i16* %11 to i8*
%84 = call i32 @atoi(i8* %2)
%85 = call signext i16 @htons(i32 %84)
store i16 %85, i16* %11
%86 = sext i32 %82 to i64
%87 = getelementptr inbounds i8, i8* %80, i64 %86
%88 = call i32 (i8*, ...) @memcpy(i8* %87, i16* %11, i32 2)
%89 = add nsw i32 %82, 2
%90 = call i32 (i32, ...) @write(i32 %0, i8* %80, i32 %89)
%91 = call i32 @read(i32 %0, i8* %80, i32 %89)
%92 = getelementptr inbounds i8, i8* %80, i64 1
%93 = load i8, i8* %92
br i1 %81, label %96, label %94
94: ; preds = %79
%95 = call i32 @free(i8* %80)
br label %96
96: ; preds = %94, %79
%97 = sext i8 %93 to i32
%98 = icmp eq i8 %93, 0
%99 = sub nsw i32 0, %97
%100 = select i1 %98, i32 %0, i32 %99
br label %101
101: ; preds = %29, %96, %24, %13
%102 = phi i32 [ %14, %13 ], [ %26, %24 ], [ %46, %29 ], [ %100, %96 ]
ret i32 %102
}