#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 TYPE_2__ {
  scalar_t__ sa_family;
};
struct ifreq {
  int ifr_flags;
  TYPE_1__ ifr_addr;
};
struct ifconf {
  int ifc_len;
  char *ifc_buf;
  struct ifreq *ifc_req;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ AF_INET;
int BUFSIZ;
int IFF_UP;
int /*<<< orphan*/ PMAPPORT;
int /*<<< orphan*/ SIOCGIFCONF;
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
scalar_t__ ioctl(int, int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int socket(scalar_t__, int /*<<< orphan*/, int /*<<< orphan*/);
void get_myaddress(struct sockaddr_in *addr) {
  int s;
  char buf[BUFSIZ];
  struct ifconf ifc;
  struct ifreq ifreq, *ifr;
  int len;
  if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror("get_myaddress: socket");
    exit(1);
  }
  ifc.ifc_len = sizeof(buf);
  ifc.ifc_buf = buf;
  if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
    perror("get_myaddress: ioctl (get interface configuration)");
    exit(1);
  }
  ifr = ifc.ifc_req;
  for (len = ifc.ifc_len; len; len -= sizeof ifreq) {
    ifreq = *ifr;
    if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
      perror("get_myaddress: ioctl");
      exit(1);
    }
    if ((ifreq.ifr_flags & IFF_UP) && ifr->ifr_addr.sa_family == AF_INET) {
      *addr = *((struct sockaddr_in *)&ifr->ifr_addr);
      addr->sin_port = htons(PMAPPORT);
      break;
    }
    ifr++;
  }
  (void)close(s);
}


x86 assembly (clang, -O0):
<get_myaddress>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x68(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%rdi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11d8 <get_myaddress+0x78>
lea    0xe37(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x60(%rbp),%rax
mov    -0x68(%rbp),%rcx
mov    %ecx,-0x38(%rbp)
mov    %rax,-0x30(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    121c <get_myaddress+0xbc>
lea    0xe09(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
je     12ed <get_myaddress+0x18d>
mov    -0x50(%rbp),%rax
mov    (%rax),%rcx
mov    %rcx,-0x48(%rbp)
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2d77(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    127c <get_myaddress+0x11c>
lea    0xddc(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x48(%rbp),%eax
mov    0x2d5a(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12d1 <get_myaddress+0x171>
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2d48(%rip),%rcx
cmp    (%rcx),%rax
jne    12d1 <get_myaddress+0x171>
mov    -0x8(%rbp),%rax
mov    -0x50(%rbp),%rcx
mov    0x8(%rcx),%ecx
mov    %ecx,(%rax)
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
jmp    12ed <get_myaddress+0x18d>
mov    -0x50(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x50(%rbp)
movslq -0x54(%rbp),%rax
sub    $0x10,%rax
mov    %eax,-0x54(%rbp)
jmp    122a <get_myaddress+0xca>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_myaddress>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdi,%r15
mov    0x2e3d(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2e52(%rip),%rax
mov    (%rax),%rdi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     1288 <get_myaddress+0x128>
mov    %eax,%r13d
mov    %r15,-0x30(%rbp)
mov    %r14d,-0x48(%rbp)
mov    %rbx,-0x40(%rbp)
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
mov    %r13d,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     1291 <get_myaddress+0x131>
mov    -0x48(%rbp),%ebx
test   %rbx,%rbx
je     125b <get_myaddress+0xfb>
mov    -0x38(%rbp),%r14
add    $0x8,%r14
xor    %r12d,%r12d
mov    0x2de8(%rip),%r15
jmp    1209 <get_myaddress+0xa9>
nopw   0x0(%rax,%rax,1)
add    $0x10,%r12
cmp    %r12d,%ebx
je     125b <get_myaddress+0xfb>
movups -0x8(%r14,%r12,1),%xmm0
movaps %xmm0,-0x60(%rbp)
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edi
lea    -0x60(%rbp),%rdx
call   1040 <ioctl@plt>
test   %rax,%rax
js     1272 <get_myaddress+0x112>
mov    (%r15),%eax
test   %eax,-0x60(%rbp)
je     1200 <get_myaddress+0xa0>
mov    (%r14,%r12,1),%rax
mov    0x2da8(%rip),%rcx
cmp    (%rcx),%rax
jne    1200 <get_myaddress+0xa0>
mov    -0x30(%rbp),%rbx
mov    %eax,(%rbx)
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %eax,(%rbx)
mov    %r13d,%edi
call   1050 <close@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd0(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xd71(%rip),%rdi
jmp    1279 <get_myaddress+0x119>
lea    0xd7e(%rip),%rdi
jmp    1279 <get_myaddress+0x119>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.ifconf = type { i32, i8*, %struct.ifreq* }
%struct.ifreq = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
@BUFSIZ = global i32 0
@AF_INET = global i64 0
@SOCK_DGRAM = global i32 0
@.str = [22 x i8] c"get_myaddress: socket\00"
@SIOCGIFCONF = global i32 0
@.str.1 = [51 x i8] c"get_myaddress: ioctl (get interface configuration)\00"
@SIOCGIFFLAGS = global i32 0
@.str.2 = [21 x i8] c"get_myaddress: ioctl\00"
@IFF_UP = global i32 0
@PMAPPORT = global i32 0
define void @get_myaddress(%struct.sockaddr_in* %0) {
%2 = alloca %struct.sockaddr_in*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca %struct.ifconf
%7 = alloca %struct.ifreq
%8 = alloca %struct.ifreq*
%9 = alloca i32
store %struct.sockaddr_in* %0, %struct.sockaddr_in** %2
%10 = load i32, i32* @BUFSIZ
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %4
%13 = alloca i8, i64 %11
store i64 %11, i64* %5
%14 = load i64, i64* @AF_INET
%15 = load i32, i32* @SOCK_DGRAM
%16 = call i32 @socket(i64 %14, i32 %15, i32 0)
store i32 %16, i32* %3
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %1
%19 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %1
%22 = trunc i64 %11 to i32
%23 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %6, i32 0, i32 0
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %6, i32 0, i32 1
store i8* %13, i8** %24
%25 = load i32, i32* %3
%26 = load i32, i32* @SIOCGIFCONF
%27 = bitcast %struct.ifconf* %6 to i8*
%28 = call i64 @ioctl(i32 %25, i32 %26, i8* %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %21
%31 = call i32 @perror(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %21
%34 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %6, i32 0, i32 2
%35 = load %struct.ifreq*, %struct.ifreq** %34
store %struct.ifreq* %35, %struct.ifreq** %8
%36 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %6, i32 0, i32 0
%37 = load i32, i32* %36
store i32 %37, i32* %9
br label %38
38: ; preds = %80, %33
%39 = load i32, i32* %9
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %85
41: ; preds = %38
%42 = load %struct.ifreq*, %struct.ifreq** %8
%43 = bitcast %struct.ifreq* %7 to i8*
%44 = bitcast %struct.ifreq* %42 to i8*
%45 = load i32, i32* %3
%46 = load i32, i32* @SIOCGIFFLAGS
%47 = bitcast %struct.ifreq* %7 to i8*
%48 = call i64 @ioctl(i32 %45, i32 %46, i8* %47)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %41
%51 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %41
%54 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%55 = load i32, i32* %54
%56 = load i32, i32* @IFF_UP
%57 = and i32 %55, %56
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %77
59: ; preds = %53
%60 = load %struct.ifreq*, %struct.ifreq** %8
%61 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %60, i32 0, i32 1
%62 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %61, i32 0, i32 0
%63 = load i64, i64* %62
%64 = load i64, i64* @AF_INET
%65 = icmp eq i64 %63, %64
br i1 %65, label %66, label %77
66: ; preds = %59
%67 = load %struct.sockaddr_in*, %struct.sockaddr_in** %2
%68 = load %struct.ifreq*, %struct.ifreq** %8
%69 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %68, i32 0, i32 1
%70 = bitcast %struct.TYPE_2__* %69 to %struct.sockaddr_in*
%71 = bitcast %struct.sockaddr_in* %67 to i8*
%72 = bitcast %struct.sockaddr_in* %70 to i8*
%73 = load i32, i32* @PMAPPORT
%74 = call i32 @htons(i32 %73)
%75 = load %struct.sockaddr_in*, %struct.sockaddr_in** %2
%76 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %75, i32 0, i32 0
store i32 %74, i32* %76
br label %85
77: ; preds = %59, %53
%78 = load %struct.ifreq*, %struct.ifreq** %8
%79 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %78, i32 1
store %struct.ifreq* %79, %struct.ifreq** %8
br label %80
80: ; preds = %77
%81 = load i32, i32* %9
%82 = sext i32 %81 to i64
%83 = sub i64 %82, 16
%84 = trunc i64 %83 to i32
store i32 %84, i32* %9
br label %38
85: ; preds = %66, %38
%86 = load i32, i32* %3
%87 = call i32 @close(i32 %86)
%88 = load i8*, i8** %4
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.ifconf = type { i32, i8*, %struct.ifreq* }
%struct.ifreq = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
@BUFSIZ = global i32 0
@AF_INET = global i64 0
@SOCK_DGRAM = global i32 0
@.str = [22 x i8] c"get_myaddress: socket\00"
@SIOCGIFCONF = global i32 0
@.str.1 = [51 x i8] c"get_myaddress: ioctl (get interface configuration)\00"
@SIOCGIFFLAGS = global i32 0
@.str.2 = [21 x i8] c"get_myaddress: ioctl\00"
@IFF_UP = global i32 0
@PMAPPORT = global i32 0
define void @get_myaddress(%struct.sockaddr_in* nocapture writeonly %0) {
%2 = alloca %struct.ifconf
%3 = alloca %struct.ifreq
%4 = load i32, i32* @BUFSIZ
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast %struct.ifconf* %2 to i8*
%8 = bitcast %struct.ifreq* %3 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 slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %1
%17 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %2, i64 0, i32 0
store i32 %4, i32* %17
%18 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %2, i64 0, i32 1
store i8* %6, i8** %18
%19 = load i32, i32* @SIOCGIFCONF
%20 = call i64 @ioctl(i32 %11, i32 %19, i8* %7)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %16
%26 = load i32, i32* %17
%27 = icmp eq i32 %26, 0
br i1 %27, label %61, label %28
28: ; preds = %25
%29 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %2, i64 0, i32 2
%30 = load %struct.ifreq*, %struct.ifreq** %29
%31 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
br label %32
32: ; preds = %28, %57
%33 = phi i32 [ %26, %28 ], [ %59, %57 ]
%34 = phi %struct.ifreq* [ %30, %28 ], [ %58, %57 ]
%35 = bitcast %struct.ifreq* %34 to i8*
%36 = load i32, i32* @SIOCGIFFLAGS
%37 = call i64 @ioctl(i32 %11, i32 %36, i8* %8)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %32
%40 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %32
%43 = load i32, i32* %31
%44 = load i32, i32* @IFF_UP
%45 = and i32 %44, %43
%46 = icmp eq i32 %45, 0
br i1 %46, label %57, label %47
47: ; preds = %42
%48 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %34, i64 0, i32 1, i32 0
%49 = load i64, i64* %48
%50 = load i64, i64* @AF_INET
%51 = icmp eq i64 %49, %50
br i1 %51, label %52, label %57
52: ; preds = %47
%53 = trunc i64 %49 to i32
%54 = getelementptr %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 0
store i32 %53, i32* %54
%55 = load i32, i32* @PMAPPORT
%56 = call i32 @htons(i32 %55)
store i32 %56, i32* %54
br label %61
57: ; preds = %47, %42
%58 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %34, i64 1
%59 = add i32 %33, -16
%60 = icmp eq i32 %59, 0
br i1 %60, label %61, label %32
61: ; preds = %57, %25, %52
%62 = call i32 @close(i32 %11)
ret void
}