#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 {
  int dummy;
};
struct addrinfo {
  scalar_t__ ai_addrlen;
  int /*<<< orphan*/ *ai_addr;
  int /*<<< orphan*/ ai_flags;
};
typedef scalar_t__ socklen_t;
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AI_NUMERICHOST;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(char const *, char const *, struct addrinfo *,
                       struct addrinfo **);
int /*<<< orphan*/ memcpy(struct sockaddr *, int /*<<< orphan*/ **, scalar_t__);
int /*<<< orphan*/ memset(struct addrinfo *, char, int);
int addr_sa_pton(const char *h, const char *s, struct sockaddr *sa,
                 socklen_t slen) {
  struct addrinfo hints, *ai;
  memset(&hints, '\0', sizeof(hints));
  hints.ai_flags = AI_NUMERICHOST;
  if (h == NULL || getaddrinfo(h, s, &hints, &ai) != 0)
    return (-1);
  if (ai == NULL || ai->ai_addr == NULL)
    return (-1);
  if (sa != NULL) {
    if (slen < ai->ai_addrlen)
      return (-1);
    memcpy(sa, &ai->ai_addr, ai->ai_addrlen);
  }
  freeaddrinfo(ai);
  return (0);
}


x86 assembly (clang, -O0):
<addr_sa_pton>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
mov    0x2e79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     119e <addr_sa_pton+0x5e>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1050 <getaddrinfo@plt>
cmp    $0x0,%rax
je     11aa <addr_sa_pton+0x6a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1220 <addr_sa_pton+0xe0>
cmpq   $0x0,-0x48(%rbp)
je     11c4 <addr_sa_pton+0x84>
mov    -0x48(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    11d0 <addr_sa_pton+0x90>
movl   $0xffffffff,-0x4(%rbp)
jmp    1220 <addr_sa_pton+0xe0>
cmpq   $0x0,-0x20(%rbp)
je     1210 <addr_sa_pton+0xd0>
mov    -0x28(%rbp),%rax
mov    -0x48(%rbp),%rcx
cmp    (%rcx),%rax
jge    11f8 <addr_sa_pton+0xb8>
movl   $0xffffffff,-0x4(%rbp)
jmp    1220 <addr_sa_pton+0xe0>
mov    -0x20(%rbp),%rdi
mov    -0x48(%rbp),%rsi
add    $0x8,%rsi
mov    -0x48(%rbp),%rax
mov    (%rax),%rdx
call   1040 <memcpy@plt>
mov    -0x48(%rbp),%rdi
call   1060 <freeaddrinfo@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<addr_sa_pton>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
mov    0x2e78(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    $0xffffffff,%r12d
test   %rbp,%rbp
je     1199 <addr_sa_pton+0x59>
lea    0x8(%rsp),%rdx
mov    %rsp,%rcx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1050 <getaddrinfo@plt>
test   %rax,%rax
je     11a9 <addr_sa_pton+0x69>
mov    %r12d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%rsi
test   %rsi,%rsi
je     1199 <addr_sa_pton+0x59>
cmpq   $0x0,0x8(%rsi)
je     1199 <addr_sa_pton+0x59>
test   %r14,%r14
je     11d6 <addr_sa_pton+0x96>
mov    (%rsi),%rdx
cmp    %r15,%rdx
jg     1199 <addr_sa_pton+0x59>
add    $0x8,%rsi
mov    %r14,%rdi
call   1040 <memcpy@plt>
mov    (%rsp),%rsi
mov    %rsi,%rdi
call   1060 <freeaddrinfo@plt>
xor    %r12d,%r12d
jmp    1199 <addr_sa_pton+0x59>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.addrinfo = type { i64, i32*, i32 }
@AI_NUMERICHOST = global i32 0
define i32 @addr_sa_pton(i8* %0, i8* %1, %struct.sockaddr* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.sockaddr*
%9 = alloca i64
%10 = alloca %struct.addrinfo
%11 = alloca %struct.addrinfo*
store i8* %0, i8** %6
store i8* %1, i8** %7
store %struct.sockaddr* %2, %struct.sockaddr** %8
store i64 %3, i64* %9
%12 = call i32 @memset(%struct.addrinfo* %10, i8 signext 0, i32 24)
%13 = load i32, i32* @AI_NUMERICHOST
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 2
store i32 %13, i32* %14
%15 = load i8*, i8** %6
%16 = icmp eq i8* %15, null
br i1 %16, label %22, label %17
17: ; preds = %4
%18 = load i8*, i8** %6
%19 = load i8*, i8** %7
%20 = call i64 @getaddrinfo(i8* %18, i8* %19, %struct.addrinfo* %10, %struct.addrinfo** %11)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %17, %4
store i32 -1, i32* %5
br label %53
23: ; preds = %17
%24 = load %struct.addrinfo*, %struct.addrinfo** %11
%25 = icmp eq %struct.addrinfo* %24, null
br i1 %25, label %31, label %26
26: ; preds = %23
%27 = load %struct.addrinfo*, %struct.addrinfo** %11
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i32 0, i32 1
%29 = load i32*, i32** %28
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %32
31: ; preds = %26, %23
store i32 -1, i32* %5
br label %53
32: ; preds = %26
%33 = load %struct.sockaddr*, %struct.sockaddr** %8
%34 = icmp ne %struct.sockaddr* %33, null
br i1 %34, label %35, label %50
35: ; preds = %32
%36 = load i64, i64* %9
%37 = load %struct.addrinfo*, %struct.addrinfo** %11
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i32 0, i32 0
%39 = load i64, i64* %38
%40 = icmp slt i64 %36, %39
br i1 %40, label %41, label %42
41: ; preds = %35
store i32 -1, i32* %5
br label %53
42: ; preds = %35
%43 = load %struct.sockaddr*, %struct.sockaddr** %8
%44 = load %struct.addrinfo*, %struct.addrinfo** %11
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %44, i32 0, i32 1
%46 = load %struct.addrinfo*, %struct.addrinfo** %11
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %46, i32 0, i32 0
%48 = load i64, i64* %47
%49 = call i32 @memcpy(%struct.sockaddr* %43, i32** %45, i64 %48)
br label %50
50: ; preds = %42, %32
%51 = load %struct.addrinfo*, %struct.addrinfo** %11
%52 = call i32 @freeaddrinfo(%struct.addrinfo* %51)
store i32 0, i32* %5
br label %53
53: ; preds = %50, %41, %31, %22
%54 = load i32, i32* %5
ret i32 %54
}

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