// File: /workspace/github_c_makefile_star2_O2/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 $0x78, %rsp
movq %rsi, %r13
movl %edi, %ebx
leaq 0xe55bc(%rip), %r12     # 0x4e7110
movq %r12, %rdi
callq 0x402db1
movq %rax, %r15
movq $0x4e8140, %rax         # imm = 0x4E8140
movl $0x1, (%rax)
movl $0xffdc, -0x2c(%rbp)    # imm = 0xFFDC
movl $0x0, -0x48(%rbp)
leaq -0x60(%rbp), %r14
movl $0x0, -0x44(%rbp)
movl $0x0, -0x40(%rbp)
andl $0x0, -0x60(%rbp)
movl %ebx, %edi
movq %r13, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %r14, %r8
callq 0x45c270
cmpl $0x62, %eax
je 0x401bbf
cmpl $0x70, %eax
je 0x401be7
cmpl $0x73, %eax
je 0x401bd3
cmpl $0x74, %eax
jne 0x401bef
pushq $0x1
popq %rax
movl %eax, -0x48(%rbp)
jmp 0x401b8c
movq $0x4eea60, %rax         # imm = 0x4EEA60
movq (%rax), %rdi
callq 0x40b670
movl %eax, -0x44(%rbp)
jmp 0x401b8c
movq $0x4eea60, %rax         # imm = 0x4EEA60
movq (%rax), %rdi
callq 0x40b670
movl %eax, -0x2c(%rbp)
jmp 0x401b8c
pushq $0x1
popq %rax
movl %eax, -0x40(%rbp)
jmp 0x401b8c
cmpl $-0x1, %eax
jne 0x401e2b
movq $0x4e8140, %r12         # imm = 0x4E8140
subl (%r12), %ebx
jle 0x401e81
movl -0x2c(%rbp), %eax
movq %rax, -0x68(%rbp)
movl %ebx, %r15d
pushq $0x40
popq %rsi
movq %r15, %rdi
callq 0x426af0
movq %rax, %rbx
movq %rax, %r12
xorl %r14d, %r14d
cmpq %r14, %r15
je 0x401c86
movq $0x4e8140, %rax         # imm = 0x4E8140
movl (%rax), %eax
addl %r14d, %eax
cltq
movq (%r13,%rax,8), %rsi
movq %r12, %rdi
callq 0x401fc3
movq $0x4e77a8, %rax         # imm = 0x4E77A8
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq %r12, %rdi
callq 0x4021dc
movq -0x68(%rbp), %rcx
leal (%rcx,%r14), %r8d
movq -0x38(%rbp), %rdi
leaq 0xb257b(%rip), %rsi     # 0x4b41eb
movq %rax, %rdx
movl -0x40(%rbp), %ecx
xorl %eax, %eax
callq 0x40d290
incq %r14
addq $0x40, %r12
jmp 0x401c27
pushq $0x58
popq %rsi
movq %r15, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0x426af0
movq %rax, %r15
leaq -0x518(%rip), %r14      # 0x401788
movq %rax, -0x70(%rbp)
movq -0x38(%rbp), %r13
movl -0x2c(%rbp), %r12d
subq $0x1, %r13
jb 0x401ce5
movq %rbx, (%r15)
movl -0x40(%rbp), %ecx
movl %ecx, 0xc(%r15)
movl -0x44(%rbp), %ecx
movl %ecx, 0x10(%r15)
cmpl $0x0, -0x68(%rbp)
jle 0x401ccd
movl %r12d, 0x8(%r15)
movq %r14, %rdi
movq %r15, %rsi
callq 0x4026de
incl %r12d
addq $0x40, %rbx
addq $0x58, %r15
jmp 0x401cac
addq $0x38, -0x70(%rbp)
leaq -0x60(%rbp), %r14
movl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movupd 0xb2344(%rip), %xmm0    # 0x4b4040
movapd %xmm0, -0x60(%rbp)
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r14, %r8
callq 0x45d4b0
testl %eax, %eax
jne 0x401d01
imulq $0x3b9aca00, -0x60(%rbp), %rax # imm = 0x3B9ACA00
imulq $-0x3e8, -0x58(%rbp), %rcx # imm = 0xFC18
cmpq %rcx, %rax
jne 0x401d01
movq -0x70(%rbp), %rbx
movq -0x38(%rbp), %r15
subq $0x1, %r15
jb 0x401e1e
leaq -0x24(%rbx), %r13
movq %r13, %rdi
callq 0x420c40
leaq -0x20(%rbx), %rdi
leaq -0x98(%rbp), %rsi
leaq -0x90(%rbp), %rdx
leaq -0x88(%rbp), %rcx
callq 0x401ebd
cvtsi2sdl -0x20(%rbx), %xmm0
movsd %xmm0, -0x40(%rbp)
movq %rbx, %rdi
xorl %esi, %esi
leaq -0x80(%rbp), %rdx
leaq -0x78(%rbp), %rcx
callq 0x401ebd
movl %r12d, -0x20(%rbx)
xorpd %xmm0, %xmm0
movupd %xmm0, -0x18(%rbx)
andq $0x0, -0x8(%rbx)
movl %r12d, (%rbx)
movupd %xmm0, 0x8(%rbx)
andq $0x0, 0x18(%rbx)
movq %r13, %rdi
callq 0x420c60
movq -0x98(%rbp), %rsi
movsd -0x90(%rbp), %xmm0
movsd 0xb2248(%rip), %xmm3    # 0x4b4008
divsd %xmm3, %xmm0
movsd -0x88(%rbp), %xmm1
divsd %xmm3, %xmm1
movsd -0x40(%rbp), %xmm2
divsd %xmm3, %xmm2
leaq 0xb2437(%rip), %rdi     # 0x4b4217
movb $0x3, %al
callq 0x40d350
cmpl $0x0, -0x48(%rbp)
je 0x401e15
movsd -0x80(%rbp), %xmm0
movsd 0xb220e(%rip), %xmm2    # 0x4b4008
divsd %xmm2, %xmm0
movsd -0x78(%rbp), %xmm1
divsd %xmm2, %xmm1
leaq 0xb2438(%rip), %rdi     # 0x4b4246
movb $0x2, %al
callq 0x40d350
addq $0x58, %rbx
jmp 0x401d32
pushq $0xa
popq %rdi
callq 0x4145c0
jmp 0x401cf4
movq $0x4e77a8, %rbx         # imm = 0x4E77A8
movq (%rbx), %rdi
movq $0x4e8140, %rcx         # imm = 0x4E8140
movslq (%rcx), %rcx
movq (%r13,%rcx,8), %rcx
leaq 0xb22f1(%rip), %rsi     # 0x4b413c
movl %eax, %edx
xorl %eax, %eax
callq 0x40d290
movq (%rbx), %rdi
leaq 0xb2308(%rip), %rsi     # 0x4b4166
leaq 0xc97e8(%rip), %rdx     # 0x4cb64d
leaq 0xb2235(%rip), %rcx     # 0x4b40a1
movl $0xc3, %r8d
xorl %eax, %eax
callq 0x40d290
pushq $0x1
popq %rdi
callq 0x40c1f0
movq $0x4e77a8, %rbx         # imm = 0x4E77A8
movq (%rbx), %rdi
movq (%r13), %rdx
leaq 0xb22eb(%rip), %rsi     # 0x4b4181
xorl %eax, %eax
callq 0x40d290
movq (%rbx), %rdi
leaq 0xb22bf(%rip), %rsi     # 0x4b4166
leaq 0xc979f(%rip), %rdx     # 0x4cb64d
leaq 0xb21ec(%rip), %rcx     # 0x4b40a1
movl $0xcb, %r8d
jmp 0x401e72