#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 */
typedef scalar_t__ uint32_t;
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct ifaddrs {
  TYPE_1__ *ifa_addr;
  int /*<<< orphan*/ ifa_name;
  struct ifaddrs *ifa_next;
};
struct TYPE_2__ {
  scalar_t__ sa_family;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ INET_ADDRSTRLEN;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ freeifaddrs(struct ifaddrs *);
int /*<<< orphan*/ getifaddrs(struct ifaddrs **);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ inet_ntop(scalar_t__, void *, char *, int /*<<< orphan*/);
int sscanf(char *, char *, char *, scalar_t__ *, scalar_t__ *);
int /*<<< orphan*/ strcmp(int /*<<< orphan*/, char *);
void find_ip(char addressBuffer[]) {
  FILE *routes;
  char *line = NULL;
  size_t n = 0;
  uint32_t destination = 0, gateway = 0;
  char ifname[100];
  struct ifaddrs *ifAddrStruct = NULL;
  struct ifaddrs *ifa = NULL;
  void *tmpAddrPtr = NULL;
  routes = fopen("/proc/net/route", "r");
  if (!routes) {
    return;
  }
  while (getline(&line, &n, routes) != -1) {
    if (sscanf(line, "%s %d %d", ifname, &destination, &gateway) == 3) {
      if (destination == 0) {
        break;
      }
    }
  }
  fclose(routes);
  free(line);
  getifaddrs(&ifAddrStruct);
  for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
    if (!ifa->ifa_addr) {
      continue;
    }
    if (!strcmp(ifa->ifa_name, ifname) && ifa->ifa_addr->sa_family == AF_INET) {
      tmpAddrPtr = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
      inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
    }
  }
  freeifaddrs(ifAddrStruct);
}


x86 assembly (clang, -O0):
<find_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0xa8(%rbp)
movq   $0x0,-0xb0(%rbp)
movq   $0x0,-0xb8(%rbp)
lea    0xe19(%rip),%rdi
lea    0xe22(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1207 <find_ip+0x77>
jmp    1350 <find_ip+0x1c0>
jmp    120c <find_ip+0x7c>
mov    -0x10(%rbp),%rdx
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   10b0 <getline@plt>
cmp    $0xffffffff,%eax
je     1268 <find_ip+0xd8>
mov    -0x18(%rbp),%rdi
lea    -0xa0(%rbp),%rdx
lea    0xdda(%rip),%rsi
lea    -0x28(%rbp),%rcx
lea    -0x30(%rbp),%r8
call   1080 <sscanf@plt>
cmp    $0x3,%eax
jne    1263 <find_ip+0xd3>
cmpq   $0x0,-0x28(%rbp)
jne    125e <find_ip+0xce>
jmp    1268 <find_ip+0xd8>
jmp    1263 <find_ip+0xd3>
jmp    120c <find_ip+0x7c>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
lea    -0xa8(%rbp),%rdi
call   1070 <getifaddrs@plt>
mov    -0xa8(%rbp),%rax
mov    %rax,-0xb0(%rbp)
cmpq   $0x0,-0xb0(%rbp)
je     1344 <find_ip+0x1b4>
mov    -0xb0(%rbp),%rax
cmpq   $0x0,(%rax)
jne    12b8 <find_ip+0x128>
jmp    132d <find_ip+0x19d>
mov    -0xb0(%rbp),%rax
mov    0x8(%rax),%edi
lea    -0xa0(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1328 <find_ip+0x198>
mov    -0xb0(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    0x2cfd(%rip),%rcx
cmp    (%rcx),%rax
jne    1328 <find_ip+0x198>
mov    -0xb0(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0xb8(%rbp)
mov    0x2cdc(%rip),%rax
mov    (%rax),%rdi
mov    -0xb8(%rbp),%rsi
mov    -0x8(%rbp),%rdx
mov    0x2cb7(%rip),%rax
mov    (%rax),%ecx
call   1030 <inet_ntop@plt>
jmp    132d <find_ip+0x19d>
mov    -0xb0(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0xb0(%rbp)
jmp    1294 <find_ip+0x104>
mov    -0xa8(%rbp),%rdi
call   1090 <freeifaddrs@plt>
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_ip>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %rdi,%rbp
movq   $0x0,0x8(%rsp)
movq   $0x0,0x28(%rsp)
movq   $0x0,0x10(%rsp)
movq   $0x0,0x20(%rsp)
movq   $0x0,(%rsp)
lea    0xe29(%rip),%rdi
lea    0xe32(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12d5 <find_ip+0x145>
mov    %rax,%rbx
mov    %rbp,0x18(%rsp)
lea    0x28(%rsp),%r12
lea    0xe12(%rip),%r13
lea    0x30(%rsp),%rbp
lea    0x10(%rsp),%r14
lea    0x20(%rsp),%r15
nop
lea    0x8(%rsp),%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   10b0 <getline@plt>
cmp    $0xffffffff,%eax
je     1248 <find_ip+0xb8>
mov    0x8(%rsp),%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
mov    %r14,%rcx
mov    %r15,%r8
call   1080 <sscanf@plt>
cmp    $0x3,%eax
jne    1210 <find_ip+0x80>
cmpq   $0x0,0x10(%rsp)
jne    1210 <find_ip+0x80>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0x8(%rsp),%rdi
call   1040 <free@plt>
mov    %rsp,%rdi
call   1070 <getifaddrs@plt>
mov    (%rsp),%rbp
test   %rbp,%rbp
je     12ce <find_ip+0x13e>
lea    0x30(%rsp),%rbx
mov    0x2d71(%rip),%r14
mov    0x2d5a(%rip),%r15
mov    0x18(%rsp),%r12
jmp    1299 <find_ip+0x109>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x10(%rbp),%rbp
test   %rbp,%rbp
je     12c8 <find_ip+0x138>
cmpq   $0x0,0x0(%rbp)
je     1290 <find_ip+0x100>
mov    0x8(%rbp),%edi
mov    %rbx,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
jne    1290 <find_ip+0x100>
mov    0x0(%rbp),%rsi
mov    (%rsi),%rdi
cmp    (%r14),%rdi
jne    1290 <find_ip+0x100>
mov    (%r15),%ecx
mov    %r12,%rdx
call   1030 <inet_ntop@plt>
jmp    1290 <find_ip+0x100>
mov    (%rsp),%rdi
jmp    12d0 <find_ip+0x140>
xor    %edi,%edi
call   1090 <freeifaddrs@plt>
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifaddrs = type { %struct.TYPE_2__*, i32, %struct.ifaddrs* }
%struct.TYPE_2__ = type { i64 }
%struct.sockaddr_in = type { i32 }
@.str = [16 x i8] c"/proc/net/route\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [9 x i8] c"%s %d %d\00"
@AF_INET = global i64 0
@INET_ADDRSTRLEN = global i32 0
define void @find_ip(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca [100 x i8]
%9 = alloca %struct.ifaddrs*
%10 = alloca %struct.ifaddrs*
%11 = alloca i8*
store i8* %0, i8** %2
store i8* null, i8** %4
store i64 0, i64* %5
store i64 0, i64* %6
store i64 0, i64* %7
store %struct.ifaddrs* null, %struct.ifaddrs** %9
store %struct.ifaddrs* null, %struct.ifaddrs** %10
store i8* null, i8** %11
%12 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %3
%13 = load i32*, i32** %3
%14 = icmp ne i32* %13, null
br i1 %14, label %16, label %15
15: ; preds = %1
br label %83
16: ; preds = %1
br label %17
17: ; preds = %31, %16
%18 = load i32*, i32** %3
%19 = call i32 @getline(i8** %4, i64* %5, i32* %18)
%20 = icmp ne i32 %19, -1
br i1 %20, label %21, label %32
21: ; preds = %17
%22 = load i8*, i8** %4
%23 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%24 = call i32 @sscanf(i8* %22, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %23, i64* %6, i64* %7)
%25 = icmp eq i32 %24, 3
br i1 %25, label %26, label %31
26: ; preds = %21
%27 = load i64, i64* %6
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %26
br label %32
30: ; preds = %26
br label %31
31: ; preds = %30, %21
br label %17
32: ; preds = %29, %17
%33 = load i32*, i32** %3
%34 = call i32 @fclose(i32* %33)
%35 = load i8*, i8** %4
%36 = call i32 @free(i8* %35)
%37 = call i32 @getifaddrs(%struct.ifaddrs** %9)
%38 = load %struct.ifaddrs*, %struct.ifaddrs** %9
store %struct.ifaddrs* %38, %struct.ifaddrs** %10
br label %39
39: ; preds = %76, %32
%40 = load %struct.ifaddrs*, %struct.ifaddrs** %10
%41 = icmp ne %struct.ifaddrs* %40, null
br i1 %41, label %42, label %80
42: ; preds = %39
%43 = load %struct.ifaddrs*, %struct.ifaddrs** %10
%44 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %43, i32 0, i32 0
%45 = load %struct.TYPE_2__*, %struct.TYPE_2__** %44
%46 = icmp ne %struct.TYPE_2__* %45, null
br i1 %46, label %48, label %47
47: ; preds = %42
br label %76
48: ; preds = %42
%49 = load %struct.ifaddrs*, %struct.ifaddrs** %10
%50 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %49, i32 0, i32 1
%51 = load i32, i32* %50
%52 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%53 = call i32 @strcmp(i32 %51, i8* %52)
%54 = icmp ne i32 %53, 0
br i1 %54, label %75, label %55
55: ; preds = %48
%56 = load %struct.ifaddrs*, %struct.ifaddrs** %10
%57 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %56, i32 0, i32 0
%58 = load %struct.TYPE_2__*, %struct.TYPE_2__** %57
%59 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %58, i32 0, i32 0
%60 = load i64, i64* %59
%61 = load i64, i64* @AF_INET
%62 = icmp eq i64 %60, %61
br i1 %62, label %63, label %75
63: ; preds = %55
%64 = load %struct.ifaddrs*, %struct.ifaddrs** %10
%65 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %64, i32 0, i32 0
%66 = load %struct.TYPE_2__*, %struct.TYPE_2__** %65
%67 = bitcast %struct.TYPE_2__* %66 to %struct.sockaddr_in*
%68 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %67, i32 0, i32 0
%69 = bitcast i32* %68 to i8*
store i8* %69, i8** %11
%70 = load i64, i64* @AF_INET
%71 = load i8*, i8** %11
%72 = load i8*, i8** %2
%73 = load i32, i32* @INET_ADDRSTRLEN
%74 = call i32 @inet_ntop(i64 %70, i8* %71, i8* %72, i32 %73)
br label %75
75: ; preds = %63, %55, %48
br label %76
76: ; preds = %75, %47
%77 = load %struct.ifaddrs*, %struct.ifaddrs** %10
%78 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %77, i32 0, i32 2
%79 = load %struct.ifaddrs*, %struct.ifaddrs** %78
store %struct.ifaddrs* %79, %struct.ifaddrs** %10
br label %39
80: ; preds = %39
%81 = load %struct.ifaddrs*, %struct.ifaddrs** %9
%82 = call i32 @freeifaddrs(%struct.ifaddrs* %81)
br label %83
83: ; preds = %80, %15
ret void
}

LLVM IR (-O2):
%struct.ifaddrs = type { %struct.TYPE_2__*, i32, %struct.ifaddrs* }
%struct.TYPE_2__ = type { i64 }
@.str = [16 x i8] c"/proc/net/route\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [9 x i8] c"%s %d %d\00"
@AF_INET = global i64 0
@INET_ADDRSTRLEN = global i32 0
define void @find_ip(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca [100 x i8]
%7 = alloca %struct.ifaddrs*
%8 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%9 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%10 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%11 = bitcast i64* %5 to i8*
store i64 0, i64* %5
%12 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%13 = bitcast %struct.ifaddrs** %7 to i8*
store %struct.ifaddrs* null, %struct.ifaddrs** %7
%14 = tail call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %62, label %16
16: ; preds = %1, %19
%17 = call i32 @getline(i8** %2, i64* %3, i32* %14)
%18 = icmp eq i32 %17, -1
br i1 %18, label %26, label %19
19: ; preds = %16
%20 = load i8*, i8** %2
%21 = call i32 @sscanf(i8* %20, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %12, i64* %4, i64* %5)
%22 = icmp eq i32 %21, 3
%23 = load i64, i64* %4
%24 = icmp eq i64 %23, 0
%25 = select i1 %22, i1 %24, i1 false
br i1 %25, label %26, label %16
26: ; preds = %19, %16
%27 = call i32 @fclose(i32* %14)
%28 = load i8*, i8** %2
%29 = call i32 @free(i8* %28)
%30 = call i32 @getifaddrs(%struct.ifaddrs** %7)
%31 = load %struct.ifaddrs*, %struct.ifaddrs** %7
%32 = icmp eq %struct.ifaddrs* %31, null
br i1 %32, label %59, label %33
33: ; preds = %26, %53
%34 = phi %struct.ifaddrs* [ %55, %53 ], [ %31, %26 ]
%35 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %34, i64 0, i32 0
%36 = load %struct.TYPE_2__*, %struct.TYPE_2__** %35
%37 = icmp eq %struct.TYPE_2__* %36, null
br i1 %37, label %53, label %38
38: ; preds = %33
%39 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %34, i64 0, i32 1
%40 = load i32, i32* %39
%41 = call i32 @strcmp(i32 %40, i8* %12)
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %53
43: ; preds = %38
%44 = load %struct.TYPE_2__*, %struct.TYPE_2__** %35
%45 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %44, i64 0, i32 0
%46 = load i64, i64* %45
%47 = load i64, i64* @AF_INET
%48 = icmp eq i64 %46, %47
br i1 %48, label %49, label %53
49: ; preds = %43
%50 = bitcast %struct.TYPE_2__* %44 to i8*
%51 = load i32, i32* @INET_ADDRSTRLEN
%52 = call i32 @inet_ntop(i64 %46, i8* %50, i8* %0, i32 %51)
br label %53
53: ; preds = %38, %43, %49, %33
%54 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %34, i64 0, i32 2
%55 = load %struct.ifaddrs*, %struct.ifaddrs** %54
%56 = icmp eq %struct.ifaddrs* %55, null
br i1 %56, label %57, label %33
57: ; preds = %53
%58 = load %struct.ifaddrs*, %struct.ifaddrs** %7
br label %59
59: ; preds = %57, %26
%60 = phi %struct.ifaddrs* [ %58, %57 ], [ null, %26 ]
%61 = call i32 @freeifaddrs(%struct.ifaddrs* %60)
br label %62
62: ; preds = %1, %59
ret void
}