// File: /workspace/github_c_makefile_star2_O1/unixlab[P]cloudflare-udp-ping/udpserver.c
// Function: main
int main(int argc, const char *argv[])
{
	const char *listen_addr_str = "0.0.0.0:4321";
	int thread_num = 1;
	int reuseport = 0;
	int polling = 0;
	int busy_poll = 0;

	static struct option long_options[] = {
		{"busy-poll", required_argument, 0, 'b'},
		{"threads", required_argument, 0, 't'},
		{"polling", no_argument, 0, 'p'},
		{"reuseport", no_argument, 0, 'r'},
		{"listen", required_argument, 0, 'l'},
		{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 'b':
			busy_poll = atoi(optarg);
			break;

		case 't':
			thread_num = atoi(optarg);
			break;

		case 'p':
			polling = 1;
			break;

		case 'r':
			reuseport = 1;
			break;

		case 'l':
			listen_addr_str = optarg;
			break;

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

	if (optind != argc) {
		FATAL("Usage: %s [--buf-size=<>] [--threads=<>] [--polling] "
		      "[--reuseport] [--listen=]",
		      argv[0]);
	}

	struct net_addr listen_addr;
	parse_addr(&listen_addr, listen_addr_str);

	int main_fd = -1;
	if (reuseport == 0) {
		fprintf(stderr,
			"[*] Starting udpreceiver on %s, "
			"polling=%i, busy_poll=%i, threads=%i, reuseport=%i\n",
			addr_to_str(&listen_addr), polling, busy_poll,
			thread_num, reuseport);

		main_fd = net_bind_udp(&listen_addr, 0, busy_poll);
	}

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

	int t;
	for (t = 0; t < thread_num; t++) {
		struct state *state = &array_of_states[t];
		state_init(state);
		state->polling = polling;
		if (reuseport == 0) {
			state->fd = main_fd;
		} else {
			fprintf(stderr, "[*] Starting udpreceiver on %s\n",
				addr_to_str(&listen_addr));

			int fd = net_bind_udp(&listen_addr, 1, busy_poll);
			state->fd = fd;
		}
		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;
			}
		}
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, %r12
movl %edi, %r13d
leaq 0xe4f06(%rip), %rdi     # 0x4e7110
callq 0x402dd0
movq %rax, %r15
movq $0x4e8160, %rax         # imm = 0x4E8160
movl $0x1, (%rax)
leaq 0xb1e06(%rip), %rax     # 0x4b402c
movq %rax, -0x40(%rbp)
movl $0x1, -0x38(%rbp)
movl $0x0, -0x2c(%rbp)
leaq 0xb1dc5(%rip), %r14     # 0x4b4004
movl $0x0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x90(%rbp)
movl %r13d, %edi
movq %r12, %rsi
movq %r15, %rdx
leaq 0xe4ea9(%rip), %rcx     # 0x4e7110
leaq -0x90(%rbp), %r8
callq 0x45bfc0
movl %eax, %ebx
cmpl $-0x1, %eax
je 0x4022d9
leal -0x62(%rbx), %eax
rorl %eax
cmpl $0x9, %eax
ja 0x40247e
movslq (%r14,%rax,4), %rax
addq %r14, %rax
jmpq *%rax
movq $0x4eea80, %rax         # imm = 0x4EEA80
movq (%rax), %rdi
callq 0x40b690
movl %eax, -0x2c(%rbp)
jmp 0x4022d9
movq $0x4eea80, %rax         # imm = 0x4EEA80
movq (%rax), %rdi
callq 0x40b690
movl %eax, -0x38(%rbp)
jmp 0x4022d9
movl $0x1, -0x30(%rbp)
jmp 0x4022d9
movl $0x1, -0x34(%rbp)
jmp 0x4022d9
movq $0x4eea80, %rax         # imm = 0x4EEA80
movq (%rax), %rax
movq %rax, -0x40(%rbp)
cmpl $-0x1, %ebx
jne 0x40224d
movq $0x4e8160, %rax         # imm = 0x4E8160
cmpl %r13d, (%rax)
jne 0x4024c6
leaq -0x90(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x402687
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpl $0x0, -0x34(%rbp)
movl -0x30(%rbp), %r13d
movl -0x38(%rbp), %r15d
je 0x40242b
movslq %r15d, %rdi
movl $0x1800, %esi           # imm = 0x1800
callq 0x426900
testl %r15d, %r15d
jle 0x4023f4
movq %rax, %r14
movl %r15d, %r12d
leaq 0x1d5(%rip), %r15       # 0x402511
leaq 0x8(%r14), %rax
leaq 0x7e0(%r14), %rcx
leaq 0x7f0(%r14), %rdx
leaq 0x60(%r14), %rsi
movq %rax, 0x28(%r14)
movl $0x1c, 0x30(%r14)
movq %rcx, 0x38(%r14)
movq $0x1, 0x40(%r14)
movq %rdx, 0x48(%r14)
movq $0x1000, 0x50(%r14)     # imm = 0x1000
movq %rsi, 0x7e0(%r14)
movq $0x780, 0x7e8(%r14) # imm = 0x780
movl %r13d, 0x4(%r14)
movl %ebx, %eax
cmpl $0x0, -0x34(%rbp)
jne 0x4023b0
movl %eax, (%r14)
movq %r15, %rdi
movq %r14, %rsi
callq 0x402a40
addq $0x1800, %r14           # imm = 0x1800
decq %r12
jne 0x40233c
jmp 0x4023f4
movq $0x4e77c8, %rax         # imm = 0x4E77C8
movq (%rax), %r13
leaq -0x90(%rbp), %rdi
callq 0x4027e4
movq %r13, %rdi
movl -0x30(%rbp), %r13d
leaq 0xb1d98(%rip), %rsi     # 0x4b416c
movq %rax, %rdx
xorl %eax, %eax
callq 0x40d2b0
leaq -0x90(%rbp), %rdi
movl $0x1, %esi
movl -0x2c(%rbp), %edx
callq 0x402871
jmp 0x402394
movups 0xb1e05(%rip), %xmm0    # 0x4b4200
movaps %xmm0, -0x50(%rbp)
leaq -0x50(%rbp), %r8
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x45d200
testl %eax, %eax
jne 0x4023ff
imulq $0x3b9aca00, -0x50(%rbp), %rax # imm = 0x3B9ACA00
imulq $-0x3e8, -0x48(%rbp), %rcx # imm = 0xFC18
cmpq %rcx, %rax
jne 0x4023ff
jmp 0x4023f4
movq $0x4e77c8, %rax         # imm = 0x4E77C8
movq (%rax), %rbx
leaq -0x90(%rbp), %r14
movq %r14, %rdi
callq 0x4027e4
movl $0x0, (%rsp)
leaq 0xb1cc6(%rip), %rsi     # 0x4b4118
movq %rbx, %rdi
movq %rax, %rdx
movl %r13d, %ecx
movl -0x2c(%rbp), %ebx
movl %ebx, %r8d
movl %r15d, %r9d
xorl %eax, %eax
callq 0x40d2b0
movq %r14, %rdi
xorl %esi, %esi
movl %ebx, %edx
callq 0x402871
movl %eax, %ebx
jmp 0x402319
movq $0x4e77c8, %r14         # imm = 0x4E77C8
movq (%r14), %rdi
movq $0x4e8160, %rax         # imm = 0x4E8160
movslq (%rax), %rax
movq (%r12,%rax,8), %rcx
leaq 0xb1bc7(%rip), %rsi     # 0x4b4064
movl %ebx, %edx
xorl %eax, %eax
callq 0x40d2b0
movq (%r14), %rdi
leaq 0xb1bde(%rip), %rsi     # 0x4b408e
leaq 0xc9176(%rip), %rdx     # 0x4cb62d
leaq 0xb1beb(%rip), %rcx     # 0x4b40a9
movl $0x80, %r8d
jmp 0x402500
movq $0x4e77c8, %rbx         # imm = 0x4E77C8
movq (%rbx), %rdi
movq (%r12), %rdx
leaq 0xb1bda(%rip), %rsi     # 0x4b40b5
xorl %eax, %eax
callq 0x40d2b0
movq (%rbx), %rdi
leaq 0xb1ba2(%rip), %rsi     # 0x4b408e
leaq 0xc913a(%rip), %rdx     # 0x4cb62d
leaq 0xb1baf(%rip), %rcx     # 0x4b40a9
movl $0x87, %r8d
xorl %eax, %eax
callq 0x40d2b0
movl $0x1, %edi
callq 0x40c210