#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 int ssize_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EWOULDBLOCK;
scalar_t__ errno;
int /*<<< orphan*/ log_error(char *, int);
int read(int, char *, size_t);
int strerror(scalar_t__);
ssize_t tun_recv_packet(int tun, char *buf, size_t buflen) {
  ssize_t nread = read(tun, buf, buflen);
  if (nread < sizeof(struct iphdr)) {
    if (nread < 0) {
      if (errno != EAGAIN && errno != EWOULDBLOCK)
        log_error("TUN recv failed: %s\n", strerror(errno));
    } else {
      log_error("TUN recv packet too small: %d bytes\n", (int)nread);
    }
    return -1;
  }
  return nread;
}


x86 assembly (clang, -O0):
<tun_recv_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    $0x4,%rax
jae    11df <tun_recv_packet+0xaf>
cmpl   $0x0,-0x1c(%rbp)
jge    11c4 <tun_recv_packet+0x94>
mov    0x2e6b(%rip),%rax
mov    (%rax),%rax
mov    0x2e49(%rip),%rcx
cmp    (%rcx),%rax
je     11bf <tun_recv_packet+0x8f>
mov    0x2e51(%rip),%rax
mov    (%rax),%rax
mov    0x2e3f(%rip),%rcx
cmp    (%rcx),%rax
je     11bf <tun_recv_packet+0x8f>
mov    0x2e37(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    %eax,%esi
lea    0xe46(%rip),%rdi
call   1030 <log_error@plt>
jmp    11d3 <tun_recv_packet+0xa3>
mov    -0x1c(%rbp),%esi
lea    0xe47(%rip),%rdi
call   1030 <log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e5 <tun_recv_packet+0xb5>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tun_recv_packet>:
push   %rax
call   1040 <read@plt>
cmp    $0x3,%eax
ja     113e <tun_recv_packet+0x1e>
lea    0xece(%rip),%rdi
mov    %eax,%esi
call   1030 <log_error@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret

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

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str.1 = [37 x i8] c"TUN recv packet too small: %d bytes\0A\00"
define i32 @tun_recv_packet(i32 %0, i8* nocapture %1, i64 %2) {
%4 = tail call i32 @read(i32 %0, i8* %1, i64 %2)
%5 = icmp ult i32 %4, 4
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @log_error(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %4)
br label %8
8: ; preds = %3, %6
%9 = phi i32 [ -1, %6 ], [ %4, %3 ]
ret i32 %9
}