#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_in6 {
  int dummy;
};
struct sockaddr {
  int dummy;
};
struct msghdr {
  int msg_namelen;
  int msg_iovlen;
  char *msg_control;
  int msg_controllen;
  int msg_flags;
  struct iovec *msg_iov;
  struct sockaddr *msg_name;
  int /*<<< orphan*/ member_0;
};
struct iovec {
  char *member_0;
  int member_1;
};
struct in6_addr {
  int dummy;
};
struct cmsghdr {
  scalar_t__ cmsg_level;
  scalar_t__ cmsg_type;
};
typedef int /*<<< orphan*/ cmsgbuf;

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
struct cmsghdr *CMSG_NXTHDR(struct msghdr *, struct cmsghdr *);
int CMSG_SPACE(int);
scalar_t__ IPPROTO_IPV6;
scalar_t__ IPV6_PKTINFO;
int MSG_CTRUNC;
int MSG_DONTWAIT;
int MSG_PEEK;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
size_t recvmsg(int, struct msghdr *, int);
int /*<<< orphan*/ stderr;
int check_connect(int sockfd, char *buf, int buflen, struct sockaddr *src,
                  int *ifindex) {
  struct in6_pktinfo {
    struct in6_addr ipi6_addr;
    unsigned int ipi6_ifindex;
  };
  size_t bytes;
  struct iovec iov[1] = {{buf, buflen}};
  char cmsgbuf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
  struct in6_pktinfo *p = NULL;
  struct msghdr msg = {0};
  struct cmsghdr *cmsg;
  msg.msg_name = src;
  msg.msg_namelen = sizeof(struct sockaddr_in6);
  msg.msg_iov = iov;
  msg.msg_iovlen = 1;
  msg.msg_control = cmsgbuf;
  msg.msg_controllen = sizeof(cmsgbuf);
  bytes = recvmsg(sockfd, &msg, MSG_DONTWAIT | MSG_PEEK);
  if (bytes < 0) {
    perror("recvmsg");
    return bytes;
  }
  if (msg.msg_flags & MSG_CTRUNC) {
    fprintf(stderr, "control was truncated!\n");
    return -1;
  }
  if (ifindex) {
    *ifindex = 0;
    for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
         cmsg = CMSG_NXTHDR(&msg, cmsg)) {
      if (cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO) {
        p = (struct in6_pktinfo *)(CMSG_DATA(cmsg));
        *ifindex = p->ipi6_ifindex;
        break;
      }
    }
  }
  return bytes;
}


x86 assembly (clang, -O0):
<check_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    $0x8,%edi
call   1050 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rax,-0xa0(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0xa8(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x50(%rbp)
movq   $0x0,-0x58(%rbp)
lea    -0x88(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1040 <memset@plt>
mov    -0xa8(%rbp),%rcx
mov    -0xa0(%rbp),%rax
mov    -0x20(%rbp),%rdx
mov    %rdx,-0x68(%rbp)
movl   $0x4,-0x88(%rbp)
lea    -0x40(%rbp),%rdx
mov    %rdx,-0x70(%rbp)
movl   $0x1,-0x84(%rbp)
mov    %rcx,-0x80(%rbp)
mov    %eax,-0x78(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d7e(%rip),%rax
mov    (%rax),%edx
mov    0x2d9d(%rip),%rax
or     (%rax),%edx
lea    -0x88(%rbp),%rsi
call   1080 <recvmsg@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jae    128a <check_connect+0x10a>
lea    0xd91(%rip),%rdi
call   1090 <perror@plt>
mov    -0x30(%rbp),%rax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x94(%rbp)
jmp    1391 <check_connect+0x211>
mov    -0x74(%rbp),%eax
mov    0x2d3c(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12ca <check_connect+0x14a>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
lea    0xd59(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x94(%rbp)
jmp    1391 <check_connect+0x211>
cmpq   $0x0,-0x28(%rbp)
je     1380 <check_connect+0x200>
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
lea    -0x88(%rbp),%rdi
call   10a0 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x90(%rbp)
cmpq   $0x0,-0x90(%rbp)
je     137b <check_connect+0x1fb>
mov    -0x90(%rbp),%rax
mov    (%rax),%rax
mov    0x2ca7(%rip),%rcx
cmp    (%rcx),%rax
jne    1357 <check_connect+0x1d7>
mov    -0x90(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2c9c(%rip),%rcx
cmp    (%rcx),%rax
jne    1357 <check_connect+0x1d7>
mov    -0x90(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
mov    0x4(%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
jmp    137b <check_connect+0x1fb>
jmp    135c <check_connect+0x1dc>
mov    -0x90(%rbp),%rsi
lea    -0x88(%rbp),%rdi
call   1070 <CMSG_NXTHDR@plt>
mov    %rax,-0x90(%rbp)
jmp    12f2 <check_connect+0x172>
jmp    1380 <check_connect+0x200>
mov    -0x30(%rbp),%rax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x94(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_connect>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %r8,%r14
mov    %rcx,%rbx
mov    %edi,%r15d
mov    %rsi,-0x70(%rbp)
mov    %edx,-0x68(%rbp)
mov    $0x8,%edi
call   1040 <CMSG_SPACE@plt>
mov    %rsp,%r12
mov    %eax,%ecx
mov    %rsp,%rdx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rdx
mov    %rdx,%rsp
xorps  %xmm0,%xmm0
movups %xmm0,-0x40(%rbp)
movups %xmm0,-0x50(%rbp)
mov    %rbx,-0x40(%rbp)
movabs $0x100000004,%rcx
mov    %rcx,-0x60(%rbp)
lea    -0x70(%rbp),%rcx
mov    %rcx,-0x48(%rbp)
mov    %rdx,-0x58(%rbp)
mov    %eax,-0x50(%rbp)
mov    0x2dec(%rip),%rax
mov    0x2e0d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    -0x60(%rbp),%rsi
mov    %r15d,%edi
call   1070 <recvmsg@plt>
mov    %rax,%r15
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
test   %eax,-0x4c(%rbp)
je     122c <check_connect+0xcc>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
lea    0xdf4(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%r15d
mov    %r12,%rsp
mov    %r15d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,-0x30(%rbp)
test   %r14,%r14
je     1217 <check_connect+0xb7>
mov    -0x30(%rbp),%rax
movl   $0x0,(%rax)
lea    -0x60(%rbp),%rdi
call   1080 <CMSG_FIRSTHDR@plt>
test   %rax,%rax
je     1217 <check_connect+0xb7>
mov    0x2d64(%rip),%r13
mov    0x2d6d(%rip),%r14
lea    -0x60(%rbp),%rbx
jmp    1280 <check_connect+0x120>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %rax,%rsi
call   1060 <CMSG_NXTHDR@plt>
test   %rax,%rax
je     1217 <check_connect+0xb7>
mov    (%rax),%rcx
cmp    0x0(%r13),%rcx
jne    1270 <check_connect+0x110>
mov    0x8(%rax),%rcx
cmp    (%r14),%rcx
jne    1270 <check_connect+0x110>
mov    %rax,%rdi
call   1030 <CMSG_DATA@plt>
mov    0x4(%rax),%eax
mov    -0x30(%rbp),%rcx
mov    %eax,(%rcx)
jmp    1217 <check_connect+0xb7>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.iovec = type { i8*, i32 }
%struct.in6_pktinfo = type { %struct.in6_addr, i32 }
%struct.in6_addr = type { i32 }
%struct.msghdr = type { i32, i32, i8*, i32, i32, %struct.iovec*, %struct.sockaddr*, i32 }
%struct.cmsghdr = type { i64, i64 }
@MSG_DONTWAIT = global i32 0
@MSG_PEEK = global i32 0
@.str = [8 x i8] c"recvmsg\00"
@MSG_CTRUNC = global i32 0
@stderr = global i32 0
@.str.1 = [24 x i8] c"control was truncated!\0A\00"
@IPPROTO_IPV6 = global i64 0
@IPV6_PKTINFO = global i64 0
define i32 @check_connect(i32 %0, i8* %1, i32 %2, %struct.sockaddr* %3, i32* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca %struct.sockaddr*
%11 = alloca i32*
%12 = alloca i64
%13 = alloca [1 x %struct.iovec]
%14 = alloca i8*
%15 = alloca i64
%16 = alloca %struct.in6_pktinfo*
%17 = alloca %struct.msghdr
%18 = alloca %struct.cmsghdr*
%19 = alloca i32
store i32 %0, i32* %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store %struct.sockaddr* %3, %struct.sockaddr** %10
store i32* %4, i32** %11
%20 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %13, i64 0, i64 0
%21 = getelementptr inbounds %struct.iovec, %struct.iovec* %20, i32 0, i32 0
%22 = load i8*, i8** %8
store i8* %22, i8** %21
%23 = getelementptr inbounds %struct.iovec, %struct.iovec* %20, i32 0, i32 1
%24 = load i32, i32* %9
store i32 %24, i32* %23
%25 = call i32 @CMSG_SPACE(i32 8)
%26 = zext i32 %25 to i64
%27 = call i8* @llvm.stacksave()
store i8* %27, i8** %14
%28 = alloca i8, i64 %26
store i64 %26, i64* %15
store %struct.in6_pktinfo* null, %struct.in6_pktinfo** %16
%29 = bitcast %struct.msghdr* %17 to i8*
%30 = load %struct.sockaddr*, %struct.sockaddr** %10
%31 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %17, i32 0, i32 6
store %struct.sockaddr* %30, %struct.sockaddr** %31
%32 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %17, i32 0, i32 0
store i32 4, i32* %32
%33 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %13, i64 0, i64 0
%34 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %17, i32 0, i32 5
store %struct.iovec* %33, %struct.iovec** %34
%35 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %17, i32 0, i32 1
store i32 1, i32* %35
%36 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %17, i32 0, i32 2
store i8* %28, i8** %36
%37 = trunc i64 %26 to i32
%38 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %17, i32 0, i32 3
store i32 %37, i32* %38
%39 = load i32, i32* %7
%40 = load i32, i32* @MSG_DONTWAIT
%41 = load i32, i32* @MSG_PEEK
%42 = or i32 %40, %41
%43 = call i64 @recvmsg(i32 %39, %struct.msghdr* %17, i32 %42)
store i64 %43, i64* %12
%44 = load i64, i64* %12
%45 = icmp ult i64 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %5
%47 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%48 = load i64, i64* %12
%49 = trunc i64 %48 to i32
store i32 %49, i32* %6
store i32 1, i32* %19
br label %96
50: ; preds = %5
%51 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %17, i32 0, i32 4
%52 = load i32, i32* %51
%53 = load i32, i32* @MSG_CTRUNC
%54 = and i32 %52, %53
%55 = icmp ne i32 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %50
%57 = load i32, i32* @stderr
%58 = call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %6
store i32 1, i32* %19
br label %96
59: ; preds = %50
%60 = load i32*, i32** %11
%61 = icmp ne i32* %60, null
br i1 %61, label %62, label %93
62: ; preds = %59
%63 = load i32*, i32** %11
store i32 0, i32* %63
%64 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %17)
store %struct.cmsghdr* %64, %struct.cmsghdr** %18
br label %65
65: ; preds = %89, %62
%66 = load %struct.cmsghdr*, %struct.cmsghdr** %18
%67 = icmp ne %struct.cmsghdr* %66, null
br i1 %67, label %68, label %92
68: ; preds = %65
%69 = load %struct.cmsghdr*, %struct.cmsghdr** %18
%70 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %69, i32 0, i32 0
%71 = load i64, i64* %70
%72 = load i64, i64* @IPPROTO_IPV6
%73 = icmp eq i64 %71, %72
br i1 %73, label %74, label %88
74: ; preds = %68
%75 = load %struct.cmsghdr*, %struct.cmsghdr** %18
%76 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %75, i32 0, i32 1
%77 = load i64, i64* %76
%78 = load i64, i64* @IPV6_PKTINFO
%79 = icmp eq i64 %77, %78
br i1 %79, label %80, label %88
80: ; preds = %74
%81 = load %struct.cmsghdr*, %struct.cmsghdr** %18
%82 = call i64 @CMSG_DATA(%struct.cmsghdr* %81)
%83 = inttoptr i64 %82 to %struct.in6_pktinfo*
store %struct.in6_pktinfo* %83, %struct.in6_pktinfo** %16
%84 = load %struct.in6_pktinfo*, %struct.in6_pktinfo** %16
%85 = getelementptr inbounds %struct.in6_pktinfo, %struct.in6_pktinfo* %84, i32 0, i32 1
%86 = load i32, i32* %85
%87 = load i32*, i32** %11
store i32 %86, i32* %87
br label %92
88: ; preds = %74, %68
br label %89
89: ; preds = %88
%90 = load %struct.cmsghdr*, %struct.cmsghdr** %18
%91 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %17, %struct.cmsghdr* %90)
store %struct.cmsghdr* %91, %struct.cmsghdr** %18
br label %65
92: ; preds = %80, %65
br label %93
93: ; preds = %92, %59
%94 = load i64, i64* %12
%95 = trunc i64 %94 to i32
store i32 %95, i32* %6
store i32 1, i32* %19
br label %96
96: ; preds = %93, %56, %46
%97 = load i8*, i8** %14
%98 = load i32, i32* %6
ret i32 %98
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.iovec = type { i8*, i32 }
%struct.msghdr = type { i32, i32, i8*, i32, i32, %struct.iovec*, %struct.sockaddr*, i32 }
%struct.in6_pktinfo = type { %struct.in6_addr, i32 }
%struct.in6_addr = type { i32 }
%struct.cmsghdr = type { i64, i64 }
@MSG_DONTWAIT = global i32 0
@MSG_PEEK = global i32 0
@MSG_CTRUNC = global i32 0
@stderr = global i32 0
@.str.1 = [24 x i8] c"control was truncated!\0A\00"
@IPPROTO_IPV6 = global i64 0
@IPV6_PKTINFO = global i64 0
define i32 @check_connect(i32 %0, i8* %1, i32 %2, %struct.sockaddr* %3, i32* writeonly %4) {
%6 = alloca [1 x %struct.iovec]
%7 = alloca %struct.msghdr
%8 = bitcast [1 x %struct.iovec]* %6 to i8*
%9 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %6, i64 0, i64 0
%10 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %6, i64 0, i64 0, i32 0
store i8* %1, i8** %10
%11 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %6, i64 0, i64 0, i32 1
store i32 %2, i32* %11
%12 = tail call i32 @CMSG_SPACE(i32 8)
%13 = zext i32 %12 to i64
%14 = tail call i8* @llvm.stacksave()
%15 = alloca i8, i64 %13
%16 = bitcast %struct.msghdr* %7 to i8*
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 3
%18 = bitcast i32* %17 to i8*
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 6
store %struct.sockaddr* %3, %struct.sockaddr** %19
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 0
store i32 4, i32* %20
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 5
store %struct.iovec* %9, %struct.iovec** %21
%22 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 1
store i32 1, i32* %22
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 2
store i8* %15, i8** %23
%24 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 3
store i32 %12, i32* %24
%25 = load i32, i32* @MSG_DONTWAIT
%26 = load i32, i32* @MSG_PEEK
%27 = or i32 %26, %25
%28 = call i64 @recvmsg(i32 %0, %struct.msghdr* %7, i32 %27)
%29 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 4
%30 = load i32, i32* %29
%31 = load i32, i32* @MSG_CTRUNC
%32 = and i32 %31, %30
%33 = icmp eq i32 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %5
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %63
37: ; preds = %5
%38 = icmp eq i32* %4, null
br i1 %38, label %61, label %39
39: ; preds = %37
store i32 0, i32* %4
%40 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %7)
%41 = icmp eq %struct.cmsghdr* %40, null
br i1 %41, label %61, label %42
42: ; preds = %39, %58
%43 = phi %struct.cmsghdr* [ %59, %58 ], [ %40, %39 ]
%44 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %43, i64 0, i32 0
%45 = load i64, i64* %44
%46 = load i64, i64* @IPPROTO_IPV6
%47 = icmp eq i64 %45, %46
br i1 %47, label %48, label %58
48: ; preds = %42
%49 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %43, i64 0, i32 1
%50 = load i64, i64* %49
%51 = load i64, i64* @IPV6_PKTINFO
%52 = icmp eq i64 %50, %51
br i1 %52, label %53, label %58
53: ; preds = %48
%54 = call i64 @CMSG_DATA(%struct.cmsghdr* %43)
%55 = inttoptr i64 %54 to %struct.in6_pktinfo*
%56 = getelementptr inbounds %struct.in6_pktinfo, %struct.in6_pktinfo* %55, i64 0, i32 1
%57 = load i32, i32* %56
store i32 %57, i32* %4
br label %61
58: ; preds = %42, %48
%59 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %7, %struct.cmsghdr* %43)
%60 = icmp eq %struct.cmsghdr* %59, null
br i1 %60, label %61, label %42
61: ; preds = %58, %39, %53, %37
%62 = trunc i64 %28 to i32
br label %63
63: ; preds = %61, %34
%64 = phi i32 [ -1, %34 ], [ %62, %61 ]
ret i32 %64
}