#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 */
struct sockaddr {
  int dummy;
};
struct addrinfo {
  int ai_family;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_addrlen;
  TYPE_1__ *ai_addr;
  int /*<<< orphan*/ ai_socktype;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ hints;
struct TYPE_2__ {
  int sa_family;
};

/* Variables and functions */
int /*<<< orphan*/ ENOENT;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int get_source_address(TYPE_1__ *, int /*<<< orphan*/, struct sockaddr *,
                       int /*<<< orphan*/ *);
int getaddrinfo(char const *, int /*<<< orphan*/ *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int get_name_source_address(int af, const char *name, struct sockaddr *src,
                            socklen_t *src_len) {
  struct addrinfo hints, *info, *infop;
  int rc;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = af;
  hints.ai_socktype = SOCK_DGRAM;
  rc = getaddrinfo(name, NULL, &hints, &info);
  if (rc != 0) {
    errno = ENOENT;
    return -1;
  }
  rc = -1;
  errno = ENOENT;
  infop = info;
  while (infop) {
    if (infop->ai_addr->sa_family == af) {
      rc = get_source_address(infop->ai_addr, infop->ai_addrlen, src, src_len);
      if (rc >= 0)
        break;
    }
    infop = infop->ai_next;
  }
  freeaddrinfo(info);
  return rc;
}


x86 assembly (clang, -O0):
<get_name_source_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    0x2e74(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x48(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   1050 <getaddrinfo@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
je     11b9 <get_name_source_address+0x79>
mov    0x2e26(%rip),%rax
mov    (%rax),%ecx
mov    0x2e2d(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    124b <get_name_source_address+0x10b>
movl   $0xffffffff,-0x5c(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%ecx
mov    0x2e08(%rip),%rax
mov    %ecx,(%rax)
mov    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     123c <get_name_source_address+0xfc>
mov    -0x58(%rbp),%rax
mov    0x18(%rax),%rax
mov    (%rax),%eax
cmp    -0x8(%rbp),%eax
jne    122b <get_name_source_address+0xeb>
mov    -0x58(%rbp),%rax
mov    0x18(%rax),%rdi
mov    -0x58(%rbp),%rax
mov    0x10(%rax),%esi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1030 <get_source_address@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
jl     1226 <get_name_source_address+0xe6>
jmp    123c <get_name_source_address+0xfc>
jmp    122b <get_name_source_address+0xeb>
mov    -0x58(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x58(%rbp)
jmp    11da <get_name_source_address+0x9a>
mov    -0x50(%rbp),%rdi
call   1060 <freeaddrinfo@plt>
mov    -0x5c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_name_source_address>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %edi,%r13d
lea    0x10(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    %r13d,0x10(%rsp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r12,%rdx
call   1050 <getaddrinfo@plt>
mov    0x2e2f(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e36(%rip),%rdx
mov    %ecx,(%rdx)
mov    $0xffffffff,%ebx
test   %eax,%eax
jne    11f0 <get_name_source_address+0xb0>
mov    0x8(%rsp),%rbp
mov    $0xffffffff,%ebx
test   %rbp,%rbp
jne    11c9 <get_name_source_address+0x89>
xor    %edi,%edi
jmp    11eb <get_name_source_address+0xab>
mov    0x8(%rbp),%rbp
test   %rbp,%rbp
je     11e6 <get_name_source_address+0xa6>
mov    0x18(%rbp),%rdi
cmp    %r13d,(%rdi)
jne    11c0 <get_name_source_address+0x80>
mov    0x10(%rbp),%esi
mov    %r15,%rdx
mov    %r14,%rcx
call   1030 <get_source_address@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11c0 <get_name_source_address+0x80>
mov    0x8(%rsp),%rdi
call   1060 <freeaddrinfo@plt>
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.addrinfo = type { i32, %struct.addrinfo*, i32, %struct.TYPE_2__*, i32 }
%struct.TYPE_2__ = type { i32 }
@SOCK_DGRAM = global i32 0
@ENOENT = global i32 0
@errno = global i32 0
define i32 @get_name_source_address(i32 %0, i8* %1, %struct.sockaddr* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.sockaddr*
%9 = alloca i32*
%10 = alloca %struct.addrinfo
%11 = alloca %struct.addrinfo*
%12 = alloca %struct.addrinfo*
%13 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store %struct.sockaddr* %2, %struct.sockaddr** %8
store i32* %3, i32** %9
%14 = call i32 @memset(%struct.addrinfo* %10, i32 0, i32 40)
%15 = load i32, i32* %6
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 0
store i32 %15, i32* %16
%17 = load i32, i32* @SOCK_DGRAM
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 4
store i32 %17, i32* %18
%19 = load i8*, i8** %7
%20 = call i32 @getaddrinfo(i8* %19, i32* null, %struct.addrinfo* %10, %struct.addrinfo** %11)
store i32 %20, i32* %13
%21 = load i32, i32* %13
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %4
%24 = load i32, i32* @ENOENT
store i32 %24, i32* @errno
store i32 -1, i32* %5
br label %61
25: ; preds = %4
store i32 -1, i32* %13
%26 = load i32, i32* @ENOENT
store i32 %26, i32* @errno
%27 = load %struct.addrinfo*, %struct.addrinfo** %11
store %struct.addrinfo* %27, %struct.addrinfo** %12
br label %28
28: ; preds = %53, %25
%29 = load %struct.addrinfo*, %struct.addrinfo** %12
%30 = icmp ne %struct.addrinfo* %29, null
br i1 %30, label %31, label %57
31: ; preds = %28
%32 = load %struct.addrinfo*, %struct.addrinfo** %12
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i32 0, i32 3
%34 = load %struct.TYPE_2__*, %struct.TYPE_2__** %33
%35 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %34, i32 0, i32 0
%36 = load i32, i32* %35
%37 = load i32, i32* %6
%38 = icmp eq i32 %36, %37
br i1 %38, label %39, label %53
39: ; preds = %31
%40 = load %struct.addrinfo*, %struct.addrinfo** %12
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i32 0, i32 3
%42 = load %struct.TYPE_2__*, %struct.TYPE_2__** %41
%43 = load %struct.addrinfo*, %struct.addrinfo** %12
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i32 0, i32 2
%45 = load i32, i32* %44
%46 = load %struct.sockaddr*, %struct.sockaddr** %8
%47 = load i32*, i32** %9
%48 = call i32 @get_source_address(%struct.TYPE_2__* %42, i32 %45, %struct.sockaddr* %46, i32* %47)
store i32 %48, i32* %13
%49 = load i32, i32* %13
%50 = icmp sge i32 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %39
br label %57
52: ; preds = %39
br label %53
53: ; preds = %52, %31
%54 = load %struct.addrinfo*, %struct.addrinfo** %12
%55 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %54, i32 0, i32 1
%56 = load %struct.addrinfo*, %struct.addrinfo** %55
store %struct.addrinfo* %56, %struct.addrinfo** %12
br label %28
57: ; preds = %51, %28
%58 = load %struct.addrinfo*, %struct.addrinfo** %11
%59 = call i32 @freeaddrinfo(%struct.addrinfo* %58)
%60 = load i32, i32* %13
store i32 %60, i32* %5
br label %61
61: ; preds = %57, %23
%62 = load i32, i32* %5
ret i32 %62
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.addrinfo = type { i32, %struct.addrinfo*, i32, %struct.TYPE_2__*, i32 }
%struct.TYPE_2__ = type { i32 }
@SOCK_DGRAM = global i32 0
@ENOENT = global i32 0
@errno = global i32 0
define i32 @get_name_source_address(i32 %0, i8* %1, %struct.sockaddr* %2, i32* %3) {
%5 = alloca %struct.addrinfo
%6 = alloca %struct.addrinfo*
%7 = bitcast %struct.addrinfo* %5 to i8*
%8 = bitcast %struct.addrinfo** %6 to i8*
%9 = call i32 @memset(%struct.addrinfo* %5, i32 0, i32 40)
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 0
store i32 %0, i32* %10
%11 = load i32, i32* @SOCK_DGRAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 4
store i32 %11, i32* %12
%13 = call i32 @getaddrinfo(i8* %1, i32* null, %struct.addrinfo* %5, %struct.addrinfo** %6)
%14 = icmp eq i32 %13, 0
%15 = load i32, i32* @ENOENT
store i32 %15, i32* @errno
br i1 %14, label %16, label %44
16: ; preds = %4
%17 = load %struct.addrinfo*, %struct.addrinfo** %6
%18 = icmp eq %struct.addrinfo* %17, null
br i1 %18, label %40, label %19
19: ; preds = %16, %32
%20 = phi %struct.addrinfo* [ %35, %32 ], [ %17, %16 ]
%21 = phi i32 [ %33, %32 ], [ -1, %16 ]
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 3
%23 = load %struct.TYPE_2__*, %struct.TYPE_2__** %22
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i64 0, i32 0
%25 = load i32, i32* %24
%26 = icmp eq i32 %25, %0
br i1 %26, label %27, label %32
27: ; preds = %19
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 2
%29 = load i32, i32* %28
%30 = call i32 @get_source_address(%struct.TYPE_2__* %23, i32 %29, %struct.sockaddr* %2, i32* %3)
%31 = icmp sgt i32 %30, -1
br i1 %31, label %37, label %32
32: ; preds = %27, %19
%33 = phi i32 [ %30, %27 ], [ %21, %19 ]
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 1
%35 = load %struct.addrinfo*, %struct.addrinfo** %34
%36 = icmp eq %struct.addrinfo* %35, null
br i1 %36, label %37, label %19
37: ; preds = %27, %32
%38 = phi i32 [ %33, %32 ], [ %30, %27 ]
%39 = load %struct.addrinfo*, %struct.addrinfo** %6
br label %40
40: ; preds = %37, %16
%41 = phi %struct.addrinfo* [ null, %16 ], [ %39, %37 ]
%42 = phi i32 [ -1, %16 ], [ %38, %37 ]
%43 = call i32 @freeaddrinfo(%struct.addrinfo* %41)
br label %44
44: ; preds = %4, %40
%45 = phi i32 [ %42, %40 ], [ -1, %4 ]
ret i32 %45
}