// File: /workspace/github_c_makefile_star2_O1/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), %r15
movq %r15, 0x70(%rsp)
movq $0x2000, 0x78(%rsp)     # imm = 0x2000
xorl %eax, %eax
movq %rax, 0x40(%rsp)
movl $0x0, 0x48(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movups %xmm0, 0x5c(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0xa0(%rsp)
movq %rax, 0xb0(%rsp)
movw $0x10, 0x40(%rsp)
movl $0x1c, 0x50(%rsp)
callq 0x2100
movl %eax, 0x5c(%rsp)
movb $0x2, 0x60(%rsp)
movw $0x301, 0x56(%rsp)      # imm = 0x301
xorl %eax, %eax
testb %bpl, %bpl
setne %al
leal 0x1a(,%rax,8), %eax
movw %ax, 0x54(%rsp)
movb $0x1, 0x66(%rsp)
movl $0x10, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x24e0
testl %eax, %eax
js 0x8b21
movl %eax, %ebx
leaq 0x50(%rsp), %rsi
movl (%rsi), %edx
leaq 0x40(%rsp), %r14
movl %eax, %edi
xorl %ecx, %ecx
movq %r14, %r8
movl $0xc, %r9d
callq 0x21e0
testq %rax, %rax
js 0x8b52
leaq 0x80(%rsp), %rsi
movq %r14, (%rsi)
movl $0xc, 0x8(%rsi)
leaq 0x70(%rsp), %rax
movq %rax, 0x10(%rsi)
movq $0x1, 0x18(%rsi)
movq $0x0, 0x20(%rsi)
movl %ebx, 0x30(%rsp)
movl %ebx, %edi
xorl %edx, %edx
callq 0x2360
movq %rax, %r14
leaq 0x8afe(%rip), %rbx      # 0x1130d
leaq 0x859a(%rip), %rcx      # 0x10db0
xorl %r10d, %r10d
xorl %r9d, %r9d
xorl %r8d, %r8d
xorl %r11d, %r11d
xorl %esi, %esi
movq $0x0, 0x38(%rsp)
cmpq $0x10, %r14
jb 0x8b42
movq %r15, %r12
movl (%r15), %r13d
cmpq $0x10, %r13
jb 0x8b42
cmpq %r13, %r14
jb 0x8b42
movzwl 0x4(%r12), %eax
cmpl $0x2, %eax
je 0x8865
cmpl $0x3, %eax
jne 0x8872
jmp 0x8b42
movl 0x10(%r12), %edi
testl %edi, %edi
jne 0x8b75
leal 0x3(%r13), %r15d
andl $-0x4, %r15d
subq %r15, %r14
addq %r12, %r15
movb 0x14(%r12), %al
addb $-0x41, %al
cmpb $0x3, %al
ja 0x882d
movl %ebp, 0x34(%rsp)
addl $-0x1c, %r13d
cmpl $0x4, %r13d
jl 0x895b
leaq 0x1c(%r12), %rbp
movzwl (%rbp), %eax
cmpl $0x4, %eax
jb 0x895b
cmpl %eax, %r13d
jl 0x895b
movzwl 0x2(%rbp), %edx
leal -0x1(%rdx), %eax
cmpl $0xe, %eax
ja 0x88d6
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl 0x4(%rbp), %r10d
movb $0x2, %sil
jmp 0x8941
xorl %edi, %edi
movl %esi, 0x1c(%rsp)
movq %rbx, %rsi
xorl %eax, %eax
movl %r9d, 0x24(%rsp)
movl %r10d, 0x28(%rsp)
movl %r8d, 0x20(%rsp)
movl %r11d, 0x2c(%rsp)
callq 0xd5b0
movl 0x1c(%rsp), %esi
movl 0x2c(%rsp), %r11d
movl 0x20(%rsp), %r8d
movl 0x28(%rsp), %r10d
movl 0x24(%rsp), %r9d
leaq 0x8497(%rip), %rcx      # 0x10db0
jmp 0x8941
movl 0x4(%rbp), %r8d
jmp 0x8941
movl 0x4(%rbp), %r9d
jmp 0x8941
movl 0x4(%rbp), %r11d
jmp 0x8941
movl 0x4(%rbp), %r10d
xorl %esi, %esi
jmp 0x8941
leaq 0x4(%rbp), %rax
movq %rax, 0x38(%rsp)
movb $0x1, %sil
movzwl (%rbp), %eax
addl $0x3, %eax
andl $-0x4, %eax
subl %eax, %r13d
addq %rax, %rbp
cmpl $0x3, %r13d
jg 0x88a2
movl 0x34(%rsp), %ebp
testb %bpl, %bpl
je 0x89fa
movl %r8d, 0x20(%rsp)
movl %r9d, 0x24(%rsp)
movq %rbx, %r13
cmpb $0x2, %sil
je 0x8a4f
movzbl %sil, %eax
cmpl $0x1, %eax
je 0x8a27
testl %eax, %eax
movq %r13, %rbx
leaq 0x8418(%rip), %rcx      # 0x10db0
movl 0x24(%rsp), %r9d
movl 0x20(%rsp), %r8d
jne 0x882d
movl %esi, %ebx
movzbl 0x12(%r12), %esi
movsbl 0x14(%r12), %edx
movl $0x1, (%rsp)
movsbl %bl, %r9d
movl %r10d, %edi
movl %r11d, %ecx
xorl %r8d, %r8d
movl %r10d, 0x28(%rsp)
movl %r11d, %r12d
callq 0x7f9f
movl %ebx, %esi
movl %r12d, %r11d
movl 0x20(%rsp), %r8d
movl 0x28(%rsp), %r10d
movl 0x24(%rsp), %r9d
leaq 0x83be(%rip), %rcx      # 0x10db0
movq %r13, %rbx
jmp 0x882d
movzbl 0x17(%r12), %eax
cmpl $0x1, %eax
movl %r11d, 0x2c(%rsp)
je 0x8aa4
cmpl $0x7, %eax
je 0x8a9e
cmpl $0x9, %eax
jne 0x8aaa
movb $0x1, %cl
jmp 0x8aac
movl %esi, 0x1c(%rsp)
movzbl 0x11(%r12), %esi
movsbl 0x14(%r12), %edx
movl $0x1, (%rsp)
movsbl 0x1c(%rsp), %r9d
movl %r10d, %edi
movl %r11d, %ecx
xorl %r8d, %r8d
jmp 0x8a70
movsbl 0x14(%r12), %edx
movl $0x1, (%rsp)
movsbl %sil, %r9d
xorl %edi, %edi
movl %esi, 0x1c(%rsp)
xorl %esi, %esi
movl %r11d, %ecx
movq 0x38(%rsp), %r8
movl %r10d, %ebx
movl %r11d, %r12d
callq 0x7f9f
movl 0x1c(%rsp), %esi
movl %r12d, %r11d
movl %ebx, %r10d
movq %r13, %rbx
leaq 0x8321(%rip), %rcx      # 0x10db0
movl 0x24(%rsp), %r9d
movl 0x20(%rsp), %r8d
jmp 0x882d
movb $0x2, %cl
movb $0x1, %al
jmp 0x8aae
xorl %eax, %eax
xorl %ecx, %ecx
jmp 0x8aae
movb $0x3, %cl
xorl %eax, %eax
testb %al, %al
movl $0x0, %eax
cmovnel %eax, %r10d
cmovnel %eax, %r9d
movzbl 0x11(%r12), %esi
movzbl 0x14(%r12), %eax
movl %ecx, 0x1c(%rsp)
movzbl %cl, %ecx
movl %ecx, 0x8(%rsp)
movl %eax, (%rsp)
movl $0x1, 0x10(%rsp)
movl %r10d, %edi
movl %r9d, %edx
xorl %ecx, %ecx
movl %r9d, %ebx
movl %r8d, %r12d
leaq 0x81e9(%rip), %r9       # 0x10cdd
movl %r10d, %r13d
callq 0x79b0
movl %r12d, %r8d
movl %r13d, %r10d
movl %ebx, %r9d
leaq 0x8801(%rip), %rbx      # 0x1130d
leaq 0x829d(%rip), %rcx      # 0x10db0
movl 0x2c(%rsp), %r11d
movl 0x1c(%rsp), %esi
jmp 0x882d
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x86fc(%rip), %rsi      # 0x11230
xorl %edi, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0xd5b0
jmp 0x8bb0
movl 0x30(%rsp), %edi
callq 0x21f0
movl $0x1, %eax
jmp 0x8bb5
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x8713(%rip), %rsi      # 0x11278
xorl %edi, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0xd5b0
movl %ebx, %edi
jmp 0x8bab
testb %bpl, %bpl
leaq 0x8780(%rip), %rax      # 0x112ff
leaq 0x876b(%rip), %r14      # 0x112f1
cmoveq %rax, %r14
negl %edi
callq 0x2480
leaq 0x873c(%rip), %rsi      # 0x112d4
xorl %edi, %edi
movq %r14, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xd5b0
movl 0x30(%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