// File: /workspace/github_c_makefile_star2_O0/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
subq $0xc0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0xffdc, -0x14(%rbp)    # imm = 0xFFDC
movl $0x0, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
leaq 0xe63a4(%rip), %rdi     # 0x4e8110
callq 0x403980
movq %rax, -0x28(%rbp)
movq $0x4e9140, %rax         # imm = 0x4E9140
movl $0x1, (%rax)
movl $0x0, -0x2c(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rdx
leaq 0xe6375(%rip), %rcx     # 0x4e8110
leaq -0x2c(%rbp), %r8
callq 0x45caf0
movl %eax, -0x30(%rbp)
cmpl $-0x1, -0x30(%rbp)
jne 0x401db2
jmp 0x401e87
movl -0x30(%rbp), %eax
addl $-0x62, %eax
movl %eax, %ecx
movq %rcx, -0xb8(%rbp)
subl $0x12, %eax
ja 0x401e1d
movq -0xb8(%rbp), %rax
leaq 0xb323c(%rip), %rcx     # 0x4b5010
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq $0x4efa60, %rax         # imm = 0x4EFA60
movq (%rax), %rdi
callq 0x40c2f0
movl %eax, -0x14(%rbp)
jmp 0x401e82
movl $0x1, -0x18(%rbp)
jmp 0x401e82
movq $0x4efa60, %rax         # imm = 0x4EFA60
movq (%rax), %rdi
callq 0x40c2f0
movl %eax, -0x1c(%rbp)
jmp 0x401e82
movl $0x1, -0x20(%rbp)
jmp 0x401e82
jmp 0x401e1f
movq $0x4e87a8, %rax         # imm = 0x4E87A8
movq (%rax), %rdi
movl -0x30(%rbp), %edx
movq -0x10(%rbp), %rax
movq $0x4e9140, %rcx         # imm = 0x4E9140
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rcx
leaq 0xb3347(%rip), %rsi     # 0x4b518c
movb $0x0, %al
callq 0x40df10
movq $0x4e87a8, %rax         # imm = 0x4E87A8
movq (%rax), %rdi
leaq 0xb3359(%rip), %rsi     # 0x4b51b6
leaq 0xca889(%rip), %rdx     # 0x4cc6ed
leaq 0xb3286(%rip), %rcx     # 0x4b50f1
movl $0xc3, %r8d
movb $0x0, %al
callq 0x40df10
movl $0x1, %edi
callq 0x40ce70
jmp 0x401d82
movl -0x8(%rbp), %eax
movq $0x4e9140, %rcx         # imm = 0x4E9140
subl (%rcx), %eax
movl %eax, -0x34(%rbp)
cmpl $0x1, -0x34(%rbp)
jge 0x401ef3
jmp 0x401e9e
movq $0x4e87a8, %rax         # imm = 0x4E87A8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0xb331b(%rip), %rsi     # 0x4b51d1
movb $0x0, %al
callq 0x40df10
movq $0x4e87a8, %rax         # imm = 0x4E87A8
movq (%rax), %rdi
leaq 0xb32e8(%rip), %rsi     # 0x4b51b6
leaq 0xca818(%rip), %rdx     # 0x4cc6ed
leaq 0xb3215(%rip), %rcx     # 0x4b50f1
movl $0xcb, %r8d
movb $0x0, %al
callq 0x40df10
movl $0x1, %edi
callq 0x40ce70
movslq -0x34(%rbp), %rdi
movl $0x40, %esi
callq 0x427390
movq %rax, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x401fa1
movq -0x10(%rbp), %rax
movq $0x4e9140, %rcx         # imm = 0x4E9140
movl (%rcx), %ecx
addl -0x44(%rbp), %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x50(%rbp)
movq -0x40(%rbp), %rdi
movslq -0x44(%rbp), %rax
shlq $0x6, %rax
addq %rax, %rdi
movq -0x50(%rbp), %rsi
callq 0x402510
movq $0x4e87a8, %rax         # imm = 0x4E87A8
movq (%rax), %rax
movq %rax, -0xc0(%rbp)
movq -0x40(%rbp), %rdi
movslq -0x44(%rbp), %rax
shlq $0x6, %rax
addq %rax, %rdi
callq 0x4027f0
movq -0xc0(%rbp), %rdi
movq %rax, %rdx
movl -0x18(%rbp), %ecx
movl -0x14(%rbp), %r8d
addl -0x44(%rbp), %r8d
leaq 0xb32af(%rip), %rsi     # 0x4b523b
movb $0x0, %al
callq 0x40df10
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x401f0c
movslq -0x34(%rbp), %rdi
movl $0x58, %esi
callq 0x427390
movq %rax, -0x58(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x40202d
movq -0x58(%rbp), %rax
movslq -0x44(%rbp), %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x40(%rbp), %rcx
movslq -0x44(%rbp), %rax
shlq $0x6, %rax
addq %rax, %rcx
movq -0x60(%rbp), %rax
movq %rcx, (%rax)
movl -0x18(%rbp), %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0xc(%rax)
movl -0x1c(%rbp), %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0x10(%rax)
cmpl $0x0, -0x14(%rbp)
jle 0x402012
movl -0x14(%rbp), %ecx
addl -0x44(%rbp), %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x60(%rbp), %rsi
leaq -0x93d(%rip), %rdi      # 0x4016e0
callq 0x402fb0
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x401fba
jmp 0x40202f
movq 0xb305a(%rip), %rax     # 0x4b5090
movq %rax, -0x70(%rbp)
movq 0xb3057(%rip), %rax     # 0x4b5098
movq %rax, -0x68(%rbp)
xorl %edi, %edi
xorl %eax, %eax
movl %eax, %ecx
leaq -0x70(%rbp), %r8
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x45dd30
movl %eax, -0x74(%rbp)
cmpl $0x0, -0x74(%rbp)
je 0x402065
jmp 0x402045
imulq $0x3b9aca00, -0x70(%rbp), %rax # imm = 0x3B9ACA00
imulq $0x3e8, -0x68(%rbp), %rcx # imm = 0x3E8
addq %rcx, %rax
cmpq $0x0, %rax
jne 0x402080
jmp 0x402082
jmp 0x402045
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x4021c7
movq -0x58(%rbp), %rax
movslq -0x44(%rbp), %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rdi
addq $0x14, %rdi
callq 0x4214e0
movq -0x80(%rbp), %rdi
addq $0x18, %rdi
leaq -0x88(%rbp), %rsi
leaq -0x90(%rbp), %rdx
leaq -0x98(%rbp), %rcx
callq 0x4021e0
movq -0x80(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0xac(%rbp)
movq -0x80(%rbp), %rdi
addq $0x38, %rdi
xorl %eax, %eax
movl %eax, %esi
leaq -0xa0(%rbp), %rdx
leaq -0xa8(%rbp), %rcx
callq 0x4021e0
movq -0x80(%rbp), %rdi
addq $0x18, %rdi
callq 0x402340
movq -0x80(%rbp), %rdi
addq $0x38, %rdi
callq 0x402340
movq -0x80(%rbp), %rdi
addq $0x14, %rdi
callq 0x421500
movq -0x88(%rbp), %rsi
movsd -0x90(%rbp), %xmm0
movsd 0xb2ec7(%rip), %xmm1    # 0x4b5008
divsd %xmm1, %xmm0
movsd -0x98(%rbp), %xmm1
movsd 0xb2eb3(%rip), %xmm2    # 0x4b5008
divsd %xmm2, %xmm1
cvtsi2sdl -0xac(%rbp), %xmm2
movsd 0xb2e9f(%rip), %xmm3    # 0x4b5008
divsd %xmm3, %xmm2
leaq 0xb30f3(%rip), %rdi     # 0x4b5267
movb $0x3, %al
callq 0x40dfd0
cmpl $0x0, -0x20(%rbp)
je 0x4021b7
movsd -0xa0(%rbp), %xmm0
movsd 0xb2e77(%rip), %xmm1    # 0x4b5008
divsd %xmm1, %xmm0
movsd -0xa8(%rbp), %xmm1
movsd 0xb2e63(%rip), %xmm2    # 0x4b5008
divsd %xmm2, %xmm1
leaq 0xb30e6(%rip), %rdi     # 0x4b5296
movb $0x2, %al
callq 0x40dfd0
jmp 0x4021b9
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x402089
leaq 0xb393c(%rip), %rdi     # 0x4b5b0a
movb $0x0, %al
callq 0x40dfd0
jmp 0x40202f
nopw (%rax,%rax)