#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 TCP_Connect(char *, char *);
int /*<<< orphan*/ atoi(char *);
short htons(int /*<<< orphan*/);
char inet_pton(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ memcpy(char *, short *, int);
int /*<<< orphan*/ read(int, char *, int);
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
int socks4(int socket, char *host, char *port, char *user, char *pass) {
  (void)user;
  (void)pass;
  if (socket < 0)
    return TCP_Connect(host, port);
  static char greeting[9] = {0x04, 0x01};
  static char buffer[8];
  char isIP = inet_pton(AF_INET, host, greeting + 4);
  if (!isIP) {
    greeting[4] = 0;
    greeting[5] = 0;
    greeting[6] = 0;
    greeting[7] = 1;
  }
  short ns_port = htons(atoi(port));
  memcpy(greeting + 2, &ns_port, 2);
  write(socket, greeting, 9);
  if (!isIP)
    write(socket, host, strlen(host) + 1);
  read(socket, buffer, 8);
  return buffer[1] == 0x5a ? socket : -buffer[1];
  ;
}


x86 assembly (clang, -O0):
<socks4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%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    11ba <socks4+0x3a>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <TCP_Connect@plt>
mov    %eax,-0x4(%rbp)
jmp    12b2 <socks4+0x132>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    0x2e92(%rip),%rdx
add    $0x4,%rdx
call   1080 <inet_pton@plt>
mov    %al,-0x29(%rbp)
cmpb   $0x0,-0x29(%rbp)
jne    1200 <socks4+0x80>
movb   $0x0,0x2e79(%rip)
movb   $0x0,0x2e73(%rip)
movb   $0x0,0x2e6d(%rip)
movb   $0x1,0x2e67(%rip)
mov    -0x18(%rbp),%rdi
call   10a0 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %ax,-0x2c(%rbp)
lea    0x2e47(%rip),%rdi
lea    -0x2c(%rbp),%rsi
mov    $0x2,%edx
call   1070 <memcpy@plt>
mov    -0x8(%rbp),%edi
lea    0x2e2d(%rip),%rsi
mov    $0x9,%edx
call   1030 <write@plt>
cmpb   $0x0,-0x29(%rbp)
jne    126f <socks4+0xef>
mov    -0x8(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
lea    0x2df7(%rip),%rsi
mov    $0x8,%edx
call   1060 <read@plt>
movsbl 0x2de7(%rip),%eax
cmp    $0x5a,%eax
jne    129e <socks4+0x11e>
mov    -0x8(%rbp),%eax
mov    %eax,-0x40(%rbp)
jmp    12ac <socks4+0x12c>
movsbl 0x2dcc(%rip),%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socks4>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbp
mov    %rsi,%r14
test   %edi,%edi
js     1246 <socks4+0xc6>
mov    %edi,%ebx
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
lea    0x2ebd(%rip),%rdx
mov    %r14,%rsi
call   1080 <inet_pton@plt>
mov    %eax,%r15d
test   %al,%al
jne    11c0 <socks4+0x40>
movl   $0x1000000,0x2ea4(%rip)
mov    %rbp,%rdi
call   10a0 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %ax,0x6(%rsp)
lea    0x2e87(%rip),%rdi
lea    0x6(%rsp),%rsi
mov    $0x2,%edx
call   1070 <memcpy@plt>
lea    0x2e6f(%rip),%rsi
mov    %ebx,%edi
mov    $0x9,%edx
call   1030 <write@plt>
test   %r15b,%r15b
jne    1217 <socks4+0x97>
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%edx
mov    %ebx,%edi
mov    %r14,%rsi
call   1030 <write@plt>
lea    0x2e52(%rip),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1060 <read@plt>
movsbl 0x2e40(%rip),%ecx
mov    %ecx,%eax
neg    %eax
cmp    $0x5a,%ecx
cmove  %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
mov    %rbp,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <TCP_Connect@plt>

LLVM IR (-O0):
@socks4.greeting = internal global [9 x i8] c"\04\01\00\00\00\00\00\00\00"
@socks4.buffer = internal global [8 x i8] zeroinitializer
@AF_INET = global i32 0
define i32 @socks4(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 i16
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
%14 = load i8*, i8** %10
%15 = load i8*, i8** %11
%16 = load i32, i32* %7
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %5
%19 = load i8*, i8** %8
%20 = load i8*, i8** %9
%21 = call i32 @TCP_Connect(i8* %19, i8* %20)
store i32 %21, i32* %6
br label %59
22: ; preds = %5
%23 = load i32, i32* @AF_INET
%24 = load i8*, i8** %8
%25 = call signext i8 @inet_pton(i32 %23, i8* %24, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @socks4.greeting, i64 0, i64 4))
store i8 %25, i8* %12
%26 = load i8, i8* %12
%27 = icmp ne i8 %26, 0
br i1 %27, label %29, label %28
28: ; preds = %22
store i8 0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @socks4.greeting, i64 0, i64 4)
store i8 0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @socks4.greeting, i64 0, i64 5)
store i8 0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @socks4.greeting, i64 0, i64 6)
store i8 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @socks4.greeting, i64 0, i64 7)
br label %29
29: ; preds = %28, %22
%30 = load i8*, i8** %9
%31 = call i32 @atoi(i8* %30)
%32 = call signext i16 @htons(i32 %31)
store i16 %32, i16* %13
%33 = call i32 @memcpy(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @socks4.greeting, i64 0, i64 2), i16* %13, i32 2)
%34 = load i32, i32* %7
%35 = call i32 @write(i32 %34, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @socks4.greeting, i64 0, i64 0), i32 9)
%36 = load i8, i8* %12
%37 = icmp ne i8 %36, 0
br i1 %37, label %45, label %38
38: ; preds = %29
%39 = load i32, i32* %7
%40 = load i8*, i8** %8
%41 = load i8*, i8** %8
%42 = call i32 @strlen(i8* %41)
%43 = add nsw i32 %42, 1
%44 = call i32 @write(i32 %39, i8* %40, i32 %43)
br label %45
45: ; preds = %38, %29
%46 = load i32, i32* %7
%47 = call i32 @read(i32 %46, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @socks4.buffer, i64 0, i64 0), i32 8)
%48 = load i8, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @socks4.buffer, i64 0, i64 1)
%49 = sext i8 %48 to i32
%50 = icmp eq i32 %49, 90
br i1 %50, label %51, label %53
51: ; preds = %45
%52 = load i32, i32* %7
br label %57
53: ; preds = %45
%54 = load i8, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @socks4.buffer, i64 0, i64 1)
%55 = sext i8 %54 to i32
%56 = sub nsw i32 0, %55
br label %57
57: ; preds = %53, %51
%58 = phi i32 [ %52, %51 ], [ %56, %53 ]
store i32 %58, i32* %6
br label %59
59: ; preds = %57, %18
%60 = load i32, i32* %6
ret i32 %60
}

LLVM IR (-O2):
@socks4.greeting = internal global [9 x i8] c"\04\01\00\00\00\00\00\00\00"
@socks4.buffer = internal global [8 x i8] zeroinitializer
@AF_INET = global i32 0
define i32 @socks4(i32 %0, i8* %1, i8* %2, i8* nocapture readnone %3, i8* nocapture readnone %4) {
%6 = alloca i16
%7 = icmp slt i32 %0, 0
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @TCP_Connect(i8* %1, i8* %2)
br label %32
10: ; preds = %5
%11 = load i32, i32* @AF_INET
%12 = tail call signext i8 @inet_pton(i32 %11, i8* %1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @socks4.greeting, i64 0, i64 4))
%13 = icmp eq i8 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %10
store i8 0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @socks4.greeting, i64 0, i64 4)
store i8 0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @socks4.greeting, i64 0, i64 5)
store i8 0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @socks4.greeting, i64 0, i64 6)
store i8 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @socks4.greeting, i64 0, i64 7)
br label %15
15: ; preds = %14, %10
%16 = bitcast i16* %6 to i8*
%17 = tail call i32 @atoi(i8* %2)
%18 = tail call signext i16 @htons(i32 %17)
store i16 %18, i16* %6
%19 = call i32 @memcpy(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @socks4.greeting, i64 0, i64 2), i16* %6, i32 2)
%20 = call i32 @write(i32 %0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @socks4.greeting, i64 0, i64 0), i32 9)
br i1 %13, label %21, label %25
21: ; preds = %15
%22 = call i32 @strlen(i8* %1)
%23 = add nsw i32 %22, 1
%24 = call i32 @write(i32 %0, i8* %1, i32 %23)
br label %25
25: ; preds = %21, %15
%26 = call i32 @read(i32 %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @socks4.buffer, i64 0, i64 0), i32 8)
%27 = load i8, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @socks4.buffer, i64 0, i64 1)
%28 = sext i8 %27 to i32
%29 = icmp eq i8 %27, 90
%30 = sub nsw i32 0, %28
%31 = select i1 %29, i32 %0, i32 %30
br label %32
32: ; preds = %25, %8
%33 = phi i32 [ %9, %8 ], [ %31, %25 ]
ret i32 %33
}