#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;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ stderr;
int init_sockaddr(struct sockaddr_in *name, const char *hostname,
                  unsigned short int port) {
  struct hostent *hostinfo;
  name->sin_family = AF_INET;
  name->sin_port = htons(port);
  hostinfo = gethostbyname(hostname);
  if (hostinfo == NULL) {
    fprintf(stderr, "Unknown host %s.\n", hostname);
    return -1;
  }
  name->sin_addr = *(struct in_addr *)hostinfo->h_addr;
  return 0;
}


x86 assembly (clang, -O0):
<init_sockaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dx,%ax
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %ax,-0x1a(%rbp)
mov    0x2e9a(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
movzwl -0x1a(%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    11a6 <init_sockaddr+0x76>
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe6b(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bc <init_sockaddr+0x8c>
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):
<init_sockaddr>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2ea7(%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     1168 <init_sockaddr+0x38>
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,(%rbx)
xor    %eax,%eax
jmp    1185 <init_sockaddr+0x55>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
lea    0xe88(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
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
@stderr = global i32 0
@.str = [18 x i8] c"Unknown host %s.\0A\00"
define i32 @init_sockaddr(%struct.sockaddr_in* %0, i8* %1, i16 zeroext %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr_in*
%6 = alloca i8*
%7 = alloca i16
%8 = alloca %struct.hostent*
store %struct.sockaddr_in* %0, %struct.sockaddr_in** %5
store i8* %1, i8** %6
store i16 %2, i16* %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 i16, i16* %7
%13 = call i32 @htons(i16 zeroext %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 %24
20: ; preds = %3
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %6
%23 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %22)
store i32 -1, i32* %4
br label %33
24: ; preds = %3
%25 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %25, i32 0, i32 0
%27 = load %struct.hostent*, %struct.hostent** %8
%28 = getelementptr inbounds %struct.hostent, %struct.hostent* %27, i32 0, i32 0
%29 = load i64, i64* %28
%30 = inttoptr i64 %29 to %struct.in_addr*
%31 = bitcast %struct.in_addr* %26 to i8*
%32 = bitcast %struct.in_addr* %30 to i8*
store i32 0, i32* %4
br label %33
33: ; preds = %24, %20
%34 = load i32, i32* %4
ret i32 %34
}

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
@stderr = global i32 0
@.str = [18 x i8] c"Unknown host %s.\0A\00"
define i32 @init_sockaddr(%struct.sockaddr_in* nocapture writeonly %0, i8* %1, i16 zeroext %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(i16 zeroext %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 %10, label %13
10: ; preds = %3
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %19
13: ; preds = %3
%14 = getelementptr inbounds %struct.hostent, %struct.hostent* %8, i64 0, i32 0
%15 = load i64, i64* %14
%16 = inttoptr i64 %15 to i32*
%17 = getelementptr %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 0, i32 0
%18 = load i32, i32* %16
store i32 %18, i32* %17
br label %19
19: ; preds = %13, %10
%20 = phi i32 [ -1, %10 ], [ 0, %13 ]
ret i32 %20
}