#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 {
  char *ifr_name;
  int /*<<< orphan*/ ifr_addr;
};
struct ifconf {
  int ifc_len;
  scalar_t__ ifc_buf;
  int /*<<< orphan*/ member_0;
};
typedef scalar_t__ caddr_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIFCONF;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
void getlocalhostip() {
  int MAXINTERFACES = 16;
  int fd, interface = 0;
  struct ifreq buf[MAXINTERFACES];
  struct ifconf ifc = {0};
  if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0) {
    ifc.ifc_len = sizeof(buf);
    ifc.ifc_buf = (caddr_t)buf;
    if (!ioctl(fd, SIOCGIFCONF, (char *)&ifc)) {
      interface = ifc.ifc_len / sizeof(struct ifreq);
      printf("interface: %d\n\n", interface);
      while (interface-- > 0) {
        struct sockaddr_in *ip_addr = 0;
        ip_addr = (struct sockaddr_in *)&(buf[interface].ifr_addr);
        printf("%s : %s\n", buf[interface].ifr_name,
               inet_ntoa(ip_addr->sin_addr));
      }
    }
    close(fd);
  }
}


x86 assembly (clang, -O0):
<getlocalhostip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x10,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x4(%rbp),%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
shl    $0x4,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jl     1298 <getlocalhostip+0x138>
mov    -0x48(%rbp),%rax
mov    -0x50(%rbp),%rcx
shl    $0x4,%rcx
mov    %ecx,-0x38(%rbp)
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%eax
jne    1290 <getlocalhostip+0x130>
movslq -0x38(%rbp),%rax
shr    $0x4,%rax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0xc(%rbp)
cmp    $0x0,%eax
jle    128b <getlocalhostip+0x12b>
mov    -0x48(%rbp),%rax
movq   $0x0,-0x40(%rbp)
movslq -0xc(%rbp),%rdx
shl    $0x4,%rdx
mov    %rax,%rcx
add    %rdx,%rcx
add    $0x8,%rcx
mov    %rcx,-0x40(%rbp)
movslq -0xc(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    (%rax),%rax
mov    %rax,-0x58(%rbp)
mov    -0x40(%rbp),%rax
mov    (%rax),%edi
call   1030 <inet_ntoa@plt>
mov    -0x58(%rbp),%rsi
mov    %rax,%rdx
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    121e <getlocalhostip+0xbe>
jmp    1290 <getlocalhostip+0x130>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getlocalhostip>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x120,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     1230 <getlocalhostip+0xe0>
mov    %eax,%r14d
movl   $0x100,(%rsp)
lea    0x20(%rsp),%rax
mov    %rax,0x8(%rsp)
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %r14d,%edi
call   1050 <ioctl@plt>
test   %eax,%eax
jne    1228 <getlocalhostip+0xd8>
mov    (%rsp),%ebx
mov    %ebx,%ebp
sar    $0x4,%ebp
lea    0xe35(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
cmp    $0x10,%ebx
jl     1228 <getlocalhostip+0xd8>
lea    0x1(%rbp),%ebx
add    $0xffffffff,%ebp
shl    $0x4,%rbp
add    %rsp,%rbp
add    $0x28,%rbp
lea    0xe1f(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    -0x8(%rbp),%r12
mov    0x0(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
add    $0xffffffff,%ebx
add    $0xfffffffffffffff0,%rbp
cmp    $0x1,%ebx
jg     1200 <getlocalhostip+0xb0>
mov    %r14d,%edi
call   1060 <close@plt>
add    $0x120,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifconf = type { i32, i64, i32 }
%struct.sockaddr_in = type { i32 }
%struct.ifreq = type { i8*, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFCONF = global i32 0
@.str = [16 x i8] c"interface: %d\0A\0A\00"
@.str.1 = [9 x i8] c"%s : %s\0A\00"
define void @getlocalhostip() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca %struct.ifconf
%7 = alloca %struct.sockaddr_in*
store i32 16, i32* %1
store i32 0, i32* %3
%8 = load i32, i32* %1
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %4
%11 = alloca %struct.ifreq, i64 %9
store i64 %9, i64* %5
%12 = bitcast %struct.ifconf* %6 to i8*
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_DGRAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %2
%16 = icmp sge i32 %15, 0
br i1 %16, label %17, label %60
17: ; preds = %0
%18 = mul nuw i64 16, %9
%19 = trunc i64 %18 to i32
%20 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %6, i32 0, i32 0
store i32 %19, i32* %20
%21 = ptrtoint %struct.ifreq* %11 to i64
%22 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %6, i32 0, i32 1
store i64 %21, i64* %22
%23 = load i32, i32* %2
%24 = load i32, i32* @SIOCGIFCONF
%25 = bitcast %struct.ifconf* %6 to i8*
%26 = call i32 @ioctl(i32 %23, i32 %24, i8* %25)
%27 = icmp ne i32 %26, 0
br i1 %27, label %57, label %28
28: ; preds = %17
%29 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %6, i32 0, i32 0
%30 = load i32, i32* %29
%31 = sext i32 %30 to i64
%32 = udiv i64 %31, 16
%33 = trunc i64 %32 to i32
store i32 %33, i32* %3
%34 = load i32, i32* %3
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %40, %28
%37 = load i32, i32* %3
%38 = add nsw i32 %37, -1
store i32 %38, i32* %3
%39 = icmp sgt i32 %37, 0
br i1 %39, label %40, label %56
40: ; preds = %36
store %struct.sockaddr_in* null, %struct.sockaddr_in** %7
%41 = load i32, i32* %3
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %11, i64 %42
%44 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %43, i32 0, i32 1
%45 = bitcast i32* %44 to %struct.sockaddr_in*
store %struct.sockaddr_in* %45, %struct.sockaddr_in** %7
%46 = load i32, i32* %3
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %11, i64 %47
%49 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %48, i32 0, i32 0
%50 = load i8*, i8** %49
%51 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %51, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call i8* @inet_ntoa(i32 %53)
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %50, i8* %54)
br label %36
56: ; preds = %36
br label %57
57: ; preds = %56, %17
%58 = load i32, i32* %2
%59 = call i32 @close(i32 %58)
br label %60
60: ; preds = %57, %0
%61 = load i8*, i8** %4
ret void
}

LLVM IR (-O2):
%struct.ifconf = type { i32, i64, i32 }
%struct.ifreq = type { i8*, i32 }
%struct.sockaddr_in = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFCONF = global i32 0
@.str = [16 x i8] c"interface: %d\0A\0A\00"
@.str.1 = [9 x i8] c"%s : %s\0A\00"
define void @getlocalhostip() {
%1 = alloca %struct.ifconf
%2 = alloca [16 x %struct.ifreq]
%3 = bitcast %struct.ifconf* %1 to i8*
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_DGRAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp sgt i32 %6, -1
br i1 %7, label %8, label %36
8: ; preds = %0
%9 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %1, i64 0, i32 0
store i32 256, i32* %9
%10 = ptrtoint [16 x %struct.ifreq]* %2 to i64
%11 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %1, i64 0, i32 1
store i64 %10, i64* %11
%12 = load i32, i32* @SIOCGIFCONF
%13 = call i32 @ioctl(i32 %6, i32 %12, i8* %3)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %34
15: ; preds = %8
%16 = load i32, i32* %9
%17 = ashr i32 %16, 4
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = icmp sgt i32 %16, 15
br i1 %19, label %20, label %34
20: ; preds = %15
%21 = zext i32 %17 to i64
br label %22
22: ; preds = %20, %22
%23 = phi i64 [ %21, %20 ], [ %24, %22 ]
%24 = add nsw i64 %23, -1
%25 = and i64 %24, 4294967295
%26 = getelementptr inbounds [16 x %struct.ifreq], [16 x %struct.ifreq]* %2, i64 0, i64 %25, i32 1
%27 = getelementptr inbounds [16 x %struct.ifreq], [16 x %struct.ifreq]* %2, i64 0, i64 %25, i32 0
%28 = load i8*, i8** %27
%29 = load i32, i32* %26
%30 = call i8* @inet_ntoa(i32 %29)
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %28, i8* %30)
%32 = trunc i64 %23 to i32
%33 = icmp sgt i32 %32, 1
br i1 %33, label %22, label %34
34: ; preds = %22, %15, %8
%35 = call i32 @close(i32 %6)
br label %36
36: ; preds = %34, %0
ret void
}