#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;
  int /*<<< orphan*/ **h_addr_list;
};
typedef int /*<<< orphan*/ host;

/* Variables and functions */
struct hostent *gethostbyname(char *);
scalar_t__ gethostname(char *, int);
char *inet_ntoa(struct in_addr);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int get_localip(char *ip) {
  char host[100] = {0};
  if (gethostname(host, sizeof(host)) < 0) {
    return -1;
  }
  struct hostent *hp;
  if ((hp = gethostbyname(host)) == NULL) {
    return -1;
  }
  int i = 0;
  while (hp->h_addr_list[i] != NULL) {
    printf("ip: %s\n", inet_ntoa(*(struct in_addr *)hp->h_addr_list[i]));
    i++;
  }
  strcpy(ip, inet_ntoa(*(struct in_addr *)hp->h_addr));
  return 0;
}


x86 assembly (clang, -O0):
<get_localip>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x80(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1060 <memset@plt>
lea    -0x80(%rbp),%rdi
mov    $0x64,%esi
call   1080 <gethostname@plt>
cmp    $0x0,%rax
jge    11a3 <get_localip+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    1262 <get_localip+0x102>
lea    -0x80(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x88(%rbp)
cmp    $0x0,%rax
jne    11c9 <get_localip+0x69>
movl   $0xffffffff,-0x4(%rbp)
jmp    1262 <get_localip+0x102>
movl   $0x0,-0x8c(%rbp)
mov    -0x88(%rbp),%rax
mov    0x8(%rax),%rax
movslq -0x8c(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     1230 <get_localip+0xd0>
mov    -0x88(%rbp),%rax
mov    0x8(%rax),%rax
movslq -0x8c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xde9(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8c(%rbp)
jmp    11d3 <get_localip+0x73>
mov    -0x10(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    -0x88(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    -0x98(%rbp),%rdi
mov    %rax,%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_localip>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    %rdi,%r14
xorps  %xmm0,%xmm0
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
movl   $0x0,0x60(%rsp)
mov    %rsp,%rdi
mov    $0x64,%esi
call   1070 <gethostname@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     120a <get_localip+0xba>
mov    %rsp,%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     120a <get_localip+0xba>
mov    %rax,%rbx
mov    0x8(%rax),%rax
mov    (%rax),%rax
test   %rax,%rax
je     11f3 <get_localip+0xa3>
mov    $0x8,%ebp
lea    0xe3c(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %r15,%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    0x8(%rbx),%rax
mov    (%rax,%rbp,1),%rax
add    $0x8,%rbp
test   %rax,%rax
jne    11d0 <get_localip+0x80>
mov    (%rbx),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %r14,%rdi
mov    %rax,%rsi
call   1030 <strcpy@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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