// File: /workspace/github_c_makefile_star2_O0/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
subq $0xe0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq 0xb28b0(%rip), %rax     # 0x4b4050
movq %rax, -0x18(%rbp)
movl $0x1, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
leaq 0xe5949(%rip), %rdi     # 0x4e7110
callq 0x403300
movq %rax, -0x30(%rbp)
movq $0x4e8160, %rax         # imm = 0x4E8160
movl $0x1, (%rax)
movl $0x0, -0x34(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
movq -0x30(%rbp), %rdx
leaq 0xe591a(%rip), %rcx     # 0x4e7110
leaq -0x34(%rbp), %r8
callq 0x45c280
movl %eax, -0x38(%rbp)
cmpl $-0x1, -0x38(%rbp)
jne 0x40180d
jmp 0x4018f2
movl -0x38(%rbp), %eax
addl $-0x62, %eax
movl %eax, %ecx
movq %rcx, -0xc0(%rbp)
subl $0x12, %eax
ja 0x401888
movq -0xc0(%rbp), %rax
leaq 0xb27d5(%rip), %rcx     # 0x4b4004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq $0x4eea80, %rax         # imm = 0x4EEA80
movq (%rax), %rdi
callq 0x40bc30
movl %eax, -0x28(%rbp)
jmp 0x4018ed
movq $0x4eea80, %rax         # imm = 0x4EEA80
movq (%rax), %rdi
callq 0x40bc30
movl %eax, -0x1c(%rbp)
jmp 0x4018ed
movl $0x1, -0x24(%rbp)
jmp 0x4018ed
movl $0x1, -0x20(%rbp)
jmp 0x4018ed
movq $0x4eea80, %rax         # imm = 0x4EEA80
movq (%rax), %rax
movq %rax, -0x18(%rbp)
jmp 0x4018ed
jmp 0x40188a
movq $0x4e77c8, %rax         # imm = 0x4E77C8
movq (%rax), %rdi
movl -0x38(%rbp), %edx
movq -0x10(%rbp), %rax
movq $0x4e8160, %rcx         # imm = 0x4E8160
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rcx
leaq 0xb27d8(%rip), %rsi     # 0x4b4088
movb $0x0, %al
callq 0x40d850
movq $0x4e77c8, %rax         # imm = 0x4E77C8
movq (%rax), %rdi
leaq 0xb27ea(%rip), %rsi     # 0x4b40b2
leaq 0xc9dbe(%rip), %rdx     # 0x4cb68d
leaq 0xb27f7(%rip), %rcx     # 0x4b40cd
movl $0x80, %r8d
movb $0x0, %al
callq 0x40d850
movl $0x1, %edi
callq 0x40c7b0
jmp 0x4017dd
movq $0x4e8160, %rax         # imm = 0x4E8160
movl (%rax), %eax
cmpl -0x8(%rbp), %eax
je 0x401957
jmp 0x401902
movq $0x4e77c8, %rax         # imm = 0x4E77C8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0xb27bf(%rip), %rsi     # 0x4b40d9
movb $0x0, %al
callq 0x40d850
movq $0x4e77c8, %rax         # imm = 0x4E77C8
movq (%rax), %rdi
leaq 0xb2780(%rip), %rsi     # 0x4b40b2
leaq 0xc9d54(%rip), %rdx     # 0x4cb68d
leaq 0xb278d(%rip), %rcx     # 0x4b40cd
movl $0x87, %r8d
movb $0x0, %al
callq 0x40d850
movl $0x1, %edi
callq 0x40c7b0
movq -0x18(%rbp), %rsi
leaq -0x78(%rbp), %rdi
callq 0x401e90
movl $0xffffffff, -0x7c(%rbp) # imm = 0xFFFFFFFF
cmpl $0x0, -0x20(%rbp)
jne 0x4019c5
movq $0x4e77c8, %rax         # imm = 0x4E77C8
movq (%rax), %rax
movq %rax, -0xc8(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x402170
movq -0xc8(%rbp), %rdi
movq %rax, %rdx
movl -0x24(%rbp), %ecx
movl -0x28(%rbp), %r8d
movl -0x1c(%rbp), %r9d
movl -0x20(%rbp), %eax
leaq 0xb2792(%rip), %rsi     # 0x4b413c
movl %eax, (%rsp)
movb $0x0, %al
callq 0x40d850
movl -0x28(%rbp), %edx
leaq -0x78(%rbp), %rdi
xorl %esi, %esi
callq 0x402240
movl %eax, -0x7c(%rbp)
movslq -0x1c(%rbp), %rdi
movl $0x1800, %esi           # imm = 0x1800
callq 0x426bd0
movq %rax, -0x88(%rbp)
movl $0x0, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x401abe
movq -0x88(%rbp), %rax
movslq -0x8c(%rbp), %rcx
imulq $0x1800, %rcx, %rcx     # imm = 0x1800
addq %rcx, %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rdi
callq 0x4016e0
movl -0x24(%rbp), %ecx
movq -0x98(%rbp), %rax
movl %ecx, 0x4(%rax)
cmpl $0x0, -0x20(%rbp)
jne 0x401a3f
movl -0x7c(%rbp), %ecx
movq -0x98(%rbp), %rax
movl %ecx, (%rax)
jmp 0x401a97
movq $0x4e77c8, %rax         # imm = 0x4E77C8
movq (%rax), %rax
movq %rax, -0xd0(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x402170
movq -0xd0(%rbp), %rdi
movq %rax, %rdx
leaq 0xb2726(%rip), %rsi     # 0x4b4190
movb $0x0, %al
callq 0x40d850
movl -0x28(%rbp), %edx
leaq -0x78(%rbp), %rdi
movl $0x1, %esi
callq 0x402240
movl %eax, -0x9c(%rbp)
movl -0x9c(%rbp), %ecx
movq -0x98(%rbp), %rax
movl %ecx, (%rax)
movq -0x98(%rbp), %rsi
leaq 0x8b(%rip), %rdi        # 0x401b30
callq 0x402930
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x4019e4
jmp 0x401ac0
movq 0xb2761(%rip), %rax     # 0x4b4228
movq %rax, -0xb0(%rbp)
movq 0xb275b(%rip), %rax     # 0x4b4230
movq %rax, -0xa8(%rbp)
xorl %edi, %edi
xorl %eax, %eax
movl %eax, %ecx
leaq -0xb0(%rbp), %r8
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x45d4c0
movl %eax, -0xb4(%rbp)
cmpl $0x0, -0xb4(%rbp)
je 0x401b05
jmp 0x401adc
imulq $0x3b9aca00, -0xb0(%rbp), %rax # imm = 0x3B9ACA00
imulq $0x3e8, -0xa8(%rbp), %rcx # imm = 0x3E8
addq %rcx, %rax
cmpq $0x0, %rax
jne 0x401b26
jmp 0x401b28
jmp 0x401adc
jmp 0x401ac0
nopw (%rax,%rax)