#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 {
  int sin6_port;
};
struct sockaddr_in {
  int sin_port;
};
struct sockaddr {
  scalar_t__ sa_family;
};
typedef int in_port_t;

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
in_port_t chitcp_get_addr_port(struct sockaddr *addr) {
  if (!(addr->sa_family == AF_INET || addr->sa_family == AF_INET6))
    return -1;
  if (addr->sa_family == AF_INET) {
    struct sockaddr_in *sin = (struct sockaddr_in *)addr;
    return sin->sin_port;
  } else if (addr->sa_family == AF_INET6) {
    struct sockaddr_in6 *sin = (struct sockaddr_in6 *)addr;
    return sin->sin6_port;
  }
  return 0;
}


x86 assembly (clang, -O0):
<chitcp_get_addr_port>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2ed2(%rip),%rcx
cmp    (%rcx),%rax
je     1142 <chitcp_get_addr_port+0x42>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2eb3(%rip),%rcx
cmp    (%rcx),%rax
je     1142 <chitcp_get_addr_port+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a8 <chitcp_get_addr_port+0xa8>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e98(%rip),%rcx
cmp    (%rcx),%rax
jne    116f <chitcp_get_addr_port+0x6f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11a8 <chitcp_get_addr_port+0xa8>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e63(%rip),%rcx
cmp    (%rcx),%rax
jne    119c <chitcp_get_addr_port+0x9c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11a8 <chitcp_get_addr_port+0xa8>
jmp    11a1 <chitcp_get_addr_port+0xa1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<chitcp_get_addr_port>:
mov    (%rdi),%rcx
mov    0x2ede(%rip),%rax
mov    (%rax),%rdi
cmp    %rdi,%rcx
sete   %sil
mov    0x2ec5(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
sete   %dl
cmove  %ecx,%eax
or     %sil,%dl
mov    $0xffffffff,%edx
cmovne %ecx,%edx
cmove  %edx,%eax
cmp    %rdi,%rcx
cmove  %edx,%eax
ret

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

LLVM IR (-O2):
%struct.sockaddr = type { i64 }
@AF_INET = global i64 0
@AF_INET6 = global i64 0
define i32 @chitcp_get_addr_port(%struct.sockaddr* nocapture %0) {
%2 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%3 = load i64, i64* %2
%4 = load i64, i64* @AF_INET
%5 = icmp eq i64 %3, %4
%6 = load i64, i64* @AF_INET6
%7 = icmp eq i64 %3, %6
%8 = select i1 %5, i1 true, i1 %7
%9 = trunc i64 %3 to i32
%10 = xor i1 %8, true
%11 = or i1 %5, %10
%12 = select i1 %8, i32 %9, i32 -1
%13 = select i1 %7, i32 %9, i32 0
%14 = select i1 %11, i32 %12, i32 %13
ret i32 %14
}