#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 scalar_t__ uint64_t;
struct msghdr {
  scalar_t__ 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 *);
struct cmsghdr *CMSG_NXTHDR(struct msghdr *, struct cmsghdr *);
int /*<<< orphan*/ memcpy(void *, int /*<<< orphan*/, scalar_t__);
uint64_t c_get_cmsg(void *buf, uint64_t buflen, uint64_t offset,
                    struct cmsghdr *cmsg, void *data, uint64_t datalen) {
  struct msghdr mhdr;
  struct cmsghdr *f, *p, *n;
  mhdr.msg_control = buf;
  mhdr.msg_controllen = buflen;
  mhdr.msg_flags = 0;
  if (offset > mhdr.msg_controllen)
    return 0;
  f = CMSG_FIRSTHDR(&mhdr);
  p = ((struct cmsghdr *)(((char *)f) + offset));
  if (((uint64_t)(((char *)p) - ((char *)f))) >= buflen) {
    return 0;
  }
  cmsg->cmsg_level = p->cmsg_level;
  cmsg->cmsg_type = p->cmsg_type;
  cmsg->cmsg_len = p->cmsg_len;
  uint64_t actual_data = cmsg->cmsg_len - sizeof *cmsg;
  if (actual_data < datalen)
    datalen = actual_data;
  if (data && datalen > 0)
    memcpy(data, CMSG_DATA(p), datalen);
  n = CMSG_NXTHDR(&mhdr, p);
  if (!n)
    return buflen;
  uint64_t new_offset = ((uint64_t)(((char *)n) - ((char *)f)));
  return new_offset;
}


x86 assembly (clang, -O0):
<c_get_cmsg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x50(%rbp),%rax
jle    1196 <c_get_cmsg+0x56>
movq   $0x0,-0x8(%rbp)
jmp    1294 <c_get_cmsg+0x154>
lea    -0x50(%rbp),%rdi
call   1060 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rax
mov    -0x58(%rbp),%rcx
sub    %rcx,%rax
cmp    -0x18(%rbp),%rax
jl     11d1 <c_get_cmsg+0x91>
movq   $0x0,-0x8(%rbp)
jmp    1294 <c_get_cmsg+0x154>
mov    -0x60(%rbp),%rax
mov    0x8(%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x60(%rbp),%rax
mov    0x4(%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x60(%rbp),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
movslq (%rax),%rax
sub    $0xc,%rax
mov    %rax,-0x70(%rbp)
mov    -0x70(%rbp),%rax
cmp    -0x38(%rbp),%rax
jge    121e <c_get_cmsg+0xde>
mov    -0x70(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1254 <c_get_cmsg+0x114>
cmpq   $0x0,-0x38(%rbp)
jle    1254 <c_get_cmsg+0x114>
mov    -0x30(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x60(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    -0x80(%rbp),%rdi
mov    %eax,%esi
mov    -0x38(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x60(%rbp),%rsi
lea    -0x50(%rbp),%rdi
call   1050 <CMSG_NXTHDR@plt>
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
jne    127d <c_get_cmsg+0x13d>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1294 <c_get_cmsg+0x154>
mov    -0x68(%rbp),%rax
mov    -0x58(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x78(%rbp)
mov    -0x78(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<c_get_cmsg>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,0x20(%rsp)
mov    %rsi,0x10(%rsp)
movq   $0x0,0x18(%rsp)
cmp    %rsi,%rdx
jg     11eb <c_get_cmsg+0xab>
mov    %r9,%r13
mov    %r8,%r15
mov    %rcx,%rbp
mov    %rdx,%rbx
mov    %rsi,%r14
lea    0x10(%rsp),%rdi
call   1060 <CMSG_FIRSTHDR@plt>
cmp    %r14,%rbx
jge    11eb <c_get_cmsg+0xab>
mov    %rax,0x8(%rsp)
add    %rax,%rbx
mov    0x8(%rbx),%eax
mov    %eax,0x8(%rbp)
mov    0x4(%rbx),%eax
mov    %eax,0x4(%rbp)
movslq (%rbx),%r12
mov    %r12d,0x0(%rbp)
add    $0xfffffffffffffff4,%r12
cmp    %r13,%r12
cmovge %r13,%r12
test   %r15,%r15
je     11cd <c_get_cmsg+0x8d>
test   %r12,%r12
jle    11cd <c_get_cmsg+0x8d>
mov    %rbx,%rdi
call   1030 <CMSG_DATA@plt>
mov    %r15,%rdi
mov    %eax,%esi
mov    %r12,%rdx
call   1040 <memcpy@plt>
lea    0x10(%rsp),%rdi
mov    %rbx,%rsi
call   1050 <CMSG_NXTHDR@plt>
mov    %rax,%rcx
sub    0x8(%rsp),%rax
test   %rcx,%rcx
cmove  %r14,%rax
jmp    11ed <c_get_cmsg+0xad>
xor    %eax,%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 { i64, i64, i8* }
define i64 @c_get_cmsg(i8* %0, i64 %1, i64 %2, %struct.cmsghdr* %3, i8* %4, i64 %5) {
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca %struct.cmsghdr*
%12 = alloca i8*
%13 = alloca i64
%14 = alloca %struct.msghdr
%15 = alloca %struct.cmsghdr*
%16 = alloca %struct.cmsghdr*
%17 = alloca %struct.cmsghdr*
%18 = alloca i64
%19 = alloca i64
store i8* %0, i8** %8
store i64 %1, i64* %9
store i64 %2, i64* %10
store %struct.cmsghdr* %3, %struct.cmsghdr** %11
store i8* %4, i8** %12
store i64 %5, i64* %13
%20 = load i8*, i8** %8
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %14, i32 0, i32 2
store i8* %20, i8** %21
%22 = load i64, i64* %9
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %14, i32 0, i32 0
store i64 %22, i64* %23
%24 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %14, i32 0, i32 1
store i64 0, i64* %24
%25 = load i64, i64* %10
%26 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %14, i32 0, i32 0
%27 = load i64, i64* %26
%28 = icmp sgt i64 %25, %27
br i1 %28, label %29, label %30
29: ; preds = %6
store i64 0, i64* %7
br label %101
30: ; preds = %6
%31 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %14)
store %struct.cmsghdr* %31, %struct.cmsghdr** %15
%32 = load %struct.cmsghdr*, %struct.cmsghdr** %15
%33 = bitcast %struct.cmsghdr* %32 to i8*
%34 = load i64, i64* %10
%35 = getelementptr inbounds i8, i8* %33, i64 %34
%36 = bitcast i8* %35 to %struct.cmsghdr*
store %struct.cmsghdr* %36, %struct.cmsghdr** %16
%37 = load %struct.cmsghdr*, %struct.cmsghdr** %16
%38 = bitcast %struct.cmsghdr* %37 to i8*
%39 = load %struct.cmsghdr*, %struct.cmsghdr** %15
%40 = bitcast %struct.cmsghdr* %39 to i8*
%41 = ptrtoint i8* %38 to i64
%42 = ptrtoint i8* %40 to i64
%43 = sub i64 %41, %42
%44 = load i64, i64* %9
%45 = icmp sge i64 %43, %44
br i1 %45, label %46, label %47
46: ; preds = %30
store i64 0, i64* %7
br label %101
47: ; preds = %30
%48 = load %struct.cmsghdr*, %struct.cmsghdr** %16
%49 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %48, i32 0, i32 2
%50 = load i32, i32* %49
%51 = load %struct.cmsghdr*, %struct.cmsghdr** %11
%52 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %51, i32 0, i32 2
store i32 %50, i32* %52
%53 = load %struct.cmsghdr*, %struct.cmsghdr** %16
%54 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %53, i32 0, i32 1
%55 = load i32, i32* %54
%56 = load %struct.cmsghdr*, %struct.cmsghdr** %11
%57 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %56, i32 0, i32 1
store i32 %55, i32* %57
%58 = load %struct.cmsghdr*, %struct.cmsghdr** %16
%59 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %58, i32 0, i32 0
%60 = load i32, i32* %59
%61 = load %struct.cmsghdr*, %struct.cmsghdr** %11
%62 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %61, i32 0, i32 0
store i32 %60, i32* %62
%63 = load %struct.cmsghdr*, %struct.cmsghdr** %11
%64 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %63, i32 0, i32 0
%65 = load i32, i32* %64
%66 = sext i32 %65 to i64
%67 = sub i64 %66, 12
store i64 %67, i64* %18
%68 = load i64, i64* %18
%69 = load i64, i64* %13
%70 = icmp slt i64 %68, %69
br i1 %70, label %71, label %73
71: ; preds = %47
%72 = load i64, i64* %18
store i64 %72, i64* %13
br label %73
73: ; preds = %71, %47
%74 = load i8*, i8** %12
%75 = icmp ne i8* %74, null
br i1 %75, label %76, label %85
76: ; preds = %73
%77 = load i64, i64* %13
%78 = icmp sgt i64 %77, 0
br i1 %78, label %79, label %85
79: ; preds = %76
%80 = load i8*, i8** %12
%81 = load %struct.cmsghdr*, %struct.cmsghdr** %16
%82 = call i32 @CMSG_DATA(%struct.cmsghdr* %81)
%83 = load i64, i64* %13
%84 = call i32 @memcpy(i8* %80, i32 %82, i64 %83)
br label %85
85: ; preds = %79, %76, %73
%86 = load %struct.cmsghdr*, %struct.cmsghdr** %16
%87 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %14, %struct.cmsghdr* %86)
store %struct.cmsghdr* %87, %struct.cmsghdr** %17
%88 = load %struct.cmsghdr*, %struct.cmsghdr** %17
%89 = icmp ne %struct.cmsghdr* %88, null
br i1 %89, label %92, label %90
90: ; preds = %85
%91 = load i64, i64* %9
store i64 %91, i64* %7
br label %101
92: ; preds = %85
%93 = load %struct.cmsghdr*, %struct.cmsghdr** %17
%94 = bitcast %struct.cmsghdr* %93 to i8*
%95 = load %struct.cmsghdr*, %struct.cmsghdr** %15
%96 = bitcast %struct.cmsghdr* %95 to i8*
%97 = ptrtoint i8* %94 to i64
%98 = ptrtoint i8* %96 to i64
%99 = sub i64 %97, %98
store i64 %99, i64* %19
%100 = load i64, i64* %19
store i64 %100, i64* %7
br label %101
101: ; preds = %92, %90, %46, %29
%102 = load i64, i64* %7
ret i64 %102
}

LLVM IR (-O2):
%struct.cmsghdr = type { i32, i32, i32 }
%struct.msghdr = type { i64, i64, i8* }
define i64 @c_get_cmsg(i8* %0, i64 %1, i64 %2, %struct.cmsghdr* nocapture writeonly %3, i8* %4, i64 %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 i64 %1, i64* %10
%11 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 1
store i64 0, i64* %11
%12 = icmp sgt i64 %2, %1
br i1 %12, label %50, label %13
13: ; preds = %6
%14 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %7)
%15 = bitcast %struct.cmsghdr* %14 to i8*
%16 = getelementptr inbounds i8, i8* %15, i64 %2
%17 = bitcast i8* %16 to %struct.cmsghdr*
%18 = ptrtoint i8* %16 to i64
%19 = ptrtoint %struct.cmsghdr* %14 to i64
%20 = sub i64 %18, %19
%21 = icmp slt i64 %20, %1
br i1 %21, label %22, label %50
22: ; preds = %13
%23 = getelementptr inbounds i8, i8* %16, i64 8
%24 = bitcast i8* %23 to i32*
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %3, i64 0, i32 2
store i32 %25, i32* %26
%27 = getelementptr inbounds i8, i8* %16, i64 4
%28 = bitcast i8* %27 to i32*
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %3, i64 0, i32 1
store i32 %29, i32* %30
%31 = bitcast i8* %16 to i32*
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %3, i64 0, i32 0
store i32 %32, i32* %33
%34 = sext i32 %32 to i64
%35 = add nsw i64 %34, -12
%36 = icmp slt i64 %35, %5
%37 = select i1 %36, i64 %35, i64 %5
%38 = icmp ne i8* %4, null
%39 = icmp sgt i64 %37, 0
%40 = select i1 %38, i1 %39, i1 false
br i1 %40, label %41, label %44
41: ; preds = %22
%42 = call i32 @CMSG_DATA(%struct.cmsghdr* %17)
%43 = call i32 @memcpy(i8* %4, i32 %42, i64 %37)
br label %44
44: ; preds = %41, %22
%45 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %7, %struct.cmsghdr* %17)
%46 = icmp eq %struct.cmsghdr* %45, null
%47 = ptrtoint %struct.cmsghdr* %45 to i64
%48 = sub i64 %47, %19
%49 = select i1 %46, i64 %1, i64 %48
br label %50
50: ; preds = %13, %6, %44
%51 = phi i64 [ %49, %44 ], [ 0, %6 ], [ 0, %13 ]
ret i64 %51
}