#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 sockaddr_in {
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct hostent {
  scalar_t__ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int);
int net_initSocket(struct sockaddr_in *name, const char *host, int port) {
  struct hostent *hostinfo;
  name->sin_family = AF_INET;
  name->sin_port = htons(port);
  hostinfo = gethostbyname(host);
  if (hostinfo == NULL) {
    return -1;
  }
  name->sin_addr = *(struct in_addr *)hostinfo->h_addr;
  return 0;
}


x86 assembly (clang, -O0):
<net_initSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    0x2eae(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x1c(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x18(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1178 <net_initSocket+0x58>
movl   $0xffffffff,-0x4(%rbp)
jmp    118e <net_initSocket+0x6e>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rcx),%rcx
mov    (%rcx),%ecx
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_initSocket>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2eb7(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rdi)
mov    %edx,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rbx)
mov    %r14,%rdi
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     1158 <net_initSocket+0x38>
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,(%rbx)
xor    %eax,%eax
jmp    115d <net_initSocket+0x3d>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
@AF_INET = global i32 0
define i32 @net_initSocket(%struct.sockaddr_in* nocapture writeonly %0, i8* %1, i32 %2) {
%4 = load i32, i32* @AF_INET
%5 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 2
store i32 %4, i32* %5
%6 = tail call i32 @htons(i32 %2)
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 1
store i32 %6, i32* %7
%8 = tail call %struct.hostent* @gethostbyname(i8* %1)
%9 = icmp eq %struct.hostent* %8, null
br i1 %9, label %16, label %10
10: ; preds = %3
%11 = getelementptr inbounds %struct.hostent, %struct.hostent* %8, i64 0, i32 0
%12 = load i64, i64* %11
%13 = inttoptr i64 %12 to i32*
%14 = getelementptr %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 0, i32 0
%15 = load i32, i32* %13
store i32 %15, i32* %14
br label %16
16: ; preds = %3, %10
%17 = phi i32 [ 0, %10 ], [ -1, %3 ]
ret i32 %17
}