#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_in6 {
  int dummy;
};
struct msghdr {
  int msg_namelen;
  int msg_iovlen;
  char *msg_control;
  int msg_controllen;
  struct iovec *msg_iov;
  struct sockaddr_in6 *msg_name;
};
struct iovec {
  void *iov_base;
  size_t iov_len;
};
struct cmsghdr {
  scalar_t__ cmsg_level;
  scalar_t__ cmsg_type;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ cbuf;

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
struct cmsghdr *CMSG_NXTHDR(struct msghdr *, struct cmsghdr *);
int CMSG_SPACE(int);
int /*<<< orphan*/ EAGAIN;
scalar_t__ IPPROTO_IPV6;
scalar_t__ IPV6_HOPLIMIT;
int /*<<< orphan*/ errno;
int recvmsg(int, struct msghdr *, int);
ssize_t receive_packet(int fd, void *buf, size_t len, int flags,
                       struct sockaddr_in6 *addr) {
  char cbuf[CMSG_SPACE(sizeof(int))];
  struct iovec iov = {.iov_base = buf, .iov_len = len};
  struct msghdr hdr = {.msg_name = addr,
                       .msg_namelen = sizeof(*addr),
                       .msg_iov = &iov,
                       .msg_iovlen = 1,
                       .msg_control = cbuf,
                       .msg_controllen = sizeof(cbuf)};
  ssize_t val = recvmsg(fd, &hdr, flags);
  if (val == -1)
    return val;
  for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(&hdr); cmsg != NULL;
       cmsg = CMSG_NXTHDR(&hdr, cmsg)) {
    if ((cmsg->cmsg_level == IPPROTO_IPV6) &&
        (cmsg->cmsg_type == IPV6_HOPLIMIT)) {
      if (255 != *(int *)CMSG_DATA(cmsg)) {
        errno = EAGAIN;
        return -1;
      }
    }
  }
  return val;
}


x86 assembly (clang, -O0):
<receive_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    $0x4,%edi
call   1040 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdx
mov    %rdx,-0x48(%rbp)
mov    -0x18(%rbp),%rdx
mov    %rdx,-0x40(%rbp)
movl   $0x4,-0x70(%rbp)
movl   $0x1,-0x6c(%rbp)
mov    %rcx,-0x68(%rbp)
mov    %eax,-0x60(%rbp)
lea    -0x48(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x1c(%rbp),%edx
lea    -0x70(%rbp),%rsi
call   1060 <recvmsg@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0xffffffff,-0x74(%rbp)
jne    11fb <receive_packet+0xab>
mov    -0x74(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x78(%rbp)
jmp    12ad <receive_packet+0x15d>
lea    -0x70(%rbp),%rdi
call   1070 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x80(%rbp)
cmpq   $0x0,-0x80(%rbp)
je     12a0 <receive_packet+0x150>
mov    -0x80(%rbp),%rax
mov    (%rax),%rax
mov    0x2daf(%rip),%rcx
cmp    (%rcx),%rax
jne    1285 <receive_packet+0x135>
mov    -0x80(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2daf(%rip),%rcx
cmp    (%rcx),%rax
jne    1285 <receive_packet+0x135>
mov    -0x80(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    %rax,%rcx
mov    $0xff,%eax
cmp    (%rcx),%eax
je     1280 <receive_packet+0x130>
mov    0x2d5e(%rip),%rax
mov    (%rax),%ecx
mov    0x2d6d(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x78(%rbp)
jmp    12ad <receive_packet+0x15d>
jmp    1285 <receive_packet+0x135>
jmp    128a <receive_packet+0x13a>
mov    -0x80(%rbp),%rsi
lea    -0x70(%rbp),%rdi
call   1050 <CMSG_NXTHDR@plt>
mov    %rax,-0x80(%rbp)
jmp    1208 <receive_packet+0xb8>
mov    -0x74(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x78(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receive_packet>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r8,%r15
mov    %ecx,%r14d
mov    %rdx,%r13
mov    %rsi,%rbx
mov    %edi,%r12d
mov    $0x4,%edi
call   1040 <CMSG_SPACE@plt>
mov    %eax,%ecx
mov    %rsp,%rdx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rdx
mov    %rdx,%rsp
mov    %rbx,-0x60(%rbp)
mov    %r13,-0x58(%rbp)
movabs $0x100000004,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rdx,-0x48(%rbp)
mov    %eax,-0x40(%rbp)
lea    -0x60(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    %r15,-0x30(%rbp)
lea    -0x50(%rbp),%rsi
mov    %r12d,%edi
mov    %r14d,%edx
call   1060 <recvmsg@plt>
mov    %eax,%r15d
mov    $0xffffffff,%eax
cmp    $0xffffffff,%r15d
je     1251 <receive_packet+0x101>
lea    -0x50(%rbp),%rdi
call   1070 <CMSG_FIRSTHDR@plt>
test   %rax,%rax
je     124e <receive_packet+0xfe>
mov    %rax,%rbx
mov    0x2de5(%rip),%r13
mov    0x2df6(%rip),%r14
lea    -0x50(%rbp),%r12
jmp    1213 <receive_packet+0xc3>
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %rbx,%rsi
call   1050 <CMSG_NXTHDR@plt>
mov    %rax,%rbx
test   %rax,%rax
je     124e <receive_packet+0xfe>
mov    (%rbx),%rax
cmp    0x0(%r13),%rax
jne    1200 <receive_packet+0xb0>
mov    0x8(%rbx),%rax
cmp    (%r14),%rax
jne    1200 <receive_packet+0xb0>
mov    %rbx,%rdi
call   1030 <CMSG_DATA@plt>
cmpl   $0xff,(%rax)
je     1200 <receive_packet+0xb0>
mov    0x2d84(%rip),%rax
mov    (%rax),%eax
mov    0x2d93(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
jmp    1251 <receive_packet+0x101>
mov    %r15d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32 }
%struct.iovec = type { i8*, i64 }
%struct.msghdr = type { i32, i32, i8*, i32, %struct.iovec*, %struct.sockaddr_in6* }
%struct.cmsghdr = type { i64, i64 }
@IPPROTO_IPV6 = global i64 0
@IPV6_HOPLIMIT = global i64 0
@EAGAIN = global i32 0
@errno = global i32 0
define i32 @receive_packet(i32 %0, i8* %1, i64 %2, i32 %3, %struct.sockaddr_in6* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca %struct.sockaddr_in6*
%12 = alloca i8*
%13 = alloca i64
%14 = alloca %struct.iovec
%15 = alloca %struct.msghdr
%16 = alloca i32
%17 = alloca i32
%18 = alloca %struct.cmsghdr*
store i32 %0, i32* %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store i32 %3, i32* %10
store %struct.sockaddr_in6* %4, %struct.sockaddr_in6** %11
%19 = call i32 @CMSG_SPACE(i32 4)
%20 = zext i32 %19 to i64
%21 = call i8* @llvm.stacksave()
store i8* %21, i8** %12
%22 = alloca i8, i64 %20
store i64 %20, i64* %13
%23 = getelementptr inbounds %struct.iovec, %struct.iovec* %14, i32 0, i32 0
%24 = load i8*, i8** %8
store i8* %24, i8** %23
%25 = getelementptr inbounds %struct.iovec, %struct.iovec* %14, i32 0, i32 1
%26 = load i64, i64* %9
store i64 %26, i64* %25
%27 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %15, i32 0, i32 0
store i32 4, i32* %27
%28 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %15, i32 0, i32 1
store i32 1, i32* %28
%29 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %15, i32 0, i32 2
store i8* %22, i8** %29
%30 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %15, i32 0, i32 3
%31 = trunc i64 %20 to i32
store i32 %31, i32* %30
%32 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %15, i32 0, i32 4
store %struct.iovec* %14, %struct.iovec** %32
%33 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %15, i32 0, i32 5
%34 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %11
store %struct.sockaddr_in6* %34, %struct.sockaddr_in6** %33
%35 = load i32, i32* %7
%36 = load i32, i32* %10
%37 = call i32 @recvmsg(i32 %35, %struct.msghdr* %15, i32 %36)
store i32 %37, i32* %16
%38 = load i32, i32* %16
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %5
%41 = load i32, i32* %16
store i32 %41, i32* %6
store i32 1, i32* %17
br label %74
42: ; preds = %5
%43 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %15)
store %struct.cmsghdr* %43, %struct.cmsghdr** %18
br label %44
44: ; preds = %69, %42
%45 = load %struct.cmsghdr*, %struct.cmsghdr** %18
%46 = icmp ne %struct.cmsghdr* %45, null
br i1 %46, label %47, label %72
47: ; preds = %44
%48 = load %struct.cmsghdr*, %struct.cmsghdr** %18
%49 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %48, i32 0, i32 0
%50 = load i64, i64* %49
%51 = load i64, i64* @IPPROTO_IPV6
%52 = icmp eq i64 %50, %51
br i1 %52, label %53, label %68
53: ; preds = %47
%54 = load %struct.cmsghdr*, %struct.cmsghdr** %18
%55 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %54, i32 0, i32 1
%56 = load i64, i64* %55
%57 = load i64, i64* @IPV6_HOPLIMIT
%58 = icmp eq i64 %56, %57
br i1 %58, label %59, label %68
59: ; preds = %53
%60 = load %struct.cmsghdr*, %struct.cmsghdr** %18
%61 = call i64 @CMSG_DATA(%struct.cmsghdr* %60)
%62 = inttoptr i64 %61 to i32*
%63 = load i32, i32* %62
%64 = icmp ne i32 255, %63
br i1 %64, label %65, label %67
65: ; preds = %59
%66 = load i32, i32* @EAGAIN
store i32 %66, i32* @errno
store i32 -1, i32* %6
store i32 1, i32* %17
br label %74
67: ; preds = %59
br label %68
68: ; preds = %67, %53, %47
br label %69
69: ; preds = %68
%70 = load %struct.cmsghdr*, %struct.cmsghdr** %18
%71 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %15, %struct.cmsghdr* %70)
store %struct.cmsghdr* %71, %struct.cmsghdr** %18
br label %44
72: ; preds = %44
%73 = load i32, i32* %16
store i32 %73, i32* %6
store i32 1, i32* %17
br label %74
74: ; preds = %72, %65, %40
%75 = load i8*, i8** %12
%76 = load i32, i32* %6
ret i32 %76
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32 }
%struct.iovec = type { i8*, i64 }
%struct.msghdr = type { i32, i32, i8*, i32, %struct.iovec*, %struct.sockaddr_in6* }
%struct.cmsghdr = type { i64, i64 }
@IPPROTO_IPV6 = global i64 0
@IPV6_HOPLIMIT = global i64 0
@EAGAIN = global i32 0
@errno = global i32 0
define i32 @receive_packet(i32 %0, i8* %1, i64 %2, i32 %3, %struct.sockaddr_in6* %4) {
%6 = alloca %struct.iovec
%7 = alloca %struct.msghdr
%8 = tail call i32 @CMSG_SPACE(i32 4)
%9 = zext i32 %8 to i64
%10 = alloca i8, i64 %9
%11 = bitcast %struct.iovec* %6 to i8*
%12 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i64 0, i32 0
store i8* %1, i8** %12
%13 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i64 0, i32 1
store i64 %2, i64* %13
%14 = bitcast %struct.msghdr* %7 to i8*
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 0
store i32 4, i32* %15
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 1
store i32 1, i32* %16
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 2
store i8* %10, i8** %17
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 3
store i32 %8, i32* %18
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 4
store %struct.iovec* %6, %struct.iovec** %19
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 5
store %struct.sockaddr_in6* %4, %struct.sockaddr_in6** %20
%21 = call i32 @recvmsg(i32 %0, %struct.msghdr* %7, i32 %3)
%22 = icmp eq i32 %21, -1
br i1 %22, label %47, label %23
23: ; preds = %5
%24 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %7)
%25 = icmp eq %struct.cmsghdr* %24, null
br i1 %25, label %47, label %26
26: ; preds = %23, %44
%27 = phi %struct.cmsghdr* [ %45, %44 ], [ %24, %23 ]
%28 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %27, i64 0, i32 0
%29 = load i64, i64* %28
%30 = load i64, i64* @IPPROTO_IPV6
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %44
32: ; preds = %26
%33 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %27, i64 0, i32 1
%34 = load i64, i64* %33
%35 = load i64, i64* @IPV6_HOPLIMIT
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %44
37: ; preds = %32
%38 = call i64 @CMSG_DATA(%struct.cmsghdr* %27)
%39 = inttoptr i64 %38 to i32*
%40 = load i32, i32* %39
%41 = icmp eq i32 %40, 255
br i1 %41, label %44, label %42
42: ; preds = %37
%43 = load i32, i32* @EAGAIN
store i32 %43, i32* @errno
br label %47
44: ; preds = %26, %32, %37
%45 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %7, %struct.cmsghdr* %27)
%46 = icmp eq %struct.cmsghdr* %45, null
br i1 %46, label %47, label %26
47: ; preds = %44, %23, %42, %5
%48 = phi i32 [ -1, %5 ], [ -1, %42 ], [ %21, %23 ], [ %21, %44 ]
ret i32 %48
}