#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(unsigned short);
scalar_t__ inet_aton(char const *, struct in_addr *);
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 && inet_aton(hostname, &name->sin_addr))
    return 1;
  name->sin_addr = *(struct in_addr *)hostinfo->h_addr;
  return 1;
}


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    11a4 <init_sockaddr+0x74>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <inet_aton@plt>
cmp    $0x0,%rax
je     11a4 <init_sockaddr+0x74>
movl   $0x1,-0x4(%rbp)
jmp    11ba <init_sockaddr+0x8a>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rcx),%rcx
mov    (%rcx),%ecx
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_sockaddr>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2ea6(%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>
mov    %rax,%r15
test   %rax,%rax
jne    1171 <init_sockaddr+0x41>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <inet_aton@plt>
test   %rax,%rax
jne    1178 <init_sockaddr+0x48>
mov    (%r15),%rax
mov    (%rax),%eax
mov    %eax,(%rbx)
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %r15
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 @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 ne %struct.hostent* %18, null
br i1 %19, label %27, label %20
20: ; preds = %3
%21 = load i8*, i8** %6
%22 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %22, i32 0, i32 0
%24 = call i64 @inet_aton(i8* %21, %struct.in_addr* %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %20
store i32 1, i32* %4
br label %36
27: ; preds = %20, %3
%28 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %28, i32 0, i32 0
%30 = load %struct.hostent*, %struct.hostent** %8
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %30, i32 0, i32 0
%32 = load i64, i64* %31
%33 = inttoptr i64 %32 to %struct.in_addr*
%34 = bitcast %struct.in_addr* %29 to i8*
%35 = bitcast %struct.in_addr* %33 to i8*
store i32 1, i32* %4
br label %36
36: ; preds = %27, %26
%37 = load i32, i32* %4
ret i32 %37
}

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 @init_sockaddr(%struct.sockaddr_in* %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 %14
10: ; preds = %3
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 0
%12 = tail call i64 @inet_aton(i8* %1, %struct.in_addr* %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %10, %3
%15 = getelementptr inbounds %struct.hostent, %struct.hostent* %8, i64 0, i32 0
%16 = load i64, i64* %15
%17 = inttoptr i64 %16 to i32*
%18 = getelementptr %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 0, i32 0
%19 = load i32, i32* %17
store i32 %19, i32* %18
br label %20
20: ; preds = %10, %14
ret i32 1
}