#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_in {
  int /*<<< orphan*/ sin_port;
};
struct ifaddrs {
  int ifa_flags;
  struct ifaddrs *ifa_next;
  TYPE_1__ *ifa_addr;
};
typedef int /*<<< orphan*/ bool_t;
struct TYPE_2__ {
  scalar_t__ sa_family;
};

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ FALSE;
int IFF_LOOPBACK;
int IFF_UP;
int /*<<< orphan*/ PMAPPORT;
int /*<<< orphan*/ TRUE;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ freeifaddrs(struct ifaddrs *);
scalar_t__ getifaddrs(struct ifaddrs **);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
bool_t __get_myaddress(struct sockaddr_in *addr) {
  struct ifaddrs *ifa;
  if (getifaddrs(&ifa) != 0) {
    perror("get_myaddress: getifaddrs");
    exit(1);
  }
  int loopback = 1;
  struct ifaddrs *run;
again:
  run = ifa;
  while (run != NULL) {
    if ((run->ifa_flags & IFF_UP) && run->ifa_addr != NULL &&
        run->ifa_addr->sa_family == AF_INET &&
        ((run->ifa_flags & IFF_LOOPBACK) || loopback == 0)) {
      *addr = *((struct sockaddr_in *)run->ifa_addr);
      addr->sin_port = htons(PMAPPORT);
      goto out;
    }
    run = run->ifa_next;
  }
  if (loopback == 1) {
    loopback = 0;
    goto again;
  }
out:
  freeifaddrs(ifa);
  return run == NULL ? FALSE : TRUE;
}


x86 assembly (clang, -O0):
<__get_myaddress>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
call   1040 <getifaddrs@plt>
cmp    $0x0,%rax
je     1185 <__get_myaddress+0x35>
lea    0xe8a(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movl   $0x1,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     123f <__get_myaddress+0xef>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    0x2e2c(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     122e <__get_myaddress+0xde>
mov    -0x20(%rbp),%rax
cmpq   $0x0,0x10(%rax)
je     122e <__get_myaddress+0xde>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rax
mov    (%rax),%rax
mov    0x2e08(%rip),%rcx
cmp    (%rcx),%rax
jne    122e <__get_myaddress+0xde>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    0x2e0a(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    1203 <__get_myaddress+0xb3>
cmpl   $0x0,-0x14(%rbp)
jne    122e <__get_myaddress+0xde>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    0x10(%rcx),%rcx
mov    (%rcx),%ecx
mov    %ecx,(%rax)
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
jmp    125a <__get_myaddress+0x10a>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    1194 <__get_myaddress+0x44>
cmpl   $0x1,-0x14(%rbp)
jne    1255 <__get_myaddress+0x105>
movl   $0x0,-0x14(%rbp)
jmp    118c <__get_myaddress+0x3c>
jmp    125a <__get_myaddress+0x10a>
mov    -0x10(%rbp),%rdi
call   1050 <freeifaddrs@plt>
cmpq   $0x0,-0x20(%rbp)
jne    127f <__get_myaddress+0x12f>
mov    0x2d43(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    128b <__get_myaddress+0x13b>
mov    0x2d4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__get_myaddress>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
lea    0x8(%rsp),%rdi
call   1040 <getifaddrs@plt>
test   %rax,%rax
jne    122f <__get_myaddress+0xdf>
mov    0x8(%rsp),%rdi
mov    0x2e3d(%rip),%r15
test   %rdi,%rdi
je     121d <__get_myaddress+0xcd>
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    0x2e4c(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e5a(%rip),%rdx
mov    (%rdx),%r8d
mov    %rdi,%rsi
jmp    11b9 <__get_myaddress+0x69>
cs nopw 0x0(%rax,%rax,1)
mov    0x8(%rsi),%rsi
test   %rsi,%rsi
je     11d4 <__get_myaddress+0x84>
mov    (%rsi),%edx
test   %edx,%eax
je     11b0 <__get_myaddress+0x60>
mov    0x10(%rsi),%rbx
test   %rbx,%rbx
je     11b0 <__get_myaddress+0x60>
cmp    %rcx,(%rbx)
jne    11b0 <__get_myaddress+0x60>
and    %r8d,%edx
je     11b0 <__get_myaddress+0x60>
jmp    11fb <__get_myaddress+0xab>
test   %rdi,%rdi
je     121d <__get_myaddress+0xcd>
mov    %rdi,%rdx
jmp    11e9 <__get_myaddress+0x99>
xchg   %ax,%ax
mov    0x8(%rdx),%rdx
test   %rdx,%rdx
je     121d <__get_myaddress+0xcd>
test   %eax,(%rdx)
je     11e0 <__get_myaddress+0x90>
mov    0x10(%rdx),%rbx
test   %rbx,%rbx
je     11e0 <__get_myaddress+0x90>
cmp    %rcx,(%rbx)
jne    11e0 <__get_myaddress+0x90>
mov    (%rbx),%eax
mov    %eax,(%r14)
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %eax,(%r14)
mov    0x2db8(%rip),%r15
mov    0x8(%rsp),%rdi
call   1050 <freeifaddrs@plt>
mov    (%r15),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdca(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.ifaddrs = type { i32, %struct.ifaddrs*, %struct.TYPE_2__* }
%struct.TYPE_2__ = type { i64 }
@.str = [26 x i8] c"get_myaddress: getifaddrs\00"
@IFF_UP = global i32 0
@AF_INET = global i64 0
@IFF_LOOPBACK = global i32 0
@PMAPPORT = global i32 0
@FALSE = global i32 0
@TRUE = global i32 0
define i32 @__get_myaddress(%struct.sockaddr_in* %0) {
%2 = alloca %struct.sockaddr_in*
%3 = alloca %struct.ifaddrs*
%4 = alloca i32
%5 = alloca %struct.ifaddrs*
store %struct.sockaddr_in* %0, %struct.sockaddr_in** %2
%6 = call i64 @getifaddrs(%struct.ifaddrs** %3)
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %1
store i32 1, i32* %4
br label %12
12: ; preds = %66, %11
%13 = load %struct.ifaddrs*, %struct.ifaddrs** %3
store %struct.ifaddrs* %13, %struct.ifaddrs** %5
br label %14
14: ; preds = %59, %12
%15 = load %struct.ifaddrs*, %struct.ifaddrs** %5
%16 = icmp ne %struct.ifaddrs* %15, null
br i1 %16, label %17, label %63
17: ; preds = %14
%18 = load %struct.ifaddrs*, %struct.ifaddrs** %5
%19 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %18, i32 0, i32 0
%20 = load i32, i32* %19
%21 = load i32, i32* @IFF_UP
%22 = and i32 %20, %21
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %59
24: ; preds = %17
%25 = load %struct.ifaddrs*, %struct.ifaddrs** %5
%26 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %25, i32 0, i32 2
%27 = load %struct.TYPE_2__*, %struct.TYPE_2__** %26
%28 = icmp ne %struct.TYPE_2__* %27, null
br i1 %28, label %29, label %59
29: ; preds = %24
%30 = load %struct.ifaddrs*, %struct.ifaddrs** %5
%31 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %30, i32 0, i32 2
%32 = load %struct.TYPE_2__*, %struct.TYPE_2__** %31
%33 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %32, i32 0, i32 0
%34 = load i64, i64* %33
%35 = load i64, i64* @AF_INET
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %59
37: ; preds = %29
%38 = load %struct.ifaddrs*, %struct.ifaddrs** %5
%39 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %38, i32 0, i32 0
%40 = load i32, i32* %39
%41 = load i32, i32* @IFF_LOOPBACK
%42 = and i32 %40, %41
%43 = icmp ne i32 %42, 0
br i1 %43, label %47, label %44
44: ; preds = %37
%45 = load i32, i32* %4
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %59
47: ; preds = %44, %37
%48 = load %struct.sockaddr_in*, %struct.sockaddr_in** %2
%49 = load %struct.ifaddrs*, %struct.ifaddrs** %5
%50 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %49, i32 0, i32 2
%51 = load %struct.TYPE_2__*, %struct.TYPE_2__** %50
%52 = bitcast %struct.TYPE_2__* %51 to %struct.sockaddr_in*
%53 = bitcast %struct.sockaddr_in* %48 to i8*
%54 = bitcast %struct.sockaddr_in* %52 to i8*
%55 = load i32, i32* @PMAPPORT
%56 = call i32 @htons(i32 %55)
%57 = load %struct.sockaddr_in*, %struct.sockaddr_in** %2
%58 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %57, i32 0, i32 0
store i32 %56, i32* %58
br label %68
59: ; preds = %44, %29, %24, %17
%60 = load %struct.ifaddrs*, %struct.ifaddrs** %5
%61 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %60, i32 0, i32 1
%62 = load %struct.ifaddrs*, %struct.ifaddrs** %61
store %struct.ifaddrs* %62, %struct.ifaddrs** %5
br label %14
63: ; preds = %14
%64 = load i32, i32* %4
%65 = icmp eq i32 %64, 1
br i1 %65, label %66, label %67
66: ; preds = %63
store i32 0, i32* %4
br label %12
67: ; preds = %63
br label %68
68: ; preds = %67, %47
%69 = load %struct.ifaddrs*, %struct.ifaddrs** %3
%70 = call i32 @freeifaddrs(%struct.ifaddrs* %69)
%71 = load %struct.ifaddrs*, %struct.ifaddrs** %5
%72 = icmp eq %struct.ifaddrs* %71, null
br i1 %72, label %73, label %75
73: ; preds = %68
%74 = load i32, i32* @FALSE
br label %77
75: ; preds = %68
%76 = load i32, i32* @TRUE
br label %77
77: ; preds = %75, %73
%78 = phi i32 [ %74, %73 ], [ %76, %75 ]
ret i32 %78
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.ifaddrs = type { i32, %struct.ifaddrs*, %struct.TYPE_2__* }
%struct.TYPE_2__ = type { i64 }
@.str = [26 x i8] c"get_myaddress: getifaddrs\00"
@IFF_UP = global i32 0
@AF_INET = global i64 0
@IFF_LOOPBACK = global i32 0
@PMAPPORT = global i32 0
@FALSE = global i32 0
@TRUE = global i32 0
define i32 @__get_myaddress(%struct.sockaddr_in* nocapture writeonly %0) {
%2 = alloca %struct.ifaddrs*
%3 = bitcast %struct.ifaddrs** %2 to i8*
%4 = call i64 @getifaddrs(%struct.ifaddrs** %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %12
6: ; preds = %1
%7 = load %struct.ifaddrs*, %struct.ifaddrs** %2
%8 = icmp eq %struct.ifaddrs* %7, null
%9 = load i32, i32* @IFF_UP
%10 = load i64, i64* @AF_INET
%11 = load i32, i32* @IFF_LOOPBACK
br i1 %8, label %63, label %15
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %6, %32
%16 = phi %struct.ifaddrs* [ %34, %32 ], [ %7, %6 ]
%17 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %16, i64 0, i32 0
%18 = load i32, i32* %17
%19 = and i32 %9, %18
%20 = icmp eq i32 %19, 0
br i1 %20, label %32, label %21
21: ; preds = %15
%22 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %16, i64 0, i32 2
%23 = load %struct.TYPE_2__*, %struct.TYPE_2__** %22
%24 = icmp eq %struct.TYPE_2__* %23, null
br i1 %24, label %32, label %25
25: ; preds = %21
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i64 0, i32 0
%27 = load i64, i64* %26
%28 = icmp ne i64 %27, %10
%29 = and i32 %11, %18
%30 = icmp eq i32 %29, 0
%31 = select i1 %28, i1 true, i1 %30
br i1 %31, label %32, label %55
32: ; preds = %25, %21, %15
%33 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %16, i64 0, i32 1
%34 = load %struct.ifaddrs*, %struct.ifaddrs** %33
%35 = icmp eq %struct.ifaddrs* %34, null
br i1 %35, label %36, label %15
36: ; preds = %32
br i1 %8, label %63, label %37
37: ; preds = %36, %51
%38 = phi %struct.ifaddrs* [ %53, %51 ], [ %7, %36 ]
%39 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %38, i64 0, i32 0
%40 = load i32, i32* %39
%41 = and i32 %9, %40
%42 = icmp eq i32 %41, 0
br i1 %42, label %51, label %43
43: ; preds = %37
%44 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %38, i64 0, i32 2
%45 = load %struct.TYPE_2__*, %struct.TYPE_2__** %44
%46 = icmp eq %struct.TYPE_2__* %45, null
br i1 %46, label %51, label %47
47: ; preds = %43
%48 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %45, i64 0, i32 0
%49 = load i64, i64* %48
%50 = icmp eq i64 %49, %10
br i1 %50, label %55, label %51
51: ; preds = %47, %43, %37
%52 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %38, i64 0, i32 1
%53 = load %struct.ifaddrs*, %struct.ifaddrs** %52
%54 = icmp eq %struct.ifaddrs* %53, null
br i1 %54, label %63, label %37
55: ; preds = %25, %47
%56 = phi %struct.TYPE_2__* [ %45, %47 ], [ %23, %25 ]
%57 = bitcast %struct.TYPE_2__* %56 to i32*
%58 = getelementptr %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 0
%59 = load i32, i32* %57
store i32 %59, i32* %58
%60 = load i32, i32* @PMAPPORT
%61 = call i32 @htons(i32 %60)
store i32 %61, i32* %58
%62 = load %struct.ifaddrs*, %struct.ifaddrs** %2
br label %63
63: ; preds = %51, %36, %6, %55
%64 = phi %struct.ifaddrs* [ %62, %55 ], [ %7, %6 ], [ %7, %36 ], [ %7, %51 ]
%65 = phi i32* [ @TRUE, %55 ], [ @FALSE, %6 ], [ @FALSE, %36 ], [ @FALSE, %51 ]
%66 = call i32 @freeifaddrs(%struct.ifaddrs* %64)
%67 = load i32, i32* %65
ret i32 %67
}