#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*/ m_buffer;
typedef int /*<<< orphan*/ c_buffer;

/* Variables and functions */
unsigned char *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 odbierz(int socket) {
  struct msghdr msg = {0};
  char m_buffer[256];
  struct iovec io = {.iov_base = m_buffer, .iov_len = sizeof(m_buffer)};
  msg.msg_iov = &io;
  msg.msg_iovlen = 1;
  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);
  unsigned char *data = CMSG_DATA(cmsg);
  err_remark("About to extract fd\n");
  int fd = *((int *)data);
  err_remark("Extracted fd %d\n", fd);
  return fd;
}


x86 assembly (clang, -O0):
<odbierz>:
push   %rbp
mov    %rsp,%rbp
sub    $0x260,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
lea    -0x130(%rbp),%rax
mov    %rax,-0x140(%rbp)
movl   $0x100,-0x138(%rbp)
lea    -0x140(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x1,-0x30(%rbp)
lea    -0x240(%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    11de <odbierz+0x7e>
lea    0xe27(%rip),%rdi
call   1050 <err_syserr@plt>
lea    -0x30(%rbp),%rdi
call   1080 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x248(%rbp)
mov    -0x248(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    %rax,-0x250(%rbp)
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1070 <err_remark@plt>
mov    -0x250(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x254(%rbp)
mov    -0x254(%rbp),%esi
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1070 <err_remark@plt>
mov    -0x254(%rbp),%eax
add    $0x260,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<odbierz>:
push   %rbx
sub    $0x240,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
movq   $0x0,0x20(%rsp)
lea    0x140(%rsp),%rax
mov    %rax,0x30(%rsp)
movl   $0x100,0x38(%rsp)
lea    0x30(%rsp),%rax
mov    %rax,0x18(%rsp)
movl   $0x1,(%rsp)
lea    0x40(%rsp),%rax
mov    %rax,0x8(%rsp)
movl   $0x100,0x10(%rsp)
mov    %rsp,%rsi
xor    %edx,%edx
call   1050 <recvmsg@plt>
test   %rax,%rax
jns    11c0 <odbierz+0x70>
lea    0xe45(%rip),%rdi
call   1040 <err_syserr@plt>
mov    %rsp,%rdi
call   1070 <CMSG_FIRSTHDR@plt>
mov    %rax,%rdi
call   1030 <CMSG_DATA@plt>
mov    %rax,%rbx
lea    0xe41(%rip),%rdi
xor    %eax,%eax
call   1060 <err_remark@plt>
mov    (%rbx),%ebx
lea    0xe46(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <err_remark@plt>
mov    %ebx,%eax
add    $0x240,%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"
define i32 @odbierz(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.msghdr
%4 = alloca [256 x i8]
%5 = alloca %struct.iovec
%6 = alloca [256 x i8]
%7 = alloca %struct.cmsghdr*
%8 = alloca i8*
%9 = alloca i32
store i32 %0, i32* %2
%10 = bitcast %struct.msghdr* %3 to i8*
%11 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i32 0, i32 0
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
store i8* %12, i8** %11
%13 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i32 0, i32 1
store i32 256, i32* %13
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i32 0, i32 3
store %struct.iovec* %5, %struct.iovec** %14
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i32 0, i32 0
store i32 1, i32* %15
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i32 0, i32 1
store i8* %16, i8** %17
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i32 0, i32 2
store i32 256, i32* %18
%19 = load i32, i32* %2
%20 = call i64 @recvmsg(i32 %19, %struct.msghdr* %3, i32 0)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %1
%23 = call i32 @err_syserr(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %22, %1
%25 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %3)
store %struct.cmsghdr* %25, %struct.cmsghdr** %7
%26 = load %struct.cmsghdr*, %struct.cmsghdr** %7
%27 = call i8* @CMSG_DATA(%struct.cmsghdr* %26)
store i8* %27, i8** %8
%28 = call i32 (i8*, ...) @err_remark(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%29 = load i8*, i8** %8
%30 = bitcast i8* %29 to i32*
%31 = load i32, i32* %30
store i32 %31, i32* %9
%32 = load i32, i32* %9
%33 = call i32 (i8*, ...) @err_remark(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %32)
%34 = load i32, i32* %9
ret i32 %34
}

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"
define i32 @odbierz(i32 %0) {
%2 = alloca %struct.msghdr
%3 = alloca [256 x i8]
%4 = alloca %struct.iovec
%5 = alloca [256 x i8]
%6 = bitcast %struct.msghdr* %2 to i8*
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%8 = bitcast %struct.iovec* %4 to i8*
%9 = getelementptr inbounds %struct.iovec, %struct.iovec* %4, i64 0, i32 0
store i8* %7, i8** %9
%10 = getelementptr inbounds %struct.iovec, %struct.iovec* %4, i64 0, i32 1
store i32 256, i32* %10
%11 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %2, i64 0, i32 3
store %struct.iovec* %4, %struct.iovec** %11
%12 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %2, i64 0, i32 0
store i32 1, i32* %12
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %2, i64 0, i32 1
store i8* %13, i8** %14
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %2, i64 0, i32 2
store i32 256, i32* %15
%16 = call i64 @recvmsg(i32 %0, %struct.msghdr* %2, i32 0)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %1
%19 = call i32 @err_syserr(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %1
%21 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %2)
%22 = call i8* @CMSG_DATA(%struct.cmsghdr* %21)
%23 = call i32 (i8*, ...) @err_remark(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%24 = bitcast i8* %22 to i32*
%25 = load i32, i32* %24
%26 = call i32 (i8*, ...) @err_remark(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %25)
ret i32 %25
}