#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;
};
struct ifconf {
  int ifc_len;
  char *ifc_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*/, struct ifconf *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int getlocalip(char *outip) {
  int i = 0;
  int sockfd;
  struct ifconf ifconf;
  char buf[512];
  struct ifreq *ifreq;
  char *ip;
  ifconf.ifc_len = 512;
  ifconf.ifc_buf = buf;
  if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    return -1;
  }
  ioctl(sockfd, SIOCGIFCONF, &ifconf);
  close(sockfd);
  ifreq = (struct ifreq *)buf;
  for (i = (ifconf.ifc_len / sizeof(struct ifreq)); i > 0; i--) {
    ip = inet_ntoa(((struct sockaddr_in *)&(ifreq->ifr_addr))->sin_addr);
    if (strcmp(ip, "127.0.0.1") == 0) {
      ifreq++;
      continue;
    }
    strcpy(outip, ip);
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<getlocalip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x200,-0x28(%rbp)
lea    -0x230(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11b9 <getlocalip+0x59>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <getlocalip+0x113>
mov    -0x18(%rbp),%edi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1050 <ioctl@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
lea    -0x230(%rbp),%rax
mov    %rax,-0x238(%rbp)
movslq -0x28(%rbp),%rax
shr    $0x2,%rax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    126c <getlocalip+0x10c>
mov    -0x238(%rbp),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x240(%rbp)
mov    -0x240(%rbp),%rdi
lea    0xde4(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1242 <getlocalip+0xe2>
mov    -0x238(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x238(%rbp)
jmp    125e <getlocalip+0xfe>
mov    -0x10(%rbp),%rdi
mov    -0x240(%rbp),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
jmp    1273 <getlocalip+0x113>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    11ef <getlocalip+0x8f>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getlocalip>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rdi,%r14
movl   $0x200,(%rsp)
lea    0x10(%rsp),%r13
mov    %r13,0x8(%rsp)
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
js     1218 <getlocalip+0xb8>
mov    %eax,%ebp
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1050 <ioctl@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    (%rsp),%ebx
cmp    $0x4,%ebx
jl     1218 <getlocalip+0xb8>
shr    $0x2,%ebx
add    $0x1,%ebx
lea    0xe27(%rip),%r12
nopl   0x0(%rax)
mov    0x0(%r13),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r12,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    120a <getlocalip+0xaa>
add    $0x4,%r13
add    $0xffffffff,%ebx
cmp    $0x1,%ebx
jg     11e0 <getlocalip+0x80>
jmp    1218 <getlocalip+0xb8>
mov    %r14,%rdi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifconf = type { i32, i8* }
%struct.ifreq = type { i32 }
%struct.sockaddr_in = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFCONF = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
define i32 @getlocalip(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.ifconf
%7 = alloca [512 x i8]
%8 = alloca %struct.ifreq*
%9 = alloca i8*
store i8* %0, i8** %3
store i32 0, i32* %4
%10 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %6, i32 0, i32 0
store i32 512, i32* %10
%11 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%12 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %6, i32 0, i32 1
store i8* %11, i8** %12
%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* %5
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %1
store i32 -1, i32* %2
br label %55
18: ; preds = %1
%19 = load i32, i32* %5
%20 = load i32, i32* @SIOCGIFCONF
%21 = call i32 @ioctl(i32 %19, i32 %20, %struct.ifconf* %6)
%22 = load i32, i32* %5
%23 = call i32 @close(i32 %22)
%24 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%25 = bitcast i8* %24 to %struct.ifreq*
store %struct.ifreq* %25, %struct.ifreq** %8
%26 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %6, i32 0, i32 0
%27 = load i32, i32* %26
%28 = sext i32 %27 to i64
%29 = udiv i64 %28, 4
%30 = trunc i64 %29 to i32
store i32 %30, i32* %4
br label %31
31: ; preds = %51, %18
%32 = load i32, i32* %4
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %54
34: ; preds = %31
%35 = load %struct.ifreq*, %struct.ifreq** %8
%36 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %35, i32 0, i32 0
%37 = bitcast i32* %36 to %struct.sockaddr_in*
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %37, i32 0, i32 0
%39 = load i32, i32* %38
%40 = call i8* @inet_ntoa(i32 %39)
store i8* %40, i8** %9
%41 = load i8*, i8** %9
%42 = call i64 @strcmp(i8* %41, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %34
%45 = load %struct.ifreq*, %struct.ifreq** %8
%46 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %45, i32 1
store %struct.ifreq* %46, %struct.ifreq** %8
br label %51
47: ; preds = %34
%48 = load i8*, i8** %3
%49 = load i8*, i8** %9
%50 = call i32 @strcpy(i8* %48, i8* %49)
store i32 0, i32* %2
br label %55
51: ; preds = %44
%52 = load i32, i32* %4
%53 = add nsw i32 %52, -1
store i32 %53, i32* %4
br label %31
54: ; preds = %31
store i32 -1, i32* %2
br label %55
55: ; preds = %54, %47, %17
%56 = load i32, i32* %2
ret i32 %56
}

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