#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;
int /*<<< orphan*/ MSG_DONTWAIT;
scalar_t__ errno;
int /*<<< orphan*/ log_error(char *, int);
int recv(int, char *, size_t, int /*<<< orphan*/);
int strerror(scalar_t__);
ssize_t sock_recv_packet(int sock, char *buf, size_t buflen) {
  ssize_t nread = recv(sock, buf, buflen, MSG_DONTWAIT);
  if (nread < sizeof(struct iphdr)) {
    if (nread < 0) {
      if (errno != EAGAIN && errno != EWOULDBLOCK)
        log_error("UDP recv failed: %s\n", strerror(errno));
    } else {
      log_error("UDP recv packet too small: %d bytes\n", (int)nread);
    }
    return -1;
  }
  return nread;
}


x86 assembly (clang, -O0):
<sock_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
mov    0x2e7b(%rip),%rax
mov    (%rax),%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    $0x4,%rax
jae    11e8 <sock_recv_packet+0xb8>
cmpl   $0x0,-0x1c(%rbp)
jge    11cd <sock_recv_packet+0x9d>
mov    0x2e62(%rip),%rax
mov    (%rax),%rax
mov    0x2e38(%rip),%rcx
cmp    (%rcx),%rax
je     11c8 <sock_recv_packet+0x98>
mov    0x2e48(%rip),%rax
mov    (%rax),%rax
mov    0x2e36(%rip),%rcx
cmp    (%rcx),%rax
je     11c8 <sock_recv_packet+0x98>
mov    0x2e2e(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    %eax,%esi
lea    0xe3d(%rip),%rdi
call   1040 <log_error@plt>
jmp    11dc <sock_recv_packet+0xac>
mov    -0x1c(%rbp),%esi
lea    0xe3e(%rip),%rdi
call   1040 <log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <sock_recv_packet+0xbe>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_recv_packet>:
push   %rax
mov    0x2eb8(%rip),%rax
mov    (%rax),%ecx
call   1030 <recv@plt>
cmp    $0x3,%eax
ja     1147 <sock_recv_packet+0x27>
lea    0xec5(%rip),%rdi
mov    %eax,%esi
call   1040 <log_error@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret

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

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