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

/* Variables and functions */
int /*<<< orphan*/ abort();
scalar_t__ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ memset(struct msghdr *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ read_ancillary_fds(struct msghdr *);
int recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
void receiver(int fd) {
  char buffer[24], control_buf[256];
  struct msghdr msg;
  struct iovec iov;
  struct cmsghdr *cmsg;
  int n;
  sleep(2);
  while (1) {
    memset(&msg, 0, sizeof(msg));
    memset(&iov, 0, sizeof(iov));
    iov.iov_base = buffer;
    iov.iov_len = sizeof(buffer);
    msg.msg_iov = &iov;
    msg.msg_iovlen = 1;
    msg.msg_control = control_buf;
    msg.msg_controllen = sizeof(control_buf);
    n = recvmsg(fd, &msg, 0);
    fprintf(stderr, "recvmsg: %d (%m) controllen=%d\n", n, msg.msg_controllen);
    if (n <= 0)
      break;
    read_ancillary_fds(&msg);
  }
  if (close(fd)) {
    perror("close");
    abort();
  }
  fprintf(stderr, "receiver exiting\n");
}


x86 assembly (clang, -O0):
<receiver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x180,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x2,%edi
call   10a0 <sleep@plt>
lea    -0x148(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
lea    -0x170(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
lea    -0x20(%rbp),%rax
mov    %rax,-0x170(%rbp)
movl   $0x18,-0x168(%rbp)
lea    -0x170(%rbp),%rax
mov    %rax,-0x128(%rbp)
movl   $0x1,-0x13c(%rbp)
lea    -0x120(%rbp),%rax
mov    %rax,-0x138(%rbp)
movl   $0x100,-0x130(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x148(%rbp),%rsi
xor    %edx,%edx
call   1080 <recvmsg@plt>
mov    %eax,-0x17c(%rbp)
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
mov    -0x17c(%rbp),%edx
mov    -0x130(%rbp),%ecx
lea    0xdca(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
cmpl   $0x0,-0x17c(%rbp)
jg     124f <receiver+0xcf>
jmp    1260 <receiver+0xe0>
lea    -0x148(%rbp),%rdi
call   1040 <read_ancillary_fds@plt>
jmp    1198 <receiver+0x18>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
je     1285 <receiver+0x105>
lea    0xda7(%rip),%rdi
call   1090 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
lea    0xd91(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
add    $0x180,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receiver>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x178,%rsp
mov    %edi,%r14d
mov    $0x2,%edi
call   10a0 <sleep@plt>
mov    %rsp,%rbx
lea    0x28(%rsp),%r12
lea    0x70(%rsp),%r13
lea    0xe4e(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
lea    0x50(%rsp),%rax
mov    %rax,0x28(%rsp)
movl   $0x18,0x30(%rsp)
mov    %r12,0x20(%rsp)
movl   $0x1,0xc(%rsp)
mov    %r13,0x10(%rsp)
movl   $0x100,0x18(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1080 <recvmsg@plt>
mov    %eax,%ebp
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
mov    0x18(%rsp),%ecx
mov    %r15,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
test   %ebp,%ebp
jle    1240 <receiver+0xc0>
mov    %rbx,%rdi
call   1040 <read_ancillary_fds@plt>
jmp    11c0 <receiver+0x40>
mov    %r14d,%edi
call   1060 <close@plt>
test   %rax,%rax
jne    1276 <receiver+0xf6>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
lea    0xdc9(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
add    $0x178,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda3(%rip),%rdi
call   1090 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
%struct.msghdr = type { i8*, i32, i32, i8*, i32, %struct.msghdr* }
%struct.iovec = type { i8*, i32, i32, i8*, i32, %struct.iovec* }
%struct.cmsghdr = type { i32 }
@stderr = global i32 0
@.str = [32 x i8] c"recvmsg: %d (%m) controllen=%d\0A\00"
@.str.1 = [6 x i8] c"close\00"
@.str.2 = [18 x i8] c"receiver exiting\0A\00"
define void @receiver(i32 %0) {
%2 = alloca i32
%3 = alloca [24 x i8]
%4 = alloca [256 x i8]
%5 = alloca %struct.msghdr
%6 = alloca %struct.iovec
%7 = alloca %struct.cmsghdr*
%8 = alloca i32
store i32 %0, i32* %2
%9 = call i32 @sleep(i32 2)
br label %10
10: ; preds = %1, %33
%11 = call i32 @memset(%struct.msghdr* %5, i32 0, i32 40)
%12 = bitcast %struct.iovec* %6 to %struct.msghdr*
%13 = call i32 @memset(%struct.msghdr* %12, i32 0, i32 40)
%14 = getelementptr inbounds [24 x i8], [24 x i8]* %3, i64 0, i64 0
%15 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i32 0, i32 0
store i8* %14, i8** %15
%16 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i32 0, i32 1
store i32 24, i32* %16
%17 = bitcast %struct.iovec* %6 to %struct.msghdr*
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 5
store %struct.msghdr* %17, %struct.msghdr** %18
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 2
store i32 1, i32* %19
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 3
store i8* %20, i8** %21
%22 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 4
store i32 256, i32* %22
%23 = load i32, i32* %2
%24 = call i32 @recvmsg(i32 %23, %struct.msghdr* %5, i32 0)
store i32 %24, i32* %8
%25 = load i32, i32* @stderr
%26 = load i32, i32* %8
%27 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 4
%28 = load i32, i32* %27
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %26, i32 %28)
%30 = load i32, i32* %8
%31 = icmp sle i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %10
br label %35
33: ; preds = %10
%34 = call i32 @read_ancillary_fds(%struct.msghdr* %5)
br label %10
35: ; preds = %32
%36 = load i32, i32* %2
%37 = call i64 @close(i32 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%41 = call i32 (...) @abort()
unreachable
42: ; preds = %35
%43 = load i32, i32* @stderr
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
%struct.msghdr = type { i8*, i32, i32, i8*, i32, %struct.msghdr* }
%struct.iovec = type { i8*, i32, i32, i8*, i32, %struct.iovec* }
%struct.cmsghdr = type { i32 }
@stderr = global i32 0
@.str = [32 x i8] c"recvmsg: %d (%m) controllen=%d\0A\00"
@.str.1 = [6 x i8] c"close\00"
@.str.2 = [18 x i8] c"receiver exiting\0A\00"
define void @receiver(i32 %0) {
%2 = alloca [24 x i8]
%3 = alloca [256 x i8]
%4 = alloca %struct.msghdr
%5 = alloca %struct.iovec
%6 = getelementptr inbounds [24 x i8], [24 x i8]* %2, i64 0, i64 0
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%8 = bitcast %struct.msghdr* %4 to i8*
%9 = bitcast %struct.iovec* %5 to i8*
%10 = tail call i32 @sleep(i32 2)
%11 = bitcast %struct.iovec* %5 to %struct.msghdr*
%12 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i64 0, i32 0
%13 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i64 0, i32 1
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 5
%15 = bitcast %struct.msghdr** %14 to %struct.iovec**
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 2
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 3
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 4
br label %19
19: ; preds = %27, %1
%20 = call i32 @memset(%struct.msghdr* %4, i32 0, i32 40)
%21 = call i32 @memset(%struct.msghdr* %11, i32 0, i32 40)
store i8* %6, i8** %12
store i32 24, i32* %13
store %struct.iovec* %5, %struct.iovec** %15
store i32 1, i32* %16
store i8* %7, i8** %17
store i32 256, i32* %18
%22 = call i32 @recvmsg(i32 %0, %struct.msghdr* %4, i32 0)
%23 = load i32, i32* @stderr
%24 = load i32, i32* %18
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %22, i32 %24)
%26 = icmp slt i32 %22, 1
br i1 %26, label %29, label %27
27: ; preds = %19
%28 = call i32 @read_ancillary_fds(%struct.msghdr* %4)
br label %19
29: ; preds = %19
%30 = call i64 @close(i32 %0)
%31 = icmp eq i64 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %29
%33 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%34 = call i32 (...) @abort()
unreachable
35: ; preds = %29
%36 = load i32, i32* @stderr
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
ret void
}