#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 in_addr {
  int dummy;
};
struct sockaddr_in {
  struct in_addr sin_addr;
};
struct TYPE_2__ {
  scalar_t__ sa_family;
};
struct ifreq {
  TYPE_1__ ifr_addr;
  int /*<<< orphan*/ ifr_name;
};
struct ifconf {
  int ifc_len;
  scalar_t__ ifc_buf;
};
typedef scalar_t__ caddr_t;

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ SIOCGIFCONF;
char *find_default_interface();
int /*<<< orphan*/ free(void *);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifconf *);
int /*<<< orphan*/ perror(char *);
struct ifreq *realloc(void *, intptr_t);
scalar_t__ strcmp(int /*<<< orphan*/, char *);
int find_local_address(int sockfd, struct in_addr *addr) {
  struct ifreq *ifr = NULL;
  struct ifconf buf = {
      .ifc_len = sizeof *ifr,
      .ifc_buf = (caddr_t)NULL,
  };
  intptr_t new_len;
  int ret = -1;
  char *interface = find_default_interface();
  if (interface == NULL)
    return -1;
  do {
    new_len = buf.ifc_len * 2;
    if (new_len / 2 != buf.ifc_len)
      goto error;
    ifr = realloc((void *)buf.ifc_buf, new_len);
    if (ifr == NULL) {
      perror("realloc");
      goto error;
    }
    buf.ifc_buf = (caddr_t)ifr;
    buf.ifc_len = new_len;
    if (ioctl(sockfd, SIOCGIFCONF, &buf) < 0) {
      perror("ioctl");
      goto error;
    }
  } while (buf.ifc_len >= new_len);
  for (buf.ifc_buf += buf.ifc_len; (char *)ifr < (char *)buf.ifc_buf; ifr++) {
    if (ifr->ifr_addr.sa_family != AF_INET)
      continue;
    if (strcmp(ifr->ifr_name, interface) == 0) {
      *addr = ((struct sockaddr_in *)&(ifr->ifr_addr))->sin_addr;
      ret = 0;
      break;
    }
  }
  buf.ifc_buf -= buf.ifc_len;
error:
  free((void *)buf.ifc_buf);
  return ret;
}


x86 assembly (clang, -O0):
<find_local_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0xe82(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe7f(%rip),%rax
mov    %rax,-0x20(%rbp)
movl   $0xffffffff,-0x34(%rbp)
mov    $0x0,%al
call   1060 <find_default_interface@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11b6 <find_local_address+0x56>
movl   $0xffffffff,-0x4(%rbp)
jmp    1305 <find_local_address+0x1a5>
jmp    11bb <find_local_address+0x5b>
mov    -0x28(%rbp),%eax
shl    $0x1,%eax
cltq
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    $0x2,%ecx
cqto
idiv   %rcx
movslq -0x28(%rbp),%rcx
cmp    %rcx,%rax
je     11e7 <find_local_address+0x87>
jmp    12f6 <find_local_address+0x196>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1070 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1214 <find_local_address+0xb4>
lea    0xe06(%rip),%rdi
call   1080 <perror@plt>
jmp    12f6 <find_local_address+0x196>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rax
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    1253 <find_local_address+0xf3>
lea    0xdcf(%rip),%rdi
call   1080 <perror@plt>
jmp    12f6 <find_local_address+0x196>
jmp    1258 <find_local_address+0xf8>
movslq -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jge    11bb <find_local_address+0x5b>
movslq -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
cmp    %rcx,%rax
jae    12e7 <find_local_address+0x187>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2d57(%rip),%rcx
cmp    (%rcx),%rax
je     129f <find_local_address+0x13f>
jmp    12d6 <find_local_address+0x176>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x40(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    12d1 <find_local_address+0x171>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%ecx
mov    %ecx,(%rax)
movl   $0x0,-0x34(%rbp)
jmp    12e7 <find_local_address+0x187>
jmp    12d6 <find_local_address+0x176>
mov    -0x18(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x18(%rbp)
jmp    1272 <find_local_address+0x112>
movslq -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_local_address>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
mov    %edi,%r12d
movups 0xe85(%rip),%xmm0
movaps %xmm0,(%rsp)
xor    %eax,%eax
call   1060 <find_default_interface@plt>
test   %rax,%rax
je     1268 <find_local_address+0x108>
mov    %rax,0x20(%rsp)
mov    %rbx,0x18(%rsp)
mov    $0x20,%esi
xor    %edi,%edi
call   1070 <realloc@plt>
test   %rax,%rax
je     1204 <find_local_address+0xa4>
mov    %rax,%rbx
mov    $0x20,%r15d
mov    0x2e1e(%rip),%r14
mov    %rsp,%r13
nopl   (%rax)
mov    %rbx,0x8(%rsp)
mov    %r15d,(%rsp)
mov    (%r14),%esi
mov    %r12d,%edi
mov    %r13,%rdx
call   1040 <ioctl@plt>
test   %rax,%rax
js     12a9 <find_local_address+0x149>
movslq (%rsp),%rbp
cmp    %r15d,%ebp
jl     1210 <find_local_address+0xb0>
mov    0x8(%rsp),%rdi
add    %rbp,%rbp
mov    %rbp,%rsi
call   1070 <realloc@plt>
mov    %rax,%rbx
mov    %ebp,%r15d
test   %rax,%rax
jne    11c0 <find_local_address+0x60>
lea    0xe05(%rip),%rdi
jmp    12b0 <find_local_address+0x150>
add    0x8(%rsp),%rbp
mov    %rbp,0x8(%rsp)
mov    $0xffffffff,%r12d
cmp    %rbp,%rbx
jae    1281 <find_local_address+0x121>
mov    0x2dbc(%rip),%r14
mov    (%r14),%rax
mov    0x20(%rsp),%r15
jmp    1249 <find_local_address+0xe9>
cs nopw 0x0(%rax,%rax,1)
add    $0x10,%rbx
cmp    %rbp,%rbx
jae    1281 <find_local_address+0x121>
cmp    %rax,(%rbx)
jne    1240 <find_local_address+0xe0>
mov    0x8(%rbx),%edi
mov    %r15,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     1270 <find_local_address+0x110>
mov    (%r14),%rax
mov    0x8(%rsp),%rbp
jmp    1240 <find_local_address+0xe0>
mov    $0xffffffff,%r12d
jmp    1297 <find_local_address+0x137>
mov    (%rbx),%eax
mov    0x18(%rsp),%rcx
mov    %eax,(%rcx)
xor    %r12d,%r12d
mov    0x8(%rsp),%rbp
movslq (%rsp),%rax
sub    %rax,%rbp
mov    %rbp,0x8(%rsp)
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %r12d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd68(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%r12d
jmp    128d <find_local_address+0x12d>

LLVM IR (-O0):
%struct.ifconf = type { i32, i64 }
%struct.in_addr = type { i32 }
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.sockaddr_in = type { %struct.in_addr }
@__const.find_local_address.buf = %struct.ifconf { i32 16, i64 0 }
@.str = [8 x i8] c"realloc\00"
@SIOCGIFCONF = global i32 0
@.str.1 = [6 x i8] c"ioctl\00"
@AF_INET = global i64 0
define i32 @find_local_address(i32 %0, %struct.in_addr* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.in_addr*
%6 = alloca %struct.ifreq*
%7 = alloca %struct.ifconf
%8 = alloca i64
%9 = alloca i32
%10 = alloca i8*
store i32 %0, i32* %4
store %struct.in_addr* %1, %struct.in_addr** %5
store %struct.ifreq* null, %struct.ifreq** %6
%11 = bitcast %struct.ifconf* %7 to i8*
store i32 -1, i32* %9
%12 = call i8* (...) @find_default_interface()
store i8* %12, i8** %10
%13 = load i8*, i8** %10
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 -1, i32* %3
br label %113
16: ; preds = %2
br label %17
17: ; preds = %53, %16
%18 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %7, i32 0, i32 0
%19 = load i32, i32* %18
%20 = mul nsw i32 %19, 2
%21 = sext i32 %20 to i64
store i64 %21, i64* %8
%22 = load i64, i64* %8
%23 = sdiv i64 %22, 2
%24 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %7, i32 0, i32 0
%25 = load i32, i32* %24
%26 = sext i32 %25 to i64
%27 = icmp ne i64 %23, %26
br i1 %27, label %28, label %29
28: ; preds = %17
br label %107
29: ; preds = %17
%30 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %7, i32 0, i32 1
%31 = load i64, i64* %30
%32 = inttoptr i64 %31 to i8*
%33 = load i64, i64* %8
%34 = call %struct.ifreq* @realloc(i8* %32, i64 %33)
store %struct.ifreq* %34, %struct.ifreq** %6
%35 = load %struct.ifreq*, %struct.ifreq** %6
%36 = icmp eq %struct.ifreq* %35, null
br i1 %36, label %37, label %39
37: ; preds = %29
%38 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %107
39: ; preds = %29
%40 = load %struct.ifreq*, %struct.ifreq** %6
%41 = ptrtoint %struct.ifreq* %40 to i64
%42 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %7, i32 0, i32 1
store i64 %41, i64* %42
%43 = load i64, i64* %8
%44 = trunc i64 %43 to i32
%45 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %7, i32 0, i32 0
store i32 %44, i32* %45
%46 = load i32, i32* %4
%47 = load i32, i32* @SIOCGIFCONF
%48 = call i64 @ioctl(i32 %46, i32 %47, %struct.ifconf* %7)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %39
%51 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %107
52: ; preds = %39
br label %53
53: ; preds = %52
%54 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %7, i32 0, i32 0
%55 = load i32, i32* %54
%56 = sext i32 %55 to i64
%57 = load i64, i64* %8
%58 = icmp sge i64 %56, %57
br i1 %58, label %17, label %59
59: ; preds = %53
%60 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %7, i32 0, i32 0
%61 = load i32, i32* %60
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %7, i32 0, i32 1
%64 = load i64, i64* %63
%65 = add nsw i64 %64, %62
store i64 %65, i64* %63
br label %66
66: ; preds = %97, %59
%67 = load %struct.ifreq*, %struct.ifreq** %6
%68 = bitcast %struct.ifreq* %67 to i8*
%69 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %7, i32 0, i32 1
%70 = load i64, i64* %69
%71 = inttoptr i64 %70 to i8*
%72 = icmp ult i8* %68, %71
br i1 %72, label %73, label %100
73: ; preds = %66
%74 = load %struct.ifreq*, %struct.ifreq** %6
%75 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %74, i32 0, i32 0
%76 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %75, i32 0, i32 0
%77 = load i64, i64* %76
%78 = load i64, i64* @AF_INET
%79 = icmp ne i64 %77, %78
br i1 %79, label %80, label %81
80: ; preds = %73
br label %97
81: ; preds = %73
%82 = load %struct.ifreq*, %struct.ifreq** %6
%83 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %82, i32 0, i32 1
%84 = load i32, i32* %83
%85 = load i8*, i8** %10
%86 = call i64 @strcmp(i32 %84, i8* %85)
%87 = icmp eq i64 %86, 0
br i1 %87, label %88, label %96
88: ; preds = %81
%89 = load %struct.in_addr*, %struct.in_addr** %5
%90 = load %struct.ifreq*, %struct.ifreq** %6
%91 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %90, i32 0, i32 0
%92 = bitcast %struct.TYPE_2__* %91 to %struct.sockaddr_in*
%93 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %92, i32 0, i32 0
%94 = bitcast %struct.in_addr* %89 to i8*
%95 = bitcast %struct.in_addr* %93 to i8*
store i32 0, i32* %9
br label %100
96: ; preds = %81
br label %97
97: ; preds = %96, %80
%98 = load %struct.ifreq*, %struct.ifreq** %6
%99 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %98, i32 1
store %struct.ifreq* %99, %struct.ifreq** %6
br label %66
100: ; preds = %88, %66
%101 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %7, i32 0, i32 0
%102 = load i32, i32* %101
%103 = sext i32 %102 to i64
%104 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %7, i32 0, i32 1
%105 = load i64, i64* %104
%106 = sub nsw i64 %105, %103
store i64 %106, i64* %104
br label %107
107: ; preds = %100, %50, %37, %28
%108 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %7, i32 0, i32 1
%109 = load i64, i64* %108
%110 = inttoptr i64 %109 to i8*
%111 = call i32 @free(i8* %110)
%112 = load i32, i32* %9
store i32 %112, i32* %3
br label %113
113: ; preds = %107, %15
%114 = load i32, i32* %3
ret i32 %114
}

LLVM IR (-O2):
%struct.ifconf = type { i32, i64 }
%struct.in_addr = type { i32 }
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64 }
@__const.find_local_address.buf = %struct.ifconf { i32 16, i64 0 }
@.str = [8 x i8] c"realloc\00"
@SIOCGIFCONF = global i32 0
@.str.1 = [6 x i8] c"ioctl\00"
@AF_INET = global i64 0
define i32 @find_local_address(i32 %0, %struct.in_addr* nocapture writeonly %1) {
%3 = alloca %struct.ifconf
%4 = bitcast %struct.ifconf* %3 to i8*
%5 = tail call i8* (...) @find_default_interface()
%6 = icmp eq i8* %5, null
br i1 %6, label %80, label %7
7: ; preds = %2
%8 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %3, i64 0, i32 0
%9 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %3, i64 0, i32 1
%10 = call %struct.ifreq* @realloc(i8* null, i64 32)
%11 = icmp eq %struct.ifreq* %10, null
br i1 %11, label %12, label %14
12: ; preds = %26, %7
%13 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %75
14: ; preds = %7, %26
%15 = phi %struct.ifreq* [ %31, %26 ], [ %10, %7 ]
%16 = phi i32 [ %28, %26 ], [ 32, %7 ]
%17 = ptrtoint %struct.ifreq* %15 to i64
store i64 %17, i64* %9
store i32 %16, i32* %8
%18 = load i32, i32* @SIOCGIFCONF
%19 = call i64 @ioctl(i32 %0, i32 %18, %struct.ifconf* %3)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %75
23: ; preds = %14
%24 = load i32, i32* %8
%25 = icmp slt i32 %24, %16
br i1 %25, label %33, label %26
26: ; preds = %23
%27 = load i64, i64* %9
%28 = shl nsw i32 %24, 1
%29 = sext i32 %28 to i64
%30 = inttoptr i64 %27 to i8*
%31 = call %struct.ifreq* @realloc(i8* %30, i64 %29)
%32 = icmp eq %struct.ifreq* %31, null
br i1 %32, label %12, label %14
33: ; preds = %23
%34 = sext i32 %24 to i64
%35 = load i64, i64* %9
%36 = add nsw i64 %35, %34
store i64 %36, i64* %9
%37 = bitcast %struct.ifreq* %15 to i8*
%38 = inttoptr i64 %36 to i8*
%39 = icmp ult i8* %37, %38
br i1 %39, label %40, label %69
40: ; preds = %33
%41 = load i64, i64* @AF_INET
br label %42
42: ; preds = %40, %62
%43 = phi i64 [ %63, %62 ], [ %36, %40 ]
%44 = phi i64 [ %64, %62 ], [ %41, %40 ]
%45 = phi %struct.ifreq* [ %65, %62 ], [ %15, %40 ]
%46 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %45, i64 0, i32 0, i32 0
%47 = load i64, i64* %46
%48 = icmp eq i64 %47, %44
br i1 %48, label %49, label %62
49: ; preds = %42
%50 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %45, i64 0, i32 1
%51 = load i32, i32* %50
%52 = call i64 @strcmp(i32 %51, i8* %5)
%53 = icmp eq i64 %52, 0
br i1 %53, label %57, label %54
54: ; preds = %49
%55 = load i64, i64* @AF_INET
%56 = load i64, i64* %9
br label %62
57: ; preds = %49
%58 = bitcast %struct.ifreq* %45 to i32*
%59 = getelementptr %struct.in_addr, %struct.in_addr* %1, i64 0, i32 0
%60 = load i32, i32* %58
store i32 %60, i32* %59
%61 = load i64, i64* %9
br label %69
62: ; preds = %54, %42
%63 = phi i64 [ %56, %54 ], [ %43, %42 ]
%64 = phi i64 [ %55, %54 ], [ %44, %42 ]
%65 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %45, i64 1
%66 = bitcast %struct.ifreq* %65 to i8*
%67 = inttoptr i64 %63 to i8*
%68 = icmp ult i8* %66, %67
br i1 %68, label %42, label %69
69: ; preds = %62, %33, %57
%70 = phi i64 [ %61, %57 ], [ %36, %33 ], [ %63, %62 ]
%71 = phi i32 [ 0, %57 ], [ -1, %33 ], [ -1, %62 ]
%72 = load i32, i32* %8
%73 = sext i32 %72 to i64
%74 = sub nsw i64 %70, %73
store i64 %74, i64* %9
br label %75
75: ; preds = %69, %21, %12
%76 = phi i32 [ -1, %12 ], [ -1, %21 ], [ %71, %69 ]
%77 = load i64, i64* %9
%78 = inttoptr i64 %77 to i8*
%79 = call i32 @free(i8* %78)
br label %80
80: ; preds = %2, %75
%81 = phi i32 [ %76, %75 ], [ -1, %2 ]
ret i32 %81
}