#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 /*<<< orphan*/ sin6_port;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int sa_family;
};

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
unsigned short ntohs(int /*<<< orphan*/);
unsigned short sockaddr_port(struct sockaddr *addr) {
  switch (addr->sa_family) {
  case AF_INET: {
    struct sockaddr_in *sin = (void *)addr;
    return ntohs(sin->sin_port);
  }
  case AF_INET6: {
    struct sockaddr_in6 *sin = (void *)addr;
    return ntohs(sin->sin6_port);
  }
  default:
    return 0;
  }
}


x86 assembly (clang, -O0):
<sockaddr_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
sub    $0x80,%eax
je     1164 <sockaddr_port+0x54>
jmp    1135 <sockaddr_port+0x25>
mov    -0x24(%rbp),%eax
sub    $0x81,%eax
jne    1180 <sockaddr_port+0x70>
jmp    1148 <sockaddr_port+0x38>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1030 <ntohs@plt>
mov    %ax,-0x2(%rbp)
jmp    1186 <sockaddr_port+0x76>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1030 <ntohs@plt>
mov    %ax,-0x2(%rbp)
jmp    1186 <sockaddr_port+0x76>
movw   $0x0,-0x2(%rbp)
movzwl -0x2(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sockaddr_port>:
mov    (%rdi),%edi
cmp    $0x81,%edi
je     1127 <sockaddr_port+0x17>
cmp    $0x80,%edi
jne    112c <sockaddr_port+0x1c>
mov    $0x80,%edi
jmp    1030 <ntohs@plt>
xor    %eax,%eax
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
define zeroext i16 @sockaddr_port(%struct.sockaddr* %0) {
%2 = alloca i16
%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 i32, i32* %7
switch i32 %8, label %25 [
i32 129, label %9
i32 128, label %17
]
9: ; preds = %1
%10 = load %struct.sockaddr*, %struct.sockaddr** %3
%11 = bitcast %struct.sockaddr* %10 to i8*
%12 = bitcast i8* %11 to %struct.sockaddr_in*
store %struct.sockaddr_in* %12, %struct.sockaddr_in** %4
%13 = load %struct.sockaddr_in*, %struct.sockaddr_in** %4
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 0
%15 = load i32, i32* %14
%16 = call zeroext i16 @ntohs(i32 %15)
store i16 %16, i16* %2
br label %26
17: ; preds = %1
%18 = load %struct.sockaddr*, %struct.sockaddr** %3
%19 = bitcast %struct.sockaddr* %18 to i8*
%20 = bitcast i8* %19 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %20, %struct.sockaddr_in6** %5
%21 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %5
%22 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %21, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call zeroext i16 @ntohs(i32 %23)
store i16 %24, i16* %2
br label %26
25: ; preds = %1
store i16 0, i16* %2
br label %26
26: ; preds = %25, %17, %9
%27 = load i16, i16* %2
ret i16 %27
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
define zeroext i16 @sockaddr_port(%struct.sockaddr* nocapture %0) {
%2 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%3 = load i32, i32* %2
switch i32 %3, label %8 [
i32 129, label %5
i32 128, label %4
]
4: ; preds = %1
br label %5
5: ; preds = %1, %4
%6 = phi i32 [ 128, %4 ], [ %3, %1 ]
%7 = tail call zeroext i16 @ntohs(i32 %6)
br label %8
8: ; preds = %5, %1
%9 = phi i16 [ 0, %1 ], [ %7, %5 ]
ret i16 %9
}