// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x2120, %rsp           # imm = 0x2120
movb %dil, %al
movb %al, -0x5(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movb $0x0, -0x29(%rbp)
movq $0x0, -0x2038(%rbp)
leaq -0x2030(%rbp), %rax
movq %rax, -0x2058(%rbp)
movq $0x2000, -0x2050(%rbp) # imm = 0x2000
leaq -0x20c8(%rbp), %rax
movq %rax, -0x2098(%rbp)
leaq -0x20c8(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x20a8(%rbp)
leaq -0x2044(%rbp), %rdi
xorl %esi, %esi
movl $0xc, %edx
callq 0x21e0
leaq -0x20c8(%rbp), %rdi
xorl %esi, %esi
movl $0x1c, %edx
callq 0x21e0
leaq -0x2090(%rbp), %rdi
xorl %esi, %esi
movl $0x38, %edx
callq 0x21e0
movw $0x10, -0x2044(%rbp)
movq -0x2098(%rbp), %rax
movl $0x1c, (%rax)
callq 0x2100
movl %eax, %ecx
movq -0x2098(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x20a8(%rbp), %rax
movb $0x2, (%rax)
movq -0x2098(%rbp), %rax
movw $0x301, 0x6(%rax)       # imm = 0x301
movsbl -0x5(%rbp), %edx
movl $0x1a, %eax
movl $0x22, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movw %ax, %cx
movq -0x2098(%rbp), %rax
movw %cx, 0x4(%rax)
movq -0x20a8(%rbp), %rax
movb $0x1, 0x6(%rax)
movl $0x10, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x2500
movl %eax, -0xc(%rbp)
cmpl $0x0, %eax
jge 0xb87d
callq 0x2070
movl (%rax), %edi
callq 0x24a0
movq %rax, %rdx
xorl %edi, %edi
leaq 0xaa06(%rip), %rsi      # 0x16270
movb $0x0, %al
callq 0x123f0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xbdd0
movl -0xc(%rbp), %edi
leaq -0x20c8(%rbp), %rsi
movq -0x2098(%rbp), %rax
movl (%rax), %eax
movl %eax, %edx
leaq -0x2044(%rbp), %rax
movq %rax, -0x20d8(%rbp)
movq -0x20d8(%rbp), %r8
xorl %ecx, %ecx
movl $0xc, %r9d
callq 0x2200
cmpq $0x0, %rax
jge 0xb8ed
callq 0x2070
movl (%rax), %edi
callq 0x24a0
movq %rax, %rdx
xorl %edi, %edi
leaq 0xa9e6(%rip), %rsi      # 0x162b8
movb $0x0, %al
callq 0x123f0
movl -0xc(%rbp), %edi
callq 0x2210
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xbdd0
leaq -0x2044(%rbp), %rax
movq %rax, -0x2090(%rbp)
movl $0xc, -0x2088(%rbp)
leaq -0x2058(%rbp), %rax
movq %rax, -0x2080(%rbp)
movq $0x1, -0x2078(%rbp)
movq $0x0, -0x2070(%rbp)
movl -0xc(%rbp), %edi
leaq -0x2090(%rbp), %rsi
xorl %edx, %edx
callq 0x2380
movq %rax, -0x18(%rbp)
leaq -0x2030(%rbp), %rax
movq %rax, -0x2098(%rbp)
xorl %eax, %eax
cmpq $0x10, -0x18(%rbp)
movb %al, -0x20d9(%rbp)
jb 0xb98a
movq -0x2098(%rbp), %rax
movl (%rax), %eax
movl %eax, %ecx
xorl %eax, %eax
cmpq $0x10, %rcx
movb %al, -0x20d9(%rbp)
jb 0xb98a
movq -0x2098(%rbp), %rax
movl (%rax), %eax
cmpq -0x18(%rbp), %rax
setbe %al
movb %al, -0x20d9(%rbp)
movb -0x20d9(%rbp), %al
testb $0x1, %al
jne 0xb999
jmp 0xbdc1
movq -0x2098(%rbp), %rax
movzwl 0x4(%rax), %eax
cmpl $0x3, %eax
jne 0xb9ae
jmp 0xbdc1
movq -0x2098(%rbp), %rax
movzwl 0x4(%rax), %eax
cmpl $0x2, %eax
jne 0xba2a
movq -0x2098(%rbp), %rax
cmpl $0x0, 0x10(%rax)
je 0xba2a
movsbl -0x5(%rbp), %edx
leaq 0xa969(%rip), %rax      # 0x1633f
leaq 0xa954(%rip), %rcx      # 0x16331
cmpl $0x0, %edx
cmovneq %rcx, %rax
movq %rax, -0x20e8(%rbp)
movq -0x2098(%rbp), %rax
xorl %edi, %edi
subl 0x10(%rax), %edi
callq 0x24a0
movq -0x20e8(%rbp), %rdx
movq %rax, %rcx
xorl %edi, %edi
leaq 0xa905(%rip), %rsi      # 0x16314
movb $0x0, %al
callq 0x123f0
movl -0xc(%rbp), %edi
callq 0x2210
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xbdd0
movq -0x2098(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x20a0(%rbp)
movq -0x20a0(%rbp), %rax
addq $0xc, %rax
movq %rax, -0x20d0(%rbp)
movq -0x2098(%rbp), %rax
movl (%rax), %eax
subq $0x1c, %rax
movl %eax, -0x10(%rbp)
movq -0x2098(%rbp), %rax
movl (%rax), %eax
addl $0x4, %eax
subl $0x1, %eax
andl $-0x4, %eax
movl %eax, %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x18(%rbp)
movq -0x2098(%rbp), %rax
movq -0x2098(%rbp), %rcx
movl (%rcx), %ecx
addl $0x4, %ecx
subl $0x1, %ecx
andl $-0x4, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x2098(%rbp)
movq -0x20a0(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x43, %eax
je 0xbae9
movq -0x20a0(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x41, %eax
je 0xbae9
movq -0x20a0(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x42, %eax
je 0xbae9
movq -0x20a0(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x44, %eax
je 0xbae9
jmp 0xb94c
jmp 0xbaeb
xorl %eax, %eax
cmpl $0x4, -0x10(%rbp)
movb %al, -0x20e9(%rbp)
jl 0xbb29
movq -0x20d0(%rbp), %rax
movzwl (%rax), %eax
movl %eax, %ecx
xorl %eax, %eax
cmpq $0x4, %rcx
movb %al, -0x20e9(%rbp)
jb 0xbb29
movq -0x20d0(%rbp), %rax
movzwl (%rax), %eax
cmpl -0x10(%rbp), %eax
setle %al
movb %al, -0x20e9(%rbp)
movb -0x20e9(%rbp), %al
testb $0x1, %al
jne 0xbb38
jmp 0xbc3e
movq -0x20d0(%rbp), %rax
movzwl 0x2(%rax), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x20f8(%rbp)
subl $0xe, %eax
ja 0xbbdd
movq -0x20f8(%rbp), %rax
leaq 0xa2b3(%rip), %rcx      # 0x15e18
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x20d0(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x1c(%rbp)
movb $0x2, -0x29(%rbp)
jmp 0xbbf8
movq -0x20d0(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x1c(%rbp)
movb $0x0, -0x29(%rbp)
jmp 0xbbf8
movq -0x20d0(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x20(%rbp)
jmp 0xbbf8
movq -0x20d0(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x24(%rbp)
jmp 0xbbf8
movq -0x20d0(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x28(%rbp)
jmp 0xbbf8
movq -0x20d0(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x2038(%rbp)
movb $0x1, -0x29(%rbp)
jmp 0xbbf8
jmp 0xbbf8
jmp 0xbbf8
movq -0x20d0(%rbp), %rax
movzwl 0x2(%rax), %edx
xorl %edi, %edi
leaq 0xa75c(%rip), %rsi      # 0x1634d
movb $0x0, %al
callq 0x123f0
movq -0x20d0(%rbp), %rax
movzwl (%rax), %ecx
addl $0x4, %ecx
subl $0x1, %ecx
andl $-0x4, %ecx
movl -0x10(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x10(%rbp)
movq -0x20d0(%rbp), %rax
movq -0x20d0(%rbp), %rcx
movzwl (%rcx), %ecx
addl $0x4, %ecx
subl $0x1, %ecx
andl $-0x4, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x20d0(%rbp)
jmp 0xbaeb
cmpb $0x0, -0x5(%rbp)
je 0xbd16
movsbl -0x29(%rbp), %eax
movl %eax, -0x20fc(%rbp)
testl %eax, %eax
je 0xbc75
jmp 0xbc58
movl -0x20fc(%rbp), %eax
subl $0x1, %eax
je 0xbcad
jmp 0xbc65
movl -0x20fc(%rbp), %eax
subl $0x2, %eax
je 0xbce5
jmp 0xbd11
movl -0x1c(%rbp), %edi
movq -0x20a0(%rbp), %rax
movb 0x2(%rax), %dl
movq -0x20a0(%rbp), %rax
movb 0x4(%rax), %al
movl -0x28(%rbp), %ecx
xorl %esi, %esi
movl %esi, %r8d
movzbl %dl, %esi
movsbl %al, %edx
movsbl -0x29(%rbp), %r9d
movl $0x1, (%rsp)
callq 0xacd0
jmp 0xbd11
movl -0x1c(%rbp), %edi
movq -0x20a0(%rbp), %rax
movb 0x1(%rax), %dl
movq -0x20a0(%rbp), %rax
movb 0x4(%rax), %al
movl -0x28(%rbp), %ecx
xorl %esi, %esi
movl %esi, %r8d
movzbl %dl, %esi
movsbl %al, %edx
movsbl -0x29(%rbp), %r9d
movl $0x1, (%rsp)
callq 0xacd0
jmp 0xbd11
movq -0x20a0(%rbp), %rax
movb 0x4(%rax), %al
movl -0x28(%rbp), %ecx
movq -0x2038(%rbp), %r8
xorl %esi, %esi
movl %esi, %edi
movsbl %al, %edx
movsbl -0x29(%rbp), %r9d
movl $0x1, (%rsp)
callq 0xacd0
jmp 0xbdbc
movq -0x20a0(%rbp), %rax
movzbl 0x7(%rax), %eax
movl %eax, -0x2100(%rbp)
subl $0x1, %eax
je 0xbd48
jmp 0xbd2e
movl -0x2100(%rbp), %eax
subl $0x7, %eax
je 0xbd54
jmp 0xbd3b
movl -0x2100(%rbp), %eax
subl $0x9, %eax
je 0xbd4e
jmp 0xbd5a
movb $0x0, -0x29(%rbp)
jmp 0xbd5e
movb $0x1, -0x29(%rbp)
jmp 0xbd5e
movb $0x2, -0x29(%rbp)
jmp 0xbd5e
movb $0x3, -0x29(%rbp)
movsbl -0x29(%rbp), %eax
cmpl $0x2, %eax
jne 0xbd75
movl $0x0, -0x20(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %edi
movq -0x20a0(%rbp), %rax
movb 0x1(%rax), %sil
movl -0x20(%rbp), %edx
movl -0x24(%rbp), %r8d
movq -0x20a0(%rbp), %rax
movb 0x4(%rax), %al
xorl %ecx, %ecx
leaq 0x9fe4(%rip), %r9       # 0x15d81
movzbl %sil, %esi
movzbl %al, %eax
movl %eax, (%rsp)
movsbl -0x29(%rbp), %eax
movl %eax, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
callq 0xa420
jmp 0xb94c
movl -0xc(%rbp), %edi
callq 0x2210
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x2120, %rsp           # imm = 0x2120
popq %rbp
retq
nopl (%rax)