#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 {
  int dummy;
};
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 socklen_t;
typedef int /*<<< orphan*/ c_buffer;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
unsigned char *CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int /*<<< orphan*/ MSG_DONTWAIT;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memcpy(struct sockaddr *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int ouistiti_recvaddr(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
  int ret = -1;
  struct msghdr msg = {0};
  int length;
  char buffer[256];
  struct iovec io = {.iov_base = buffer, .iov_len = sizeof(buffer)};
  msg.msg_iov = &io;
  msg.msg_iovlen = 1;
  char c_buffer[256];
  memset(c_buffer, 0, sizeof(c_buffer));
  msg.msg_control = c_buffer;
  msg.msg_controllen = sizeof(c_buffer);
  ret = recvmsg(sockfd, &msg, MSG_DONTWAIT);
  if (ret > 0) {
    if (addrlen && addr && msg.msg_iov) {
      length = *addrlen;
      *addrlen = msg.msg_iov[0].iov_len;
      length = (*addrlen < length) ? *addrlen : length;
      memcpy(addr, msg.msg_iov[0].iov_base, length);
    }
    {
      struct cmsghdr *cmsg;
      unsigned char *data;
      cmsg = CMSG_FIRSTHDR(&msg);
      data = CMSG_DATA(cmsg);
      if (data) {
        ret = *(int *)data;
        close(sockfd);
      }
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<ouistiti_recvaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x280,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
lea    -0x150(%rbp),%rax
mov    %rax,-0x160(%rbp)
movl   $0x100,-0x158(%rbp)
lea    -0x160(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x1,-0x48(%rbp)
lea    -0x260(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
lea    -0x260(%rbp),%rax
mov    %rax,-0x40(%rbp)
movl   $0x100,-0x38(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
lea    -0x48(%rbp),%rsi
call   1070 <recvmsg@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    12c4 <ouistiti_recvaddr+0x164>
cmpq   $0x0,-0x18(%rbp)
je     127a <ouistiti_recvaddr+0x11a>
cmpq   $0x0,-0x10(%rbp)
je     127a <ouistiti_recvaddr+0x11a>
cmpq   $0x0,-0x30(%rbp)
je     127a <ouistiti_recvaddr+0x11a>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
cmp    -0x4c(%rbp),%eax
jge    1255 <ouistiti_recvaddr+0xf5>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x274(%rbp)
jmp    125e <ouistiti_recvaddr+0xfe>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x274(%rbp)
mov    -0x274(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
mov    -0x4c(%rbp),%edx
call   1060 <memcpy@plt>
lea    -0x48(%rbp),%rdi
call   1080 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x268(%rbp)
mov    -0x268(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    %rax,-0x270(%rbp)
cmpq   $0x0,-0x270(%rbp)
je     12bf <ouistiti_recvaddr+0x15f>
mov    -0x270(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
jmp    12c4 <ouistiti_recvaddr+0x164>
mov    -0x1c(%rbp),%eax
add    $0x280,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ouistiti_recvaddr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x248,%rsp
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %edi,%r14d
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),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
mov    %rbp,0x8(%rsp)
movl   $0x100,0x10(%rsp)
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rsi
mov    %r14d,%edi
call   1070 <recvmsg@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1236 <ouistiti_recvaddr+0xd6>
test   %r15,%r15
je     1217 <ouistiti_recvaddr+0xb7>
test   %rbx,%rbx
je     1217 <ouistiti_recvaddr+0xb7>
mov    0x18(%rsp),%rax
test   %rax,%rax
je     1217 <ouistiti_recvaddr+0xb7>
mov    (%rbx),%edx
mov    0x8(%rax),%ecx
mov    %ecx,(%rbx)
cmp    %edx,%ecx
cmovl  %ecx,%edx
mov    (%rax),%rsi
mov    %r15,%rdi
call   1060 <memcpy@plt>
mov    %rsp,%rdi
call   1080 <CMSG_FIRSTHDR@plt>
mov    %rax,%rdi
call   1030 <CMSG_DATA@plt>
test   %rax,%rax
je     1236 <ouistiti_recvaddr+0xd6>
mov    (%rax),%ebp
mov    %r14d,%edi
call   1050 <close@plt>
mov    %ebp,%eax
add    $0x248,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.msghdr = type { i32, i8*, i32, %struct.iovec*, i32 }
%struct.iovec = type { i8*, i32 }
%struct.cmsghdr = type { i32 }
@MSG_DONTWAIT = global i32 0
define i32 @ouistiti_recvaddr(i32 %0, %struct.sockaddr* %1, i32* %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca %struct.msghdr
%9 = alloca i32
%10 = alloca [256 x i8]
%11 = alloca %struct.iovec
%12 = alloca [256 x i8]
%13 = alloca %struct.cmsghdr*
%14 = alloca i8*
store i32 %0, i32* %4
store %struct.sockaddr* %1, %struct.sockaddr** %5
store i32* %2, i32** %6
store i32 -1, i32* %7
%15 = bitcast %struct.msghdr* %8 to i8*
%16 = getelementptr inbounds %struct.iovec, %struct.iovec* %11, i32 0, i32 0
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
store i8* %17, i8** %16
%18 = getelementptr inbounds %struct.iovec, %struct.iovec* %11, i32 0, i32 1
store i32 256, i32* %18
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 3
store %struct.iovec* %11, %struct.iovec** %19
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 0
store i32 1, i32* %20
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%22 = call i32 @memset(i8* %21, i32 0, i32 256)
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%24 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 1
store i8* %23, i8** %24
%25 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 2
store i32 256, i32* %25
%26 = load i32, i32* %4
%27 = load i32, i32* @MSG_DONTWAIT
%28 = call i32 @recvmsg(i32 %26, %struct.msghdr* %8, i32 %27)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %82
31: ; preds = %3
%32 = load i32*, i32** %6
%33 = icmp ne i32* %32, null
br i1 %33, label %34, label %69
34: ; preds = %31
%35 = load %struct.sockaddr*, %struct.sockaddr** %5
%36 = icmp ne %struct.sockaddr* %35, null
br i1 %36, label %37, label %69
37: ; preds = %34
%38 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 3
%39 = load %struct.iovec*, %struct.iovec** %38
%40 = icmp ne %struct.iovec* %39, null
br i1 %40, label %41, label %69
41: ; preds = %37
%42 = load i32*, i32** %6
%43 = load i32, i32* %42
store i32 %43, i32* %9
%44 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 3
%45 = load %struct.iovec*, %struct.iovec** %44
%46 = getelementptr inbounds %struct.iovec, %struct.iovec* %45, i64 0
%47 = getelementptr inbounds %struct.iovec, %struct.iovec* %46, i32 0, i32 1
%48 = load i32, i32* %47
%49 = load i32*, i32** %6
store i32 %48, i32* %49
%50 = load i32*, i32** %6
%51 = load i32, i32* %50
%52 = load i32, i32* %9
%53 = icmp slt i32 %51, %52
br i1 %53, label %54, label %57
54: ; preds = %41
%55 = load i32*, i32** %6
%56 = load i32, i32* %55
br label %59
57: ; preds = %41
%58 = load i32, i32* %9
br label %59
59: ; preds = %57, %54
%60 = phi i32 [ %56, %54 ], [ %58, %57 ]
store i32 %60, i32* %9
%61 = load %struct.sockaddr*, %struct.sockaddr** %5
%62 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 3
%63 = load %struct.iovec*, %struct.iovec** %62
%64 = getelementptr inbounds %struct.iovec, %struct.iovec* %63, i64 0
%65 = getelementptr inbounds %struct.iovec, %struct.iovec* %64, i32 0, i32 0
%66 = load i8*, i8** %65
%67 = load i32, i32* %9
%68 = call i32 @memcpy(%struct.sockaddr* %61, i8* %66, i32 %67)
br label %69
69: ; preds = %59, %37, %34, %31
%70 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %8)
store %struct.cmsghdr* %70, %struct.cmsghdr** %13
%71 = load %struct.cmsghdr*, %struct.cmsghdr** %13
%72 = call i8* @CMSG_DATA(%struct.cmsghdr* %71)
store i8* %72, i8** %14
%73 = load i8*, i8** %14
%74 = icmp ne i8* %73, null
br i1 %74, label %75, label %81
75: ; preds = %69
%76 = load i8*, i8** %14
%77 = bitcast i8* %76 to i32*
%78 = load i32, i32* %77
store i32 %78, i32* %7
%79 = load i32, i32* %4
%80 = call i32 @close(i32 %79)
br label %81
81: ; preds = %75, %69
br label %82
82: ; preds = %81, %3
%83 = load i32, i32* %7
ret i32 %83
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.msghdr = type { i32, i8*, i32, %struct.iovec*, i32 }
%struct.iovec = type { i8*, i32 }
%struct.cmsghdr = type { i32 }
@MSG_DONTWAIT = global i32 0
define i32 @ouistiti_recvaddr(i32 %0, %struct.sockaddr* %1, i32* %2) {
%4 = alloca %struct.msghdr
%5 = alloca [256 x i8]
%6 = alloca %struct.iovec
%7 = alloca [256 x i8]
%8 = bitcast %struct.msghdr* %4 to i8*
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%10 = bitcast %struct.iovec* %6 to i8*
%11 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i64 0, i32 0
store i8* %9, i8** %11
%12 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i64 0, i32 1
store i32 256, i32* %12
%13 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 3
store %struct.iovec* %6, %struct.iovec** %13
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 0
store i32 1, i32* %14
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%16 = call i32 @memset(i8* %15, i32 0, i32 256)
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 1
store i8* %15, i8** %17
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 2
store i32 256, i32* %18
%19 = load i32, i32* @MSG_DONTWAIT
%20 = call i32 @recvmsg(i32 %0, %struct.msghdr* %4, i32 %19)
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %46
22: ; preds = %3
%23 = icmp ne i32* %2, null
%24 = icmp ne %struct.sockaddr* %1, null
%25 = and i1 %24, %23
%26 = load %struct.iovec*, %struct.iovec** %13
%27 = icmp ne %struct.iovec* %26, null
%28 = select i1 %25, i1 %27, i1 false
br i1 %28, label %29, label %38
29: ; preds = %22
%30 = load i32, i32* %2
%31 = getelementptr inbounds %struct.iovec, %struct.iovec* %26, i64 0, i32 1
%32 = load i32, i32* %31
store i32 %32, i32* %2
%33 = icmp slt i32 %32, %30
%34 = select i1 %33, i32 %32, i32 %30
%35 = getelementptr inbounds %struct.iovec, %struct.iovec* %26, i64 0, i32 0
%36 = load i8*, i8** %35
%37 = call i32 @memcpy(%struct.sockaddr* %1, i8* %36, i32 %34)
br label %38
38: ; preds = %29, %22
%39 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %4)
%40 = call i8* @CMSG_DATA(%struct.cmsghdr* %39)
%41 = icmp eq i8* %40, null
br i1 %41, label %46, label %42
42: ; preds = %38
%43 = bitcast i8* %40 to i32*
%44 = load i32, i32* %43
%45 = call i32 @close(i32 %0)
br label %46
46: ; preds = %38, %42, %3
%47 = phi i32 [ %20, %3 ], [ %44, %42 ], [ %20, %38 ]
ret i32 %47
}