#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;
};
struct TYPE_2__ {
  scalar_t__ sa_family;
};

/* Variables and functions */
scalar_t__ AF_INET;
int IFF_LOOPBACK;
int IFF_UP;
int /*<<< orphan*/ PMAPPORT;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ freeifaddrs(struct ifaddrs *);
scalar_t__ getifaddrs(struct ifaddrs **);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
void get_myaddress(struct sockaddr_in *addr) {
  struct ifaddrs *ifa;
  if (getifaddrs(&ifa) != 0) {
    perror("get_myaddress: getifaddrs");
    exit(1);
  }
  int loopback = 0;
  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 == 1 && (run->ifa_flags & IFF_LOOPBACK)))) {
      *addr = *((struct sockaddr_in *)run->ifa_addr);
      addr->sin_port = htons(PMAPPORT);
      goto out;
    }
    run = run->ifa_next;
  }
  if (loopback == 0) {
    loopback = 1;
    goto again;
  }
out:
  freeifaddrs(ifa);
}


x86 assembly (clang, -O0):
<get_myaddress>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%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   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1257 <get_myaddress+0x107>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    0x2e2c(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1246 <get_myaddress+0xf6>
mov    -0x20(%rbp),%rax
cmpq   $0x0,0x10(%rax)
je     1246 <get_myaddress+0xf6>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rax
mov    (%rax),%rax
mov    0x2e08(%rip),%rcx
cmp    (%rcx),%rax
jne    1246 <get_myaddress+0xf6>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    0x2e0a(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     121b <get_myaddress+0xcb>
cmpl   $0x1,-0x14(%rbp)
jne    1246 <get_myaddress+0xf6>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    0x2de8(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1246 <get_myaddress+0xf6>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    0x10(%rcx),%rcx
mov    (%rcx),%ecx
mov    %ecx,(%rax)
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
jmp    1272 <get_myaddress+0x122>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    1194 <get_myaddress+0x44>
cmpl   $0x0,-0x14(%rbp)
jne    126d <get_myaddress+0x11d>
movl   $0x1,-0x14(%rbp)
jmp    118c <get_myaddress+0x3c>
jmp    1272 <get_myaddress+0x122>
mov    -0x10(%rbp),%rdi
call   1050 <freeifaddrs@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_myaddress>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    %rsp,%rdi
call   1040 <getifaddrs@plt>
test   %rax,%rax
jne    121a <get_myaddress+0xca>
mov    (%rsp),%rdi
test   %rdi,%rdi
je     120b <get_myaddress+0xbb>
mov    0x2e5c(%rip),%rax
mov    (%rax),%eax
mov    0x2e5b(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e69(%rip),%rdx
mov    (%rdx),%r8d
mov    %rdi,%rdx
jmp    11a9 <get_myaddress+0x59>
nopw   0x0(%rax,%rax,1)
mov    0x8(%rdx),%rdx
test   %rdx,%rdx
je     11c4 <get_myaddress+0x74>
mov    (%rdx),%ebx
test   %ebx,%eax
je     11a0 <get_myaddress+0x50>
mov    0x10(%rdx),%rsi
test   %rsi,%rsi
je     11a0 <get_myaddress+0x50>
cmp    %rcx,(%rsi)
jne    11a0 <get_myaddress+0x50>
and    %r8d,%ebx
jne    11a0 <get_myaddress+0x50>
jmp    11eb <get_myaddress+0x9b>
test   %rdi,%rdi
je     120b <get_myaddress+0xbb>
mov    %rdi,%rdx
jmp    11d9 <get_myaddress+0x89>
xchg   %ax,%ax
mov    0x8(%rdx),%rdx
test   %rdx,%rdx
je     120d <get_myaddress+0xbd>
test   %eax,(%rdx)
je     11d0 <get_myaddress+0x80>
mov    0x10(%rdx),%rsi
test   %rsi,%rsi
je     11d0 <get_myaddress+0x80>
cmp    %rcx,(%rsi)
jne    11d0 <get_myaddress+0x80>
mov    0x10(%rdx),%rax
mov    (%rax),%eax
mov    %eax,(%r14)
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %eax,(%r14)
mov    (%rsp),%rdi
jmp    120d <get_myaddress+0xbd>
xor    %edi,%edi
call   1050 <freeifaddrs@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xddf(%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
define void @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 0, i32* %4
br label %12
12: ; preds = %73, %11
%13 = load %struct.ifaddrs*, %struct.ifaddrs** %3
store %struct.ifaddrs* %13, %struct.ifaddrs** %5
br label %14
14: ; preds = %66, %12
%15 = load %struct.ifaddrs*, %struct.ifaddrs** %5
%16 = icmp ne %struct.ifaddrs* %15, null
br i1 %16, label %17, label %70
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 %66
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 %66
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 %66
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 %44, label %54
44: ; preds = %37
%45 = load i32, i32* %4
%46 = icmp eq i32 %45, 1
br i1 %46, label %47, label %66
47: ; preds = %44
%48 = load %struct.ifaddrs*, %struct.ifaddrs** %5
%49 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %48, i32 0, i32 0
%50 = load i32, i32* %49
%51 = load i32, i32* @IFF_LOOPBACK
%52 = and i32 %50, %51
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %66
54: ; preds = %47, %37
%55 = load %struct.sockaddr_in*, %struct.sockaddr_in** %2
%56 = load %struct.ifaddrs*, %struct.ifaddrs** %5
%57 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %56, i32 0, i32 2
%58 = load %struct.TYPE_2__*, %struct.TYPE_2__** %57
%59 = bitcast %struct.TYPE_2__* %58 to %struct.sockaddr_in*
%60 = bitcast %struct.sockaddr_in* %55 to i8*
%61 = bitcast %struct.sockaddr_in* %59 to i8*
%62 = load i32, i32* @PMAPPORT
%63 = call i32 @htons(i32 %62)
%64 = load %struct.sockaddr_in*, %struct.sockaddr_in** %2
%65 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %64, i32 0, i32 0
store i32 %63, i32* %65
br label %75
66: ; preds = %47, %44, %29, %24, %17
%67 = load %struct.ifaddrs*, %struct.ifaddrs** %5
%68 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %67, i32 0, i32 1
%69 = load %struct.ifaddrs*, %struct.ifaddrs** %68
store %struct.ifaddrs* %69, %struct.ifaddrs** %5
br label %14
70: ; preds = %14
%71 = load i32, i32* %4
%72 = icmp eq i32 %71, 0
br i1 %72, label %73, label %74
73: ; preds = %70
store i32 1, i32* %4
br label %12
74: ; preds = %70
br label %75
75: ; preds = %74, %54
%76 = load %struct.ifaddrs*, %struct.ifaddrs** %3
%77 = call i32 @freeifaddrs(%struct.ifaddrs* %76)
ret void
}

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
define void @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 %65, 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, %42
%16 = phi %struct.ifaddrs* [ %44, %42 ], [ %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 %42, 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 %42, 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 eq i64 %27, %10
%29 = and i32 %11, %18
%30 = icmp eq i32 %29, 0
%31 = select i1 %28, i1 %30, i1 false
br i1 %31, label %32, label %42
32: ; preds = %25, %57
%33 = phi %struct.ifaddrs* [ %48, %57 ], [ %16, %25 ]
%34 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %33, i64 0, i32 2
%35 = bitcast %struct.TYPE_2__** %34 to i32**
%36 = load i32*, i32** %35
%37 = getelementptr %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 0
%38 = load i32, i32* %36
store i32 %38, i32* %37
%39 = load i32, i32* @PMAPPORT
%40 = call i32 @htons(i32 %39)
store i32 %40, i32* %37
%41 = load %struct.ifaddrs*, %struct.ifaddrs** %2
br label %65
42: ; preds = %25, %21, %15
%43 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %16, i64 0, i32 1
%44 = load %struct.ifaddrs*, %struct.ifaddrs** %43
%45 = icmp eq %struct.ifaddrs* %44, null
br i1 %45, label %46, label %15
46: ; preds = %42
br i1 %8, label %65, label %47
47: ; preds = %46, %61
%48 = phi %struct.ifaddrs* [ %63, %61 ], [ %7, %46 ]
%49 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %48, i64 0, i32 0
%50 = load i32, i32* %49
%51 = and i32 %9, %50
%52 = icmp eq i32 %51, 0
br i1 %52, label %61, label %53
53: ; preds = %47
%54 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %48, i64 0, i32 2
%55 = load %struct.TYPE_2__*, %struct.TYPE_2__** %54
%56 = icmp eq %struct.TYPE_2__* %55, null
br i1 %56, label %61, label %57
57: ; preds = %53
%58 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %55, i64 0, i32 0
%59 = load i64, i64* %58
%60 = icmp eq i64 %59, %10
br i1 %60, label %32, label %61
61: ; preds = %57, %53, %47
%62 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %48, i64 0, i32 1
%63 = load %struct.ifaddrs*, %struct.ifaddrs** %62
%64 = icmp eq %struct.ifaddrs* %63, null
br i1 %64, label %65, label %47
65: ; preds = %61, %6, %46, %32
%66 = phi %struct.ifaddrs* [ %41, %32 ], [ null, %46 ], [ null, %6 ], [ %7, %61 ]
%67 = call i32 @freeifaddrs(%struct.ifaddrs* %66)
ret void
}