#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 sockaddr_un {
  int /*<<< orphan*/ sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};
struct msghdr {
  int msg_iovlen;
  char *msg_control;
  int msg_controllen;
  struct msghdr *msg_iov;
};
struct iovec {
  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*/ fd_buf;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int CMSG_SPACE(int);
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memcpy(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ memset(struct msghdr *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ recv(int, char *, int, int /*<<< orphan*/);
int recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int main(int argc, char *argv[]) {
  int ret, len;
  int pass_sd;
  char write_buf[1024];
  struct iovec iov;
  struct msghdr msg;
  int accepted;
  int sock;
  struct sockaddr_un downstream;
  sock = socket(AF_UNIX, SOCK_STREAM, 0);
  downstream.sun_family = AF_UNIX;
  strcpy(downstream.sun_path, argv[1]);
  len = sizeof(downstream.sun_path) + sizeof(downstream.sun_family);
  if (connect(sock, (struct sockaddr *)&downstream, len) == -1) {
    perror("connect");
    exit(1);
  }
  memset(&msg, 0, sizeof(msg));
  memset(&iov, 0, sizeof(iov));
  char fd_buf[CMSG_SPACE(sizeof(int))];
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;
  msg.msg_control = fd_buf;
  msg.msg_controllen = sizeof(fd_buf);
  printf("Waiting on recvmsg\n");
  ret = recvmsg(sock, &msg, 0);
  if (ret < 0) {
    perror("recvmsg() failed");
    close(sock);
    exit(-1);
  }
  struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
  memcpy(&pass_sd, CMSG_DATA(cmsg), sizeof(int));
  printf("Received descriptor = %d\n", pass_sd);
  accepted = accept(pass_sd, NULL, NULL);
  printf("Waiting on data: ");
  recv(accepted, write_buf, 1024, 0);
  printf("%s", write_buf);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x490,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x468(%rbp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x46c(%rbp)
mov    -0x470(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1040 <strcpy@plt>
movl   $0x8,-0x18(%rbp)
mov    -0x468(%rbp),%edi
lea    -0x470(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   10f0 <connect@plt>
cmp    $0xffffffff,%eax
jne    127e <main+0x8e>
lea    0xd91(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    -0x460(%rbp),%rdi
xor    %esi,%esi
mov    %esi,-0x490(%rbp)
mov    $0x20,%edx
mov    %edx,-0x48c(%rbp)
call   1070 <memset@plt>
mov    -0x490(%rbp),%esi
mov    -0x48c(%rbp),%edx
lea    -0x440(%rbp),%rdi
call   1070 <memset@plt>
mov    $0x4,%edi
call   1090 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rsp,%rcx
mov    %rcx,-0x478(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,%rsp
mov    %rax,-0x480(%rbp)
lea    -0x440(%rbp),%rdx
mov    %rdx,-0x448(%rbp)
movl   $0x1,-0x460(%rbp)
mov    %rcx,-0x458(%rbp)
mov    %eax,-0x450(%rbp)
lea    0xcf6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x468(%rbp),%edi
lea    -0x460(%rbp),%rsi
xor    %edx,%edx
call   10b0 <recvmsg@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    135b <main+0x16b>
lea    0xcdb(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x468(%rbp),%edi
call   1080 <close@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>
lea    -0x460(%rbp),%rdi
call   1100 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x488(%rbp)
mov    -0x488(%rbp),%rdi
call   1050 <CMSG_DATA@plt>
mov    %eax,%esi
lea    -0x1c(%rbp),%rdi
mov    $0x4,%edx
call   10a0 <memcpy@plt>
mov    -0x1c(%rbp),%esi
lea    0xc99(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x1c(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10d0 <accept@plt>
mov    %eax,-0x464(%rbp)
lea    0xc90(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x464(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
lea    -0x420(%rbp),%rsi
lea    0xc74(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x478(%rbp),%rax
mov    %rax,%rsp
xor    %eax,%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x450,%rsp
mov    %rsi,%rbx
mov    0x2dcc(%rip),%r14
mov    (%r14),%edi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
mov    %eax,%r15d
mov    (%r14),%eax
mov    %eax,-0x2c(%rbp)
mov    0x8(%rbx),%rsi
call   1040 <strcpy@plt>
lea    -0x30(%rbp),%rsi
mov    %r15d,%edi
mov    $0x8,%edx
call   1100 <connect@plt>
cmp    $0xffffffff,%eax
je     135d <main+0x15d>
lea    -0x50(%rbp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1080 <memset@plt>
lea    -0x70(%rbp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1080 <memset@plt>
mov    $0x4,%edi
call   10a0 <CMSG_SPACE@plt>
mov    %rsp,%r12
mov    %eax,%ecx
mov    %rsp,%rdx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rdx
mov    %rdx,%rsp
mov    %rbx,-0x38(%rbp)
movl   $0x1,-0x50(%rbp)
mov    %rdx,-0x48(%rbp)
mov    %eax,-0x40(%rbp)
lea    0xd8e(%rip),%rdi
call   1050 <puts@plt>
mov    %r15d,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   10c0 <recvmsg@plt>
test   %eax,%eax
js     1373 <main+0x173>
lea    -0x50(%rbp),%rdi
call   1110 <CMSG_FIRSTHDR@plt>
mov    %rax,%rdi
call   1060 <CMSG_DATA@plt>
lea    -0x24(%rbp),%rdi
mov    %eax,%esi
mov    $0x4,%edx
call   10b0 <memcpy@plt>
mov    -0x24(%rbp),%esi
lea    0xd1a(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    -0x24(%rbp),%edi
xor    %esi,%esi
xor    %edx,%edx
call   10e0 <accept@plt>
mov    %eax,%ebx
lea    0xd18(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
lea    -0x470(%rbp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
lea    0xd04(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r12,%rsp
xor    %eax,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc9c(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
lea    0xc8e(%rip),%rdi
call   10d0 <perror@plt>
mov    %r15d,%edi
call   1090 <close@plt>
mov    $0xffffffff,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.iovec = type { i32, i8*, i32, %struct.iovec* }
%struct.msghdr = type { i32, i8*, i32, %struct.msghdr* }
%struct.sockaddr_un = type { i32, i32 }
%struct.cmsghdr = type { i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [8 x i8] c"connect\00"
@.str.1 = [20 x i8] c"Waiting on recvmsg\0A\00"
@.str.2 = [17 x i8] c"recvmsg() failed\00"
@.str.3 = [26 x i8] c"Received descriptor = %d\0A\00"
@.str.4 = [18 x i8] c"Waiting on data: \00"
@.str.5 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [1024 x i8]
%9 = alloca %struct.iovec
%10 = alloca %struct.msghdr
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.sockaddr_un
%14 = alloca i8*
%15 = alloca i64
%16 = alloca %struct.cmsghdr*
store i32 %0, i32* %3
store i8** %1, i8*** %4
%17 = load i32, i32* @AF_UNIX
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
store i32 %19, i32* %12
%20 = load i32, i32* @AF_UNIX
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %13, i32 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %13, i32 0, i32 0
%23 = load i32, i32* %22
%24 = load i8**, i8*** %4
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32 @strcpy(i32 %23, i8* %26)
store i32 8, i32* %6
%28 = load i32, i32* %12
%29 = bitcast %struct.sockaddr_un* %13 to %struct.sockaddr*
%30 = load i32, i32* %6
%31 = call i32 @connect(i32 %28, %struct.sockaddr* %29, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %2
%34 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %2
%37 = call i32 @memset(%struct.msghdr* %10, i32 0, i32 32)
%38 = bitcast %struct.iovec* %9 to %struct.msghdr*
%39 = call i32 @memset(%struct.msghdr* %38, i32 0, i32 32)
%40 = call i32 @CMSG_SPACE(i32 4)
%41 = zext i32 %40 to i64
%42 = call i8* @llvm.stacksave()
store i8* %42, i8** %14
%43 = alloca i8, i64 %41
store i64 %41, i64* %15
%44 = bitcast %struct.iovec* %9 to %struct.msghdr*
%45 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 3
store %struct.msghdr* %44, %struct.msghdr** %45
%46 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 0
store i32 1, i32* %46
%47 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 1
store i8* %43, i8** %47
%48 = trunc i64 %41 to i32
%49 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 2
store i32 %48, i32* %49
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%51 = load i32, i32* %12
%52 = call i32 @recvmsg(i32 %51, %struct.msghdr* %10, i32 0)
store i32 %52, i32* %5
%53 = load i32, i32* %5
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %60
55: ; preds = %36
%56 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%57 = load i32, i32* %12
%58 = call i32 @close(i32 %57)
%59 = call i32 @exit(i32 -1)
unreachable
60: ; preds = %36
%61 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %10)
store %struct.cmsghdr* %61, %struct.cmsghdr** %16
%62 = load %struct.cmsghdr*, %struct.cmsghdr** %16
%63 = call i32 @CMSG_DATA(%struct.cmsghdr* %62)
%64 = call i32 @memcpy(i32* %7, i32 %63, i32 4)
%65 = load i32, i32* %7
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %65)
%67 = load i32, i32* %7
%68 = call i32 @accept(i32 %67, i32* null, i32* null)
store i32 %68, i32* %11
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%70 = load i32, i32* %11
%71 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%72 = call i32 @recv(i32 %70, i8* %71, i32 1024, i32 0)
%73 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %73)
%75 = load i8*, i8** %14
ret i32 0
}

LLVM IR (-O2):
%struct.iovec = type { i32, i8*, i32, %struct.iovec* }
%struct.msghdr = type { i32, i8*, i32, %struct.msghdr* }
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
%struct.cmsghdr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [8 x i8] c"connect\00"
@.str.2 = [17 x i8] c"recvmsg() failed\00"
@.str.3 = [26 x i8] c"Received descriptor = %d\0A\00"
@.str.4 = [18 x i8] c"Waiting on data: \00"
@.str.5 = [3 x i8] c"%s\00"
@str = [19 x i8] c"Waiting on recvmsg\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca %struct.iovec
%6 = alloca %struct.msghdr
%7 = alloca %struct.sockaddr_un
%8 = bitcast i32* %3 to i8*
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%10 = bitcast %struct.iovec* %5 to i8*
%11 = bitcast %struct.msghdr* %6 to i8*
%12 = bitcast %struct.sockaddr_un* %7 to i8*
%13 = load i32, i32* @AF_UNIX
%14 = load i32, i32* @SOCK_STREAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = load i32, i32* @AF_UNIX
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i64 0, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = tail call i32 @strcpy(i32 undef, i8* %19)
%21 = bitcast %struct.sockaddr_un* %7 to %struct.sockaddr*
%22 = call i32 @connect(i32 %15, %struct.sockaddr* %21, i32 8)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %2
%25 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %2
%28 = call i32 @memset(%struct.msghdr* %6, i32 0, i32 32)
%29 = bitcast %struct.iovec* %5 to %struct.msghdr*
%30 = call i32 @memset(%struct.msghdr* %29, i32 0, i32 32)
%31 = call i32 @CMSG_SPACE(i32 4)
%32 = zext i32 %31 to i64
%33 = call i8* @llvm.stacksave()
%34 = alloca i8, i64 %32
%35 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 3
%36 = bitcast %struct.msghdr** %35 to %struct.iovec**
store %struct.iovec* %5, %struct.iovec** %36
%37 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 0
store i32 1, i32* %37
%38 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 1
store i8* %34, i8** %38
%39 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 2
store i32 %31, i32* %39
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%41 = call i32 @recvmsg(i32 %15, %struct.msghdr* %6, i32 0)
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %27
%44 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%45 = call i32 @close(i32 %15)
%46 = call i32 @exit(i32 -1)
unreachable
47: ; preds = %27
%48 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %6)
%49 = call i32 @CMSG_DATA(%struct.cmsghdr* %48)
%50 = call i32 @memcpy(i32* %3, i32 %49, i32 4)
%51 = load i32, i32* %3
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %51)
%53 = load i32, i32* %3
%54 = call i32 @accept(i32 %53, i32* null, i32* null)
%55 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%56 = call i32 @recv(i32 %54, i8* %9, i32 1024, i32 0)
%57 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %9)
ret i32 0
}