#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_port;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ error(char *);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int);
struct sockaddr_in *malloc(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
struct sockaddr_in *get_sockaddr_in(const char *hostname, int port) {
  struct hostent *hp;
  struct sockaddr_in *addr = malloc(sizeof(struct sockaddr_in));
  if (!addr)
    error("malloc");
  addr->sin_family = AF_INET;
  hp = gethostbyname(hostname);
  if (hp == 0)
    error("Unknown host");
  memcpy(&addr->sin_addr, hp->h_addr, hp->h_length);
  addr->sin_port = htons(port);
  return addr;
}


x86 assembly (clang, -O0):
<get_sockaddr_in>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0xc,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1184 <get_sockaddr_in+0x34>
lea    0xe81(%rip),%rdi
call   1070 <error@plt>
mov    0x2e5d(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x8(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b8 <get_sockaddr_in+0x68>
lea    0xe54(%rip),%rdi
call   1070 <error@plt>
mov    -0x20(%rbp),%rdi
add    $0x4,%rdi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   1050 <memcpy@plt>
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_sockaddr_in>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbp
mov    $0xc,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1178 <get_sockaddr_in+0x28>
lea    0xe8d(%rip),%rdi
call   1070 <error@plt>
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rbx)
mov    %rbp,%rdi
call   1040 <gethostbyname@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11a0 <get_sockaddr_in+0x50>
lea    0xe6c(%rip),%rdi
call   1070 <error@plt>
mov    %rbx,%rdi
add    $0x4,%rdi
mov    0x0(%rbp),%edx
mov    0x4(%rbp),%esi
call   1050 <memcpy@plt>
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,(%rbx)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
@.str = [7 x i8] c"malloc\00"
@AF_INET = global i32 0
@.str.1 = [13 x i8] c"Unknown host\00"
define %struct.sockaddr_in* @get_sockaddr_in(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.hostent*
%6 = alloca %struct.sockaddr_in*
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = call %struct.sockaddr_in* @malloc(i32 12)
store %struct.sockaddr_in* %7, %struct.sockaddr_in** %6
%8 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%9 = icmp ne %struct.sockaddr_in* %8, null
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = call i32 @error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %2
%13 = load i32, i32* @AF_INET
%14 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 2
store i32 %13, i32* %15
%16 = load i8*, i8** %3
%17 = call %struct.hostent* @gethostbyname(i8* %16)
store %struct.hostent* %17, %struct.hostent** %5
%18 = load %struct.hostent*, %struct.hostent** %5
%19 = icmp eq %struct.hostent* %18, null
br i1 %19, label %20, label %22
20: ; preds = %12
%21 = call i32 @error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %12
%23 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %23, i32 0, i32 1
%25 = load %struct.hostent*, %struct.hostent** %5
%26 = getelementptr inbounds %struct.hostent, %struct.hostent* %25, i32 0, i32 1
%27 = load i32, i32* %26
%28 = load %struct.hostent*, %struct.hostent** %5
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %28, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 @memcpy(i32* %24, i32 %27, i32 %30)
%32 = load i32, i32* %4
%33 = call i32 @htons(i32 %32)
%34 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %34, i32 0, i32 0
store i32 %33, i32* %35
%36 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
ret %struct.sockaddr_in* %36
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
@.str = [7 x i8] c"malloc\00"
@AF_INET = global i32 0
@.str.1 = [13 x i8] c"Unknown host\00"
define %struct.sockaddr_in* @get_sockaddr_in(i8* %0, i32 %1) {
%3 = tail call %struct.sockaddr_in* @malloc(i32 12)
%4 = icmp eq %struct.sockaddr_in* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %2
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %8, i32* %9
%10 = tail call %struct.hostent* @gethostbyname(i8* %0)
%11 = icmp eq %struct.hostent* %10, null
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = tail call i32 @error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %7
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%16 = getelementptr inbounds %struct.hostent, %struct.hostent* %10, i64 0, i32 1
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.hostent, %struct.hostent* %10, i64 0, i32 0
%19 = load i32, i32* %18
%20 = tail call i32 @memcpy(i32* %15, i32 %17, i32 %19)
%21 = tail call i32 @htons(i32 %1)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %21, i32* %22
ret %struct.sockaddr_in* %3
}