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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EAGAIN;
int EAI_AGAIN;
int EAI_SYSTEM;
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memcpy(struct sockaddr_storage *, int /*<<< orphan*/, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
bool kcn_sockaddr_aton(struct sockaddr_storage *ss, const char *nodename,
                       const char *servname) {
  struct addrinfo hai, *rai;
  int error;
  memset(&hai, 0, sizeof(hai));
  hai.ai_family = AF_INET;
  hai.ai_socktype = SOCK_STREAM;
  hai.ai_flags = 0;
  hai.ai_protocol = 0;
  error = getaddrinfo(nodename, servname, &hai, &rai);
  if (error != 0)
    goto bad;
  assert(rai != NULL);
  assert(sizeof(*ss) >= rai->ai_addrlen);
  memcpy(ss, rai->ai_addr, rai->ai_addrlen);
  freeaddrinfo(rai);
  return true;
bad:
  if (error == EAI_AGAIN)
    errno = EAGAIN;
  else if (error != EAI_SYSTEM)
    errno = EINVAL;
  return false;
}


x86 assembly (clang, -O0):
<kcn_sockaddr_aton>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1060 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
je     11c3 <kcn_sockaddr_aton+0x73>
jmp    121c <kcn_sockaddr_aton+0xcc>
cmpq   $0x0,-0x48(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x48(%rbp),%rax
movslq (%rax),%rcx
mov    $0x4,%eax
cmp    %rcx,%rax
setae  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   1050 <memcpy@plt>
mov    -0x48(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
movl   $0x1,-0x4(%rbp)
jmp    1275 <kcn_sockaddr_aton+0x125>
mov    -0x4c(%rbp),%eax
mov    0x2da2(%rip),%rcx
cmp    (%rcx),%eax
jne    1245 <kcn_sockaddr_aton+0xf5>
mov    0x2d73(%rip),%rax
mov    (%rax),%ecx
mov    0x2d82(%rip),%rax
mov    %ecx,(%rax)
jmp    126e <kcn_sockaddr_aton+0x11e>
mov    -0x4c(%rbp),%eax
mov    0x2d91(%rip),%rcx
cmp    (%rcx),%eax
je     1269 <kcn_sockaddr_aton+0x119>
mov    0x2d5a(%rip),%rax
mov    (%rax),%ecx
mov    0x2d59(%rip),%rax
mov    %ecx,(%rax)
jmp    126e <kcn_sockaddr_aton+0x11e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kcn_sockaddr_aton>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0x8(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1060 <getaddrinfo@plt>
test   %eax,%eax
je     11d2 <kcn_sockaddr_aton+0x82>
mov    %eax,%ecx
mov    0x2e10(%rip),%rax
cmp    (%rax),%ecx
jne    1215 <kcn_sockaddr_aton+0xc5>
mov    0x2de5(%rip),%rax
mov    (%rax),%eax
mov    0x2df4(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
jmp    1234 <kcn_sockaddr_aton+0xe4>
xor    %edi,%edi
cmpq   $0x0,(%rsp)
setne  %dil
call   1040 <assert@plt>
mov    (%rsp),%rax
xor    %edi,%edi
cmpl   $0x5,(%rax)
setb   %dil
call   1040 <assert@plt>
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %r14,%rdi
call   1050 <memcpy@plt>
mov    (%rsp),%rdi
call   1070 <freeaddrinfo@plt>
mov    $0x1,%eax
jmp    1234 <kcn_sockaddr_aton+0xe4>
mov    0x2dc4(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%ecx
je     1234 <kcn_sockaddr_aton+0xe4>
mov    0x2d8f(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2d8e(%rip),%rdx
mov    %ecx,(%rdx)
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.addrinfo = type { i32, i32, i64, i64, i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@EAI_AGAIN = global i32 0
@EAGAIN = global i32 0
@errno = global i32 0
@EAI_SYSTEM = global i32 0
@EINVAL = global i32 0
define i32 @kcn_sockaddr_aton(%struct.sockaddr_storage* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr_storage*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.addrinfo
%9 = alloca %struct.addrinfo*
%10 = alloca i32
store %struct.sockaddr_storage* %0, %struct.sockaddr_storage** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%11 = call i32 @memset(%struct.addrinfo* %8, i32 0, i32 32)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 5
store i32 %12, i32* %13
%14 = load i32, i32* @SOCK_STREAM
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 4
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 3
store i64 0, i64* %16
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 2
store i64 0, i64* %17
%18 = load i8*, i8** %6
%19 = load i8*, i8** %7
%20 = call i32 @getaddrinfo(i8* %18, i8* %19, %struct.addrinfo* %8, %struct.addrinfo** %9)
store i32 %20, i32* %10
%21 = load i32, i32* %10
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %3
br label %46
24: ; preds = %3
%25 = load %struct.addrinfo*, %struct.addrinfo** %9
%26 = icmp ne %struct.addrinfo* %25, null
%27 = zext i1 %26 to i32
%28 = call i32 @assert(i32 %27)
%29 = load %struct.addrinfo*, %struct.addrinfo** %9
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = sext i32 %31 to i64
%33 = icmp uge i64 4, %32
%34 = zext i1 %33 to i32
%35 = call i32 @assert(i32 %34)
%36 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%37 = load %struct.addrinfo*, %struct.addrinfo** %9
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i32 0, i32 1
%39 = load i32, i32* %38
%40 = load %struct.addrinfo*, %struct.addrinfo** %9
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 @memcpy(%struct.sockaddr_storage* %36, i32 %39, i32 %42)
%44 = load %struct.addrinfo*, %struct.addrinfo** %9
%45 = call i32 @freeaddrinfo(%struct.addrinfo* %44)
store i32 1, i32* %4
br label %60
46: ; preds = %23
%47 = load i32, i32* %10
%48 = load i32, i32* @EAI_AGAIN
%49 = icmp eq i32 %47, %48
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = load i32, i32* @EAGAIN
store i32 %51, i32* @errno
br label %59
52: ; preds = %46
%53 = load i32, i32* %10
%54 = load i32, i32* @EAI_SYSTEM
%55 = icmp ne i32 %53, %54
br i1 %55, label %56, label %58
56: ; preds = %52
%57 = load i32, i32* @EINVAL
store i32 %57, i32* @errno
br label %58
58: ; preds = %56, %52
br label %59
59: ; preds = %58, %50
store i32 0, i32* %4
br label %60
60: ; preds = %59, %24
%61 = load i32, i32* %4
ret i32 %61
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.addrinfo = type { i32, i32, i64, i64, i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@EAI_AGAIN = global i32 0
@EAGAIN = global i32 0
@errno = global i32 0
@EAI_SYSTEM = global i32 0
@EINVAL = global i32 0
define i32 @kcn_sockaddr_aton(%struct.sockaddr_storage* %0, i8* %1, i8* %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 @memset(%struct.addrinfo* %4, i32 0, i32 32)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 5
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 2
%14 = bitcast i64* %13 to i8*
%15 = call i32 @getaddrinfo(i8* %1, i8* %2, %struct.addrinfo* %4, %struct.addrinfo** %5)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %36
17: ; preds = %3
%18 = load %struct.addrinfo*, %struct.addrinfo** %5
%19 = icmp ne %struct.addrinfo* %18, null
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = load %struct.addrinfo*, %struct.addrinfo** %5
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 0
%24 = load i32, i32* %23
%25 = icmp ult i32 %24, 5
%26 = zext i1 %25 to i32
%27 = call i32 @assert(i32 %26)
%28 = load %struct.addrinfo*, %struct.addrinfo** %5
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 1
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @memcpy(%struct.sockaddr_storage* %0, i32 %30, i32 %32)
%34 = load %struct.addrinfo*, %struct.addrinfo** %5
%35 = call i32 @freeaddrinfo(%struct.addrinfo* %34)
br label %46
36: ; preds = %3
%37 = load i32, i32* @EAI_AGAIN
%38 = icmp eq i32 %15, %37
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = load i32, i32* @EAGAIN
store i32 %40, i32* @errno
br label %46
41: ; preds = %36
%42 = load i32, i32* @EAI_SYSTEM
%43 = icmp eq i32 %15, %42
br i1 %43, label %46, label %44
44: ; preds = %41
%45 = load i32, i32* @EINVAL
store i32 %45, i32* @errno
br label %46
46: ; preds = %39, %44, %41, %17
%47 = phi i32 [ 1, %17 ], [ 0, %41 ], [ 0, %44 ], [ 0, %39 ]
ret i32 %47
}