#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 msghdr {
  int msg_iovlen;
  char *msg_control;
  int msg_controllen;
  struct iovec *msg_iov;
  int /*<<< orphan*/ member_0;
};
struct iovec {
  char *iov_base;
  int iov_len;
};
struct cmsghdr {
  int dummy;
};
typedef int /*<<< orphan*/ c_buffer;

/* Variables and functions */
void *CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int /*<<< orphan*/ err_remark(char *, ...);
int /*<<< orphan*/ err_syserr(char *);
scalar_t__ recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int recv_fd(int socket) {
  struct msghdr msg = {0};
  char m_buffer[256];
  char buf1[10] = {0};
  char buf2[10] = {0};
  struct iovec io[2];
  io[0].iov_base = buf1;
  io[0].iov_len = 3;
  io[1].iov_base = buf2;
  io[2].iov_len = 3;
  msg.msg_iov = io;
  msg.msg_iovlen = 2;
  char c_buffer[256];
  msg.msg_control = c_buffer;
  msg.msg_controllen = sizeof(c_buffer);
  if (recvmsg(socket, &msg, 0) < 0)
    err_syserr("Failed to receive message\n");
  struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
  void *data = CMSG_DATA(cmsg);
  err_remark("About to extract fd\n");
  int fd = *((int *)data);
  err_remark("Extracted fd %d\n", fd);
  err_remark("Extracted buf1 %s\n", buf1);
  err_remark("Extracted buf2 %s\n", buf2);
  return fd;
}


x86 assembly (clang, -O0):
<recv_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x290,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
lea    -0x13a(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1040 <memset@plt>
lea    -0x144(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1040 <memset@plt>
lea    -0x13a(%rbp),%rax
mov    %rax,-0x170(%rbp)
movl   $0x3,-0x168(%rbp)
lea    -0x144(%rbp),%rax
mov    %rax,-0x160(%rbp)
movl   $0x3,-0x148(%rbp)
lea    -0x170(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x2,-0x30(%rbp)
lea    -0x270(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x100,-0x20(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
xor    %edx,%edx
call   1060 <recvmsg@plt>
cmp    $0x0,%rax
jge    121c <recv_fd+0xbc>
lea    0xde9(%rip),%rdi
call   1050 <err_syserr@plt>
lea    -0x30(%rbp),%rdi
call   1080 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x278(%rbp)
mov    -0x278(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    %rax,-0x280(%rbp)
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1070 <err_remark@plt>
mov    -0x280(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x284(%rbp)
mov    -0x284(%rbp),%esi
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1070 <err_remark@plt>
lea    -0x13a(%rbp),%rsi
lea    0xdc3(%rip),%rdi
mov    $0x0,%al
call   1070 <err_remark@plt>
lea    -0x144(%rbp),%rsi
lea    0xdc1(%rip),%rdi
mov    $0x0,%al
call   1070 <err_remark@plt>
mov    -0x284(%rbp),%eax
add    $0x290,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_fd>:
push   %rbx
sub    $0x170,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movq   $0x0,0x40(%rsp)
movw   $0x0,0x18(%rsp)
movq   $0x0,0x10(%rsp)
movw   $0x0,0x8(%rsp)
movq   $0x0,(%rsp)
lea    0x10(%rsp),%rax
mov    %rax,0x50(%rsp)
movl   $0x3,0x58(%rsp)
mov    %rsp,%rax
mov    %rax,0x60(%rsp)
movl   $0x3,0x78(%rsp)
lea    0x50(%rsp),%rax
mov    %rax,0x38(%rsp)
movl   $0x2,0x20(%rsp)
lea    0x70(%rsp),%rax
mov    %rax,0x28(%rsp)
movl   $0x100,0x30(%rsp)
lea    0x20(%rsp),%rsi
xor    %edx,%edx
call   1050 <recvmsg@plt>
test   %rax,%rax
jns    11f0 <recv_fd+0xa0>
lea    0xe15(%rip),%rdi
call   1040 <err_syserr@plt>
lea    0x20(%rsp),%rdi
call   1070 <CMSG_FIRSTHDR@plt>
mov    %rax,%rdi
call   1030 <CMSG_DATA@plt>
mov    %rax,%rbx
lea    0xe0f(%rip),%rdi
xor    %eax,%eax
call   1060 <err_remark@plt>
mov    (%rbx),%ebx
lea    0xe14(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <err_remark@plt>
lea    0xe15(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1060 <err_remark@plt>
lea    0xe15(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1060 <err_remark@plt>
mov    %ebx,%eax
add    $0x170,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.msghdr = type { i32, i8*, i32, %struct.iovec*, i32 }
%struct.iovec = type { i8*, i32 }
%struct.cmsghdr = type { i32 }
@.str = [27 x i8] c"Failed to receive message\0A\00"
@.str.1 = [21 x i8] c"About to extract fd\0A\00"
@.str.2 = [17 x i8] c"Extracted fd %d\0A\00"
@.str.3 = [19 x i8] c"Extracted buf1 %s\0A\00"
@.str.4 = [19 x i8] c"Extracted buf2 %s\0A\00"
define i32 @recv_fd(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.msghdr
%4 = alloca [256 x i8]
%5 = alloca [10 x i8]
%6 = alloca [10 x i8]
%7 = alloca [2 x %struct.iovec]
%8 = alloca [256 x i8]
%9 = alloca %struct.cmsghdr*
%10 = alloca i8*
%11 = alloca i32
store i32 %0, i32* %2
%12 = bitcast %struct.msghdr* %3 to i8*
%13 = bitcast [10 x i8]* %5 to i8*
%14 = bitcast [10 x i8]* %6 to i8*
%15 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%16 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %7, i64 0, i64 0
%17 = getelementptr inbounds %struct.iovec, %struct.iovec* %16, i32 0, i32 0
store i8* %15, i8** %17
%18 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %7, i64 0, i64 0
%19 = getelementptr inbounds %struct.iovec, %struct.iovec* %18, i32 0, i32 1
store i32 3, i32* %19
%20 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%21 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %7, i64 0, i64 1
%22 = getelementptr inbounds %struct.iovec, %struct.iovec* %21, i32 0, i32 0
store i8* %20, i8** %22
%23 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %7, i64 0, i64 2
%24 = getelementptr inbounds %struct.iovec, %struct.iovec* %23, i32 0, i32 1
store i32 3, i32* %24
%25 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %7, i64 0, i64 0
%26 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i32 0, i32 3
store %struct.iovec* %25, %struct.iovec** %26
%27 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i32 0, i32 0
store i32 2, i32* %27
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%29 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i32 0, i32 1
store i8* %28, i8** %29
%30 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i32 0, i32 2
store i32 256, i32* %30
%31 = load i32, i32* %2
%32 = call i64 @recvmsg(i32 %31, %struct.msghdr* %3, i32 0)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %1
%35 = call i32 @err_syserr(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %36
36: ; preds = %34, %1
%37 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %3)
store %struct.cmsghdr* %37, %struct.cmsghdr** %9
%38 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%39 = call i8* @CMSG_DATA(%struct.cmsghdr* %38)
store i8* %39, i8** %10
%40 = call i32 (i8*, ...) @err_remark(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%41 = load i8*, i8** %10
%42 = bitcast i8* %41 to i32*
%43 = load i32, i32* %42
store i32 %43, i32* %11
%44 = load i32, i32* %11
%45 = call i32 (i8*, ...) @err_remark(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %44)
%46 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%47 = call i32 (i8*, ...) @err_remark(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %46)
%48 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%49 = call i32 (i8*, ...) @err_remark(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %48)
%50 = load i32, i32* %11
ret i32 %50
}

LLVM IR (-O2):
%struct.msghdr = type { i32, i8*, i32, %struct.iovec*, i32 }
%struct.iovec = type { i8*, i32 }
%struct.cmsghdr = type { i32 }
@.str = [27 x i8] c"Failed to receive message\0A\00"
@.str.1 = [21 x i8] c"About to extract fd\0A\00"
@.str.2 = [17 x i8] c"Extracted fd %d\0A\00"
@.str.3 = [19 x i8] c"Extracted buf1 %s\0A\00"
@.str.4 = [19 x i8] c"Extracted buf2 %s\0A\00"
define i32 @recv_fd(i32 %0) {
%2 = alloca %struct.msghdr
%3 = alloca [10 x i8]
%4 = alloca [10 x i8]
%5 = alloca [2 x %struct.iovec]
%6 = alloca [256 x i8]
%7 = bitcast %struct.msghdr* %2 to i8*
%8 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%9 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%10 = bitcast [2 x %struct.iovec]* %5 to i8*
%11 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %5, i64 0, i64 0
%12 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %5, i64 0, i64 0, i32 0
store i8* %8, i8** %12
%13 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %5, i64 0, i64 0, i32 1
store i32 3, i32* %13
%14 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %5, i64 0, i64 1, i32 0
store i8* %9, i8** %14
%15 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %5, i64 0, i64 2, i32 1
store i32 3, i32* %15
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %2, i64 0, i32 3
store %struct.iovec* %11, %struct.iovec** %16
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %2, i64 0, i32 0
store i32 2, i32* %17
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %2, i64 0, i32 1
store i8* %18, i8** %19
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %2, i64 0, i32 2
store i32 256, i32* %20
%21 = call i64 @recvmsg(i32 %0, %struct.msghdr* %2, i32 0)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %1
%24 = call i32 @err_syserr(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %23, %1
%26 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %2)
%27 = call i8* @CMSG_DATA(%struct.cmsghdr* %26)
%28 = call i32 (i8*, ...) @err_remark(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%29 = bitcast i8* %27 to i32*
%30 = load i32, i32* %29
%31 = call i32 (i8*, ...) @err_remark(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%32 = call i32 (i8*, ...) @err_remark(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %8)
%33 = call i32 (i8*, ...) @err_remark(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %9)
ret i32 %30
}