#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 {
  scalar_t__ ai_family;
  scalar_t__ ai_addr;
};

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ INET6_ADDRSTRLEN;
int /*<<< orphan*/ inet_ntop(scalar_t__, void *, char *, int /*<<< orphan*/);
int ntohs(unsigned short);
int get_port_and_ip(struct addrinfo *res, char *ip) {
  void *addr;
  unsigned short int port;
  if (res->ai_family == AF_INET) {
    port = ((struct sockaddr_in *)res->ai_addr)->sin_port;
    addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
  } else {
    port = ((struct sockaddr_in6 *)res->ai_addr)->sin6_port;
    addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
  }
  inet_ntop(res->ai_family, addr, ip, INET6_ADDRSTRLEN);
  return ntohs(port);
}


x86 assembly (clang, -O0):
<get_port_and_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    0x2ea2(%rip),%rcx
cmp    (%rcx),%rax
jne    116b <get_port_and_ip+0x4b>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%ax
mov    %ax,-0x1a(%rbp)
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
add    $0x4,%rax
mov    %rax,-0x18(%rbp)
jmp    118a <get_port_and_ip+0x6a>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%ax
mov    %ax,-0x1a(%rbp)
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
add    $0x4,%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    0x2e48(%rip),%rax
mov    (%rax),%ecx
call   1030 <inet_ntop@plt>
movzwl -0x1a(%rbp),%edi
call   1040 <ntohs@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_port_and_ip>:
push   %rbx
mov    %rsi,%rdx
mov    (%rdi),%rax
mov    0x8(%rdi),%rsi
movzwl (%rsi),%ebx
add    $0x4,%rsi
mov    0x2eaf(%rip),%rcx
mov    (%rcx),%ecx
mov    %rax,%rdi
call   1030 <inet_ntop@plt>
mov    %ebx,%edi
pop    %rbx
jmp    1040 <ntohs@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i64, i64 }
%struct.sockaddr_in = type { i16, i32 }
%struct.sockaddr_in6 = type { i16, i32 }
@AF_INET = global i64 0
@INET6_ADDRSTRLEN = global i32 0
define i32 @get_port_and_ip(%struct.addrinfo* %0, i8* %1) {
%3 = alloca %struct.addrinfo*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i16
store %struct.addrinfo* %0, %struct.addrinfo** %3
store i8* %1, i8** %4
%7 = load %struct.addrinfo*, %struct.addrinfo** %3
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 0
%9 = load i64, i64* %8
%10 = load i64, i64* @AF_INET
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %25
12: ; preds = %2
%13 = load %struct.addrinfo*, %struct.addrinfo** %3
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i32 0, i32 1
%15 = load i64, i64* %14
%16 = inttoptr i64 %15 to %struct.sockaddr_in*
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %16, i32 0, i32 0
%18 = load i16, i16* %17
store i16 %18, i16* %6
%19 = load %struct.addrinfo*, %struct.addrinfo** %3
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i32 0, i32 1
%21 = load i64, i64* %20
%22 = inttoptr i64 %21 to %struct.sockaddr_in*
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %22, i32 0, i32 1
%24 = bitcast i32* %23 to i8*
store i8* %24, i8** %5
br label %38
25: ; preds = %2
%26 = load %struct.addrinfo*, %struct.addrinfo** %3
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i32 0, i32 1
%28 = load i64, i64* %27
%29 = inttoptr i64 %28 to %struct.sockaddr_in6*
%30 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %29, i32 0, i32 0
%31 = load i16, i16* %30
store i16 %31, i16* %6
%32 = load %struct.addrinfo*, %struct.addrinfo** %3
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i32 0, i32 1
%34 = load i64, i64* %33
%35 = inttoptr i64 %34 to %struct.sockaddr_in6*
%36 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %35, i32 0, i32 1
%37 = bitcast i32* %36 to i8*
store i8* %37, i8** %5
br label %38
38: ; preds = %25, %12
%39 = load %struct.addrinfo*, %struct.addrinfo** %3
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i32 0, i32 0
%41 = load i64, i64* %40
%42 = load i8*, i8** %5
%43 = load i8*, i8** %4
%44 = load i32, i32* @INET6_ADDRSTRLEN
%45 = call i32 @inet_ntop(i64 %41, i8* %42, i8* %43, i32 %44)
%46 = load i16, i16* %6
%47 = call i32 @ntohs(i16 zeroext %46)
ret i32 %47
}

LLVM IR (-O2):
%struct.addrinfo = type { i64, i64 }
%struct.sockaddr_in = type { i16, i32 }
%struct.sockaddr_in6 = type { i16, i32 }
@AF_INET = global i64 0
@INET6_ADDRSTRLEN = global i32 0
define i32 @get_port_and_ip(%struct.addrinfo* nocapture %0, i8* %1) {
%3 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 0
%4 = load i64, i64* %3
%5 = load i64, i64* @AF_INET
%6 = icmp eq i64 %4, %5
%7 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 1
%8 = load i64, i64* %7
%9 = inttoptr i64 %8 to %struct.sockaddr_in*
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i64 0, i32 0
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i64 0, i32 1
%12 = inttoptr i64 %8 to %struct.sockaddr_in6*
%13 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %12, i64 0, i32 0
%14 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %12, i64 0, i32 1
%15 = select i1 %6, i32* %11, i32* %14
%16 = select i1 %6, i16* %10, i16* %13
%17 = load i16, i16* %16
%18 = bitcast i32* %15 to i8*
%19 = load i32, i32* @INET6_ADDRSTRLEN
%20 = tail call i32 @inet_ntop(i64 %4, i8* %18, i8* %1, i32 %19)
%21 = tail call i32 @ntohs(i16 zeroext %17)
ret i32 %21
}