#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 read(int, unsigned char *, int);
unsigned char strlen(char const *);
unsigned char write(int, ...);
int hs_socks5(int sockfd, const char *user, const char *pass) {
  unsigned char buf[16];
  buf[0] = 0x05;
  if (write(sockfd, buf, 1) != 1)
    return -1;
  buf[0] = 2;
  if (write(sockfd, buf, 1) != 1)
    return -1;
  buf[0] = 0x00;
  buf[1] = 0x02;
  if (write(sockfd, buf, 2) != 2)
    return -1;
  if (read(sockfd, buf, 1) != 1)
    return -1;
  if (buf[0] != 0x05)
    return -1;
  if (read(sockfd, buf, 1) != 1)
    return -1;
  switch (buf[0]) {
  case 0x00:
    break;
  case 0x02:
    if (user == NULL || pass == NULL)
      return -1;
    buf[0] = 0x01;
    if (write(sockfd, buf, 1) != 1)
      return -1;
    buf[0] = strlen(user);
    if (write(sockfd, buf, 1) != 1)
      return -1;
    if (write(sockfd, user, buf[0]) != buf[0])
      return -1;
    buf[0] = strlen(pass);
    if (write(sockfd, buf, 1) != 1)
      return -1;
    if (write(sockfd, pass, buf[0]) != buf[0])
      return -1;
    if (read(sockfd, buf, 2) != 2)
      return -1;
    if (buf[1] != 0x00)
      return -1;
    break;
  default:
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<hs_socks5>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movb   $0x5,-0x30(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1030 <write@plt>
movzbl %al,%eax
cmp    $0x1,%eax
je     1172 <hs_socks5+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d3 <hs_socks5+0x2a3>
movb   $0x2,-0x30(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1030 <write@plt>
movzbl %al,%eax
cmp    $0x1,%eax
je     11a1 <hs_socks5+0x71>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d3 <hs_socks5+0x2a3>
movb   $0x0,-0x30(%rbp)
movb   $0x2,-0x2f(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x2,%edx
mov    $0x0,%al
call   1030 <write@plt>
movzbl %al,%eax
cmp    $0x2,%eax
je     11d4 <hs_socks5+0xa4>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d3 <hs_socks5+0x2a3>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
je     11fa <hs_socks5+0xca>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d3 <hs_socks5+0x2a3>
movzbl -0x30(%rbp),%eax
cmp    $0x5,%eax
je     1213 <hs_socks5+0xe3>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d3 <hs_socks5+0x2a3>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
je     1239 <hs_socks5+0x109>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d3 <hs_socks5+0x2a3>
movzbl -0x30(%rbp),%eax
mov    %eax,-0x34(%rbp)
test   %eax,%eax
je     125e <hs_socks5+0x12e>
jmp    124d <hs_socks5+0x11d>
mov    -0x34(%rbp),%eax
sub    $0x2,%eax
je     1263 <hs_socks5+0x133>
jmp    13c0 <hs_socks5+0x290>
jmp    13cc <hs_socks5+0x29c>
cmpq   $0x0,-0x10(%rbp)
je     1279 <hs_socks5+0x149>
cmpq   $0x0,-0x18(%rbp)
jne    1285 <hs_socks5+0x155>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d3 <hs_socks5+0x2a3>
movb   $0x1,-0x30(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1030 <write@plt>
movzbl %al,%eax
cmp    $0x1,%eax
je     12b4 <hs_socks5+0x184>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d3 <hs_socks5+0x2a3>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %al,-0x30(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1030 <write@plt>
movzbl %al,%eax
cmp    $0x1,%eax
je     12eb <hs_socks5+0x1bb>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d3 <hs_socks5+0x2a3>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movzbl -0x30(%rbp),%edx
mov    $0x0,%al
call   1030 <write@plt>
movzbl %al,%eax
movzbl -0x30(%rbp),%ecx
cmp    %ecx,%eax
je     1318 <hs_socks5+0x1e8>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d3 <hs_socks5+0x2a3>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %al,-0x30(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1030 <write@plt>
movzbl %al,%eax
cmp    $0x1,%eax
je     134f <hs_socks5+0x21f>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d3 <hs_socks5+0x2a3>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
movzbl -0x30(%rbp),%edx
mov    $0x0,%al
call   1030 <write@plt>
movzbl %al,%eax
movzbl -0x30(%rbp),%ecx
cmp    %ecx,%eax
je     137c <hs_socks5+0x24c>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d3 <hs_socks5+0x2a3>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x2,%edx
call   1050 <read@plt>
cmp    $0x2,%eax
je     13a2 <hs_socks5+0x272>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d3 <hs_socks5+0x2a3>
movzbl -0x2f(%rbp),%eax
cmp    $0x0,%eax
je     13bb <hs_socks5+0x28b>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d3 <hs_socks5+0x2a3>
jmp    13cc <hs_socks5+0x29c>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d3 <hs_socks5+0x2a3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hs_socks5>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
movb   $0x5,(%rsp)
mov    %rsp,%rsi
mov    $0x1,%edx
xor    %eax,%eax
call   1030 <write@plt>
mov    $0xffffffff,%ebx
cmp    $0x1,%al
jne    12a1 <hs_socks5+0x171>
movb   $0x2,(%rsp)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x1,%edx
xor    %eax,%eax
call   1030 <write@plt>
cmp    $0x1,%al
jne    12a1 <hs_socks5+0x171>
movw   $0x200,(%rsp)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x2,%edx
xor    %eax,%eax
call   1030 <write@plt>
cmp    $0x2,%al
jne    12a1 <hs_socks5+0x171>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
jne    12a1 <hs_socks5+0x171>
cmpb   $0x5,(%rsp)
jne    12a1 <hs_socks5+0x171>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
jne    12a1 <hs_socks5+0x171>
mov    (%rsp),%al
test   %al,%al
je     129f <hs_socks5+0x16f>
cmp    $0x2,%al
jne    12a1 <hs_socks5+0x171>
test   %r15,%r15
je     12a1 <hs_socks5+0x171>
test   %r14,%r14
je     12a1 <hs_socks5+0x171>
movb   $0x1,(%rsp)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x1,%edx
xor    %eax,%eax
call   1030 <write@plt>
cmp    $0x1,%al
jne    12a1 <hs_socks5+0x171>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %al,(%rsp)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x1,%edx
xor    %eax,%eax
call   1030 <write@plt>
cmp    $0x1,%al
jne    12a1 <hs_socks5+0x171>
movzbl (%rsp),%edx
mov    %ebp,%edi
mov    %r15,%rsi
xor    %eax,%eax
call   1030 <write@plt>
cmp    (%rsp),%al
jne    12a1 <hs_socks5+0x171>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %al,(%rsp)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x1,%edx
xor    %eax,%eax
call   1030 <write@plt>
cmp    $0x1,%al
jne    12a1 <hs_socks5+0x171>
movzbl (%rsp),%edx
mov    %ebp,%edi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <write@plt>
cmp    (%rsp),%al
jne    12a1 <hs_socks5+0x171>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1050 <read@plt>
cmp    $0x2,%eax
jne    12a1 <hs_socks5+0x171>
cmpb   $0x0,0x1(%rsp)
jne    12a1 <hs_socks5+0x171>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @hs_socks5(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [16 x i8]
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%9 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
store i8 5, i8* %9
%10 = load i32, i32* %5
%11 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%12 = call zeroext i8 (i32, ...) @write(i32 %10, i8* %11, i32 1)
%13 = zext i8 %12 to i32
%14 = icmp ne i32 %13, 1
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 -1, i32* %4
br label %132
16: ; preds = %3
%17 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
store i8 2, i8* %17
%18 = load i32, i32* %5
%19 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%20 = call zeroext i8 (i32, ...) @write(i32 %18, i8* %19, i32 1)
%21 = zext i8 %20 to i32
%22 = icmp ne i32 %21, 1
br i1 %22, label %23, label %24
23: ; preds = %16
store i32 -1, i32* %4
br label %132
24: ; preds = %16
%25 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
store i8 0, i8* %25
%26 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 1
store i8 2, i8* %26
%27 = load i32, i32* %5
%28 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%29 = call zeroext i8 (i32, ...) @write(i32 %27, i8* %28, i32 2)
%30 = zext i8 %29 to i32
%31 = icmp ne i32 %30, 2
br i1 %31, label %32, label %33
32: ; preds = %24
store i32 -1, i32* %4
br label %132
33: ; preds = %24
%34 = load i32, i32* %5
%35 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%36 = call i32 @read(i32 %34, i8* %35, i32 1)
%37 = icmp ne i32 %36, 1
br i1 %37, label %38, label %39
38: ; preds = %33
store i32 -1, i32* %4
br label %132
39: ; preds = %33
%40 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%41 = load i8, i8* %40
%42 = zext i8 %41 to i32
%43 = icmp ne i32 %42, 5
br i1 %43, label %44, label %45
44: ; preds = %39
store i32 -1, i32* %4
br label %132
45: ; preds = %39
%46 = load i32, i32* %5
%47 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%48 = call i32 @read(i32 %46, i8* %47, i32 1)
%49 = icmp ne i32 %48, 1
br i1 %49, label %50, label %51
50: ; preds = %45
store i32 -1, i32* %4
br label %132
51: ; preds = %45
%52 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%53 = load i8, i8* %52
%54 = zext i8 %53 to i32
switch i32 %54, label %130 [
i32 0, label %55
i32 2, label %56
]
55: ; preds = %51
br label %131
56: ; preds = %51
%57 = load i8*, i8** %6
%58 = icmp eq i8* %57, null
br i1 %58, label %62, label %59
59: ; preds = %56
%60 = load i8*, i8** %7
%61 = icmp eq i8* %60, null
br i1 %61, label %62, label %63
62: ; preds = %59, %56
store i32 -1, i32* %4
br label %132
63: ; preds = %59
%64 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
store i8 1, i8* %64
%65 = load i32, i32* %5
%66 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%67 = call zeroext i8 (i32, ...) @write(i32 %65, i8* %66, i32 1)
%68 = zext i8 %67 to i32
%69 = icmp ne i32 %68, 1
br i1 %69, label %70, label %71
70: ; preds = %63
store i32 -1, i32* %4
br label %132
71: ; preds = %63
%72 = load i8*, i8** %6
%73 = call zeroext i8 @strlen(i8* %72)
%74 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
store i8 %73, i8* %74
%75 = load i32, i32* %5
%76 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%77 = call zeroext i8 (i32, ...) @write(i32 %75, i8* %76, i32 1)
%78 = zext i8 %77 to i32
%79 = icmp ne i32 %78, 1
br i1 %79, label %80, label %81
80: ; preds = %71
store i32 -1, i32* %4
br label %132
81: ; preds = %71
%82 = load i32, i32* %5
%83 = load i8*, i8** %6
%84 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%85 = load i8, i8* %84
%86 = zext i8 %85 to i32
%87 = call zeroext i8 (i32, ...) @write(i32 %82, i8* %83, i32 %86)
%88 = zext i8 %87 to i32
%89 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%90 = load i8, i8* %89
%91 = zext i8 %90 to i32
%92 = icmp ne i32 %88, %91
br i1 %92, label %93, label %94
93: ; preds = %81
store i32 -1, i32* %4
br label %132
94: ; preds = %81
%95 = load i8*, i8** %7
%96 = call zeroext i8 @strlen(i8* %95)
%97 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
store i8 %96, i8* %97
%98 = load i32, i32* %5
%99 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%100 = call zeroext i8 (i32, ...) @write(i32 %98, i8* %99, i32 1)
%101 = zext i8 %100 to i32
%102 = icmp ne i32 %101, 1
br i1 %102, label %103, label %104
103: ; preds = %94
store i32 -1, i32* %4
br label %132
104: ; preds = %94
%105 = load i32, i32* %5
%106 = load i8*, i8** %7
%107 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%108 = load i8, i8* %107
%109 = zext i8 %108 to i32
%110 = call zeroext i8 (i32, ...) @write(i32 %105, i8* %106, i32 %109)
%111 = zext i8 %110 to i32
%112 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%113 = load i8, i8* %112
%114 = zext i8 %113 to i32
%115 = icmp ne i32 %111, %114
br i1 %115, label %116, label %117
116: ; preds = %104
store i32 -1, i32* %4
br label %132
117: ; preds = %104
%118 = load i32, i32* %5
%119 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%120 = call i32 @read(i32 %118, i8* %119, i32 2)
%121 = icmp ne i32 %120, 2
br i1 %121, label %122, label %123
122: ; preds = %117
store i32 -1, i32* %4
br label %132
123: ; preds = %117
%124 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 1
%125 = load i8, i8* %124
%126 = zext i8 %125 to i32
%127 = icmp ne i32 %126, 0
br i1 %127, label %128, label %129
128: ; preds = %123
store i32 -1, i32* %4
br label %132
129: ; preds = %123
br label %131
130: ; preds = %51
store i32 -1, i32* %4
br label %132
131: ; preds = %129, %55
store i32 0, i32* %4
br label %132
132: ; preds = %131, %130, %128, %122, %116, %103, %93, %80, %70, %62, %50, %44, %38, %32, %23, %15
%133 = load i32, i32* %4
ret i32 %133
}

LLVM IR (-O2):
define i32 @hs_socks5(i32 %0, i8* %1, i8* %2) {
%4 = alloca [16 x i8]
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
store i8 5, i8* %5
%6 = call zeroext i8 (i32, ...) @write(i32 %0, i8* %5, i32 1)
%7 = icmp eq i8 %6, 1
br i1 %7, label %8, label %60
8: ; preds = %3
store i8 2, i8* %5
%9 = call zeroext i8 (i32, ...) @write(i32 %0, i8* %5, i32 1)
%10 = icmp eq i8 %9, 1
br i1 %10, label %11, label %60
11: ; preds = %8
store i8 0, i8* %5
%12 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 1
store i8 2, i8* %12
%13 = call zeroext i8 (i32, ...) @write(i32 %0, i8* %5, i32 2)
%14 = icmp eq i8 %13, 2
br i1 %14, label %15, label %60
15: ; preds = %11
%16 = call i32 @read(i32 %0, i8* %5, i32 1)
%17 = icmp eq i32 %16, 1
%18 = load i8, i8* %5
%19 = icmp eq i8 %18, 5
%20 = select i1 %17, i1 %19, i1 false
br i1 %20, label %21, label %60
21: ; preds = %15
%22 = call i32 @read(i32 %0, i8* %5, i32 1)
%23 = icmp eq i32 %22, 1
br i1 %23, label %24, label %60
24: ; preds = %21
%25 = load i8, i8* %5
switch i8 %25, label %60 [
i8 0, label %59
i8 2, label %26
]
26: ; preds = %24
%27 = icmp eq i8* %1, null
%28 = icmp eq i8* %2, null
%29 = or i1 %27, %28
br i1 %29, label %60, label %30
30: ; preds = %26
store i8 1, i8* %5
%31 = call zeroext i8 (i32, ...) @write(i32 %0, i8* %5, i32 1)
%32 = icmp eq i8 %31, 1
br i1 %32, label %33, label %60
33: ; preds = %30
%34 = call zeroext i8 @strlen(i8* %1)
store i8 %34, i8* %5
%35 = call zeroext i8 (i32, ...) @write(i32 %0, i8* %5, i32 1)
%36 = icmp eq i8 %35, 1
br i1 %36, label %37, label %60
37: ; preds = %33
%38 = load i8, i8* %5
%39 = zext i8 %38 to i32
%40 = call zeroext i8 (i32, ...) @write(i32 %0, i8* %1, i32 %39)
%41 = load i8, i8* %5
%42 = icmp eq i8 %40, %41
br i1 %42, label %43, label %60
43: ; preds = %37
%44 = call zeroext i8 @strlen(i8* %2)
store i8 %44, i8* %5
%45 = call zeroext i8 (i32, ...) @write(i32 %0, i8* %5, i32 1)
%46 = icmp eq i8 %45, 1
br i1 %46, label %47, label %60
47: ; preds = %43
%48 = load i8, i8* %5
%49 = zext i8 %48 to i32
%50 = call zeroext i8 (i32, ...) @write(i32 %0, i8* %2, i32 %49)
%51 = load i8, i8* %5
%52 = icmp eq i8 %50, %51
br i1 %52, label %53, label %60
53: ; preds = %47
%54 = call i32 @read(i32 %0, i8* %5, i32 2)
%55 = icmp eq i32 %54, 2
%56 = load i8, i8* %12
%57 = icmp eq i8 %56, 0
%58 = select i1 %55, i1 %57, i1 false
br i1 %58, label %59, label %60
59: ; preds = %53, %24
br label %60
60: ; preds = %24, %53, %47, %43, %37, %33, %30, %26, %21, %15, %11, %8, %3, %59
%61 = phi i32 [ 0, %59 ], [ -1, %3 ], [ -1, %8 ], [ -1, %11 ], [ -1, %15 ], [ -1, %21 ], [ -1, %26 ], [ -1, %30 ], [ -1, %33 ], [ -1, %37 ], [ -1, %43 ], [ -1, %47 ], [ -1, %53 ], [ -1, %24 ]
ret i32 %61
}