#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 */
typedef size_t ssize_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EWOULDBLOCK;
scalar_t__ errno;
int /*<<< orphan*/ log_error(char *, int, ...);
int /*<<< orphan*/ strerror(scalar_t__);
size_t write(int, char *, size_t);
void tun_send_packet(int tun, char *pkt, size_t pktlen) {
  ssize_t nsent;
_retry:
  nsent = write(tun, pkt, pktlen);
  if (nsent != pktlen) {
    if (nsent < 0) {
      if (errno == EAGAIN || errno == EWOULDBLOCK)
        goto _retry;
      log_error("TUN send failed: %s\n", strerror(errno));
    } else {
      log_error("Was only able to send %d out of %d bytes to TUN\n", (int)nsent,
                (int)pktlen);
    }
  }
}


x86 assembly (clang, -O0):
<tun_send_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     11ec <tun_send_packet+0xbc>
cmpq   $0x0,-0x20(%rbp)
jae    11cd <tun_send_packet+0x9d>
mov    0x2e69(%rip),%rax
mov    (%rax),%rax
mov    0x2e47(%rip),%rcx
cmp    (%rcx),%rax
je     11a4 <tun_send_packet+0x74>
mov    0x2e4f(%rip),%rax
mov    (%rax),%rax
mov    0x2e3d(%rip),%rcx
cmp    (%rcx),%rax
jne    11a9 <tun_send_packet+0x79>
jmp    1143 <tun_send_packet+0x13>
mov    0x2e30(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    %eax,%esi
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1040 <log_error@plt>
jmp    11e7 <tun_send_packet+0xb7>
mov    -0x20(%rbp),%rax
mov    %eax,%esi
mov    -0x18(%rbp),%rax
mov    %eax,%edx
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1040 <log_error@plt>
jmp    11ec <tun_send_packet+0xbc>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tun_send_packet>:
push   %rbx
mov    %rdx,%rbx
call   1030 <write@plt>
cmp    %rbx,%rax
jne    1130 <tun_send_packet+0x10>
pop    %rbx
ret
lea    0xec9(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
pop    %rbx
jmp    1040 <log_error@plt>

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str = [21 x i8] c"TUN send failed: %s\0A\00"
@.str.1 = [49 x i8] c"Was only able to send %d out of %d bytes to TUN\0A\00"
define void @tun_send_packet(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
br label %8
8: ; preds = %27, %3
%9 = load i32, i32* %4
%10 = load i8*, i8** %5
%11 = load i64, i64* %6
%12 = call i64 @write(i32 %9, i8* %10, i64 %11)
store i64 %12, i64* %7
%13 = load i64, i64* %7
%14 = load i64, i64* %6
%15 = icmp ne i64 %13, %14
br i1 %15, label %16, label %39
16: ; preds = %8
%17 = load i64, i64* %7
%18 = icmp ult i64 %17, 0
br i1 %18, label %19, label %32
19: ; preds = %16
%20 = load i64, i64* @errno
%21 = load i64, i64* @EAGAIN
%22 = icmp eq i64 %20, %21
br i1 %22, label %27, label %23
23: ; preds = %19
%24 = load i64, i64* @errno
%25 = load i64, i64* @EWOULDBLOCK
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %23, %19
br label %8
28: ; preds = %23
%29 = load i64, i64* @errno
%30 = call i32 @strerror(i64 %29)
%31 = call i32 (i8*, i32, ...) @log_error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %30)
br label %38
32: ; preds = %16
%33 = load i64, i64* %7
%34 = trunc i64 %33 to i32
%35 = load i64, i64* %6
%36 = trunc i64 %35 to i32
%37 = call i32 (i8*, i32, ...) @log_error(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i32 %34, i32 %36)
br label %38
38: ; preds = %32, %28
br label %39
39: ; preds = %38, %8
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str.1 = [49 x i8] c"Was only able to send %d out of %d bytes to TUN\0A\00"
define void @tun_send_packet(i32 %0, i8* nocapture %1, i64 %2) {
%4 = tail call i64 @write(i32 %0, i8* %1, i64 %2)
%5 = icmp eq i64 %4, %2
br i1 %5, label %10, label %6
6: ; preds = %3
%7 = trunc i64 %4 to i32
%8 = trunc i64 %2 to i32
%9 = tail call i32 (i8*, i32, ...) @log_error(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i32 %7, i32 %8)
br label %10
10: ; preds = %6, %3
ret void
}