// File: /workspace/github_c_makefile_star2_O0/GangZhuo[P]dohclient/src/http.c
// Function: http_socks5_handshake
static int http_socks5_handshake(http_ctx_t* ctx, http_conn_t* conn)
{
	int r;
	conn_status cs = conn->conn.status;
	stream_t* s = &conn->proxy_stream;
	int rsize = stream_rsize(s);

	if (s->cap < 1024) {
		if (stream_set_cap(s, 1024)) {
			return -1;
		}
	}

	switch (cs) {
	case cs_connected:
		logv("sending authentication methods\n");
		if (rsize == 0) {
			s->array[0] = 0x05;
			s->array[1] = 0x01;
			s->array[2] = 0x00;
			s->pos = 0;
			s->size = 3;
		}
		r = tcp_send(conn->conn.sock, s);
		if (r < 0) {
			loge("tcp_send() error\n");
			return -1;
		}
		if (stream_rsize(s) > 0) {
			conn->conn.status = cs_socks5_sending_method;
		}
		else {
			conn->conn.status = cs_socks5_waiting_method;
		}
		break;
	case cs_socks5_waiting_method:
		logv("receiving authentication methods\n");
		r = tcp_recv(conn->conn.sock, s->array, s->cap);
		if (r != 2) {
			loge("tcp_recv() error\n");
			return -1;
		}
		if (s->array[0] == 0x05 && s->array[1] == 0x00) {
			s->array[0] = 0x05;
			s->array[1] = 0x01;
			s->array[2] = 0x00;
			if (conn->pool->key.addr.addr.ss_family == AF_INET) {
				struct sockaddr_in* addr = (struct sockaddr_in*)&conn->pool->key.addr.addr;
				int port = htons(addr->sin_port);
				s->array[3] = 0x01;
				memcpy(s->array + 4, &addr->sin_addr, 4);
				s->array[8] = (char)((port >> 8) & 0xff);
				s->array[9] = (char)((port >> 0) & 0xff);
				s->size = 10;
			}
			else {
				s->array[3] = 0x04;
				struct sockaddr_in6* addr = (struct sockaddr_in6*)&conn->pool->key.addr.addr;
				int port = htons(addr->sin6_port);
				s->array[3] = 0x01;
				memcpy(s->array + 4, &addr->sin6_addr, 16);
				s->array[20] = (char)((port >> 8) & 0xff);
				s->array[21] = (char)((port >> 0) & 0xff);
				s->size = 22;
			}
			s->pos = 0;
			conn->conn.status = cs_socks5_sending_connect;
			r = tcp_send(conn->conn.sock, s);
			if (r < 0) {
				loge("tcp_send() error\n");
				return -1;
			}
			if (stream_rsize(s) > 0) {
				conn->conn.status = cs_socks5_sending_connect;
			}
			else {
				conn->conn.status = cs_socks5_waiting_connect;
			}
		}
		else {
			loge("no support method\n");
			return -1;
		}
		break;
	case cs_socks5_sending_connect:
		logv("connecting target server\n");
		r = tcp_send(conn->conn.sock, s);
		if (r < 0) {
			loge("tcp_send() error\n");
			return -1;
		}
		if (stream_rsize(s) > 0) {
			conn->conn.status = cs_socks5_sending_connect;
		}
		else {
			conn->conn.status = cs_socks5_waiting_connect;
		}
		break;
	case cs_socks5_waiting_connect:
		logv("receiving connection status\n");
		r = tcp_recv(conn->conn.sock, s->array, s->cap);
		if (r <= 0) {
			loge("tcp_recv() error\n");
			return -1;
		}
		if (s->array[0] == 0x05 && s->array[1] == 0x00) {
			conn->conn.status = cs_socks5_handshaked;
			stream_free(s);
			logd("socks5 handshaked\n");
			return http_ssl_handshake(ctx, conn);
		}
		else {
			loge("connect error\n");
			return -1;
		}
		break;
	default:
		loge("unknown status\n");
		return -1;
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x18(%rbp), %rax
movl 0x48(%rax), %eax
movl %eax, -0x20(%rbp)
movq -0x18(%rbp), %rax
addq $0x110, %rax            # imm = 0x110
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x28(%rbp), %rcx
subl 0xc(%rcx), %eax
movl %eax, -0x2c(%rbp)
movq -0x28(%rbp), %rax
cmpl $0x400, 0x10(%rax)      # imm = 0x400
jge 0x231d7
movq -0x28(%rbp), %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x2d900
cmpl $0x0, %eax
je 0x231d5
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2383c
jmp 0x231d7
movl -0x20(%rbp), %eax
addl $-0x2, %eax
movl %eax, %ecx
movq %rcx, -0x58(%rbp)
subl $0x6, %eax
ja 0x237f6
movq -0x58(%rbp), %rax
leaq 0x14209(%rip), %rcx     # 0x37400
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
jmp 0x23202
movb $0x0, %al
callq 0x242d0
cmpl $0x8, (%rax)
jl 0x23234
movl $0x8, %edi
leaq 0x14202(%rip), %rsi     # 0x3741c
leaq 0x1493a(%rip), %rdx     # 0x37b5b
movl $0x3c9, %ecx            # imm = 0x3C9
leaq 0x14944(%rip), %r8      # 0x37b71
movb $0x0, %al
callq 0x243b0
jmp 0x23236
cmpl $0x0, -0x2c(%rbp)
jne 0x23272
movq -0x28(%rbp), %rax
movq (%rax), %rax
movb $0x5, (%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movb $0x1, 0x1(%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movb $0x0, 0x2(%rax)
movq -0x28(%rbp), %rax
movl $0x0, 0xc(%rax)
movq -0x28(%rbp), %rax
movl $0x3, 0x8(%rax)
movq -0x18(%rbp), %rax
movl 0x10(%rax), %edi
movq -0x28(%rbp), %rsi
callq 0x265a0
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jge 0x232cd
jmp 0x2328d
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0x232bf
movl $0x3, %edi
leaq 0x14177(%rip), %rsi     # 0x3741c
leaq 0x148af(%rip), %rdx     # 0x37b5b
movl $0x3d3, %ecx            # imm = 0x3D3
leaq 0x13612(%rip), %r8      # 0x368ca
movb $0x0, %al
callq 0x243b0
jmp 0x232c1
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2383c
movq -0x28(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x28(%rbp), %rcx
subl 0xc(%rcx), %eax
cmpl $0x0, %eax
jle 0x232ed
movq -0x18(%rbp), %rax
movl $0x5, 0x48(%rax)
jmp 0x232f8
movq -0x18(%rbp), %rax
movl $0x6, 0x48(%rax)
jmp 0x23835
jmp 0x232ff
movb $0x0, %al
callq 0x242d0
cmpl $0x8, (%rax)
jl 0x23331
movl $0x8, %edi
leaq 0x14105(%rip), %rsi     # 0x3741c
leaq 0x1483d(%rip), %rdx     # 0x37b5b
movl $0x3de, %ecx            # imm = 0x3DE
leaq 0x14867(%rip), %r8      # 0x37b91
movb $0x0, %al
callq 0x243b0
jmp 0x23333
movq -0x18(%rbp), %rax
movl 0x10(%rax), %edi
movq -0x28(%rbp), %rax
movq (%rax), %rsi
movq -0x28(%rbp), %rax
movl 0x10(%rax), %edx
callq 0x26730
movl %eax, -0x1c(%rbp)
cmpl $0x2, -0x1c(%rbp)
je 0x23398
jmp 0x23358
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0x2338a
movl $0x3, %edi
leaq 0x140ac(%rip), %rsi     # 0x3741c
leaq 0x147e4(%rip), %rdx     # 0x37b5b
movl $0x3e1, %ecx            # imm = 0x3E1
leaq 0x135cc(%rip), %r8      # 0x3694f
movb $0x0, %al
callq 0x243b0
jmp 0x2338c
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2383c
movq -0x28(%rbp), %rax
movq (%rax), %rax
movsbl (%rax), %eax
cmpl $0x5, %eax
jne 0x23598
movq -0x28(%rbp), %rax
movq (%rax), %rax
movsbl 0x1(%rax), %eax
cmpl $0x0, %eax
jne 0x23598
movq -0x28(%rbp), %rax
movq (%rax), %rax
movb $0x5, (%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movb $0x1, 0x1(%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movb $0x0, 0x2(%rax)
movq -0x18(%rbp), %rax
movq 0x88(%rax), %rax
movzwl (%rax), %eax
cmpl $0x2, %eax
jne 0x2346e
movq -0x18(%rbp), %rax
movq 0x88(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movzwl 0x2(%rax), %edi
callq 0x3070
movzwl %ax, %eax
movl %eax, -0x3c(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movb $0x1, 0x3(%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
movl 0x4(%rcx), %ecx
movl %ecx, 0x4(%rax)
movl -0x3c(%rbp), %eax
sarl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movq (%rax), %rax
movb %cl, 0x8(%rax)
movl -0x3c(%rbp), %eax
sarl $0x0, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movq (%rax), %rax
movb %cl, 0x9(%rax)
movq -0x28(%rbp), %rax
movl $0xa, 0x8(%rax)
jmp 0x234fa
movq -0x28(%rbp), %rax
movq (%rax), %rax
movb $0x4, 0x3(%rax)
movq -0x18(%rbp), %rax
movq 0x88(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movzwl 0x2(%rax), %edi
callq 0x3070
movzwl %ax, %eax
movl %eax, -0x4c(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movb $0x1, 0x3(%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x48(%rbp), %rcx
movq 0x8(%rcx), %rdx
movq %rdx, 0x4(%rax)
movq 0x10(%rcx), %rcx
movq %rcx, 0xc(%rax)
movl -0x4c(%rbp), %eax
sarl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movq (%rax), %rax
movb %cl, 0x14(%rax)
movl -0x4c(%rbp), %eax
sarl $0x0, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movq (%rax), %rax
movb %cl, 0x15(%rax)
movq -0x28(%rbp), %rax
movl $0x16, 0x8(%rax)
movq -0x28(%rbp), %rax
movl $0x0, 0xc(%rax)
movq -0x18(%rbp), %rax
movl $0x7, 0x48(%rax)
movq -0x18(%rbp), %rax
movl 0x10(%rax), %edi
movq -0x28(%rbp), %rsi
callq 0x265a0
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jge 0x2356b
jmp 0x2352b
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0x2355d
movl $0x3, %edi
leaq 0x13ed9(%rip), %rsi     # 0x3741c
leaq 0x14611(%rip), %rdx     # 0x37b5b
movl $0x3ff, %ecx            # imm = 0x3FF
leaq 0x13374(%rip), %r8      # 0x368ca
movb $0x0, %al
callq 0x243b0
jmp 0x2355f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2383c
movq -0x28(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x28(%rbp), %rcx
subl 0xc(%rcx), %eax
cmpl $0x0, %eax
jle 0x2358b
movq -0x18(%rbp), %rax
movl $0x7, 0x48(%rax)
jmp 0x23596
movq -0x18(%rbp), %rax
movl $0x8, 0x48(%rax)
jmp 0x235da
jmp 0x2359a
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0x235cc
movl $0x3, %edi
leaq 0x13e6a(%rip), %rsi     # 0x3741c
leaq 0x145a2(%rip), %rdx     # 0x37b5b
movl $0x40a, %ecx            # imm = 0x40A
leaq 0x1339c(%rip), %r8      # 0x36961
movb $0x0, %al
callq 0x243b0
jmp 0x235ce
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2383c
jmp 0x23835
jmp 0x235e1
movb $0x0, %al
callq 0x242d0
cmpl $0x8, (%rax)
jl 0x23613
movl $0x8, %edi
leaq 0x13e23(%rip), %rsi     # 0x3741c
leaq 0x1455b(%rip), %rdx     # 0x37b5b
movl $0x40f, %ecx            # imm = 0x40F
leaq 0x145a7(%rip), %r8      # 0x37bb3
movb $0x0, %al
callq 0x243b0
jmp 0x23615
movq -0x18(%rbp), %rax
movl 0x10(%rax), %edi
movq -0x28(%rbp), %rsi
callq 0x265a0
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jge 0x23670
jmp 0x23630
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0x23662
movl $0x3, %edi
leaq 0x13dd4(%rip), %rsi     # 0x3741c
leaq 0x1450c(%rip), %rdx     # 0x37b5b
movl $0x412, %ecx            # imm = 0x412
leaq 0x1326f(%rip), %r8      # 0x368ca
movb $0x0, %al
callq 0x243b0
jmp 0x23664
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2383c
movq -0x28(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x28(%rbp), %rcx
subl 0xc(%rcx), %eax
cmpl $0x0, %eax
jle 0x23690
movq -0x18(%rbp), %rax
movl $0x7, 0x48(%rax)
jmp 0x2369b
movq -0x18(%rbp), %rax
movl $0x8, 0x48(%rax)
jmp 0x23835
jmp 0x236a2
movb $0x0, %al
callq 0x242d0
cmpl $0x8, (%rax)
jl 0x236d4
movl $0x8, %edi
leaq 0x13d62(%rip), %rsi     # 0x3741c
leaq 0x1449a(%rip), %rdx     # 0x37b5b
movl $0x41d, %ecx            # imm = 0x41D
leaq 0x133a0(%rip), %r8      # 0x36a6d
movb $0x0, %al
callq 0x243b0
jmp 0x236d6
movq -0x18(%rbp), %rax
movl 0x10(%rax), %edi
movq -0x28(%rbp), %rax
movq (%rax), %rsi
movq -0x28(%rbp), %rax
movl 0x10(%rax), %edx
callq 0x26730
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jg 0x2373b
jmp 0x236fb
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0x2372d
movl $0x3, %edi
leaq 0x13d09(%rip), %rsi     # 0x3741c
leaq 0x14441(%rip), %rdx     # 0x37b5b
movl $0x420, %ecx            # imm = 0x420
leaq 0x13229(%rip), %r8      # 0x3694f
movb $0x0, %al
callq 0x243b0
jmp 0x2372f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2383c
movq -0x28(%rbp), %rax
movq (%rax), %rax
movsbl (%rax), %eax
cmpl $0x5, %eax
jne 0x237b7
movq -0x28(%rbp), %rax
movq (%rax), %rax
movsbl 0x1(%rax), %eax
cmpl $0x0, %eax
jne 0x237b7
movq -0x18(%rbp), %rax
movl $0x9, 0x48(%rax)
movq -0x28(%rbp), %rdi
callq 0x2d640
movb $0x0, %al
callq 0x242d0
cmpl $0x7, (%rax)
jl 0x237a0
movl $0x7, %edi
leaq 0x13c96(%rip), %rsi     # 0x3741c
leaq 0x143ce(%rip), %rdx     # 0x37b5b
movl $0x426, %ecx            # imm = 0x426
leaq 0x131db(%rip), %r8      # 0x36974
movb $0x0, %al
callq 0x243b0
jmp 0x237a2
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x20730
movl %eax, -0x4(%rbp)
jmp 0x2383c
jmp 0x237b9
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0x237eb
movl $0x3, %edi
leaq 0x13c4b(%rip), %rsi     # 0x3741c
leaq 0x14383(%rip), %rdx     # 0x37b5b
movl $0x42a, %ecx            # imm = 0x42A
leaq 0x131a3(%rip), %r8      # 0x36987
movb $0x0, %al
callq 0x243b0
jmp 0x237ed
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2383c
jmp 0x237f8
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0x2382a
movl $0x3, %edi
leaq 0x13c0c(%rip), %rsi     # 0x3741c
leaq 0x14344(%rip), %rdx     # 0x37b5b
movl $0x42f, %ecx            # imm = 0x42F
leaq 0x13173(%rip), %r8      # 0x36996
movb $0x0, %al
callq 0x243b0
jmp 0x2382c
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2383c
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)