// File: /workspace/github_c_makefile_star2_O3/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 $0x58, %rsp
movq %rsi, %rbx
movl %edi, %r13d
leaq 0xe5ad4(%rip), %rdi     # 0x4e8110
callq 0x403241
movq %rax, %r14
movq $0x4e9140, %rax         # imm = 0x4E9140
movl $0x1, (%rax)
movl $0xffdc, %r15d          # imm = 0xFFDC
movl $0x0, -0x3c(%rbp)
leaq -0x70(%rbp), %r12
movl $0x0, -0x50(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x0, -0x70(%rbp)
movl %r13d, %edi
movq %rbx, %rsi
movq %r14, %rdx
leaq 0xe5a89(%rip), %rcx     # 0x4e8110
movq %r12, %r8
callq 0x45c6f0
cmpl $0x72, %eax
jg 0x4026a7
cmpl $0x62, %eax
je 0x4026d2
cmpl $0x70, %eax
jne 0x4026e6
movl $0x1, -0x38(%rbp)
jmp 0x402670
cmpl $0x74, %eax
je 0x4026c9
cmpl $0x73, %eax
jne 0x402a16
movq $0x4efa60, %rax         # imm = 0x4EFA60
movq (%rax), %rdi
callq 0x40bb00
movl %eax, %r15d
jmp 0x402670
movl $0x1, -0x3c(%rbp)
jmp 0x402670
movq $0x4efa60, %rax         # imm = 0x4EFA60
movq (%rax), %rdi
callq 0x40bb00
movl %eax, -0x50(%rbp)
jmp 0x402670
cmpl $-0x1, %eax
jne 0x402a16
movq $0x4e9140, %rax         # imm = 0x4E9140
subl (%rax), %r13d
jle 0x402a6d
movl %r15d, %eax
movq %rax, -0x48(%rbp)
movl %r13d, %edi
movl $0x40, %esi
movq %rdi, -0x30(%rbp)
callq 0x426f80
movq %rax, %r13
movq %rax, %r12
xorl %r14d, %r14d
movq $0x4e9140, %rax         # imm = 0x4E9140
movl (%rax), %eax
addl %r14d, %eax
cltq
movq (%rbx,%rax,8), %rsi
movq %r12, %rdi
callq 0x402aeb
movq $0x4e87a8, %rax         # imm = 0x4E87A8
movq (%rax), %rax
movq %rax, -0x60(%rbp)
movq %r12, %rdi
callq 0x402c48
movq -0x48(%rbp), %rcx
leal (%rcx,%r14), %r8d
movq -0x60(%rbp), %rdi
leaq 0xb1a90(%rip), %rsi     # 0x4b41f3
movq %rax, %rdx
movl -0x38(%rbp), %ecx
xorl %eax, %eax
callq 0x40d720
incq %r14
addq $0x40, %r12
cmpq %r14, -0x30(%rbp)
jne 0x402720
movq -0x48(%rbp), %rbx
movl $0x58, %esi
movq -0x30(%rbp), %r14
movq %r14, %rdi
callq 0x426f80
movq %rax, %r12
movq %rax, -0x58(%rbp)
movq %r13, (%r12)
movl -0x38(%rbp), %eax
movl %eax, 0xc(%r12)
movl -0x50(%rbp), %eax
movl %eax, 0x10(%r12)
testl %ebx, %ebx
jle 0x4027b6
movl %r15d, 0x8(%r12)
leaq -0x5f5(%rip), %rdi      # 0x4021c8
movq %r12, %rsi
callq 0x402ead
incl %r15d
addq $0x40, %r13
addq $0x58, %r12
decq %r14
jne 0x402799
movq -0x58(%rbp), %rax
leaq 0x18(%rax), %r15
addq $0x14, %rax
movq %rax, -0x58(%rbp)
imulq $0x58, -0x30(%rbp), %rax
movq %rax, -0x78(%rbp)
leaq -0x70(%rbp), %rbx
movupd 0xb1846(%rip), %xmm0    # 0x4b4040
movapd %xmm0, -0x70(%rbp)
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movq %rbx, %r8
callq 0x45d930
testl %eax, %eax
jne 0x4027ff
imulq $0x3b9aca00, -0x70(%rbp), %rax # imm = 0x3B9ACA00
imulq $-0x3e8, -0x68(%rbp), %rcx # imm = 0xFC18
cmpq %rcx, %rax
jne 0x4027ff
xorl %ebx, %ebx
movq -0x58(%rbp), %rax
leaq (%rax,%rbx), %rdi
movq %rdi, -0x38(%rbp)
callq 0x4210d0
xorps %xmm0, %xmm0
cvtsi2sdq 0x8(%r15,%rbx), %xmm0
movq 0x18(%r15,%rbx), %r13
movq %r13, %xmm2
movq 0xb17b9(%rip), %xmm5    # 0x4b4010
punpckldq %xmm5, %xmm2    # xmm2 = xmm2[0],xmm5[0],xmm2[1],xmm5[1]
movapd 0xb17bd(%rip), %xmm6    # 0x4b4020
subpd %xmm6, %xmm2
movapd %xmm2, %xmm1
unpckhpd %xmm2, %xmm1            # xmm1 = xmm1[1],xmm2[1]
addsd %xmm2, %xmm1
movapd %xmm0, %xmm2
divsd %xmm1, %xmm2
movapd %xmm2, %xmm3
divsd 0xb17c9(%rip), %xmm3    # 0x4b4050
movsd %xmm3, -0x60(%rbp)
xorpd %xmm7, %xmm7
xorpd %xmm3, %xmm3
cmpq $0x3, %r13
jb 0x4028d0
movq 0x10(%r15,%rbx), %xmm3
punpckldq %xmm5, %xmm3    # xmm3 = xmm3[0],xmm5[0],xmm3[1],xmm5[1]
subpd %xmm6, %xmm3
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
addsd %xmm3, %xmm4
mulsd %xmm2, %xmm0
subsd %xmm0, %xmm4
divsd %xmm1, %xmm4
andpd 0xb1767(%rip), %xmm4    # 0x4b4030
xorps %xmm3, %xmm3
sqrtsd %xmm4, %xmm3
movsd %xmm3, -0x50(%rbp)
xorps %xmm0, %xmm0
cvtsi2sdq 0x28(%r15,%rbx), %xmm0
movq 0x38(%r15,%rbx), %r14
movq %r14, %xmm2
punpckldq %xmm5, %xmm2    # xmm2 = xmm2[0],xmm5[0],xmm2[1],xmm5[1]
subpd %xmm6, %xmm2
movapd %xmm2, %xmm1
unpckhpd %xmm2, %xmm1            # xmm1 = xmm1[1],xmm2[1]
addsd %xmm2, %xmm1
movapd %xmm0, %xmm2
divsd %xmm1, %xmm2
movapd %xmm2, %xmm3
divsd 0xb173f(%rip), %xmm3    # 0x4b4050
movsd %xmm3, -0x48(%rbp)
cmpq $0x3, %r14
jb 0x402952
movq 0x30(%r15,%rbx), %xmm3
punpckldq %xmm5, %xmm3    # xmm3 = xmm3[0],xmm5[0],xmm3[1],xmm5[1]
subpd %xmm6, %xmm3
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
addsd %xmm3, %xmm4
mulsd %xmm2, %xmm0
subsd %xmm0, %xmm4
divsd %xmm1, %xmm4
andpd 0xb16e5(%rip), %xmm4    # 0x4b4030
xorps %xmm7, %xmm7
sqrtsd %xmm4, %xmm7
movsd %xmm7, -0x30(%rbp)
leaq (%r15,%rbx), %rax
addq $0x8, %rax
movl -0x8(%rax), %r12d
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 -0x38(%rbp), %rdi
callq 0x4210f0
xorpd %xmm0, %xmm0
testq %r13, %r13
je 0x40299c
movsd -0x60(%rbp), %xmm0
movsd 0xb16ac(%rip), %xmm3    # 0x4b4050
movsd -0x50(%rbp), %xmm1
divsd %xmm3, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %r12d, %xmm2
divsd %xmm3, %xmm2
leaq 0xb185f(%rip), %rdi     # 0x4b421f
movq %r13, %rsi
movb $0x3, %al
callq 0x40d7e0
cmpl $0x0, -0x3c(%rbp)
je 0x4029f9
xorpd %xmm0, %xmm0
testq %r14, %r14
je 0x4029de
movsd -0x48(%rbp), %xmm0
movsd -0x30(%rbp), %xmm1
divsd 0xb1665(%rip), %xmm1    # 0x4b4050
leaq 0xb185c(%rip), %rdi     # 0x4b424e
movb $0x2, %al
callq 0x40d7e0
addq $0x58, %rbx
cmpq %rbx, -0x78(%rbp)
jne 0x40282a
movl $0xa, %edi
callq 0x414a50
jmp 0x4027ee
movq $0x4e87a8, %r14         # imm = 0x4E87A8
movq (%r14), %rdi
movq $0x4e9140, %rcx         # imm = 0x4E9140
movslq (%rcx), %rcx
movq (%rbx,%rcx,8), %rcx
leaq 0xb170f(%rip), %rsi     # 0x4b4144
movl %eax, %edx
xorl %eax, %eax
callq 0x40d720
movq (%r14), %rdi
leaq 0xb1726(%rip), %rsi     # 0x4b416e
leaq 0xc8c1e(%rip), %rdx     # 0x4cb66d
leaq 0xb1653(%rip), %rcx     # 0x4b40a9
movl $0xc3, %r8d
xorl %eax, %eax
callq 0x40d720
movl $0x1, %edi
callq 0x40c680
movq $0x4e87a8, %r14         # imm = 0x4E87A8
movq (%r14), %rdi
movq (%rbx), %rdx
leaq 0xb1708(%rip), %rsi     # 0x4b4189
xorl %eax, %eax
callq 0x40d720
movq (%r14), %rdi
leaq 0xb16dc(%rip), %rsi     # 0x4b416e
leaq 0xc8bd4(%rip), %rdx     # 0x4cb66d
leaq 0xb1609(%rip), %rcx     # 0x4b40a9
movl $0xcb, %r8d
jmp 0x402a5c