#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 iphdr {
  int dummy;
};
typedef scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ decrement_ttl(struct iphdr *);
scalar_t__ sock_recv_packet(int, char *, size_t);
int /*<<< orphan*/ tun_send_packet(int, char *, scalar_t__);
int udp_to_tun(int sock, int tun, char *buf, size_t buflen) {
  struct iphdr *iph;
  ssize_t pktlen = sock_recv_packet(sock, buf, buflen);
  if (pktlen < 0)
    return 0;
  iph = (struct iphdr *)buf;
  if (!decrement_ttl(iph)) {
    goto _active;
  }
  tun_send_packet(tun, buf, pktlen);
_active:
  return 1;
}


x86 assembly (clang, -O0):
<udp_to_tun>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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 <sock_recv_packet@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    1171 <udp_to_tun+0x41>
movl   $0x0,-0x4(%rbp)
jmp    11a7 <udp_to_tun+0x77>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <decrement_ttl@plt>
cmp    $0x0,%eax
jne    1190 <udp_to_tun+0x60>
jmp    11a0 <udp_to_tun+0x70>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1040 <tun_send_packet@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_to_tun>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %rdx,%rsi
mov    %rcx,%rdx
call   1030 <sock_recv_packet@plt>
test   %rax,%rax
js     1172 <udp_to_tun+0x42>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1050 <decrement_ttl@plt>
mov    $0x1,%r15d
test   %eax,%eax
je     1175 <udp_to_tun+0x45>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1040 <tun_send_packet@plt>
jmp    1175 <udp_to_tun+0x45>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iphdr = type { i32 }
define i32 @udp_to_tun(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 i64
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%12 = load i32, i32* %6
%13 = load i8*, i8** %8
%14 = load i64, i64* %9
%15 = call i64 @sock_recv_packet(i32 %12, i8* %13, i64 %14)
store i64 %15, i64* %11
%16 = load i64, i64* %11
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %4
store i32 0, i32* %5
br label %32
19: ; preds = %4
%20 = load i8*, i8** %8
%21 = bitcast i8* %20 to %struct.iphdr*
store %struct.iphdr* %21, %struct.iphdr** %10
%22 = load %struct.iphdr*, %struct.iphdr** %10
%23 = call i32 @decrement_ttl(%struct.iphdr* %22)
%24 = icmp ne i32 %23, 0
br i1 %24, label %26, label %25
25: ; preds = %19
br label %31
26: ; preds = %19
%27 = load i32, i32* %7
%28 = load i8*, i8** %8
%29 = load i64, i64* %11
%30 = call i32 @tun_send_packet(i32 %27, i8* %28, i64 %29)
br label %31
31: ; preds = %26, %25
store i32 1, i32* %5
br label %32
32: ; preds = %31, %18
%33 = load i32, i32* %5
ret i32 %33
}

LLVM IR (-O2):
%struct.iphdr = type { i32 }
define i32 @udp_to_tun(i32 %0, i32 %1, i8* %2, i64 %3) {
%5 = tail call i64 @sock_recv_packet(i32 %0, i8* %2, i64 %3)
%6 = icmp slt i64 %5, 0
br i1 %6, label %13, label %7
7: ; preds = %4
%8 = bitcast i8* %2 to %struct.iphdr*
%9 = tail call i32 @decrement_ttl(%struct.iphdr* %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %7
%12 = tail call i32 @tun_send_packet(i32 %1, i8* %2, i64 %5)
br label %13
13: ; preds = %11, %7, %4
%14 = phi i32 [ 0, %4 ], [ 1, %7 ], [ 1, %11 ]
ret i32 %14
}