// File: /workspace/github_c_makefile_star2_O3/open-mesh-mirror[P]batmand/linux/route.c
// Function: flush_routes_rules
int flush_routes_rules(int8_t is_rule)
{
	int netlink_sock, rtl;
	size_t len;
	int32_t dest = 0, router = 0, ifi = 0;
	uint32_t prio = 0;
	int8_t rule_type = 0;
	char buf[8192], *dev = NULL;
	struct sockaddr_nl nladdr;
	struct iovec iov;
	struct msghdr msg;
	struct nlmsghdr *nh;
	struct rtmsg *rtm;
	struct req_s {
		struct rtmsg rtm;
	} *req;
	char req_buf[NLMSG_LENGTH(sizeof(struct req_s))] ALIGN_WORD;

	struct rtattr *rtap;

	iov.iov_base = buf;
	iov.iov_len  = sizeof(buf);

	nh = (struct nlmsghdr *)req_buf;
	req = (struct req_s*)NLMSG_DATA(req_buf);
	memset(&nladdr, 0, sizeof(struct sockaddr_nl));
	memset(req_buf, 0, NLMSG_LENGTH(sizeof(struct req_s)));
	memset(&msg, 0, sizeof(struct msghdr));

	nladdr.nl_family = AF_NETLINK;

	nh->nlmsg_len = NLMSG_LENGTH(sizeof(struct req_s));
	nh->nlmsg_pid = getpid();
	req->rtm.rtm_family = AF_INET;

	nh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
	nh->nlmsg_type = (is_rule ? RTM_GETRULE : RTM_GETROUTE);
	req->rtm.rtm_scope = RTN_UNICAST;

	if ((netlink_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0) {

		debug_output(0, "Error - can't create netlink socket for flushing the routing table: %s\n", strerror(errno));
		return -1;

	}


	if (sendto(netlink_sock, req_buf, nh->nlmsg_len, 0, (struct sockaddr *)&nladdr, sizeof(struct sockaddr_nl)) < 0) {

		debug_output(0, "Error - can't send message to kernel via netlink socket for flushing the routing table: %s\n", strerror(errno));
		close(netlink_sock);
		return -1;

	}

	msg.msg_name = (void *)&nladdr;
	msg.msg_namelen = sizeof(nladdr);
	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;
	msg.msg_control = NULL;

	len = recvmsg(netlink_sock, &msg, 0);
	nh = (struct nlmsghdr *)buf;

	while (NLMSG_OK(nh, len)) {

		if (nh->nlmsg_type == NLMSG_DONE)
			break;

		if ((nh->nlmsg_type == NLMSG_ERROR) && (((struct nlmsgerr*)NLMSG_DATA(nh))->error != 0)) {

			debug_output(0, "Error - can't flush %s: %s \n", (is_rule ? "routing rules" : "routing table"), strerror(-((struct nlmsgerr*)NLMSG_DATA(nh))->error));
			close(netlink_sock);
			return -1;

		}

		rtm = (struct rtmsg *)NLMSG_DATA(nh);
		rtap = (struct rtattr *)RTM_RTA(rtm);
		rtl = RTM_PAYLOAD(nh);

		nh = NLMSG_NEXT(nh, len);

		if ((rtm->rtm_table != BATMAN_RT_TABLE_UNREACH) && (rtm->rtm_table != BATMAN_RT_TABLE_NETWORKS) &&
			(rtm->rtm_table != BATMAN_RT_TABLE_HOSTS) && (rtm->rtm_table != BATMAN_RT_TABLE_TUNNEL))
			continue;

		while (RTA_OK(rtap, rtl)) {

			switch(rtap->rta_type) {

				case RTA_DST:
					dest = *((int32_t *)RTA_DATA(rtap));
					rule_type = ROUTE_TYPE_UNREACHABLE;
					break;

				case RTA_SRC:
					dest = *((int32_t *)RTA_DATA(rtap));
					rule_type = ROUTE_TYPE_UNICAST;
					break;

				case RTA_GATEWAY:
					router = *((int32_t *)RTA_DATA(rtap));
					break;

				case RTA_OIF:
					ifi = *((int32_t *)RTA_DATA(rtap));
					break;

				case RTA_PRIORITY:
					prio = *((uint32_t *)RTA_DATA(rtap));
					break;

				case RTA_IIF:
					dev = ((char *)RTA_DATA(rtap));
					rule_type = ROUTE_TYPE_THROW;
					break;

				case 15:  /* FIXME: RTA_TABLE is not always available - not needed but avoid warning */
					break;

				case RTA_PREFSRC:  /* rta_type 7 - not needed but avoid warning */
					break;

				default:
					debug_output(0, "Error - unknown rta type: %i \n", rtap->rta_type);
					break;

			}

			rtap = RTA_NEXT(rtap,rtl);

		}

		if (is_rule) {
			switch (rule_type) {
			case ROUTE_TYPE_UNICAST:
				add_del_rule(dest, rtm->rtm_src_len, rtm->rtm_table, prio, 0 , rule_type, RULE_DEL);
				break;
			case ROUTE_TYPE_THROW:
				add_del_rule(dest, rtm->rtm_dst_len, rtm->rtm_table, prio, 0 , rule_type, RULE_DEL);
				break;
			case ROUTE_TYPE_UNREACHABLE:
				add_del_rule(0 , 0, rtm->rtm_table, prio, dev , rule_type, RULE_DEL);
				break;
			}
		} else {
			switch (rtm->rtm_type) {
			case RTN_UNICAST:
				rule_type = ROUTE_TYPE_UNICAST;
				break;
			case RTN_THROW:
				rule_type = ROUTE_TYPE_THROW;
				break;
			case RTN_UNREACHABLE:
				rule_type = ROUTE_TYPE_UNREACHABLE;
				break;
			default:
				rule_type = ROUTE_TYPE_UNKNOWN;
				break;
			}

			/* sometimes dest and router are not reset */
			if (rule_type == ROUTE_TYPE_UNREACHABLE)
				dest = router = 0;

			add_del_route(dest, rtm->rtm_dst_len, router, 0, ifi, "unknown", rtm->rtm_table, rule_type, ROUTE_DEL);
		}

	}

	close(netlink_sock);
	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x20c8, %rsp           # imm = 0x20C8
movl %edi, %ebp
leaq 0xc0(%rsp), %r12
movq %r12, 0x78(%rsp)
movq $0x2000, 0x80(%rsp) # imm = 0x2000
xorl %eax, %eax
movl %eax, 0x50(%rsp)
movq $0x0, 0x48(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x60(%rsp)
movl %eax, 0x70(%rsp)
movups %xmm0, 0x90(%rsp)
movups %xmm0, 0xa0(%rsp)
movups %xmm0, 0xb0(%rsp)
movw $0x10, 0x48(%rsp)
movl $0x1c, 0x58(%rsp)
callq 0x2100
movl %eax, 0x64(%rsp)
movb $0x2, 0x68(%rsp)
movw $0x301, 0x5e(%rsp)      # imm = 0x301
xorl %eax, %eax
testb %bpl, %bpl
setne %al
leal 0x1a(,%rax,8), %eax
movw %ax, 0x5c(%rsp)
movb $0x1, 0x6e(%rsp)
movl $0x10, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x24e0
testl %eax, %eax
js 0x8e80
movl %eax, %ebx
leaq 0x58(%rsp), %rsi
leaq 0x48(%rsp), %r14
movl $0x1c, %edx
movl %eax, %edi
xorl %ecx, %ecx
movq %r14, %r8
movl $0xc, %r9d
callq 0x21e0
testq %rax, %rax
js 0x8eb1
leaq 0x88(%rsp), %rsi
movq %r14, (%rsi)
movl $0xc, 0x8(%rsi)
leaq 0x78(%rsp), %rax
movq %rax, 0x10(%rsi)
movq $0x1, 0x18(%rsi)
movq $0x0, 0x20(%rsi)
movl %ebx, 0x34(%rsp)
movl %ebx, %edi
xorl %edx, %edx
callq 0x2360
movq %rax, %r14
leaq 0x885a(%rip), %rbx      # 0x113ba
leaq 0x82f9(%rip), %rsi      # 0x10e60
xorl %r9d, %r9d
xorl %r11d, %r11d
xorl %r8d, %r8d
xorl %ecx, %ecx
movq $0x0, 0x28(%rsp)
xorl %r10d, %r10d
cmpq $0x10, %r14
jb 0x8ea1
movq %r12, %r13
movl (%r12), %r15d
cmpq $0x10, %r15
jb 0x8ea1
cmpq %r15, %r14
jb 0x8ea1
movzwl 0x4(%r13), %eax
cmpl $0x2, %eax
je 0x8bb6
cmpl $0x3, %eax
jne 0x8bc2
jmp 0x8ea1
movl 0x10(%r13), %edi
testl %edi, %edi
jne 0x8ed4
leal 0x3(%r15), %r12d
andl $-0x4, %r12d
subq %r12, %r14
addq %r13, %r12
movb 0x14(%r13), %al
addb $-0x41, %al
cmpb $0x3, %al
ja 0x8b7e
movl %ebp, 0x44(%rsp)
addl $-0x1c, %r15d
cmpl $0x4, %r15d
jl 0x8cad
leaq 0x1c(%r13), %rbp
movzwl (%rbp), %eax
cmpl $0x4, %eax
jb 0x8cad
cmpl %eax, %r15d
jb 0x8cad
movzwl 0x2(%rbp), %edx
leal -0x1(%rdx), %edi
cmpl $0xe, %edi
ja 0x8c23
movslq (%rsi,%rdi,4), %rdi
addq %rsi, %rdi
jmpq *%rdi
movl 0x4(%rbp), %r9d
movb $0x2, %dl
jmp 0x8c92
xorl %edi, %edi
movq %rbx, %rsi
xorl %eax, %eax
movl %r8d, 0x24(%rsp)
movl %ecx, 0x1c(%rsp)
movl %r9d, 0x40(%rsp)
movq %r10, 0x38(%rsp)
movl %r11d, 0x20(%rsp)
callq 0xd88c
movl 0x20(%rsp), %r11d
movq 0x38(%rsp), %r10
movl 0x40(%rsp), %r9d
movl 0x1c(%rsp), %ecx
movl 0x24(%rsp), %r8d
leaq 0x81fa(%rip), %rsi      # 0x10e60
movzwl (%rbp), %eax
jmp 0x8c97
movl 0x4(%rbp), %r8d
jmp 0x8c97
movl 0x4(%rbp), %r11d
jmp 0x8c97
movl 0x4(%rbp), %ecx
jmp 0x8c97
movl 0x4(%rbp), %r9d
movq $0x0, 0x28(%rsp)
jmp 0x8c97
leaq 0x4(%rbp), %r10
movb $0x1, %dl
movq %rdx, 0x28(%rsp)
addl $0x3, %eax
andl $-0x4, %eax
subl %eax, %r15d
addq %rax, %rbp
cmpl $0x3, %r15d
jg 0x8bf0
movl 0x44(%rsp), %ebp
testb %bpl, %bpl
je 0x8d51
movl %r11d, 0x20(%rsp)
movl %r8d, 0x24(%rsp)
movq %rbx, %r15
movq 0x28(%rsp), %rax
cmpb $0x2, %al
je 0x8dbb
movzbl %al, %eax
cmpl $0x1, %eax
je 0x8d81
testl %eax, %eax
movq %r15, %rbx
leaq 0x8174(%rip), %rsi      # 0x10e60
movl 0x24(%rsp), %r8d
movl 0x20(%rsp), %r11d
jne 0x8b7e
movzbl 0x12(%r13), %esi
movsbl 0x14(%r13), %edx
movl $0x1, (%rsp)
movq $0x0, 0x28(%rsp)
movl %r9d, %edi
xorl %r8d, %r8d
movl %ecx, 0x1c(%rsp)
movl %r9d, %r13d
xorl %r9d, %r9d
movq %r10, %rbx
callq 0x82f9
movl 0x20(%rsp), %r11d
movq %rbx, %r10
movl %r13d, %r9d
movl 0x1c(%rsp), %ecx
movl 0x24(%rsp), %r8d
leaq 0x8117(%rip), %rsi      # 0x10e60
movq %r15, %rbx
jmp 0x8b7e
movzbl 0x17(%r13), %eax
cmpl $0x1, %eax
movl %ecx, 0x1c(%rsp)
movq %r10, 0x38(%rsp)
je 0x8e17
cmpl $0x7, %eax
je 0x8e0d
cmpl $0x9, %eax
jne 0x8e1b
movb $0x1, %cl
jmp 0x8e1d
movzbl 0x11(%r13), %esi
movsbl 0x14(%r13), %edx
movl $0x1, (%rsp)
movl %r9d, %edi
xorl %r8d, %r8d
movl %ecx, 0x1c(%rsp)
movl %r9d, %r13d
movl $0x1, %r9d
movq %r10, %rbx
callq 0x82f9
movq %rbx, %r10
movl %r13d, %r9d
movl 0x1c(%rsp), %ecx
movb $0x1, %al
jmp 0x8def
movsbl 0x14(%r13), %edx
movl $0x1, (%rsp)
xorl %edi, %edi
xorl %esi, %esi
movq %r10, %r8
movl %ecx, 0x1c(%rsp)
movl %r9d, %r13d
movl $0x2, %r9d
movq %r10, %rbx
callq 0x82f9
movq %rbx, %r10
movl %r13d, %r9d
movl 0x1c(%rsp), %ecx
movb $0x2, %al
movq %rax, 0x28(%rsp)
movq %r15, %rbx
leaq 0x8062(%rip), %rsi      # 0x10e60
movl 0x24(%rsp), %r8d
movl 0x20(%rsp), %r11d
jmp 0x8b7e
movb $0x2, %cl
xorl %r9d, %r9d
xorl %r11d, %r11d
jmp 0x8e1d
xorl %ecx, %ecx
jmp 0x8e1d
movb $0x3, %cl
movzbl 0x11(%r13), %esi
movzbl 0x14(%r13), %eax
movq %rcx, 0x28(%rsp)
movzbl %cl, %ecx
movl %ecx, 0x8(%rsp)
movl %eax, (%rsp)
movl $0x1, 0x10(%rsp)
movl %r9d, %edi
movl %r11d, %edx
xorl %ecx, %ecx
movl %r8d, %r13d
movl %r9d, %ebx
leaq 0x7f0f(%rip), %r9       # 0x10d62
movl %r11d, %r15d
callq 0x7cd4
movl %r15d, %r11d
movl %ebx, %r9d
movl %r13d, %r8d
leaq 0x854f(%rip), %rbx      # 0x113ba
leaq 0x7fee(%rip), %rsi      # 0x10e60
movl 0x1c(%rsp), %ecx
movq 0x38(%rsp), %r10
jmp 0x8b7e
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x844a(%rip), %rsi      # 0x112dd
xorl %edi, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0xd88c
jmp 0x8f0f
movl 0x34(%rsp), %edi
callq 0x21f0
movl $0x1, %eax
jmp 0x8f14
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x8461(%rip), %rsi      # 0x11325
xorl %edi, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0xd88c
movl %ebx, %edi
jmp 0x8f0a
testb %bpl, %bpl
leaq 0x84ce(%rip), %rax      # 0x113ac
leaq 0x84b9(%rip), %r14      # 0x1139e
cmoveq %rax, %r14
negl %edi
callq 0x2480
leaq 0x848a(%rip), %rsi      # 0x11381
xorl %edi, %edi
movq %r14, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xd88c
movl 0x34(%rsp), %edi
callq 0x21f0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x20c8, %rsp           # imm = 0x20C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop