#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_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
struct msghdr {
  int msg_iovlen;
  int msg_namelen;
  char *msg_control;
  int msg_controllen;
  struct sockaddr *msg_name;
  scalar_t__ msg_flags;
  struct iovec *msg_iov;
};
struct iovec {
  int iov_len;
  void *iov_base;
};
struct cmsghdr {
  scalar_t__ cmsg_len;
  scalar_t__ cmsg_level;
  scalar_t__ cmsg_type;
};
typedef int /*<<< orphan*/ cmsgbuf;

/* Variables and functions */
int *CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
struct cmsghdr *CMSG_NXTHDR(struct msghdr *, struct cmsghdr *);
int CMSG_SPACE(int);
scalar_t__ IPPROTO_IP;
scalar_t__ IP_TOS;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int receive_packet(int sck, void *buf, int bufsz, struct sockaddr_in *addr,
                   int *tos) {
  struct msghdr msg;
  struct iovec iov;
  char cmsgbuf[CMSG_SPACE(100)];
  struct cmsghdr *m;
  int cc;
  iov.iov_base = buf;
  iov.iov_len = bufsz;
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;
  msg.msg_flags = 0;
  msg.msg_name = (struct sockaddr *)addr;
  msg.msg_namelen = sizeof(*addr);
  msg.msg_control = cmsgbuf;
  msg.msg_controllen = sizeof(cmsgbuf);
  cc = recvmsg(sck, &msg, 0);
  if (cc < 0) {
    perror("Error receiving packet");
    exit(1);
  }
  if (tos)
    *tos = 0;
  m = (struct cmsghdr *)&cmsgbuf;
  for (m = CMSG_FIRSTHDR(&msg); m; m = CMSG_NXTHDR(&msg, m)) {
    if (m->cmsg_len == 0) {
      perror("Null control message");
      exit(1);
    }
    if (m->cmsg_level == IPPROTO_IP && m->cmsg_type == IP_TOS) {
      if (tos)
        *tos = CMSG_DATA(m)[0];
    }
  }
  return cc;
}


x86 assembly (clang, -O0):
<receive_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    $0x64,%edi
call   1040 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rsp,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x90(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x78(%rbp)
mov    -0x10(%rbp),%rdx
mov    %rdx,-0x60(%rbp)
mov    -0x14(%rbp),%edx
mov    %edx,-0x68(%rbp)
lea    -0x68(%rbp),%rdx
mov    %rdx,-0x30(%rbp)
movl   $0x1,-0x58(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x20(%rbp),%rdx
mov    %rdx,-0x40(%rbp)
movl   $0x4,-0x54(%rbp)
mov    %rcx,-0x50(%rbp)
mov    %eax,-0x48(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x58(%rbp),%rsi
xor    %edx,%edx
call   1060 <recvmsg@plt>
mov    %eax,-0x84(%rbp)
cmpl   $0x0,-0x84(%rbp)
jge    1231 <receive_packet+0xc1>
lea    0xdde(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x28(%rbp)
je     1246 <receive_packet+0xd6>
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x90(%rbp),%rax
mov    %rax,-0x80(%rbp)
lea    -0x58(%rbp),%rdi
call   1090 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x80(%rbp)
cmpq   $0x0,-0x80(%rbp)
je     12f9 <receive_packet+0x189>
mov    -0x80(%rbp),%rax
cmpq   $0x0,(%rax)
jne    128d <receive_packet+0x11d>
lea    0xd99(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x80(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2d4c(%rip),%rcx
cmp    (%rcx),%rax
jne    12de <receive_packet+0x16e>
mov    -0x80(%rbp),%rax
mov    0x10(%rax),%rax
mov    0x2d2c(%rip),%rcx
cmp    (%rcx),%rax
jne    12de <receive_packet+0x16e>
cmpq   $0x0,-0x28(%rbp)
je     12d9 <receive_packet+0x169>
mov    -0x80(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
jmp    12de <receive_packet+0x16e>
jmp    12e3 <receive_packet+0x173>
mov    -0x80(%rbp),%rsi
lea    -0x58(%rbp),%rdi
call   1050 <CMSG_NXTHDR@plt>
mov    %rax,-0x80(%rbp)
jmp    125e <receive_packet+0xee>
mov    -0x84(%rbp),%eax
mov    -0x70(%rbp),%rcx
mov    %rcx,%rsp
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    $0x58,%rsp
mov    %r8,%r15
mov    %rcx,%r12
mov    %edx,%r13d
mov    %rsi,%rbx
mov    %edi,%r14d
mov    $0x64,%edi
call   1040 <CMSG_SPACE@plt>
mov    %rsp,-0x38(%rbp)
mov    %eax,%ecx
mov    %rsp,%rdx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rdx
mov    %rdx,%rsp
mov    %rbx,-0x40(%rbp)
mov    %r13d,-0x48(%rbp)
lea    -0x48(%rbp),%rcx
mov    %rcx,-0x50(%rbp)
movabs $0x400000001,%rcx
mov    %rcx,-0x78(%rbp)
movq   $0x0,-0x58(%rbp)
mov    %r12,-0x60(%rbp)
mov    %rdx,-0x70(%rbp)
mov    %eax,-0x68(%rbp)
lea    -0x78(%rbp),%rsi
mov    %r14d,%edi
xor    %edx,%edx
call   1060 <recvmsg@plt>
test   %eax,%eax
js     129c <receive_packet+0x12c>
mov    %eax,-0x2c(%rbp)
test   %r15,%r15
je     1207 <receive_packet+0x97>
movl   $0x0,(%r15)
lea    -0x78(%rbp),%rdi
call   1090 <CMSG_FIRSTHDR@plt>
test   %rax,%rax
je     1270 <receive_packet+0x100>
mov    %rax,%rbx
mov    0x2dc9(%rip),%r13
mov    0x2dba(%rip),%r14
lea    -0x78(%rbp),%r12
jmp    1243 <receive_packet+0xd3>
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %rbx,%rsi
call   1050 <CMSG_NXTHDR@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1270 <receive_packet+0x100>
cmpq   $0x0,(%rbx)
je     1286 <receive_packet+0x116>
mov    0x8(%rbx),%rax
cmp    0x0(%r13),%rax
jne    1230 <receive_packet+0xc0>
test   %r15,%r15
je     1230 <receive_packet+0xc0>
mov    (%r14),%rax
cmp    %rax,0x10(%rbx)
jne    1230 <receive_packet+0xc0>
mov    %rbx,%rdi
call   1030 <CMSG_DATA@plt>
mov    (%rax),%eax
mov    %eax,(%r15)
jmp    1230 <receive_packet+0xc0>
mov    -0x38(%rbp),%rsp
mov    -0x2c(%rbp),%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd8a(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xd5d(%rip),%rdi
jmp    128d <receive_packet+0x11d>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.msghdr = type { i32, i32, i8*, i32, %struct.sockaddr*, i64, %struct.iovec* }
%struct.sockaddr = type { i32 }
%struct.iovec = type { i32, i8* }
%struct.cmsghdr = type { i64, i64, i64 }
@.str = [23 x i8] c"Error receiving packet\00"
@.str.1 = [21 x i8] c"Null control message\00"
@IPPROTO_IP = global i64 0
@IP_TOS = global i64 0
define i32 @receive_packet(i32 %0, i8* %1, i32 %2, %struct.sockaddr_in* %3, i32* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca %struct.sockaddr_in*
%10 = alloca i32*
%11 = alloca %struct.msghdr
%12 = alloca %struct.iovec
%13 = alloca i8*
%14 = alloca i64
%15 = alloca %struct.cmsghdr*
%16 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store %struct.sockaddr_in* %3, %struct.sockaddr_in** %9
store i32* %4, i32** %10
%17 = call i32 @CMSG_SPACE(i32 100)
%18 = zext i32 %17 to i64
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %13
%20 = alloca i8, i64 %18
store i64 %18, i64* %14
%21 = load i8*, i8** %7
%22 = getelementptr inbounds %struct.iovec, %struct.iovec* %12, i32 0, i32 1
store i8* %21, i8** %22
%23 = load i32, i32* %8
%24 = getelementptr inbounds %struct.iovec, %struct.iovec* %12, i32 0, i32 0
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %11, i32 0, i32 6
store %struct.iovec* %12, %struct.iovec** %25
%26 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %11, i32 0, i32 0
store i32 1, i32* %26
%27 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %11, i32 0, i32 5
store i64 0, i64* %27
%28 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%29 = bitcast %struct.sockaddr_in* %28 to %struct.sockaddr*
%30 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %11, i32 0, i32 4
store %struct.sockaddr* %29, %struct.sockaddr** %30
%31 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %11, i32 0, i32 1
store i32 4, i32* %31
%32 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %11, i32 0, i32 2
store i8* %20, i8** %32
%33 = trunc i64 %18 to i32
%34 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %11, i32 0, i32 3
store i32 %33, i32* %34
%35 = load i32, i32* %6
%36 = call i32 @recvmsg(i32 %35, %struct.msghdr* %11, i32 0)
store i32 %36, i32* %16
%37 = load i32, i32* %16
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %5
%40 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %5
%43 = load i32*, i32** %10
%44 = icmp ne i32* %43, null
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = load i32*, i32** %10
store i32 0, i32* %46
br label %47
47: ; preds = %45, %42
%48 = bitcast i8* %20 to %struct.cmsghdr*
store %struct.cmsghdr* %48, %struct.cmsghdr** %15
%49 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %11)
store %struct.cmsghdr* %49, %struct.cmsghdr** %15
br label %50
50: ; preds = %84, %47
%51 = load %struct.cmsghdr*, %struct.cmsghdr** %15
%52 = icmp ne %struct.cmsghdr* %51, null
br i1 %52, label %53, label %87
53: ; preds = %50
%54 = load %struct.cmsghdr*, %struct.cmsghdr** %15
%55 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %54, i32 0, i32 0
%56 = load i64, i64* %55
%57 = icmp eq i64 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %53
%59 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %53
%62 = load %struct.cmsghdr*, %struct.cmsghdr** %15
%63 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %62, i32 0, i32 1
%64 = load i64, i64* %63
%65 = load i64, i64* @IPPROTO_IP
%66 = icmp eq i64 %64, %65
br i1 %66, label %67, label %83
67: ; preds = %61
%68 = load %struct.cmsghdr*, %struct.cmsghdr** %15
%69 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %68, i32 0, i32 2
%70 = load i64, i64* %69
%71 = load i64, i64* @IP_TOS
%72 = icmp eq i64 %70, %71
br i1 %72, label %73, label %83
73: ; preds = %67
%74 = load i32*, i32** %10
%75 = icmp ne i32* %74, null
br i1 %75, label %76, label %82
76: ; preds = %73
%77 = load %struct.cmsghdr*, %struct.cmsghdr** %15
%78 = call i32* @CMSG_DATA(%struct.cmsghdr* %77)
%79 = getelementptr inbounds i32, i32* %78, i64 0
%80 = load i32, i32* %79
%81 = load i32*, i32** %10
store i32 %80, i32* %81
br label %82
82: ; preds = %76, %73
br label %83
83: ; preds = %82, %67, %61
br label %84
84: ; preds = %83
%85 = load %struct.cmsghdr*, %struct.cmsghdr** %15
%86 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %11, %struct.cmsghdr* %85)
store %struct.cmsghdr* %86, %struct.cmsghdr** %15
br label %50
87: ; preds = %50
%88 = load i32, i32* %16
%89 = load i8*, i8** %13
ret i32 %88
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.msghdr = type { i32, i32, i8*, i32, %struct.sockaddr*, i64, %struct.iovec* }
%struct.sockaddr = type { i32 }
%struct.iovec = type { i32, i8* }
%struct.cmsghdr = type { i64, i64, i64 }
@.str = [23 x i8] c"Error receiving packet\00"
@.str.1 = [21 x i8] c"Null control message\00"
@IPPROTO_IP = global i64 0
@IP_TOS = global i64 0
define i32 @receive_packet(i32 %0, i8* %1, i32 %2, %struct.sockaddr_in* %3, i32* writeonly %4) {
%6 = alloca %struct.msghdr
%7 = alloca %struct.iovec
%8 = bitcast %struct.msghdr* %6 to i8*
%9 = bitcast %struct.iovec* %7 to i8*
%10 = tail call i32 @CMSG_SPACE(i32 100)
%11 = zext i32 %10 to i64
%12 = tail call i8* @llvm.stacksave()
%13 = alloca i8, i64 %11
%14 = getelementptr inbounds %struct.iovec, %struct.iovec* %7, i64 0, i32 1
store i8* %1, i8** %14
%15 = getelementptr inbounds %struct.iovec, %struct.iovec* %7, i64 0, i32 0
store i32 %2, i32* %15
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 6
store %struct.iovec* %7, %struct.iovec** %16
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 0
store i32 1, i32* %17
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 5
store i64 0, i64* %18
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 4
%20 = bitcast %struct.sockaddr** %19 to %struct.sockaddr_in**
store %struct.sockaddr_in* %3, %struct.sockaddr_in** %20
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 1
store i32 4, i32* %21
%22 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 2
store i8* %13, i8** %22
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 3
store i32 %10, i32* %23
%24 = call i32 @recvmsg(i32 %0, %struct.msghdr* %6, i32 0)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %5
%27 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %5
%30 = icmp ne i32* %4, null
br i1 %30, label %31, label %32
31: ; preds = %29
store i32 0, i32* %4
br label %32
32: ; preds = %31, %29
%33 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %6)
%34 = icmp eq %struct.cmsghdr* %33, null
br i1 %34, label %60, label %35
35: ; preds = %32, %57
%36 = phi %struct.cmsghdr* [ %58, %57 ], [ %33, %32 ]
%37 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %36, i64 0, i32 0
%38 = load i64, i64* %37
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %35
%44 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %36, i64 0, i32 1
%45 = load i64, i64* %44
%46 = load i64, i64* @IPPROTO_IP
%47 = icmp eq i64 %45, %46
br i1 %47, label %48, label %57
48: ; preds = %43
%49 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %36, i64 0, i32 2
%50 = load i64, i64* %49
%51 = load i64, i64* @IP_TOS
%52 = icmp eq i64 %50, %51
%53 = and i1 %30, %52
br i1 %53, label %54, label %57
54: ; preds = %48
%55 = call i32* @CMSG_DATA(%struct.cmsghdr* %36)
%56 = load i32, i32* %55
store i32 %56, i32* %4
br label %57
57: ; preds = %43, %48, %54
%58 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %6, %struct.cmsghdr* %36)
%59 = icmp eq %struct.cmsghdr* %58, null
br i1 %59, label %60, label %35
60: ; preds = %57, %32
ret i32 %24
}