#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_in6 {
  unsigned short sin6_port;
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  unsigned short sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct addrinfo {
  int ai_family;
  scalar_t__ ai_addr;
};

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int /*<<< orphan*/ EAFNOSUPPORT;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ inet_ntop(int, void *, char *, size_t);
int ntohs(unsigned short);
bool addrinfo_addr_port(struct addrinfo *ai, char *addr, size_t len,
                        int *port) {
  void *addr_raw;
  unsigned short port_raw;
  switch (ai->ai_family) {
  case AF_INET:
    addr_raw = &((struct sockaddr_in *)ai->ai_addr)->sin_addr;
    port_raw = ((struct sockaddr_in *)ai->ai_addr)->sin_port;
    break;
  case AF_INET6:
    addr_raw = &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr;
    port_raw = ((struct sockaddr_in6 *)ai->ai_addr)->sin6_port;
    break;
  default:
    errno = EAFNOSUPPORT;
    return false;
  }
  if (!inet_ntop(ai->ai_family, addr_raw, addr, len))
    return false;
  *port = ntohs(port_raw);
  return true;
}


x86 assembly (clang, -O0):
<addrinfo_addr_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
sub    $0x80,%eax
je     1188 <addrinfo_addr_port+0x68>
jmp    1151 <addrinfo_addr_port+0x31>
mov    -0x38(%rbp),%eax
sub    $0x81,%eax
jne    11ac <addrinfo_addr_port+0x8c>
jmp    1164 <addrinfo_addr_port+0x44>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
add    $0x4,%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%ax
mov    %ax,-0x32(%rbp)
jmp    11ca <addrinfo_addr_port+0xaa>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
add    $0x4,%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%ax
mov    %ax,-0x32(%rbp)
jmp    11ca <addrinfo_addr_port+0xaa>
mov    0x2e25(%rip),%rax
mov    (%rax),%ecx
mov    0x2e24(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    120e <addrinfo_addr_port+0xee>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1030 <inet_ntop@plt>
cmp    $0x0,%eax
jne    11f6 <addrinfo_addr_port+0xd6>
movl   $0x0,-0x4(%rbp)
jmp    120e <addrinfo_addr_port+0xee>
movzwl -0x32(%rbp),%edi
call   1040 <ntohs@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<addrinfo_addr_port>:
push   %rbp
push   %rbx
push   %rax
mov    %rcx,%rbx
mov    %rdx,%rcx
mov    %rsi,%rdx
mov    %rdi,%rax
mov    (%rdi),%edi
cmp    $0x80,%edi
je     1141 <addrinfo_addr_port+0x21>
cmp    $0x81,%edi
jne    1166 <addrinfo_addr_port+0x46>
mov    0x8(%rax),%rax
lea    0x4(%rax),%rsi
movzwl (%rax),%ebp
call   1030 <inet_ntop@plt>
test   %eax,%eax
je     1178 <addrinfo_addr_port+0x58>
movzwl %bp,%edi
call   1040 <ntohs@plt>
mov    %eax,(%rbx)
mov    $0x1,%eax
jmp    117a <addrinfo_addr_port+0x5a>
mov    0x2e6b(%rip),%rax
mov    (%rax),%eax
mov    0x2e6a(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i64 }
%struct.sockaddr_in = type { i16, i32 }
%struct.sockaddr_in6 = type { i16, i32 }
@EAFNOSUPPORT = global i32 0
@errno = global i32 0
define i32 @addrinfo_addr_port(%struct.addrinfo* %0, i8* %1, i64 %2, i32* %3) {
%5 = alloca i32
%6 = alloca %struct.addrinfo*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i8*
%11 = alloca i16
store %struct.addrinfo* %0, %struct.addrinfo** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32* %3, i32** %9
%12 = load %struct.addrinfo*, %struct.addrinfo** %6
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %12, i32 0, i32 0
%14 = load i32, i32* %13
switch i32 %14, label %41 [
i32 129, label %15
i32 128, label %28
]
15: ; preds = %4
%16 = load %struct.addrinfo*, %struct.addrinfo** %6
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i32 0, i32 1
%18 = load i64, i64* %17
%19 = inttoptr i64 %18 to %struct.sockaddr_in*
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %19, i32 0, i32 1
%21 = bitcast i32* %20 to i8*
store i8* %21, i8** %10
%22 = load %struct.addrinfo*, %struct.addrinfo** %6
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i32 0, i32 1
%24 = load i64, i64* %23
%25 = inttoptr i64 %24 to %struct.sockaddr_in*
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %25, i32 0, i32 0
%27 = load i16, i16* %26
store i16 %27, i16* %11
br label %43
28: ; preds = %4
%29 = load %struct.addrinfo*, %struct.addrinfo** %6
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i32 0, i32 1
%31 = load i64, i64* %30
%32 = inttoptr i64 %31 to %struct.sockaddr_in6*
%33 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %32, i32 0, i32 1
%34 = bitcast i32* %33 to i8*
store i8* %34, i8** %10
%35 = load %struct.addrinfo*, %struct.addrinfo** %6
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i32 0, i32 1
%37 = load i64, i64* %36
%38 = inttoptr i64 %37 to %struct.sockaddr_in6*
%39 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %38, i32 0, i32 0
%40 = load i16, i16* %39
store i16 %40, i16* %11
br label %43
41: ; preds = %4
%42 = load i32, i32* @EAFNOSUPPORT
store i32 %42, i32* @errno
store i32 0, i32* %5
br label %57
43: ; preds = %28, %15
%44 = load %struct.addrinfo*, %struct.addrinfo** %6
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %44, i32 0, i32 0
%46 = load i32, i32* %45
%47 = load i8*, i8** %10
%48 = load i8*, i8** %7
%49 = load i64, i64* %8
%50 = call i32 @inet_ntop(i32 %46, i8* %47, i8* %48, i64 %49)
%51 = icmp ne i32 %50, 0
br i1 %51, label %53, label %52
52: ; preds = %43
store i32 0, i32* %5
br label %57
53: ; preds = %43
%54 = load i16, i16* %11
%55 = call i32 @ntohs(i16 zeroext %54)
%56 = load i32*, i32** %9
store i32 %55, i32* %56
store i32 1, i32* %5
br label %57
57: ; preds = %53, %52, %41
%58 = load i32, i32* %5
ret i32 %58
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i64 }
%struct.sockaddr_in = type { i16, i32 }
%struct.sockaddr_in6 = type { i16, i32 }
@EAFNOSUPPORT = global i32 0
@errno = global i32 0
define i32 @addrinfo_addr_port(%struct.addrinfo* nocapture %0, i8* %1, i64 %2, i32* nocapture writeonly %3) {
%5 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 0
%6 = load i32, i32* %5
switch i32 %6, label %19 [
i32 129, label %7
i32 128, label %13
]
7: ; preds = %4
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 1
%9 = load i64, i64* %8
%10 = inttoptr i64 %9 to %struct.sockaddr_in*
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i64 0, i32 1
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i64 0, i32 0
br label %21
13: ; preds = %4
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 1
%15 = load i64, i64* %14
%16 = inttoptr i64 %15 to %struct.sockaddr_in6*
%17 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %16, i64 0, i32 1
%18 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %16, i64 0, i32 0
br label %21
19: ; preds = %4
%20 = load i32, i32* @EAFNOSUPPORT
store i32 %20, i32* @errno
br label %30
21: ; preds = %13, %7
%22 = phi i32* [ %17, %13 ], [ %11, %7 ]
%23 = phi i16* [ %18, %13 ], [ %12, %7 ]
%24 = load i16, i16* %23
%25 = bitcast i32* %22 to i8*
%26 = tail call i32 @inet_ntop(i32 %6, i8* %25, i8* %1, i64 %2)
%27 = icmp eq i32 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %21
%29 = tail call i32 @ntohs(i16 zeroext %24)
store i32 %29, i32* %3
br label %30
30: ; preds = %21, %28, %19
%31 = phi i32 [ 0, %19 ], [ 1, %28 ], [ 0, %21 ]
ret i32 %31
}