#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;
};
struct sockaddr {
  int dummy;
};
struct addrinfo {
  int /*<<< orphan*/ ai_addr;
  scalar_t__ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ bzero(struct addrinfo *, int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ getaddrinfo(char const *, int /*<<< orphan*/ *,
                               struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ memcpy(struct sockaddr *, int /*<<< orphan*/, int);
struct sockaddr *conn_get_sockaddr(const char *hostname, unsigned short port,
                                   struct sockaddr *addr) {
  struct addrinfo hints;
  struct addrinfo *results;
  bzero(&hints, sizeof(struct addrinfo));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_DGRAM;
  hints.ai_protocol = 0;
  getaddrinfo(hostname, NULL, &hints, &results);
  if (results == NULL) {
    return NULL;
  }
  memcpy(addr, results->ai_addr, sizeof(struct sockaddr));
  ((struct sockaddr_in *)addr)->sin_port = htons(port);
  freeaddrinfo(results);
  return addr;
}


x86 assembly (clang, -O0):
<conn_get_sockaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x38(%rbp),%rdi
mov    $0x18,%esi
call   1050 <bzero@plt>
mov    0x2e6c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1060 <getaddrinfo@plt>
cmpq   $0x0,-0x40(%rbp)
jne    11c2 <conn_get_sockaddr+0x72>
movq   $0x0,-0x8(%rbp)
jmp    11f8 <conn_get_sockaddr+0xa8>
mov    -0x20(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1040 <memcpy@plt>
movzwl -0x12(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x40(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<conn_get_sockaddr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%r15
mov    %esi,%r14d
mov    %rdi,%rbp
lea    0x8(%rsp),%r12
mov    %r12,%rdi
mov    $0x18,%esi
call   1050 <bzero@plt>
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
xor    %ebx,%ebx
mov    %rsp,%rcx
mov    %rbp,%rdi
xor    %esi,%esi
mov    %r12,%rdx
call   1060 <getaddrinfo@plt>
mov    (%rsp),%rax
test   %rax,%rax
je     11dc <conn_get_sockaddr+0x8c>
mov    (%rax),%esi
mov    %r15,%rdi
mov    $0x4,%edx
call   1040 <memcpy@plt>
movzwl %r14w,%edi
call   1030 <htons@plt>
mov    %eax,(%r15)
mov    (%rsp),%rdi
call   1070 <freeaddrinfo@plt>
mov    %r15,%rbx
mov    %rbx,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.addrinfo = type { i32, i64, i32, i32 }
%struct.sockaddr_in = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
define %struct.sockaddr* @conn_get_sockaddr(i8* %0, i16 zeroext %1, %struct.sockaddr* %2) {
%4 = alloca %struct.sockaddr*
%5 = alloca i8*
%6 = alloca i16
%7 = alloca %struct.sockaddr*
%8 = alloca %struct.addrinfo
%9 = alloca %struct.addrinfo*
store i8* %0, i8** %5
store i16 %1, i16* %6
store %struct.sockaddr* %2, %struct.sockaddr** %7
%10 = call i32 @bzero(%struct.addrinfo* %8, i32 24)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 3
store i32 %11, i32* %12
%13 = load i32, i32* @SOCK_DGRAM
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 2
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 1
store i64 0, i64* %15
%16 = load i8*, i8** %5
%17 = call i32 @getaddrinfo(i8* %16, i32* null, %struct.addrinfo* %8, %struct.addrinfo** %9)
%18 = load %struct.addrinfo*, %struct.addrinfo** %9
%19 = icmp eq %struct.addrinfo* %18, null
br i1 %19, label %20, label %21
20: ; preds = %3
store %struct.sockaddr* null, %struct.sockaddr** %4
br label %35
21: ; preds = %3
%22 = load %struct.sockaddr*, %struct.sockaddr** %7
%23 = load %struct.addrinfo*, %struct.addrinfo** %9
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @memcpy(%struct.sockaddr* %22, i32 %25, i32 4)
%27 = load i16, i16* %6
%28 = call i32 @htons(i16 zeroext %27)
%29 = load %struct.sockaddr*, %struct.sockaddr** %7
%30 = bitcast %struct.sockaddr* %29 to %struct.sockaddr_in*
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %30, i32 0, i32 0
store i32 %28, i32* %31
%32 = load %struct.addrinfo*, %struct.addrinfo** %9
%33 = call i32 @freeaddrinfo(%struct.addrinfo* %32)
%34 = load %struct.sockaddr*, %struct.sockaddr** %7
store %struct.sockaddr* %34, %struct.sockaddr** %4
br label %35
35: ; preds = %21, %20
%36 = load %struct.sockaddr*, %struct.sockaddr** %4
ret %struct.sockaddr* %36
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.addrinfo = type { i32, i64, i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
define %struct.sockaddr* @conn_get_sockaddr(i8* %0, i16 zeroext %1, %struct.sockaddr* %2) {
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = bitcast %struct.addrinfo* %4 to i8*
%7 = bitcast %struct.addrinfo** %5 to i8*
%8 = call i32 @bzero(%struct.addrinfo* %4, i32 24)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_DGRAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 2
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 1
store i64 0, i64* %13
%14 = call i32 @getaddrinfo(i8* %0, i32* null, %struct.addrinfo* %4, %struct.addrinfo** %5)
%15 = load %struct.addrinfo*, %struct.addrinfo** %5
%16 = icmp eq %struct.addrinfo* %15, null
br i1 %16, label %25, label %17
17: ; preds = %3
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @memcpy(%struct.sockaddr* %2, i32 %19, i32 4)
%21 = call i32 @htons(i16 zeroext %1)
%22 = getelementptr %struct.sockaddr, %struct.sockaddr* %2, i64 0, i32 0
store i32 %21, i32* %22
%23 = load %struct.addrinfo*, %struct.addrinfo** %5
%24 = call i32 @freeaddrinfo(%struct.addrinfo* %23)
br label %25
25: ; preds = %3, %17
%26 = phi %struct.sockaddr* [ %2, %17 ], [ null, %3 ]
ret %struct.sockaddr* %26
}