#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 */
typedef int /*<<< orphan*/ uint16_t;
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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
void init_sockaddr(struct sockaddr_in *name, const char *hostname,
                   uint16_t 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);
    exit(EXIT_FAILURE);
  }
  name->sin_addr = *(struct in_addr *)hostinfo->h_addr;
}


x86 assembly (clang, -O0):
<init_sockaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    0x2e8e(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b3 <init_sockaddr+0x73>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe60(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rcx),%rcx
mov    (%rcx),%ecx
mov    %ecx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_sockaddr>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e97(%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     117c <init_sockaddr+0x3c>
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
lea    0xe74(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

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"
@EXIT_FAILURE = global i32 0
define void @init_sockaddr(%struct.sockaddr_in* %0, i8* %1, i32 %2) {
%4 = alloca %struct.sockaddr_in*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.hostent*
store %struct.sockaddr_in* %0, %struct.sockaddr_in** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load i32, i32* @AF_INET
%9 = load %struct.sockaddr_in*, %struct.sockaddr_in** %4
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %8, i32* %10
%11 = load i32, i32* %6
%12 = call i32 @htons(i32 %11)
%13 = load %struct.sockaddr_in*, %struct.sockaddr_in** %4
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 1
store i32 %12, i32* %14
%15 = load i8*, i8** %5
%16 = call %struct.hostent* @gethostbyname(i8* %15)
store %struct.hostent* %16, %struct.hostent** %7
%17 = load %struct.hostent*, %struct.hostent** %7
%18 = icmp eq %struct.hostent* %17, null
br i1 %18, label %19, label %25
19: ; preds = %3
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %5
%22 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %3
%26 = load %struct.sockaddr_in*, %struct.sockaddr_in** %4
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %26, i32 0, i32 0
%28 = load %struct.hostent*, %struct.hostent** %7
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %28, i32 0, i32 0
%30 = load i64, i64* %29
%31 = inttoptr i64 %30 to %struct.in_addr*
%32 = bitcast %struct.in_addr* %27 to i8*
%33 = bitcast %struct.in_addr* %31 to i8*
ret void
}

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"
@EXIT_FAILURE = global i32 0
define void @init_sockaddr(%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 %10, label %15
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)
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %3
%16 = getelementptr inbounds %struct.hostent, %struct.hostent* %8, i64 0, i32 0
%17 = load i64, i64* %16
%18 = inttoptr i64 %17 to i32*
%19 = getelementptr %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 0, i32 0
%20 = load i32, i32* %18
store i32 %20, i32* %19
ret void
}