#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ uint32_t;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  scalar_t__ sin_family;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int dummy;
};
struct in6_addr {
  int dummy;
};
typedef int socklen_t;

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ htonl(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(struct sockaddr_in *, struct sockaddr_in const *,
                          int);
int socket(scalar_t__, int /*<<< orphan*/, int /*<<< orphan*/);
const char *findsaddr(register const struct sockaddr_in *to,
                      struct in6_addr *from) {
  const char *errstr;
  struct sockaddr_in cto, cfrom;
  int s;
  socklen_t len;
  s = socket(AF_INET, SOCK_DGRAM, 0);
  if (s == -1)
    return ("failed to open DGRAM socket for src addr selection.");
  errstr = NULL;
  len = sizeof(struct sockaddr_in);
  memcpy(&cto, to, len);
  cto.sin_port = htons(65535);
  if (connect(s, (struct sockaddr *)&cto, len) == -1) {
    errstr = "failed to connect to peer for src addr selection.";
    goto err;
  }
  if (getsockname(s, (struct sockaddr *)&cfrom, &len) == -1) {
    errstr = "failed to get socket name for src addr selection.";
    goto err;
  }
  if (len != sizeof(struct sockaddr_in) || cfrom.sin_family != AF_INET) {
    errstr = "unexpected address family in src addr selection.";
    goto err;
  }
  ((uint32_t *)from)[0] = 0;
  ((uint32_t *)from)[1] = 0;
  ((uint32_t *)from)[2] = htonl(0xffff);
  ((uint32_t *)from)[3] = cfrom.sin_addr.s_addr;
err:
  (void)close(s);
  return (errstr);
}


x86 assembly (clang, -O0):
<findsaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e61(%rip),%rax
mov    (%rax),%rdi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    11b7 <findsaddr+0x47>
lea    0xe52(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    12a8 <findsaddr+0x138>
movq   $0x0,-0x20(%rbp)
movl   $0x10,-0x48(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x48(%rbp),%edx
lea    -0x30(%rbp),%rdi
call   1060 <memcpy@plt>
mov    $0xffff,%edi
call   1030 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x44(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x48(%rbp),%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    120b <findsaddr+0x9b>
lea    0xe32(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    1298 <findsaddr+0x128>
mov    -0x44(%rbp),%edi
lea    -0x40(%rbp),%rsi
lea    -0x48(%rbp),%rdx
call   1070 <getsockname@plt>
cmp    $0xffffffff,%eax
jne    1234 <findsaddr+0xc4>
lea    0xe3b(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    1298 <findsaddr+0x128>
movslq -0x48(%rbp),%rax
cmp    $0x10,%rax
jne    1256 <findsaddr+0xe6>
mov    -0x40(%rbp),%rax
mov    0x2d9b(%rip),%rcx
cmp    (%rcx),%rax
je     1266 <findsaddr+0xf6>
lea    0xe3b(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    1298 <findsaddr+0x128>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movl   $0x0,0x4(%rax)
mov    $0xffff,%edi
call   1040 <htonl@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x38(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0xc(%rax)
mov    -0x44(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findsaddr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e5f(%rip),%r12
mov    (%r12),%rdi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     1237 <findsaddr+0xc7>
mov    %eax,%ebp
movl   $0x10,0xc(%rsp)
lea    0x20(%rsp),%r15
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1060 <memcpy@plt>
mov    $0xffff,%edi
call   1030 <htons@plt>
mov    %eax,0x2c(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x10,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
je     1240 <findsaddr+0xd0>
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebp,%edi
call   1070 <getsockname@plt>
cmp    $0xffffffff,%eax
je     1249 <findsaddr+0xd9>
lea    0xe94(%rip),%rbx
cmpl   $0x10,0xc(%rsp)
jne    1250 <findsaddr+0xe0>
mov    0x10(%rsp),%rax
cmp    (%r12),%rax
jne    1250 <findsaddr+0xe0>
movq   $0x0,(%r14)
mov    $0xffff,%edi
call   1040 <htonl@plt>
mov    %eax,0x8(%r14)
mov    0x18(%rsp),%eax
mov    %eax,0xc(%r14)
xor    %ebx,%ebx
jmp    1250 <findsaddr+0xe0>
lea    0xdc2(%rip),%rbx
jmp    1257 <findsaddr+0xe7>
lea    0xded(%rip),%rbx
jmp    1250 <findsaddr+0xe0>
lea    0xe16(%rip),%rbx
mov    %ebp,%edi
call   1050 <close@plt>
mov    %rbx,%rax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i64, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.in6_addr = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i64 0
@SOCK_DGRAM = global i32 0
@.str = [52 x i8] c"failed to open DGRAM socket for src addr selection.\00"
@.str.1 = [50 x i8] c"failed to connect to peer for src addr selection.\00"
@.str.2 = [50 x i8] c"failed to get socket name for src addr selection.\00"
@.str.3 = [49 x i8] c"unexpected address family in src addr selection.\00"
define i8* @findsaddr(%struct.sockaddr_in* %0, %struct.in6_addr* %1) {
%3 = alloca i8*
%4 = alloca %struct.sockaddr_in*
%5 = alloca %struct.in6_addr*
%6 = alloca i8*
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca i32
store %struct.sockaddr_in* %0, %struct.sockaddr_in** %4
store %struct.in6_addr* %1, %struct.in6_addr** %5
%11 = load i64, i64* @AF_INET
%12 = load i32, i32* @SOCK_DGRAM
%13 = call i32 @socket(i64 %11, i32 %12, i32 0)
store i32 %13, i32* %9
%14 = load i32, i32* %9
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %2
store i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i8** %3
br label %66
17: ; preds = %2
store i8* null, i8** %6
store i32 16, i32* %10
%18 = load %struct.sockaddr_in*, %struct.sockaddr_in** %4
%19 = load i32, i32* %10
%20 = call i32 @memcpy(%struct.sockaddr_in* %7, %struct.sockaddr_in* %18, i32 %19)
%21 = call i32 @htons(i32 65535)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %21, i32* %22
%23 = load i32, i32* %9
%24 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%25 = load i32, i32* %10
%26 = call i32 @connect(i32 %23, %struct.sockaddr* %24, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %29
28: ; preds = %17
store i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i8** %6
br label %62
29: ; preds = %17
%30 = load i32, i32* %9
%31 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%32 = call i32 @getsockname(i32 %30, %struct.sockaddr* %31, i32* %10)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %35
34: ; preds = %29
store i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8** %6
br label %62
35: ; preds = %29
%36 = load i32, i32* %10
%37 = sext i32 %36 to i64
%38 = icmp ne i64 %37, 16
br i1 %38, label %44, label %39
39: ; preds = %35
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%41 = load i64, i64* %40
%42 = load i64, i64* @AF_INET
%43 = icmp ne i64 %41, %42
br i1 %43, label %44, label %45
44: ; preds = %39, %35
store i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i8** %6
br label %62
45: ; preds = %39
%46 = load %struct.in6_addr*, %struct.in6_addr** %5
%47 = bitcast %struct.in6_addr* %46 to i32*
%48 = getelementptr inbounds i32, i32* %47, i64 0
store i32 0, i32* %48
%49 = load %struct.in6_addr*, %struct.in6_addr** %5
%50 = bitcast %struct.in6_addr* %49 to i32*
%51 = getelementptr inbounds i32, i32* %50, i64 1
store i32 0, i32* %51
%52 = call i32 @htonl(i32 65535)
%53 = load %struct.in6_addr*, %struct.in6_addr** %5
%54 = bitcast %struct.in6_addr* %53 to i32*
%55 = getelementptr inbounds i32, i32* %54, i64 2
store i32 %52, i32* %55
%56 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%57 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %56, i32 0, i32 0
%58 = load i32, i32* %57
%59 = load %struct.in6_addr*, %struct.in6_addr** %5
%60 = bitcast %struct.in6_addr* %59 to i32*
%61 = getelementptr inbounds i32, i32* %60, i64 3
store i32 %58, i32* %61
br label %62
62: ; preds = %45, %44, %34, %28
%63 = load i32, i32* %9
%64 = call i32 @close(i32 %63)
%65 = load i8*, i8** %6
store i8* %65, i8** %3
br label %66
66: ; preds = %62, %16
%67 = load i8*, i8** %3
ret i8* %67
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i64, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.in6_addr = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i64 0
@SOCK_DGRAM = global i32 0
@.str = [52 x i8] c"failed to open DGRAM socket for src addr selection.\00"
@.str.1 = [50 x i8] c"failed to connect to peer for src addr selection.\00"
@.str.2 = [50 x i8] c"failed to get socket name for src addr selection.\00"
@.str.3 = [49 x i8] c"unexpected address family in src addr selection.\00"
define i8* @findsaddr(%struct.sockaddr_in* %0, %struct.in6_addr* nocapture writeonly %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = load i64, i64* @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = tail call i32 @socket(i64 %9, i32 %10, i32 0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %43, label %13
13: ; preds = %2
store i32 16, i32* %5
%14 = call i32 @memcpy(%struct.sockaddr_in* %3, %struct.sockaddr_in* %0, i32 16)
%15 = call i32 @htons(i32 65535)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %15, i32* %16
%17 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%18 = call i32 @connect(i32 %11, %struct.sockaddr* %17, i32 16)
%19 = icmp eq i32 %18, -1
br i1 %19, label %40, label %20
20: ; preds = %13
%21 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%22 = call i32 @getsockname(i32 %11, %struct.sockaddr* %21, i32* %5)
%23 = icmp eq i32 %22, -1
br i1 %23, label %40, label %24
24: ; preds = %20
%25 = load i32, i32* %5
%26 = icmp eq i32 %25, 16
br i1 %26, label %27, label %40
27: ; preds = %24
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%29 = load i64, i64* %28
%30 = load i64, i64* @AF_INET
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %40
32: ; preds = %27
%33 = getelementptr %struct.in6_addr, %struct.in6_addr* %1, i64 0, i32 0
store i32 0, i32* %33
%34 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %1, i64 1, i32 0
store i32 0, i32* %34
%35 = call i32 @htonl(i32 65535)
%36 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %1, i64 2, i32 0
store i32 %35, i32* %36
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
%38 = load i32, i32* %37
%39 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %1, i64 3, i32 0
store i32 %38, i32* %39
br label %40
40: ; preds = %24, %27, %20, %13, %32
%41 = phi i8* [ null, %32 ], [ getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), %13 ], [ getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), %20 ], [ getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), %27 ], [ getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), %24 ]
%42 = call i32 @close(i32 %11)
br label %43
43: ; preds = %2, %40
%44 = phi i8* [ %41, %40 ], [ getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), %2 ]
ret i8* %44
}