#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
struct sockaddr_in {
  int dummy;
};
struct iphdr {
  scalar_t__ daddr;
};
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ in_addr_t;

/* Variables and functions */
int /*<<< orphan*/ decrement_ttl(struct iphdr *);
struct sockaddr_in *find_route(int /*<<< orphan*/);
int /*<<< orphan*/ send_net_unreachable(int, char *);
int /*<<< orphan*/ sock_send_packet(int, char *, scalar_t__,
                                    struct sockaddr_in *);
scalar_t__ tun_recv_packet(int, char *, size_t);
int tun_to_udp(int tun, int sock, char *buf, size_t buflen) {
  struct iphdr *iph;
  struct sockaddr_in *next_hop;
  ssize_t pktlen = tun_recv_packet(tun, buf, buflen);
  if (pktlen < 0)
    return 0;
  iph = (struct iphdr *)buf;
  next_hop = find_route((in_addr_t)iph->daddr);
  if (!next_hop) {
    send_net_unreachable(tun, buf);
    goto _active;
  }
  if (!decrement_ttl(iph)) {
    goto _active;
  }
  sock_send_packet(sock, buf, pktlen, next_hop);
_active:
  return 1;
}


x86 assembly (clang, -O0):
<tun_to_udp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <tun_recv_packet@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jge    1191 <tun_to_udp+0x41>
movl   $0x0,-0x4(%rbp)
jmp    11f9 <tun_to_udp+0xa9>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %eax,%edi
call   1040 <find_route@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11c7 <tun_to_udp+0x77>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1050 <send_net_unreachable@plt>
jmp    11f2 <tun_to_udp+0xa2>
mov    -0x28(%rbp),%rdi
call   1070 <decrement_ttl@plt>
cmp    $0x0,%eax
jne    11de <tun_to_udp+0x8e>
jmp    11f2 <tun_to_udp+0xa2>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x30(%rbp),%rcx
call   1060 <sock_send_packet@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tun_to_udp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %edi,%ebp
mov    %rdx,%rsi
mov    %rcx,%rdx
call   1030 <tun_recv_packet@plt>
test   %rax,%rax
js     11aa <tun_to_udp+0x5a>
mov    %rax,%r12
mov    (%rbx),%edi
call   1040 <find_route@plt>
test   %rax,%rax
je     11af <tun_to_udp+0x5f>
mov    %rax,%r13
mov    %rbx,%rdi
call   1070 <decrement_ttl@plt>
mov    $0x1,%r15d
test   %eax,%eax
je     11bf <tun_to_udp+0x6f>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
call   1060 <sock_send_packet@plt>
jmp    11bf <tun_to_udp+0x6f>
xor    %r15d,%r15d
jmp    11bf <tun_to_udp+0x6f>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1050 <send_net_unreachable@plt>
mov    $0x1,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iphdr = type { i64 }
%struct.sockaddr_in = type { i32 }
define i32 @tun_to_udp(i32 %0, i32 %1, i8* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca %struct.iphdr*
%11 = alloca %struct.sockaddr_in*
%12 = alloca i64
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%13 = load i32, i32* %6
%14 = load i8*, i8** %8
%15 = load i64, i64* %9
%16 = call i64 @tun_recv_packet(i32 %13, i8* %14, i64 %15)
store i64 %16, i64* %12
%17 = load i64, i64* %12
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %4
store i32 0, i32* %5
br label %46
20: ; preds = %4
%21 = load i8*, i8** %8
%22 = bitcast i8* %21 to %struct.iphdr*
store %struct.iphdr* %22, %struct.iphdr** %10
%23 = load %struct.iphdr*, %struct.iphdr** %10
%24 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %23, i32 0, i32 0
%25 = load i64, i64* %24
%26 = trunc i64 %25 to i32
%27 = call %struct.sockaddr_in* @find_route(i32 %26)
store %struct.sockaddr_in* %27, %struct.sockaddr_in** %11
%28 = load %struct.sockaddr_in*, %struct.sockaddr_in** %11
%29 = icmp ne %struct.sockaddr_in* %28, null
br i1 %29, label %34, label %30
30: ; preds = %20
%31 = load i32, i32* %6
%32 = load i8*, i8** %8
%33 = call i32 @send_net_unreachable(i32 %31, i8* %32)
br label %45
34: ; preds = %20
%35 = load %struct.iphdr*, %struct.iphdr** %10
%36 = call i32 @decrement_ttl(%struct.iphdr* %35)
%37 = icmp ne i32 %36, 0
br i1 %37, label %39, label %38
38: ; preds = %34
br label %45
39: ; preds = %34
%40 = load i32, i32* %7
%41 = load i8*, i8** %8
%42 = load i64, i64* %12
%43 = load %struct.sockaddr_in*, %struct.sockaddr_in** %11
%44 = call i32 @sock_send_packet(i32 %40, i8* %41, i64 %42, %struct.sockaddr_in* %43)
br label %45
45: ; preds = %39, %38, %30
store i32 1, i32* %5
br label %46
46: ; preds = %45, %19
%47 = load i32, i32* %5
ret i32 %47
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.iphdr = type { i64 }
define i32 @tun_to_udp(i32 %0, i32 %1, i8* %2, i64 %3) {
%5 = tail call i64 @tun_recv_packet(i32 %0, i8* %2, i64 %3)
%6 = icmp slt i64 %5, 0
br i1 %6, label %21, label %7
7: ; preds = %4
%8 = bitcast i8* %2 to i64*
%9 = load i64, i64* %8
%10 = trunc i64 %9 to i32
%11 = tail call %struct.sockaddr_in* @find_route(i32 %10)
%12 = icmp eq %struct.sockaddr_in* %11, null
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = tail call i32 @send_net_unreachable(i32 %0, i8* %2)
br label %21
15: ; preds = %7
%16 = bitcast i8* %2 to %struct.iphdr*
%17 = tail call i32 @decrement_ttl(%struct.iphdr* %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %15
%20 = tail call i32 @sock_send_packet(i32 %1, i8* %2, i64 %5, %struct.sockaddr_in* %11)
br label %21
21: ; preds = %13, %19, %15, %4
%22 = phi i32 [ 0, %4 ], [ 1, %15 ], [ 1, %19 ], [ 1, %13 ]
ret i32 %22
}