#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_family;
  int /*<<< orphan*/ sin_addr;
};
struct in_addr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_aton(char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
bool qsocket_get_addr(struct sockaddr_in *addr, const char *hostname,
                      int port) {
  memset((void *)addr, 0, sizeof(struct sockaddr_in));
  if (!inet_aton(hostname, &addr->sin_addr)) {
    struct hostent *hp;
    if ((hp = gethostbyname(hostname)) == 0)
      return false;
    memcpy(&addr->sin_addr, hp->h_addr, sizeof(struct in_addr));
  }
  addr->sin_family = AF_INET;
  addr->sin_port = htons(port);
  return true;
}


x86 assembly (clang, -O0):
<qsocket_get_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   1070 <inet_aton@plt>
cmp    $0x0,%eax
jne    11c8 <qsocket_get_addr+0x78>
mov    -0x18(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11b0 <qsocket_get_addr+0x60>
movl   $0x0,-0x4(%rbp)
jmp    11ef <qsocket_get_addr+0x9f>
mov    -0x10(%rbp),%rdi
add    $0x8,%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x1c(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<qsocket_get_addr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%r12
mov    %rdi,%rbx
xor    %ebp,%ebp
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
lea    0x8(%rbx),%r15
mov    %r12,%rdi
mov    %r15,%rsi
call   1070 <inet_aton@plt>
test   %eax,%eax
jne    119e <qsocket_get_addr+0x4e>
mov    %r12,%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     11b9 <qsocket_get_addr+0x69>
mov    (%rax),%esi
mov    %r15,%rdi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbx)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,(%rbx)
mov    $0x1,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32 }
@AF_INET = global i32 0
define i32 @qsocket_get_addr(%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 %struct.sockaddr_in*, %struct.sockaddr_in** %5
%10 = bitcast %struct.sockaddr_in* %9 to i8*
%11 = call i32 @memset(i8* %10, i32 0, i32 12)
%12 = load i8*, i8** %6
%13 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 2
%15 = call i32 @inet_aton(i8* %12, i32* %14)
%16 = icmp ne i32 %15, 0
br i1 %16, label %29, label %17
17: ; preds = %3
%18 = load i8*, i8** %6
%19 = call %struct.hostent* @gethostbyname(i8* %18)
store %struct.hostent* %19, %struct.hostent** %8
%20 = icmp eq %struct.hostent* %19, null
br i1 %20, label %21, label %22
21: ; preds = %17
store i32 0, i32* %4
br label %37
22: ; preds = %17
%23 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %23, i32 0, i32 2
%25 = load %struct.hostent*, %struct.hostent** %8
%26 = getelementptr inbounds %struct.hostent, %struct.hostent* %25, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @memcpy(i32* %24, i32 %27, i32 4)
br label %29
29: ; preds = %22, %3
%30 = load i32, i32* @AF_INET
%31 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %31, i32 0, i32 1
store i32 %30, i32* %32
%33 = load i32, i32* %7
%34 = call i32 @htons(i32 %33)
%35 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %35, i32 0, i32 0
store i32 %34, i32* %36
store i32 1, i32* %4
br label %37
37: ; preds = %29, %21
%38 = load i32, i32* %4
ret i32 %38
}

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