#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 hostent {
  int /*<<< orphan*/ h_length;
  scalar_t__ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ bcopy(char *, char *, int /*<<< orphan*/);
struct hostent *gethostbyname(char *);
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int srcUdpSocket_get_server_ip(char *host, char *ip) {
  struct hostent *hostEntry;
  struct sockaddr_in address;
  hostEntry = gethostbyname(host);
  if (hostEntry == NULL) {
    return -1;
  }
  bcopy((char *)hostEntry->h_addr, (char *)&address.sin_addr,
        hostEntry->h_length);
  char *clientIP = inet_ntoa(address.sin_addr);
  memcpy(ip, clientIP, strlen(clientIP));
  return 0;
}


x86 assembly (clang, -O0):
<srcUdpSocket_get_server_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1184 <srcUdpSocket_get_server_ip+0x34>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d6 <srcUdpSocket_get_server_ip+0x86>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1070 <bcopy@plt>
mov    -0x28(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   1060 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<srcUdpSocket_get_server_ip>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     1186 <srcUdpSocket_get_server_ip+0x46>
mov    0x8(%rax),%rsi
mov    (%rax),%edx
lea    0x4(%rsp),%rdi
call   1060 <memcpy@plt>
mov    0x4(%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <strlen@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1060 <memcpy@plt>
xor    %eax,%eax
jmp    118b <srcUdpSocket_get_server_ip+0x4b>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.hostent = type { i32, i64 }
%struct.sockaddr_in = type { i32 }
define i32 @srcUdpSocket_get_server_ip(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.hostent*
%7 = alloca %struct.sockaddr_in
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = call %struct.hostent* @gethostbyname(i8* %9)
store %struct.hostent* %10, %struct.hostent** %6
%11 = load %struct.hostent*, %struct.hostent** %6
%12 = icmp eq %struct.hostent* %11, null
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %33
14: ; preds = %2
%15 = load %struct.hostent*, %struct.hostent** %6
%16 = getelementptr inbounds %struct.hostent, %struct.hostent* %15, i32 0, i32 1
%17 = load i64, i64* %16
%18 = inttoptr i64 %17 to i8*
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%20 = bitcast i32* %19 to i8*
%21 = load %struct.hostent*, %struct.hostent** %6
%22 = getelementptr inbounds %struct.hostent, %struct.hostent* %21, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @bcopy(i8* %18, i8* %20, i32 %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i8* @inet_ntoa(i32 %26)
store i8* %27, i8** %8
%28 = load i8*, i8** %5
%29 = load i8*, i8** %8
%30 = load i8*, i8** %8
%31 = call i32 @strlen(i8* %30)
%32 = call i32 @memcpy(i8* %28, i8* %29, i32 %31)
store i32 0, i32* %3
br label %33
33: ; preds = %14, %13
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
%struct.hostent = type { i32, i64 }
define i32 @srcUdpSocket_get_server_ip(i8* %0, i8* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call %struct.hostent* @gethostbyname(i8* %0)
%6 = icmp eq %struct.hostent* %5, null
br i1 %6, label %17, label %7
7: ; preds = %2
%8 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 1
%9 = load i64, i64* %8
%10 = inttoptr i64 %9 to i8*
%11 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 0
%12 = load i32, i32* %11
%13 = load i32, i32* %3
%14 = tail call i8* @inet_ntoa(i32 %13)
%15 = tail call i32 @strlen(i8* %14)
%16 = tail call i32 @memcpy(i8* %1, i8* %14, i32 %15)
br label %17
17: ; preds = %2, %7
%18 = phi i32 [ 0, %7 ], [ -1, %2 ]
ret i32 %18
}