#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 uint64_t;
struct msghdr {
  int msg_controllen;
  scalar_t__ msg_flags;
  void *msg_control;
};
struct cmsghdr {
  int /*<<< orphan*/ cmsg_len;
  int /*<<< orphan*/ cmsg_type;
  int /*<<< orphan*/ cmsg_level;
};

/* Variables and functions */
int /*<<< orphan*/ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int /*<<< orphan*/ CMSG_LEN(int);
int CMSG_SPACE(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, void *, int);
uint64_t c_put_cmsg(void *buf, uint64_t buflen, uint64_t curlen,
                    struct cmsghdr *cmsg, void *data, uint64_t datalen) {
  struct msghdr mhdr;
  struct cmsghdr *f, *p = 0;
  mhdr.msg_control = buf;
  mhdr.msg_controllen = buflen;
  mhdr.msg_flags = 0;
  f = CMSG_FIRSTHDR(&mhdr);
  p = ((struct cmsghdr *)(((char *)f) + curlen));
  uint64_t new_length = curlen + CMSG_SPACE(datalen);
  if (new_length <= buflen) {
    p->cmsg_level = cmsg->cmsg_level;
    p->cmsg_type = cmsg->cmsg_type;
    p->cmsg_len = CMSG_LEN(datalen);
    memcpy(CMSG_DATA(p), data, datalen);
  } else {
    new_length = 0;
  }
  return new_length;
}


x86 assembly (clang, -O0):
<c_put_cmsg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    %r9d,-0x24(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
lea    -0x40(%rbp),%rdi
call   1070 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
movslq -0x10(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x58(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <CMSG_SPACE@plt>
mov    %eax,%ecx
mov    -0x58(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%eax
cmp    -0xc(%rbp),%eax
jg     1214 <c_put_cmsg+0xc4>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%ecx
mov    -0x50(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%ecx
mov    -0x50(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x24(%rbp),%edi
call   1040 <CMSG_LEN@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x50(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    %eax,%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1060 <memcpy@plt>
jmp    121b <c_put_cmsg+0xcb>
movl   $0x0,-0x54(%rbp)
mov    -0x54(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<c_put_cmsg>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9d,%r12d
mov    %r8,0x8(%rsp)
mov    %rcx,%r15
mov    %edx,%r13d
mov    %esi,%r14d
mov    %rdi,0x20(%rsp)
mov    %esi,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0x10(%rsp),%rdi
call   1070 <CMSG_FIRSTHDR@plt>
mov    %rax,%rbx
mov    %r12d,%edi
call   1050 <CMSG_SPACE@plt>
mov    %eax,%ebp
add    %r13d,%ebp
xor    %eax,%eax
cmp    %r14d,%ebp
jg     11d9 <c_put_cmsg+0x89>
movslq %r13d,%rax
add    %rax,%rbx
mov    0x8(%r15),%eax
mov    %eax,0x8(%rbx)
mov    0x4(%r15),%eax
mov    %eax,0x4(%rbx)
mov    %r12d,%edi
call   1040 <CMSG_LEN@plt>
mov    %eax,(%rbx)
mov    %rbx,%rdi
call   1030 <CMSG_DATA@plt>
mov    %eax,%edi
mov    0x8(%rsp),%rsi
mov    %r12d,%edx
call   1060 <memcpy@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.cmsghdr = type { i32, i32, i32 }
%struct.msghdr = type { i32, i64, i8* }
define i32 @c_put_cmsg(i8* %0, i32 %1, i32 %2, %struct.cmsghdr* %3, i8* %4, i32 %5) {
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.cmsghdr*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca %struct.msghdr
%14 = alloca %struct.cmsghdr*
%15 = alloca %struct.cmsghdr*
%16 = alloca i32
store i8* %0, i8** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store %struct.cmsghdr* %3, %struct.cmsghdr** %10
store i8* %4, i8** %11
store i32 %5, i32* %12
store %struct.cmsghdr* null, %struct.cmsghdr** %15
%17 = load i8*, i8** %7
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 2
store i8* %17, i8** %18
%19 = load i32, i32* %8
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 0
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 1
store i64 0, i64* %21
%22 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %13)
store %struct.cmsghdr* %22, %struct.cmsghdr** %14
%23 = load %struct.cmsghdr*, %struct.cmsghdr** %14
%24 = bitcast %struct.cmsghdr* %23 to i8*
%25 = load i32, i32* %9
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = bitcast i8* %27 to %struct.cmsghdr*
store %struct.cmsghdr* %28, %struct.cmsghdr** %15
%29 = load i32, i32* %9
%30 = load i32, i32* %12
%31 = call i32 @CMSG_SPACE(i32 %30)
%32 = add nsw i32 %29, %31
store i32 %32, i32* %16
%33 = load i32, i32* %16
%34 = load i32, i32* %8
%35 = icmp sle i32 %33, %34
br i1 %35, label %36, label %56
36: ; preds = %6
%37 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%38 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %37, i32 0, i32 2
%39 = load i32, i32* %38
%40 = load %struct.cmsghdr*, %struct.cmsghdr** %15
%41 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %40, i32 0, i32 2
store i32 %39, i32* %41
%42 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%43 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %42, i32 0, i32 1
%44 = load i32, i32* %43
%45 = load %struct.cmsghdr*, %struct.cmsghdr** %15
%46 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %45, i32 0, i32 1
store i32 %44, i32* %46
%47 = load i32, i32* %12
%48 = call i32 @CMSG_LEN(i32 %47)
%49 = load %struct.cmsghdr*, %struct.cmsghdr** %15
%50 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %49, i32 0, i32 0
store i32 %48, i32* %50
%51 = load %struct.cmsghdr*, %struct.cmsghdr** %15
%52 = call i32 @CMSG_DATA(%struct.cmsghdr* %51)
%53 = load i8*, i8** %11
%54 = load i32, i32* %12
%55 = call i32 @memcpy(i32 %52, i8* %53, i32 %54)
br label %57
56: ; preds = %6
store i32 0, i32* %16
br label %57
57: ; preds = %56, %36
%58 = load i32, i32* %16
ret i32 %58
}

LLVM IR (-O2):
%struct.cmsghdr = type { i32, i32, i32 }
%struct.msghdr = type { i32, i64, i8* }
define i32 @c_put_cmsg(i8* %0, i32 %1, i32 %2, %struct.cmsghdr* nocapture %3, i8* %4, i32 %5) {
%7 = alloca %struct.msghdr
%8 = bitcast %struct.msghdr* %7 to i8*
%9 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 2
store i8* %0, i8** %9
%10 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 0
store i32 %1, i32* %10
%11 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 1
store i64 0, i64* %11
%12 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %7)
%13 = bitcast %struct.cmsghdr* %12 to i8*
%14 = sext i32 %2 to i64
%15 = getelementptr inbounds i8, i8* %13, i64 %14
%16 = call i32 @CMSG_SPACE(i32 %5)
%17 = add nsw i32 %16, %2
%18 = icmp sgt i32 %17, %1
br i1 %18, label %33, label %19
19: ; preds = %6
%20 = bitcast i8* %15 to %struct.cmsghdr*
%21 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %3, i64 0, i32 2
%22 = load i32, i32* %21
%23 = getelementptr inbounds i8, i8* %15, i64 8
%24 = bitcast i8* %23 to i32*
store i32 %22, i32* %24
%25 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %3, i64 0, i32 1
%26 = load i32, i32* %25
%27 = getelementptr inbounds i8, i8* %15, i64 4
%28 = bitcast i8* %27 to i32*
store i32 %26, i32* %28
%29 = call i32 @CMSG_LEN(i32 %5)
%30 = bitcast i8* %15 to i32*
store i32 %29, i32* %30
%31 = call i32 @CMSG_DATA(%struct.cmsghdr* %20)
%32 = call i32 @memcpy(i32 %31, i8* %4, i32 %5)
br label %33
33: ; preds = %6, %19
%34 = phi i32 [ %17, %19 ], [ 0, %6 ]
ret i32 %34
}