#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_storage {
  int dummy;
};
struct sockaddr_in6 {
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ ss;
typedef int socklen_t;

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int get_name_source_address(int, char *, struct sockaddr *, int *);
int /*<<< orphan*/ global_unicast_address(struct sockaddr *);
int /*<<< orphan*/ memcpy(void *, int /*<<< orphan*/ *, int);
int find_global_address(int af, void *addr, int *addr_len) {
  struct sockaddr_storage ss;
  socklen_t ss_len = sizeof(ss);
  int rc;
  rc = get_name_source_address(af, "www.transmissionbt.com",
                               (struct sockaddr *)&ss, &ss_len);
  if (rc < 0)
    rc = get_name_source_address(af, "www.ietf.org", (struct sockaddr *)&ss,
                                 &ss_len);
  if (rc < 0)
    return -1;
  if (!global_unicast_address((struct sockaddr *)&ss))
    return -1;
  switch (af) {
  case AF_INET:
    if (*addr_len < 4)
      return -1;
    memcpy(addr, &((struct sockaddr_in *)&ss)->sin_addr, 4);
    *addr_len = 4;
    return 1;
  case AF_INET6:
    if (*addr_len < 16)
      return -1;
    memcpy(addr, &((struct sockaddr_in6 *)&ss)->sin6_addr, 16);
    *addr_len = 16;
    return 1;
  default:
    return -1;
  }
}


x86 assembly (clang, -O0):
<find_global_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x4,-0x24(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rdx
lea    0xea8(%rip),%rsi
lea    -0x24(%rbp),%rcx
call   1030 <get_name_source_address@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1188 <find_global_address+0x58>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rdx
lea    0xe9b(%rip),%rsi
lea    -0x24(%rbp),%rcx
call   1030 <get_name_source_address@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    119e <find_global_address+0x6e>
movl   $0xffffffff,-0x4(%rbp)
jmp    126e <find_global_address+0x13e>
lea    -0x20(%rbp),%rdi
call   1050 <global_unicast_address@plt>
cmp    $0x0,%eax
jne    11bc <find_global_address+0x8c>
movl   $0xffffffff,-0x4(%rbp)
jmp    126e <find_global_address+0x13e>
mov    -0x8(%rbp),%eax
mov    %eax,-0x2c(%rbp)
sub    $0x80,%eax
je     1226 <find_global_address+0xf6>
jmp    11d2 <find_global_address+0xa2>
mov    -0x2c(%rbp),%eax
sub    $0x81,%eax
jne    1267 <find_global_address+0x137>
jmp    11e5 <find_global_address+0xb5>
mov    -0x18(%rbp),%rax
cmpl   $0x4,(%rax)
jge    11fe <find_global_address+0xce>
movl   $0xffffffff,-0x4(%rbp)
jmp    126e <find_global_address+0x13e>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    -0x18(%rbp),%rax
movl   $0x4,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    126e <find_global_address+0x13e>
mov    -0x18(%rbp),%rax
cmpl   $0x10,(%rax)
jge    123f <find_global_address+0x10f>
movl   $0xffffffff,-0x4(%rbp)
jmp    126e <find_global_address+0x13e>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1040 <memcpy@plt>
mov    -0x18(%rbp),%rax
movl   $0x10,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    126e <find_global_address+0x13e>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_global_address>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
movl   $0x4,0xc(%rsp)
lea    0xeb1(%rip),%rsi
lea    0x8(%rsp),%rdx
lea    0xc(%rsp),%rcx
call   1030 <get_name_source_address@plt>
test   %eax,%eax
jns    117e <find_global_address+0x4e>
lea    0xeae(%rip),%rsi
lea    0x8(%rsp),%rdx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
call   1030 <get_name_source_address@plt>
test   %eax,%eax
js     11d1 <find_global_address+0xa1>
lea    0x8(%rsp),%rdi
call   1050 <global_unicast_address@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
test   %ecx,%ecx
je     11d6 <find_global_address+0xa6>
cmp    $0x80,%ebp
je     11af <find_global_address+0x7f>
cmp    $0x81,%ebp
jne    11d6 <find_global_address+0xa6>
mov    $0x4,%ebp
cmpl   $0x4,(%rbx)
jge    11b9 <find_global_address+0x89>
jmp    11d6 <find_global_address+0xa6>
mov    $0x10,%ebp
cmpl   $0x10,(%rbx)
jl     11d6 <find_global_address+0xa6>
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
mov    %ebp,%edx
call   1040 <memcpy@plt>
mov    %ebp,(%rbx)
mov    $0x1,%eax
jmp    11d6 <find_global_address+0xa6>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@.str = [23 x i8] c"www.transmissionbt.com\00"
@.str.1 = [13 x i8] c"www.ietf.org\00"
define i32 @find_global_address(i32 %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca %struct.sockaddr_storage
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32* %2, i32** %7
store i32 4, i32* %9
%11 = load i32, i32* %5
%12 = bitcast %struct.sockaddr_storage* %8 to %struct.sockaddr*
%13 = call i32 @get_name_source_address(i32 %11, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), %struct.sockaddr* %12, i32* %9)
store i32 %13, i32* %10
%14 = load i32, i32* %10
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %3
%17 = load i32, i32* %5
%18 = bitcast %struct.sockaddr_storage* %8 to %struct.sockaddr*
%19 = call i32 @get_name_source_address(i32 %17, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), %struct.sockaddr* %18, i32* %9)
store i32 %19, i32* %10
br label %20
20: ; preds = %16, %3
%21 = load i32, i32* %10
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %20
store i32 -1, i32* %4
br label %54
24: ; preds = %20
%25 = bitcast %struct.sockaddr_storage* %8 to %struct.sockaddr*
%26 = call i32 @global_unicast_address(%struct.sockaddr* %25)
%27 = icmp ne i32 %26, 0
br i1 %27, label %29, label %28
28: ; preds = %24
store i32 -1, i32* %4
br label %54
29: ; preds = %24
%30 = load i32, i32* %5
switch i32 %30, label %53 [
i32 129, label %31
i32 128, label %42
]
31: ; preds = %29
%32 = load i32*, i32** %7
%33 = load i32, i32* %32
%34 = icmp slt i32 %33, 4
br i1 %34, label %35, label %36
35: ; preds = %31
store i32 -1, i32* %4
br label %54
36: ; preds = %31
%37 = load i8*, i8** %6
%38 = bitcast %struct.sockaddr_storage* %8 to %struct.sockaddr_in*
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %38, i32 0, i32 0
%40 = call i32 @memcpy(i8* %37, i32* %39, i32 4)
%41 = load i32*, i32** %7
store i32 4, i32* %41
store i32 1, i32* %4
br label %54
42: ; preds = %29
%43 = load i32*, i32** %7
%44 = load i32, i32* %43
%45 = icmp slt i32 %44, 16
br i1 %45, label %46, label %47
46: ; preds = %42
store i32 -1, i32* %4
br label %54
47: ; preds = %42
%48 = load i8*, i8** %6
%49 = bitcast %struct.sockaddr_storage* %8 to %struct.sockaddr_in6*
%50 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %49, i32 0, i32 0
%51 = call i32 @memcpy(i8* %48, i32* %50, i32 16)
%52 = load i32*, i32** %7
store i32 16, i32* %52
store i32 1, i32* %4
br label %54
53: ; preds = %29
store i32 -1, i32* %4
br label %54
54: ; preds = %53, %47, %46, %36, %35, %28, %23
%55 = load i32, i32* %4
ret i32 %55
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [23 x i8] c"www.transmissionbt.com\00"
@.str.1 = [13 x i8] c"www.ietf.org\00"
define i32 @find_global_address(i32 %0, i8* %1, i32* nocapture %2) {
%4 = alloca %struct.sockaddr_storage
%5 = alloca i32
%6 = bitcast %struct.sockaddr_storage* %4 to i8*
%7 = bitcast i32* %5 to i8*
store i32 4, i32* %5
%8 = bitcast %struct.sockaddr_storage* %4 to %struct.sockaddr*
%9 = call i32 @get_name_source_address(i32 %0, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), %struct.sockaddr* %8, i32* %5)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = call i32 @get_name_source_address(i32 %0, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), %struct.sockaddr* %8, i32* %5)
%13 = icmp slt i32 %12, 0
br i1 %13, label %28, label %14
14: ; preds = %3, %11
%15 = call i32 @global_unicast_address(%struct.sockaddr* %8)
%16 = icmp eq i32 %15, 0
br i1 %16, label %28, label %17
17: ; preds = %14
switch i32 %0, label %28 [
i32 129, label %18
i32 128, label %21
]
18: ; preds = %17
%19 = load i32, i32* %2
%20 = icmp slt i32 %19, 4
br i1 %20, label %28, label %24
21: ; preds = %17
%22 = load i32, i32* %2
%23 = icmp slt i32 %22, 16
br i1 %23, label %28, label %24
24: ; preds = %21, %18
%25 = phi i32 [ 4, %18 ], [ 16, %21 ]
%26 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %4, i64 0, i32 0
%27 = call i32 @memcpy(i8* %1, i32* %26, i32 %25)
store i32 %25, i32* %2
br label %28
28: ; preds = %24, %17, %21, %18, %14, %11
%29 = phi i32 [ -1, %11 ], [ -1, %14 ], [ -1, %18 ], [ -1, %21 ], [ -1, %17 ], [ 1, %24 ]
ret i32 %29
}