#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 */

/* Type definitions */
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct ifreq {
  int /*<<< orphan*/ ifr_addr;
  int /*<<< orphan*/ ifr_name;
};
typedef int /*<<< orphan*/ ifr_ip;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ inet_ntoa(int /*<<< orphan*/);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
char *get_hostip(void) {
  int fd;
  struct sockaddr_in *sin;
  struct ifreq ifr_ip;
  static char ipbuf[32] = "";
  if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    goto end;
  }
  memset(&ifr_ip, 0, sizeof(ifr_ip));
  strncpy(ifr_ip.ifr_name, "eth0", sizeof(ifr_ip.ifr_name) - 1);
  if (ioctl(fd, SIOCGIFADDR, &ifr_ip) < 0) {
    memset(&ifr_ip, 0, sizeof(ifr_ip));
    strncpy(ifr_ip.ifr_name, "wlan0", sizeof(ifr_ip.ifr_name) - 1);
    if (ioctl(fd, SIOCGIFADDR, &ifr_ip) < 0)
      goto end;
  }
  sin = (struct sockaddr_in *)&ifr_ip.ifr_addr;
  strcpy(ipbuf, inet_ntoa(sin->sin_addr));
end:
  close(fd);
  return ipbuf;
}


x86 assembly (clang, -O0):
<get_hostip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
jge    11a2 <get_hostip+0x32>
jmp    1253 <get_hostip+0xe3>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    -0x14(%rbp),%edi
lea    0xe44(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    -0x4(%rbp),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0x0,%rax
jge    1232 <get_hostip+0xc2>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    -0x14(%rbp),%edi
lea    0xe06(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    -0x4(%rbp),%edi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0x0,%rax
jge    122d <get_hostip+0xbd>
jmp    1253 <get_hostip+0xe3>
jmp    1232 <get_hostip+0xc2>
lea    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1050 <inet_ntoa@plt>
mov    %eax,%esi
lea    0x2e22(%rip),%rdi
call   1040 <strcpy@plt>
mov    -0x4(%rbp),%edi
call   1080 <close@plt>
lea    0x2e0e(%rip),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_hostip>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1230 <get_hostip+0xc0>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0xc(%rsp),%edi
lea    0xe45(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    0x2e1c(%rip),%r15
mov    (%r15),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1070 <ioctl@plt>
test   %rax,%rax
jns    1219 <get_hostip+0xa9>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0xc(%rsp),%edi
lea    0xe08(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    (%r15),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1070 <ioctl@plt>
test   %rax,%rax
js     1230 <get_hostip+0xc0>
mov    0x8(%rsp),%edi
call   1050 <inet_ntoa@plt>
lea    0x2e47(%rip),%rdi
mov    %eax,%esi
call   1040 <strcpy@plt>
mov    %ebx,%edi
call   1080 <close@plt>
lea    0x2e32(%rip),%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@get_hostip.ipbuf = internal global [32 x i8] zeroinitializer
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [5 x i8] c"eth0\00"
@SIOCGIFADDR = global i32 0
@.str.1 = [6 x i8] c"wlan0\00"
define i8* @get_hostip() {
%1 = alloca %struct.ifreq
%2 = bitcast %struct.ifreq* %1 to i8*
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %27, label %7
7: ; preds = %0
%8 = call i32 @memset(%struct.ifreq* %1, i32 0, i32 8)
%9 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %1, i64 0, i32 1
%10 = load i32, i32* %9
%11 = call i32 @strncpy(i32 %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 3)
%12 = load i32, i32* @SIOCGIFADDR
%13 = call i64 @ioctl(i32 %5, i32 %12, %struct.ifreq* %1)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %7
%16 = call i32 @memset(%struct.ifreq* %1, i32 0, i32 8)
%17 = load i32, i32* %9
%18 = call i32 @strncpy(i32 %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 3)
%19 = load i32, i32* @SIOCGIFADDR
%20 = call i64 @ioctl(i32 %5, i32 %19, %struct.ifreq* %1)
%21 = icmp slt i64 %20, 0
br i1 %21, label %27, label %22
22: ; preds = %15, %7
%23 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %1, i64 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 @inet_ntoa(i32 %24)
%26 = call i32 @strcpy(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @get_hostip.ipbuf, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %15, %0, %22
%28 = call i32 @close(i32 %5)
ret i8* getelementptr inbounds ([32 x i8], [32 x i8]* @get_hostip.ipbuf, i64 0, i64 0)
}