#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_addr;
};
struct TYPE_2__ {
  int /*<<< orphan*/ sa_family;
};
struct ifreq {
  TYPE_1__ ifr_addr;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, struct ifreq *);
scalar_t__ malloc(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, scalar_t__);
char *get_my_ip_addr(void) {
  int fd;
  struct ifreq ifr;
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  ifr.ifr_addr.sa_family = AF_INET;
  strncpy(ifr.ifr_name, "eth0", IFNAMSIZ - 1);
  goto eth0;
wlan0:
  strncpy(ifr.ifr_name, "wlan0", IFNAMSIZ - 1);
eth0:
  ioctl(fd, SIOCGIFADDR, &ifr);
  char *result = (char *)malloc(sizeof(char) * 100);
  result = inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);
  if (strcmp(result, "0.0.0.0") == 0)
    goto wlan0;
  close(fd);
  return result;
}


x86 assembly (clang, -O0):
<get_my_ip_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x4(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e1e(%rip),%rax
mov    (%rax),%rdx
sub    $0x1,%rdx
lea    0xe48(%rip),%rsi
call   1030 <strncpy@plt>
jmp    11df <get_my_ip_addr+0x6f>
mov    -0xc(%rbp),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%rdx
sub    $0x1,%rdx
lea    0xe2b(%rip),%rsi
call   1030 <strncpy@plt>
mov    -0x4(%rbp),%edi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1050 <ioctl@plt>
mov    $0x64,%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xdf2(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    122d <get_my_ip_addr+0xbd>
jmp    11c2 <get_my_ip_addr+0x52>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_my_ip_addr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e5e(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebp
mov    (%rbx),%eax
mov    %eax,(%rsp)
mov    0x2e26(%rip),%rax
mov    (%rax),%rdx
add    $0xffffffffffffffff,%rdx
lea    0xe50(%rip),%rsi
call   1030 <strncpy@plt>
mov    0x2e2c(%rip),%r13
mov    0x0(%r13),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1050 <ioctl@plt>
mov    $0x64,%edi
call   1080 <malloc@plt>
mov    (%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rbx
lea    0xe25(%rip),%rsi
mov    %rax,%rdi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    125d <get_my_ip_addr+0xed>
lea    0xe0b(%rip),%r14
mov    %rsp,%r15
lea    0xe07(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x4(%rsp),%edi
mov    0x2dad(%rip),%rax
mov    (%rax),%rdx
add    $0xffffffffffffffff,%rdx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x0(%r13),%esi
mov    %ebp,%edi
mov    %r15,%rdx
call   1050 <ioctl@plt>
mov    $0x64,%edi
call   1080 <malloc@plt>
mov    (%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1210 <get_my_ip_addr+0xa0>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_in = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [5 x i8] c"eth0\00"
@IFNAMSIZ = global i64 0
@.str.1 = [6 x i8] c"wlan0\00"
@SIOCGIFADDR = global i32 0
@.str.2 = [8 x i8] c"0.0.0.0\00"
define i8* @get_my_ip_addr() {
%1 = alloca i32
%2 = alloca %struct.ifreq
%3 = alloca i8*
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_DGRAM
%6 = call i32 @socket(i32 %4, i32 %5, i32 0)
store i32 %6, i32* %1
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i32 0, i32 0
%9 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %8, i32 0, i32 0
store i32 %7, i32* %9
%10 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i32 0, i32 1
%11 = load i32, i32* %10
%12 = load i64, i64* @IFNAMSIZ
%13 = sub nsw i64 %12, 1
%14 = call i32 @strncpy(i32 %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 %13)
br label %21
15: ; preds = %35
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i32 0, i32 1
%17 = load i32, i32* %16
%18 = load i64, i64* @IFNAMSIZ
%19 = sub nsw i64 %18, 1
%20 = call i32 @strncpy(i32 %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i64 %19)
br label %21
21: ; preds = %15, %0
%22 = load i32, i32* %1
%23 = load i32, i32* @SIOCGIFADDR
%24 = call i32 @ioctl(i32 %22, i32 %23, %struct.ifreq* %2)
%25 = call i64 @malloc(i32 100)
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** %3
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i32 0, i32 0
%28 = bitcast %struct.TYPE_2__* %27 to %struct.sockaddr_in*
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %28, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i8* @inet_ntoa(i32 %30)
store i8* %31, i8** %3
%32 = load i8*, i8** %3
%33 = call i64 @strcmp(i8* %32, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %21
br label %15
36: ; preds = %21
%37 = load i32, i32* %1
%38 = call i32 @close(i32 %37)
%39 = load i8*, i8** %3
ret i8* %39
}

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [5 x i8] c"eth0\00"
@IFNAMSIZ = global i64 0
@.str.1 = [6 x i8] c"wlan0\00"
@SIOCGIFADDR = global i32 0
@.str.2 = [8 x i8] c"0.0.0.0\00"
define i8* @get_my_ip_addr() {
%1 = alloca %struct.ifreq
%2 = bitcast %struct.ifreq* %1 to i8*
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_DGRAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %1, i64 0, i32 0, i32 0
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %1, i64 0, i32 1
%9 = load i64, i64* @IFNAMSIZ
%10 = add nsw i64 %9, -1
%11 = tail call i32 @strncpy(i32 undef, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 %10)
%12 = load i32, i32* @SIOCGIFADDR
%13 = call i32 @ioctl(i32 %5, i32 %12, %struct.ifreq* %1)
%14 = call i64 @malloc(i32 100)
%15 = load i32, i32* %7
%16 = call i8* @inet_ntoa(i32 %15)
%17 = call i64 @strcmp(i8* %16, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %31
19: ; preds = %0, %19
%20 = load i32, i32* %8
%21 = load i64, i64* @IFNAMSIZ
%22 = add nsw i64 %21, -1
%23 = call i32 @strncpy(i32 %20, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i64 %22)
%24 = load i32, i32* @SIOCGIFADDR
%25 = call i32 @ioctl(i32 %5, i32 %24, %struct.ifreq* %1)
%26 = call i64 @malloc(i32 100)
%27 = load i32, i32* %7
%28 = call i8* @inet_ntoa(i32 %27)
%29 = call i64 @strcmp(i8* %28, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %19, label %31
31: ; preds = %19, %0
%32 = phi i8* [ %16, %0 ], [ %28, %19 ]
%33 = call i32 @close(i32 %5)
ret i8* %32
}