#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 in_addr {
  int dummy;
};
struct hostent {
  scalar_t__ *h_addr_list;
};
typedef int /*<<< orphan*/ hname;

/* Variables and functions */
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ gethostname(char *, int);
int /*<<< orphan*/ inet_ntoa(struct in_addr);
scalar_t__ strcmp(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int get_local_ip(char *ip) {
  char hname[128];
  struct hostent *hent;
  int i;
  gethostname(hname, sizeof(hname));
  hent = gethostbyname(hname);
  for (i = 0; hent->h_addr_list[i]; i++) {
    if (strcmp("127.0.0.1",
               inet_ntoa(*(struct in_addr *)(hent->h_addr_list[i]))) != 0) {
      strcpy(ip, inet_ntoa(*(struct in_addr *)(hent->h_addr_list[i])));
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<get_local_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x90(%rbp),%rdi
mov    $0x80,%esi
call   1070 <gethostname@plt>
lea    -0x90(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x98(%rbp)
movl   $0x0,-0x9c(%rbp)
mov    -0x98(%rbp),%rax
mov    (%rax),%rax
movslq -0x9c(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     122b <get_local_ip+0xdb>
mov    -0x98(%rbp),%rax
mov    (%rax),%rax
movslq -0x9c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %eax,%esi
lea    0xe32(%rip),%rdi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     1212 <get_local_ip+0xc2>
mov    -0x8(%rbp),%rax
mov    %rax,-0xa8(%rbp)
mov    -0x98(%rbp),%rax
mov    (%rax),%rax
movslq -0x9c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    -0xa8(%rbp),%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
jmp    1217 <get_local_ip+0xc7>
mov    -0x9c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x9c(%rbp)
jmp    118d <get_local_ip+0x3d>
xor    %eax,%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_local_ip>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    %rdi,%r14
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x80,%esi
call   1070 <gethostname@plt>
mov    %rbx,%rdi
call   1060 <gethostbyname@plt>
mov    %rax,%r12
mov    (%rax),%rax
mov    (%rax),%rax
test   %rax,%rax
je     11e3 <get_local_ip+0x93>
xor    %ebx,%ebx
lea    0xe70(%rip),%r15
jmp    11b2 <get_local_ip+0x62>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r12),%rax
mov    0x8(%rax,%rbx,1),%rax
add    $0x8,%rbx
test   %rax,%rax
je     11e3 <get_local_ip+0x93>
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %r15,%rdi
mov    %eax,%esi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11a0 <get_local_ip+0x50>
mov    (%r12),%rax
mov    (%rax,%rbx,1),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
jmp    11a0 <get_local_ip+0x50>
xor    %eax,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.hostent = type { i64* }
%struct.in_addr = type { i32 }
@.str = [10 x i8] c"127.0.0.1\00"
define i32 @get_local_ip(i8* %0) {
%2 = alloca i8*
%3 = alloca [128 x i8]
%4 = alloca %struct.hostent*
%5 = alloca i32
store i8* %0, i8** %2
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%7 = call i32 @gethostname(i8* %6, i32 128)
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%9 = call %struct.hostent* @gethostbyname(i8* %8)
store %struct.hostent* %9, %struct.hostent** %4
store i32 0, i32* %5
br label %10
10: ; preds = %48, %1
%11 = load %struct.hostent*, %struct.hostent** %4
%12 = getelementptr inbounds %struct.hostent, %struct.hostent* %11, i32 0, i32 0
%13 = load i64*, i64** %12
%14 = load i32, i32* %5
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i64, i64* %13, i64 %15
%17 = load i64, i64* %16
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %51
19: ; preds = %10
%20 = load %struct.hostent*, %struct.hostent** %4
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i32 0, i32 0
%22 = load i64*, i64** %21
%23 = load i32, i32* %5
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i64, i64* %22, i64 %24
%26 = load i64, i64* %25
%27 = inttoptr i64 %26 to %struct.in_addr*
%28 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @inet_ntoa(i32 %29)
%31 = call i64 @strcmp(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %47
33: ; preds = %19
%34 = load i8*, i8** %2
%35 = load %struct.hostent*, %struct.hostent** %4
%36 = getelementptr inbounds %struct.hostent, %struct.hostent* %35, i32 0, i32 0
%37 = load i64*, i64** %36
%38 = load i32, i32* %5
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i64, i64* %37, i64 %39
%41 = load i64, i64* %40
%42 = inttoptr i64 %41 to %struct.in_addr*
%43 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %42, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i32 @inet_ntoa(i32 %44)
%46 = call i32 @strcpy(i8* %34, i32 %45)
br label %47
47: ; preds = %33, %19
br label %48
48: ; preds = %47
%49 = load i32, i32* %5
%50 = add nsw i32 %49, 1
store i32 %50, i32* %5
br label %10
51: ; preds = %10
ret i32 0
}

LLVM IR (-O2):
%struct.hostent = type { i64* }
%struct.in_addr = type { i32 }
@.str = [10 x i8] c"127.0.0.1\00"
define i32 @get_local_ip(i8* %0) {
%2 = alloca [128 x i8]
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%4 = call i32 @gethostname(i8* %3, i32 128)
%5 = call %struct.hostent* @gethostbyname(i8* %3)
%6 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 0
%7 = load i64*, i64** %6
%8 = load i64, i64* %7
%9 = icmp eq i64 %8, 0
br i1 %9, label %34, label %10
10: ; preds = %1, %28
%11 = phi i64 [ %29, %28 ], [ 0, %1 ]
%12 = phi i64 [ %32, %28 ], [ %8, %1 ]
%13 = inttoptr i64 %12 to %struct.in_addr*
%14 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %13, i64 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @inet_ntoa(i32 %15)
%17 = call i64 @strcmp(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %28, label %19
19: ; preds = %10
%20 = load i64*, i64** %6
%21 = getelementptr inbounds i64, i64* %20, i64 %11
%22 = load i64, i64* %21
%23 = inttoptr i64 %22 to %struct.in_addr*
%24 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %23, i64 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @inet_ntoa(i32 %25)
%27 = call i32 @strcpy(i8* %0, i32 %26)
br label %28
28: ; preds = %10, %19
%29 = add nuw i64 %11, 1
%30 = load i64*, i64** %6
%31 = getelementptr inbounds i64, i64* %30, i64 %29
%32 = load i64, i64* %31
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %10
34: ; preds = %28, %1
ret i32 0
}