// File: /workspace/github_c_makefile_star2_O2/unixlab[P]cloudflare-udp-ping/net.c
// Function: net_connect_tcp
int net_connect_tcp(struct net_addr *shost, int src_port, int busy_poll)
{
	int sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (sd < 0) {
		PFATAL("socket()");
	}

	int one = 1;
	int r = setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&one,
			   sizeof(one));
	if (r < 0) {
		PFATAL("setsockopt(SO_REUSEADDR)");
	}

	if (busy_poll) {
		r = setsockopt(sd, SOL_SOCKET, SO_BUSY_POLL, (char *)&busy_poll,
			       sizeof(busy_poll));
		if (r < 0) {
			PFATAL("setsockopt(SO_BUSY_POLL)");
		}
	}

	if (src_port > 1 && src_port < 65536) {
		struct net_addr src;
		memset(&src, 0, sizeof(struct net_addr));
		char buf[32];
		snprintf(buf, sizeof(buf), "0.0.0.0:%d", src_port);
		parse_addr(&src, buf);
		if (bind(sd, src.sockaddr, src.sockaddr_len) < 0) {
			PFATAL("bind()");
		}
	}

	if (-1 == connect(sd, shost->sockaddr, shost->sockaddr_len)) {
		/* is non-blocking, so we don't get error at that point yet */
		if (EINPROGRESS != errno) {
			PFATAL("connect()");
			return -1;
		}
	}

	one = 1;
	setsockopt(sd, IPPROTO_TCP, TCP_NODELAY, (char *)&one, sizeof(one));

	return sd;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %edx, %r12d
movl %esi, %r15d
movq %rdi, %r14
movl %edx, -0x30(%rbp)
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
pushq $0x6
popq %rdx
callq 0x45f7c0
testl %eax, %eax
js 0x402c32
movl %eax, %ebx
leaq -0x2c(%rbp), %rcx
movl $0x1, (%rcx)
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
pushq $0x4
popq %r8
movl %eax, %edi
callq 0x45f780
testl %eax, %eax
js 0x402c71
testl %r12d, %r12d
je 0x402b79
pushq $0x1
popq %rsi
pushq $0x2e
popq %rdx
leaq -0x30(%rbp), %rcx
pushq $0x4
popq %r8
movl %ebx, %edi
callq 0x45f780
testl %eax, %eax
js 0x402cb0
leal -0x2(%r15), %eax
cmpl $0xfffd, %eax           # imm = 0xFFFD
ja 0x402be4
xorps %xmm0, %xmm0
leaq -0x90(%rbp), %r12
movaps %xmm0, 0x30(%r12)
movaps %xmm0, 0x20(%r12)
movaps %xmm0, 0x10(%r12)
movaps %xmm0, (%r12)
leaq 0xb18e8(%rip), %rdx     # 0x4b4494
leaq -0x50(%rbp), %r13
pushq $0x20
popq %rsi
movq %r13, %rdi
movl %r15d, %ecx
xorl %eax, %eax
callq 0x40d420
movq %r12, %rdi
movq %r13, %rsi
callq 0x401fc3
movq 0x30(%r12), %rsi
movl 0x38(%r12), %edx
movl %ebx, %edi
callq 0x45f530
testl %eax, %eax
js 0x402cec
movq 0x30(%r14), %rsi
movl 0x38(%r14), %edx
movl %ebx, %edi
callq 0x45f560
cmpl $-0x1, %eax
jne 0x402c06
callq 0x405990
cmpl $0x73, (%rax)
jne 0x402d28
leaq -0x2c(%rbp), %rcx
movl $0x1, (%rcx)
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
pushq $0x4
popq %r8
movl %ebx, %edi
callq 0x45f780
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $0x4e77a8, %rbx         # imm = 0x4E77A8
movq (%rbx), %rcx
leaq 0xb16c8(%rip), %rdi     # 0x4b430b
pushq $0x1b
popq %rsi
pushq $0x1
popq %rdx
callq 0x4142f0
movq (%rbx), %rdi
leaq 0xb1423(%rip), %rsi     # 0x4b407b
leaq 0xb185d(%rip), %rdx     # 0x4b44bc
leaq 0xb1600(%rip), %rcx     # 0x4b4266
movl $0x134, %r8d            # imm = 0x134
jmp 0x402d62
movq $0x4e77a8, %rbx         # imm = 0x4E77A8
movq (%rbx), %rcx
leaq 0xb16b2(%rip), %rdi     # 0x4b4334
pushq $0x2b
popq %rsi
pushq $0x1
popq %rdx
callq 0x4142f0
movq (%rbx), %rdi
leaq 0xb13e4(%rip), %rsi     # 0x4b407b
leaq 0xb181e(%rip), %rdx     # 0x4b44bc
leaq 0xb15c1(%rip), %rcx     # 0x4b4266
movl $0x13b, %r8d            # imm = 0x13B
jmp 0x402d62
movq $0x4e77a8, %rbx         # imm = 0x4E77A8
movq (%rbx), %rcx
leaq 0xb170f(%rip), %rdi     # 0x4b43d0
pushq $0x2b
popq %rsi
pushq $0x1
popq %rdx
callq 0x4142f0
movq (%rbx), %rdi
leaq 0xb13a5(%rip), %rsi     # 0x4b407b
leaq 0xb17df(%rip), %rdx     # 0x4b44bc
leaq 0xb1582(%rip), %rcx     # 0x4b4266
movl $0x142, %r8d            # imm = 0x142
jmp 0x402d62
movq $0x4e77a8, %rbx         # imm = 0x4E77A8
movq (%rbx), %rcx
leaq 0xb16b9(%rip), %rdi     # 0x4b43b6
pushq $0x19
popq %rsi
pushq $0x1
popq %rdx
callq 0x4142f0
movq (%rbx), %rdi
leaq 0xb1369(%rip), %rsi     # 0x4b407b
leaq 0xb17a3(%rip), %rdx     # 0x4b44bc
leaq 0xb1546(%rip), %rcx     # 0x4b4266
movl $0x14d, %r8d            # imm = 0x14D
jmp 0x402d62
movq $0x4e77a8, %rbx         # imm = 0x4E77A8
movq (%rbx), %rcx
leaq 0xb1766(%rip), %rdi     # 0x4b449f
pushq $0x1c
popq %rsi
pushq $0x1
popq %rdx
callq 0x4142f0
movq (%rbx), %rdi
leaq 0xb132d(%rip), %rsi     # 0x4b407b
leaq 0xb1767(%rip), %rdx     # 0x4b44bc
leaq 0xb150a(%rip), %rcx     # 0x4b4266
movl $0x154, %r8d            # imm = 0x154
xorl %eax, %eax
callq 0x40d290
leaq 0xb133d(%rip), %rdi     # 0x4b40ad
callq 0x40d640
movq (%rbx), %rsi
pushq $0xa
popq %rdi
callq 0x416660
pushq $0x1
popq %rdi
callq 0x40c1f0