// File: /workspace/github_c_makefile_star2_O0/open-mesh-mirror[P]batmand/linux/tun.c
// Function: add_dev_tun
int8_t add_dev_tun( struct batman_if *batman_if, uint32_t tun_addr, char *tun_dev, size_t tun_dev_size, int32_t *fd, int32_t *ifi ) {

	int32_t tmp_fd, sock_opts;
	struct ifreq ifr_tun, ifr_if;
	struct sockaddr_in addr;

	/* set up tunnel device */
	memset( &ifr_tun, 0, sizeof(ifr_tun) );
	memset( &ifr_if, 0, sizeof(ifr_if) );

	ifr_tun.ifr_flags = IFF_TUN | IFF_NO_PI;
	strncpy( ifr_tun.ifr_name, "gate%d", IFNAMSIZ );

	if ( ( *fd = open( "/dev/net/tun", O_RDWR ) ) < 0 ) {

		debug_output( 0, "Error - can't create tun device (/dev/net/tun): %s\n", strerror(errno) );
		return -1;

	}

	if ( ( ioctl( *fd, TUNSETIFF, (void *)&ifr_tun ) ) < 0 ) {

		debug_output( 0, "Error - can't create tun device (TUNSETIFF): %s\n", strerror(errno) );
		close(*fd);
		return -1;

	}

	if ( ioctl( *fd, TUNSETPERSIST, 1 ) < 0 ) {

		debug_output( 0, "Error - can't create tun device (TUNSETPERSIST): %s\n", strerror(errno) );
		close(*fd);
		return -1;

	}


	tmp_fd = socket( AF_INET, SOCK_DGRAM, 0 );

	if ( tmp_fd < 0 ) {
		debug_output( 0, "Error - can't create tun device (udp socket): %s\n", strerror(errno) );
		del_dev_tun( *fd );
		return -1;
	}


	/* set ip of this end point of tunnel */
	memset( &addr, 0, sizeof(addr) );
	addr.sin_addr.s_addr = tun_addr;
	addr.sin_family = AF_INET;
	memcpy( &ifr_tun.ifr_addr, &addr, sizeof(struct sockaddr) );


	if ( ioctl( tmp_fd, SIOCSIFADDR, &ifr_tun) < 0 ) {

		debug_output( 0, "Error - can't create tun device (SIOCSIFADDR): %s\n", strerror(errno) );
		del_dev_tun( *fd );
		close( tmp_fd );
		return -1;

	}


	if ( ioctl( tmp_fd, SIOCGIFINDEX, &ifr_tun ) < 0 ) {

		debug_output( 0, "Error - can't create tun device (SIOCGIFINDEX): %s\n", strerror(errno) );
		del_dev_tun( *fd );
		close( tmp_fd );
		return -1;

	}

	*ifi = ifr_tun.ifr_ifindex;

	if ( ioctl( tmp_fd, SIOCGIFFLAGS, &ifr_tun) < 0 ) {

		debug_output( 0, "Error - can't create tun device (SIOCGIFFLAGS): %s\n", strerror(errno) );
		del_dev_tun( *fd );
		close( tmp_fd );
		return -1;

	}

	ifr_tun.ifr_flags |= IFF_UP;
	ifr_tun.ifr_flags |= IFF_RUNNING;

	if ( ioctl( tmp_fd, SIOCSIFFLAGS, &ifr_tun) < 0 ) {

		debug_output( 0, "Error - can't create tun device (SIOCSIFFLAGS): %s\n", strerror(errno) );
		del_dev_tun( *fd );
		close( tmp_fd );
		return -1;

	}

	/* get MTU from real interface */
	strncpy( ifr_if.ifr_name, batman_if->dev, IFNAMSIZ - 1 );

	if ( ioctl( tmp_fd, SIOCGIFMTU, &ifr_if ) < 0 ) {

		debug_output( 0, "Error - can't create tun device (SIOCGIFMTU): %s\n", strerror(errno) );
		del_dev_tun( *fd );
		close( tmp_fd );
		return -1;

	}

	/* set MTU of tun interface: real MTU - 29 */
	if ( ifr_if.ifr_mtu < 100 ) {

		debug_output( 0, "Warning - MTU smaller than 100 -> can't reduce MTU anymore\n" );

	} else {

		ifr_tun.ifr_mtu = ifr_if.ifr_mtu - 29;

		if ( ioctl( tmp_fd, SIOCSIFMTU, &ifr_tun ) < 0 ) {

			debug_output( 0, "Error - can't create tun device (SIOCSIFMTU): %s\n", strerror(errno) );
			del_dev_tun( *fd );
			close( tmp_fd );
			return -1;

		}

	}


	/* make tun socket non blocking */
	sock_opts = fcntl( *fd, F_GETFL, 0 );
	fcntl( *fd, F_SETFL, sock_opts | O_NONBLOCK );


	strncpy( tun_dev, ifr_tun.ifr_name, tun_dev_size - 1 );
	close( tmp_fd );

	return 1;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
leaq -0x68(%rbp), %rdi
xorl %esi, %esi
movl $0x28, %edx
callq 0x21e0
leaq -0x90(%rbp), %rdi
xorl %esi, %esi
movl $0x28, %edx
callq 0x21e0
movw $0x1001, -0x58(%rbp)    # imm = 0x1001
leaq -0x68(%rbp), %rdi
leaq 0xa324(%rip), %rsi      # 0x1659a
movl $0x10, %edx
callq 0x2090
leaq 0xa296(%rip), %rdi      # 0x1651d
movl $0x2, %esi
movb $0x0, %al
callq 0x23d0
movq -0x30(%rbp), %rcx
movl %eax, (%rcx)
cmpl $0x0, %eax
jge 0xc2c6
callq 0x2070
movl (%rax), %edi
callq 0x24a0
movq %rax, %rdx
xorl %edi, %edi
leaq 0xa2eb(%rip), %rsi      # 0x165a1
movb $0x0, %al
callq 0x123f0
movb $-0x1, -0x1(%rbp)
jmp 0xc688
movq -0x30(%rbp), %rax
movl (%rax), %edi
movl $0x400454ca, %esi       # imm = 0x400454CA
leaq -0x68(%rbp), %rdx
movb $0x0, %al
callq 0x21f0
cmpl $0x0, %eax
jge 0xc314
callq 0x2070
movl (%rax), %edi
callq 0x24a0
movq %rax, %rdx
xorl %edi, %edi
leaq 0xa2dc(%rip), %rsi      # 0x165d5
movb $0x0, %al
callq 0x123f0
movq -0x30(%rbp), %rax
movl (%rax), %edi
callq 0x2210
movb $-0x1, -0x1(%rbp)
jmp 0xc688
movq -0x30(%rbp), %rax
movl (%rax), %edi
movl $0x400454cb, %esi       # imm = 0x400454CB
movl $0x1, %edx
movb $0x0, %al
callq 0x21f0
cmpl $0x0, %eax
jge 0xc363
callq 0x2070
movl (%rax), %edi
callq 0x24a0
movq %rax, %rdx
xorl %edi, %edi
leaq 0xa2be(%rip), %rsi      # 0x16606
movb $0x0, %al
callq 0x123f0
movq -0x30(%rbp), %rax
movl (%rax), %edi
callq 0x2210
movb $-0x1, -0x1(%rbp)
jmp 0xc688
movl $0x2, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x2500
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jge 0xc3ad
callq 0x2070
movl (%rax), %edi
callq 0x24a0
movq %rax, %rdx
xorl %edi, %edi
leaq 0xa2a9(%rip), %rsi      # 0x1663b
movb $0x0, %al
callq 0x123f0
movq -0x30(%rbp), %rax
movl (%rax), %edi
callq 0xc1c0
movb $-0x1, -0x1(%rbp)
jmp 0xc688
leaq -0xa0(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x21e0
movl -0x14(%rbp), %eax
movl %eax, -0x9c(%rbp)
movw $0x2, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x50(%rbp)
movl -0x3c(%rbp), %edi
movl $0x8916, %esi           # imm = 0x8916
leaq -0x68(%rbp), %rdx
movb $0x0, %al
callq 0x21f0
cmpl $0x0, %eax
jge 0xc43b
callq 0x2070
movl (%rax), %edi
callq 0x24a0
movq %rax, %rdx
xorl %edi, %edi
leaq 0xa255(%rip), %rsi      # 0x1666d
movb $0x0, %al
callq 0x123f0
movq -0x30(%rbp), %rax
movl (%rax), %edi
callq 0xc1c0
movl -0x3c(%rbp), %edi
callq 0x2210
movb $-0x1, -0x1(%rbp)
jmp 0xc688
movl -0x3c(%rbp), %edi
movl $0x8933, %esi           # imm = 0x8933
leaq -0x68(%rbp), %rdx
movb $0x0, %al
callq 0x21f0
cmpl $0x0, %eax
jge 0xc48e
callq 0x2070
movl (%rax), %edi
callq 0x24a0
movq %rax, %rdx
xorl %edi, %edi
leaq 0xa235(%rip), %rsi      # 0x166a0
movb $0x0, %al
callq 0x123f0
movq -0x30(%rbp), %rax
movl (%rax), %edi
callq 0xc1c0
movl -0x3c(%rbp), %edi
callq 0x2210
movb $-0x1, -0x1(%rbp)
jmp 0xc688
movl -0x58(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, (%rax)
movl -0x3c(%rbp), %edi
movl $0x8913, %esi           # imm = 0x8913
leaq -0x68(%rbp), %rdx
movb $0x0, %al
callq 0x21f0
cmpl $0x0, %eax
jge 0xc4ea
callq 0x2070
movl (%rax), %edi
callq 0x24a0
movq %rax, %rdx
xorl %edi, %edi
leaq 0xa20d(%rip), %rsi      # 0x166d4
movb $0x0, %al
callq 0x123f0
movq -0x30(%rbp), %rax
movl (%rax), %edi
callq 0xc1c0
movl -0x3c(%rbp), %edi
callq 0x2210
movb $-0x1, -0x1(%rbp)
jmp 0xc688
movswl -0x58(%rbp), %eax
orl $0x1, %eax
movw %ax, -0x58(%rbp)
movswl -0x58(%rbp), %eax
orl $0x40, %eax
movw %ax, -0x58(%rbp)
movl -0x3c(%rbp), %edi
movl $0x8914, %esi           # imm = 0x8914
leaq -0x68(%rbp), %rdx
movb $0x0, %al
callq 0x21f0
cmpl $0x0, %eax
jge 0xc553
callq 0x2070
movl (%rax), %edi
callq 0x24a0
movq %rax, %rdx
xorl %edi, %edi
leaq 0xa1d8(%rip), %rsi      # 0x16708
movb $0x0, %al
callq 0x123f0
movq -0x30(%rbp), %rax
movl (%rax), %edi
callq 0xc1c0
movl -0x3c(%rbp), %edi
callq 0x2210
movb $-0x1, -0x1(%rbp)
jmp 0xc688
leaq -0x90(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movl $0xf, %edx
callq 0x2090
movl -0x3c(%rbp), %edi
movl $0x8921, %esi           # imm = 0x8921
leaq -0x90(%rbp), %rdx
movb $0x0, %al
callq 0x21f0
cmpl $0x0, %eax
jge 0xc5c2
callq 0x2070
movl (%rax), %edi
callq 0x24a0
movq %rax, %rdx
xorl %edi, %edi
leaq 0xa19d(%rip), %rsi      # 0x1673c
movb $0x0, %al
callq 0x123f0
movq -0x30(%rbp), %rax
movl (%rax), %edi
callq 0xc1c0
movl -0x3c(%rbp), %edi
callq 0x2210
movb $-0x1, -0x1(%rbp)
jmp 0xc688
cmpl $0x64, -0x80(%rbp)
jge 0xc5da
xorl %edi, %edi
leaq 0xa19d(%rip), %rsi      # 0x1676e
movb $0x0, %al
callq 0x123f0
jmp 0xc635
movl -0x80(%rbp), %eax
subl $0x1d, %eax
movl %eax, -0x58(%rbp)
movl -0x3c(%rbp), %edi
movl $0x8922, %esi           # imm = 0x8922
leaq -0x68(%rbp), %rdx
movb $0x0, %al
callq 0x21f0
cmpl $0x0, %eax
jge 0xc633
callq 0x2070
movl (%rax), %edi
callq 0x24a0
movq %rax, %rdx
xorl %edi, %edi
leaq 0xa197(%rip), %rsi      # 0x167aa
movb $0x0, %al
callq 0x123f0
movq -0x30(%rbp), %rax
movl (%rax), %edi
callq 0xc1c0
movl -0x3c(%rbp), %edi
callq 0x2210
movb $-0x1, -0x1(%rbp)
jmp 0xc688
jmp 0xc635
movq -0x30(%rbp), %rax
movl (%rax), %edi
movl $0x3, %esi
xorl %edx, %edx
movb $0x0, %al
callq 0x20e0
movl %eax, -0x40(%rbp)
movq -0x30(%rbp), %rax
movl (%rax), %edi
movl -0x40(%rbp), %edx
orl $0x800, %edx            # imm = 0x800
movl $0x4, %esi
movb $0x0, %al
callq 0x20e0
movq -0x20(%rbp), %rdi
leaq -0x68(%rbp), %rsi
movq -0x28(%rbp), %rdx
subq $0x1, %rdx
callq 0x2090
movl -0x3c(%rbp), %edi
callq 0x2210
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)