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

/* Variables and functions */
int /*<<< orphan*/ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int /*<<< orphan*/ err_remark(char *, ...);
int /*<<< orphan*/ err_syserr(char *);
int /*<<< orphan*/ memmove(int *, int /*<<< orphan*/, int);
scalar_t__ recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int odbierz(int socket) {
  struct msghdr msg = {0};
  char m_buffer[1];
  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);
  err_remark("About to extract fd\n");
  int fd;
  memmove(&fd, CMSG_DATA(cmsg), sizeof(fd));
  err_remark("Extracted fd %d\n", fd);
  return fd;
}


x86 assembly (clang, -O0):
<odbierz>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
lea    -0x31(%rbp),%rax
mov    %rax,-0x48(%rbp)
movl   $0x1,-0x40(%rbp)
lea    -0x48(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x1,-0x30(%rbp)
lea    -0x150(%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    11e2 <odbierz+0x72>
lea    0xe23(%rip),%rdi
call   1050 <err_syserr@plt>
lea    -0x30(%rbp),%rdi
call   1090 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x158(%rbp)
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1080 <err_remark@plt>
mov    -0x158(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    %eax,%esi
lea    -0x15c(%rbp),%rdi
mov    $0x4,%edx
call   1070 <memmove@plt>
mov    -0x15c(%rbp),%esi
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1080 <err_remark@plt>
mov    -0x15c(%rbp),%eax
add    $0x160,%rsp
pop    %rbp
ret

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

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