// File: /workspace/github_c_makefile_star2_O1/unixlab[P]cloudflare-udp-ping/udpclient.c
// Function: main
int main(int argc, const char *argv[])
{
	int base_src_port = 65500;
	int polling = 0;
	int busy_poll = 0;
	int packet_timestamp = 0;

	static struct option long_options[] = {
		{"src-port", required_argument, 0, 's'},
		{"polling", no_argument, 0, 'p'},
		{"busy-poll", required_argument, 0, 'b'},
		{"timestamp", no_argument, 0, 't'},
		{NULL, 0, 0, 0}};
	const char *optstring = optstring_from_long_options(long_options);

	optind = 1;
	while (1) {
		int option_index = 0;
		int arg = getopt_long(argc, (char **)argv, optstring,
				      long_options, &option_index);
		if (arg == -1) {
			break;
		}

		switch (arg) {
		case 's':
			base_src_port = atoi(optarg);
			break;

		case 'p':
			polling = 1;
			break;

		case 'b':
			busy_poll = atoi(optarg);
			break;

		case 't':
			packet_timestamp = 1;
			break;

		default:
			FATAL("Unknown option %c: %s", arg, argv[optind]);
		}
	}

	int thread_num = argc - optind;
	if (thread_num < 1) {
		FATAL("Usage: %s [--polling] [--busy-poll=<>] [--src-port=<>] "
		      "[--timestamp] [target:port...]",
		      argv[0]);
	}
	struct net_addr *target_addrs =
		calloc(thread_num, sizeof(struct net_addr));

	int t;
	for (t = 0; t < thread_num; t++) {
		const char *target_addr_str = argv[optind + t];
		parse_addr(&target_addrs[t], target_addr_str);

		fprintf(stderr, "[*] Sending to %s, polling=%i, src_port=%i\n",
			addr_to_str(&target_addrs[t]), polling,
			base_src_port + t);
	}

	struct state *array_of_states =
		calloc(thread_num, sizeof(struct state));

	for (t = 0; t < thread_num; t++) {
		struct state *state = &array_of_states[t];
		state->target_addr = &target_addrs[t];
		state->polling = polling;
		state->busy_poll = busy_poll;
		if (base_src_port > 0) {
			state->src_port = base_src_port + t;
		}
		thread_spawn(thread_loop, state);
	}

	while (1) {
		struct timeval timeout = NSEC_TIMEVAL(MSEC_NSEC(1000UL));
		while (1) {
			int r = select(0, NULL, NULL, NULL, &timeout);
			if (r != 0) {
				continue;
			}
			if (TIMEVAL_NSEC(&timeout) == 0) {
				break;
			}
		}

		for (t = 0; t < thread_num; t++) {
			struct state *state = &array_of_states[t];

			uint64_t pps;
			double avg, stddev;
			double avg_packet, stddev_packet;

			pthread_spin_lock(&state->lock);
			stddev_get(&state->stddev, &pps, &avg, &stddev);
			int min = state->stddev.min;
			stddev_get(&state->stddev_packet, NULL, &avg_packet,
				   &stddev_packet);
			stddev_init(&state->stddev);
			stddev_init(&state->stddev_packet);
			pthread_spin_unlock(&state->lock);

			printf("pps=%6lu avg=%7.3fus dev=%7.3fus min=%6.3fus  ",
			       pps, avg / 1000., stddev / 1000.,
			       (double)min / 1000.);
			if (packet_timestamp) {
				printf("(packet=%5.3fus/%5.3f)  ",
				       avg_packet / 1000.,
				       stddev_packet / 1000.);
			}
		}
		printf("\n");
	}
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %rbx
movl %edi, %r15d
leaq 0xe5b07(%rip), %rdi     # 0x4e8110
movq %rdi, %r13
callq 0x403264
movq %rbx, %rsi
movq %rax, %r14
movq $0x4e9140, %rax         # imm = 0x4E9140
movl $0x1, (%rax)
movl $0xffdc, %r12d          # imm = 0xFFDC
movl $0x0, -0x34(%rbp)
movl $0x0, -0x48(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x80(%rbp)
movl %r15d, %edi
movq %rsi, %rbx
movq %r14, %rdx
movq %r13, %rcx
leaq -0x80(%rbp), %r8
callq 0x45c730
cmpl $0x6f, %eax
jle 0x402687
cmpl $0x70, %eax
je 0x4026a9
cmpl $0x74, %eax
je 0x4026b2
cmpl $0x73, %eax
jne 0x402a46
movq $0x4efa60, %rax         # imm = 0x4EFA60
movq (%rax), %rdi
callq 0x40bb60
movl %eax, %r12d
jmp 0x4026b9
cmpl $-0x1, %eax
je 0x4026cf
cmpl $0x62, %eax
jne 0x402a46
movq $0x4efa60, %rax         # imm = 0x4EFA60
movq (%rax), %rdi
callq 0x40bb60
movl %eax, -0x48(%rbp)
jmp 0x4026b9
movl $0x1, -0x30(%rbp)
jmp 0x4026b9
movl $0x1, -0x34(%rbp)
movb $0x1, %al
movq %rbx, %rsi
movq $0x4e9140, %rcx         # imm = 0x4E9140
testb %al, %al
jne 0x40263f
jmp 0x4026d3
xorl %eax, %eax
jmp 0x4026bb
subl (%rcx), %r15d
jle 0x402a9d
movl %r12d, %eax
movq %rax, -0x40(%rbp)
movl %r15d, -0x58(%rbp)
movl %r15d, %r14d
movl $0x40, %esi
movq %r14, %rdi
callq 0x426fe0
movq %rax, %r13
movq %rax, %rcx
xorl %r15d, %r15d
movq %rbx, -0x60(%rbp)
movq $0x4e9140, %rax         # imm = 0x4E9140
movl (%rax), %eax
addl %r15d, %eax
cltq
movq -0x60(%rbp), %rdx
movq (%rdx,%rax,8), %rsi
movq %rcx, %rbx
movq %rcx, %rdi
callq 0x402b1b
movq $0x4e87a8, %rax         # imm = 0x4E87A8
movq (%rax), %rax
movq %rax, -0x68(%rbp)
movq %rbx, %rdi
callq 0x402c78
movq -0x40(%rbp), %rcx
leal (%rcx,%r15), %r8d
movq -0x68(%rbp), %rdi
leaq 0xb1aad(%rip), %rsi     # 0x4b41fb
movq %rax, %rdx
movl -0x30(%rbp), %ecx
xorl %eax, %eax
callq 0x40d780
movq %rbx, %rcx
incq %r15
addq $0x40, %rcx
cmpq %r15, %r14
jne 0x402704
movq -0x40(%rbp), %rbx
movl $0x58, %esi
movq %r14, %rdi
callq 0x426fe0
movq %rax, %r15
movq %rax, -0x50(%rbp)
movq %r13, (%r15)
movl -0x30(%rbp), %eax
movl %eax, 0xc(%r15)
movl -0x48(%rbp), %eax
movl %eax, 0x10(%r15)
testl %ebx, %ebx
jle 0x40279b
movl %r12d, 0x8(%r15)
leaq -0x5da(%rip), %rdi      # 0x4021c8
movq %r15, %rsi
callq 0x402ed4
incl %r12d
addq $0x40, %r13
addq $0x58, %r15
decq %r14
jne 0x402782
movl -0x58(%rbp), %ecx
cmpl $0x2, %ecx
movl $0x1, %eax
cmovgel %ecx, %eax
movq -0x50(%rbp), %rbx
leaq 0x18(%rbx), %r15
addq $0x14, %rbx
movq %rbx, -0x50(%rbp)
imulq $0x58, %rax, %rax
movq %rax, -0x88(%rbp)
leaq -0x80(%rbp), %rbx
movupd 0xb1851(%rip), %xmm0    # 0x4b4040
movapd %xmm0, -0x80(%rbp)
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movq %rbx, %r8
callq 0x45d970
testl %eax, %eax
jne 0x4027f4
imulq $0x3b9aca00, -0x80(%rbp), %rax # imm = 0x3B9ACA00
imulq $-0x3e8, -0x78(%rbp), %rcx # imm = 0xFC18
cmpq %rcx, %rax
jne 0x4027f4
xorl %ebx, %ebx
movq -0x50(%rbp), %rax
leaq (%rax,%rbx), %r13
movq %r13, %rdi
callq 0x421130
xorps %xmm1, %xmm1
cvtsi2sdq 0x8(%r15,%rbx), %xmm1
movq 0x18(%r15,%rbx), %r14
movq %r14, %xmm0
movq 0xb17c5(%rip), %xmm5    # 0x4b4010
punpckldq %xmm5, %xmm0    # xmm0 = xmm0[0],xmm5[0],xmm0[1],xmm5[1]
movapd 0xb17c9(%rip), %xmm6    # 0x4b4020
subpd %xmm6, %xmm0
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
movapd %xmm1, %xmm3
divsd %xmm2, %xmm3
movapd %xmm3, %xmm0
divsd 0xb17d5(%rip), %xmm0    # 0x4b4050
movsd %xmm0, -0x60(%rbp)
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
cmpq $0x3, %r14
jb 0x4028e3
movq 0x10(%r15,%rbx), %xmm4
punpckldq %xmm5, %xmm4    # xmm4 = xmm4[0],xmm5[0],xmm4[1],xmm5[1]
subpd %xmm6, %xmm4
movapd %xmm4, %xmm0
unpckhpd %xmm4, %xmm0            # xmm0 = xmm0[1],xmm4[1]
addsd %xmm4, %xmm0
mulsd %xmm3, %xmm1
subsd %xmm1, %xmm0
divsd %xmm2, %xmm0
andpd 0xb1772(%rip), %xmm0    # 0x4b4030
ucomisd 0xb1792(%rip), %xmm0    # 0x4b4058
jb 0x4028ce
sqrtsd %xmm0, %xmm0
jmp 0x4028e3
callq 0x403300
movapd 0xb1745(%rip), %xmm6    # 0x4b4020
movq 0xb172d(%rip), %xmm5    # 0x4b4010
movsd %xmm0, -0x48(%rbp)
movq 0x18(%r15,%rbx), %rax
movq %rax, -0x68(%rbp)
movl (%r15,%rbx), %eax
movl %eax, -0x40(%rbp)
xorps %xmm1, %xmm1
cvtsi2sdq 0x28(%r15,%rbx), %xmm1
movq 0x38(%r15,%rbx), %r12
movq %r12, %xmm0
punpckldq %xmm5, %xmm0    # xmm0 = xmm0[0],xmm5[0],xmm0[1],xmm5[1]
subpd %xmm6, %xmm0
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
movapd %xmm1, %xmm3
divsd %xmm2, %xmm3
movapd %xmm3, %xmm0
divsd 0xb171c(%rip), %xmm0    # 0x4b4050
movsd %xmm0, -0x58(%rbp)
cmpq $0x3, %r12
jb 0x402988
movq 0x30(%r15,%rbx), %xmm4
punpckldq %xmm5, %xmm4    # xmm4 = xmm4[0],xmm5[0],xmm4[1],xmm5[1]
subpd %xmm6, %xmm4
movapd %xmm4, %xmm0
unpckhpd %xmm4, %xmm0            # xmm0 = xmm0[1],xmm4[1]
addsd %xmm4, %xmm0
mulsd %xmm3, %xmm1
subsd %xmm1, %xmm0
divsd %xmm2, %xmm0
andpd 0xb16c2(%rip), %xmm0    # 0x4b4030
ucomisd 0xb16e2(%rip), %xmm0    # 0x4b4058
jb 0x40297e
sqrtsd %xmm0, %xmm0
jmp 0x402983
callq 0x403300
movsd %xmm0, -0x30(%rbp)
leaq (%r15,%rbx), %rax
addq $0x8, %rax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl %edx, -0x8(%rax)
xorl %ecx, %ecx
movq %rcx, 0x10(%rax)
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movl %edx, 0x18(%rax)
movq %rcx, 0x30(%rax)
movupd %xmm0, 0x20(%rax)
movq %r13, %rdi
callq 0x421150
xorpd %xmm0, %xmm0
testq %r14, %r14
je 0x4029c8
movsd -0x60(%rbp), %xmm0
movsd 0xb1680(%rip), %xmm3    # 0x4b4050
movsd -0x48(%rbp), %xmm1
divsd %xmm3, %xmm1
xorps %xmm2, %xmm2
cvtsi2sdl -0x40(%rbp), %xmm2
divsd %xmm3, %xmm2
leaq 0xb183b(%rip), %rdi     # 0x4b4227
movq -0x68(%rbp), %rsi
movb $0x3, %al
callq 0x40d840
cmpl $0x0, -0x34(%rbp)
je 0x402a26
xorpd %xmm0, %xmm0
testq %r12, %r12
je 0x402a0b
movsd -0x58(%rbp), %xmm0
movsd -0x30(%rbp), %xmm1
divsd 0xb1638(%rip), %xmm1    # 0x4b4050
leaq 0xb1837(%rip), %rdi     # 0x4b4256
movb $0x2, %al
callq 0x40d840
addq $0x58, %rbx
cmpq %rbx, -0x88(%rbp)
jne 0x40281f
movl $0xa, %edi
callq 0x414ab0
jmp 0x4027e3
movq $0x4e87a8, %r14         # imm = 0x4E87A8
movq (%r14), %rdi
movq $0x4e9140, %rcx         # imm = 0x4E9140
movslq (%rcx), %rcx
movq (%rbx,%rcx,8), %rcx
leaq 0xb16e7(%rip), %rsi     # 0x4b414c
movl %eax, %edx
xorl %eax, %eax
callq 0x40d780
movq (%r14), %rdi
leaq 0xb16fe(%rip), %rsi     # 0x4b4176
leaq 0xc8bee(%rip), %rdx     # 0x4cb66d
leaq 0xb162b(%rip), %rcx     # 0x4b40b1
movl $0xc3, %r8d
xorl %eax, %eax
callq 0x40d780
movl $0x1, %edi
callq 0x40c6e0
movq $0x4e87a8, %r14         # imm = 0x4E87A8
movq (%r14), %rdi
movq (%rbx), %rdx
leaq 0xb16e0(%rip), %rsi     # 0x4b4191
xorl %eax, %eax
callq 0x40d780
movq (%r14), %rdi
leaq 0xb16b4(%rip), %rsi     # 0x4b4176
leaq 0xc8ba4(%rip), %rdx     # 0x4cb66d
leaq 0xb15e1(%rip), %rcx     # 0x4b40b1
movl $0xcb, %r8d
jmp 0x402a8c