#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 int /*<<< orphan*/ uint32_t;
struct msghdr {
  scalar_t__ msg_control;
};
struct cmsghdr {
  int /*<<< orphan*/ cmsg_type;
  int /*<<< orphan*/ cmsg_level;
  int /*<<< orphan*/ cmsg_len;
};
typedef int /*<<< orphan*/ cookie;

/* Variables and functions */
int /*<<< orphan*/ CMSG_DATA(struct cmsghdr *);
int /*<<< orphan*/ CMSG_LEN(int);
int /*<<< orphan*/ RDS_CMSG_ZCOPY_COOKIE;
int /*<<< orphan*/ SOL_RDS;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, int /*<<< orphan*/ *, int);
void add_zcopy_cookie(struct msghdr *msg, uint32_t cookie) {
  struct cmsghdr *cm;
  if (!msg->msg_control)
    error(1, errno, "NULL cookie");
  cm = (void *)msg->msg_control;
  cm->cmsg_len = CMSG_LEN(sizeof(cookie));
  cm->cmsg_level = SOL_RDS;
  cm->cmsg_type = RDS_CMSG_ZCOPY_COOKIE;
  memcpy(CMSG_DATA(cm), &cookie, sizeof(cookie));
}


x86 assembly (clang, -O0):
<add_zcopy_cookie>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1177 <add_zcopy_cookie+0x37>
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe8e(%rip),%rdx
call   1060 <error@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    $0x4,%edi
call   1040 <CMSG_LEN@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2e4c(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    0x2e24(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    %eax,%edi
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
call   1050 <memcpy@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_zcopy_cookie>:
push   %r14
push   %rbx
push   %rax
mov    %esi,0x4(%rsp)
mov    (%rdi),%rbx
test   %rbx,%rbx
jne    1170 <add_zcopy_cookie+0x30>
mov    %rdi,%r14
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
lea    0xe9d(%rip),%rdx
mov    $0x1,%edi
call   1060 <error@plt>
mov    (%r14),%rbx
mov    $0x4,%edi
call   1040 <CMSG_LEN@plt>
mov    %eax,0x8(%rbx)
mov    0x2e64(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbx)
mov    0x2e40(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rbx)
mov    %rbx,%rdi
call   1030 <CMSG_DATA@plt>
lea    0x4(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1050 <memcpy@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.msghdr = type { i64 }
%struct.cmsghdr = type { i32, i32, i32 }
@errno = global i32 0
@.str = [12 x i8] c"NULL cookie\00"
@SOL_RDS = global i32 0
@RDS_CMSG_ZCOPY_COOKIE = global i32 0
define void @add_zcopy_cookie(%struct.msghdr* %0, i32 %1) {
%3 = alloca %struct.msghdr*
%4 = alloca i32
%5 = alloca %struct.cmsghdr*
store %struct.msghdr* %0, %struct.msghdr** %3
store i32 %1, i32* %4
%6 = load %struct.msghdr*, %struct.msghdr** %3
%7 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 0
%8 = load i64, i64* %7
%9 = icmp ne i64 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load i32, i32* @errno
%12 = call i32 @error(i32 1, i32 %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %10, %2
%14 = load %struct.msghdr*, %struct.msghdr** %3
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %14, i32 0, i32 0
%16 = load i64, i64* %15
%17 = inttoptr i64 %16 to i8*
%18 = bitcast i8* %17 to %struct.cmsghdr*
store %struct.cmsghdr* %18, %struct.cmsghdr** %5
%19 = call i32 @CMSG_LEN(i32 4)
%20 = load %struct.cmsghdr*, %struct.cmsghdr** %5
%21 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %20, i32 0, i32 2
store i32 %19, i32* %21
%22 = load i32, i32* @SOL_RDS
%23 = load %struct.cmsghdr*, %struct.cmsghdr** %5
%24 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %23, i32 0, i32 1
store i32 %22, i32* %24
%25 = load i32, i32* @RDS_CMSG_ZCOPY_COOKIE
%26 = load %struct.cmsghdr*, %struct.cmsghdr** %5
%27 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %26, i32 0, i32 0
store i32 %25, i32* %27
%28 = load %struct.cmsghdr*, %struct.cmsghdr** %5
%29 = call i32 @CMSG_DATA(%struct.cmsghdr* %28)
%30 = call i32 @memcpy(i32 %29, i32* %4, i32 4)
ret void
}

LLVM IR (-O2):
%struct.msghdr = type { i64 }
%struct.cmsghdr = type { i32, i32, i32 }
@errno = global i32 0
@.str = [12 x i8] c"NULL cookie\00"
@SOL_RDS = global i32 0
@RDS_CMSG_ZCOPY_COOKIE = global i32 0
define void @add_zcopy_cookie(%struct.msghdr* nocapture %0, i32 %1) {
%3 = alloca i32
store i32 %1, i32* %3
%4 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %0, i64 0, i32 0
%5 = load i64, i64* %4
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = load i32, i32* @errno
%9 = tail call i32 @error(i32 1, i32 %8, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%10 = load i64, i64* %4
br label %11
11: ; preds = %7, %2
%12 = phi i64 [ %10, %7 ], [ %5, %2 ]
%13 = inttoptr i64 %12 to %struct.cmsghdr*
%14 = tail call i32 @CMSG_LEN(i32 4)
%15 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %13, i64 0, i32 2
store i32 %14, i32* %15
%16 = load i32, i32* @SOL_RDS
%17 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %13, i64 0, i32 1
store i32 %16, i32* %17
%18 = load i32, i32* @RDS_CMSG_ZCOPY_COOKIE
%19 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %13, i64 0, i32 0
store i32 %18, i32* %19
%20 = tail call i32 @CMSG_DATA(%struct.cmsghdr* %13)
%21 = call i32 @memcpy(i32 %20, i32* %3, i32 4)
ret void
}