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

/* Variables and functions */
int /*<<< orphan*/ AI_NUMERICHOST;
int addr_sa_to_xaddr(int /*<<< orphan*/ *, int /*<<< orphan*/, struct xaddr *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(char const *, int /*<<< orphan*/ *, struct addrinfo *,
                       struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, char, int);
int addr_pton(const char *p, struct xaddr *n) {
  struct addrinfo hints, *ai;
  memset(&hints, '\0', sizeof(hints));
  hints.ai_flags = AI_NUMERICHOST;
  if (p == NULL || getaddrinfo(p, NULL, &hints, &ai) != 0)
    return (-1);
  if (ai == NULL || ai->ai_addr == NULL)
    return (-1);
  if (n != NULL && addr_sa_to_xaddr(ai->ai_addr, ai->ai_addrlen, n) == -1) {
    freeaddrinfo(ai);
    return (-1);
  }
  freeaddrinfo(ai);
  return (0);
}


x86 assembly (clang, -O0):
<addr_pton>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
mov    0x2e81(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1196 <addr_pton+0x56>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1050 <getaddrinfo@plt>
cmp    $0x0,%rax
je     11a2 <addr_pton+0x62>
movl   $0xffffffff,-0x4(%rbp)
jmp    1218 <addr_pton+0xd8>
cmpq   $0x0,-0x38(%rbp)
je     11bc <addr_pton+0x7c>
mov    -0x38(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    11c8 <addr_pton+0x88>
movl   $0xffffffff,-0x4(%rbp)
jmp    1218 <addr_pton+0xd8>
cmpq   $0x0,-0x18(%rbp)
je     1208 <addr_pton+0xc8>
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x38(%rbp),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rdx
call   1040 <addr_sa_to_xaddr@plt>
cmp    $0xffffffff,%eax
jne    1208 <addr_pton+0xc8>
mov    -0x38(%rbp),%rdi
call   1060 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1218 <addr_pton+0xd8>
mov    -0x38(%rbp),%rdi
call   1060 <freeaddrinfo@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<addr_pton>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
mov    0x2e82(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    $0xffffffff,%ebp
test   %rbx,%rbx
je     118d <addr_pton+0x4d>
lea    0x8(%rsp),%rdx
mov    %rsp,%rcx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <getaddrinfo@plt>
test   %rax,%rax
je     1198 <addr_pton+0x58>
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    (%rsp),%rdi
test   %rdi,%rdi
je     118d <addr_pton+0x4d>
mov    0x8(%rdi),%rax
test   %rax,%rax
je     118d <addr_pton+0x4d>
test   %r14,%r14
je     11c5 <addr_pton+0x85>
mov    (%rdi),%esi
mov    %rax,%rdi
mov    %r14,%rdx
call   1040 <addr_sa_to_xaddr@plt>
mov    (%rsp),%rdi
cmp    $0xffffffff,%eax
je     11c9 <addr_pton+0x89>
xor    %ebp,%ebp
jmp    11ce <addr_pton+0x8e>
mov    $0xffffffff,%ebp
call   1060 <freeaddrinfo@plt>
jmp    118d <addr_pton+0x4d>

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

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