#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_storage {
  int ss_family;
};
struct sockaddr_in6 {
  int /*<<< orphan*/ sin6_port;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int getsockname(int, struct sockaddr *, int *);
int ntohs(int /*<<< orphan*/);
int idrnet_sockaddr_port(int sockfd) {
  struct sockaddr_storage address;
  socklen_t addrlen = sizeof(struct sockaddr_storage);
  int res = getsockname(sockfd, (struct sockaddr *)&address, &addrlen);
  if (res < 0) {
    return -1;
  }
  switch (address.ss_family) {
  case AF_INET:
    return ntohs(((struct sockaddr_in *)&address)->sin_port);
  case AF_INET6:
    return ntohs(((struct sockaddr_in6 *)&address)->sin6_port);
  default:
    return -1;
  }
}


x86 assembly (clang, -O0):
<idrnet_sockaddr_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x4,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1040 <getsockname@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    115b <idrnet_sockaddr_port+0x3b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ab <idrnet_sockaddr_port+0x8b>
mov    -0x10(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0x80,%eax
je     1194 <idrnet_sockaddr_port+0x74>
jmp    1171 <idrnet_sockaddr_port+0x51>
mov    -0x1c(%rbp),%eax
sub    $0x81,%eax
jne    11a4 <idrnet_sockaddr_port+0x84>
jmp    1184 <idrnet_sockaddr_port+0x64>
mov    -0x10(%rbp),%edi
call   1030 <ntohs@plt>
mov    %eax,-0x4(%rbp)
jmp    11ab <idrnet_sockaddr_port+0x8b>
mov    -0x10(%rbp),%edi
call   1030 <ntohs@plt>
mov    %eax,-0x4(%rbp)
jmp    11ab <idrnet_sockaddr_port+0x8b>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<idrnet_sockaddr_port>:
push   %rax
movl   $0x4,0x4(%rsp)
mov    %rsp,%rsi
lea    0x4(%rsp),%rdx
call   1040 <getsockname@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
test   %ecx,%ecx
js     115e <idrnet_sockaddr_port+0x3e>
mov    (%rsp),%edi
cmp    $0x81,%edi
je     1159 <idrnet_sockaddr_port+0x39>
cmp    $0x80,%edi
jne    115e <idrnet_sockaddr_port+0x3e>
mov    $0x80,%edi
call   1030 <ntohs@plt>
pop    %rcx
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
define i32 @idrnet_sockaddr_port(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_storage
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 4, i32* %5
%7 = load i32, i32* %3
%8 = bitcast %struct.sockaddr_storage* %4 to %struct.sockaddr*
%9 = call i32 @getsockname(i32 %7, %struct.sockaddr* %8, i32* %5)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 -1, i32* %2
br label %27
13: ; preds = %1
%14 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %4, i32 0, i32 0
%15 = load i32, i32* %14
switch i32 %15, label %26 [
i32 129, label %16
i32 128, label %21
]
16: ; preds = %13
%17 = bitcast %struct.sockaddr_storage* %4 to %struct.sockaddr_in*
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %17, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @ntohs(i32 %19)
store i32 %20, i32* %2
br label %27
21: ; preds = %13
%22 = bitcast %struct.sockaddr_storage* %4 to %struct.sockaddr_in6*
%23 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %22, i32 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 @ntohs(i32 %24)
store i32 %25, i32* %2
br label %27
26: ; preds = %13
store i32 -1, i32* %2
br label %27
27: ; preds = %26, %21, %16, %12
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
define i32 @idrnet_sockaddr_port(i32 %0) {
%2 = alloca %struct.sockaddr_storage
%3 = alloca i32
%4 = bitcast %struct.sockaddr_storage* %2 to i8*
%5 = bitcast i32* %3 to i8*
store i32 4, i32* %3
%6 = bitcast %struct.sockaddr_storage* %2 to %struct.sockaddr*
%7 = call i32 @getsockname(i32 %0, %struct.sockaddr* %6, i32* %3)
%8 = icmp slt i32 %7, 0
br i1 %8, label %16, label %9
9: ; preds = %1
%10 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %2, i64 0, i32 0
%11 = load i32, i32* %10
switch i32 %11, label %16 [
i32 129, label %13
i32 128, label %12
]
12: ; preds = %9
br label %13
13: ; preds = %9, %12
%14 = phi i32 [ 128, %12 ], [ %11, %9 ]
%15 = call i32 @ntohs(i32 %14)
br label %16
16: ; preds = %13, %9, %1
%17 = phi i32 [ -1, %1 ], [ -1, %9 ], [ %15, %13 ]
ret i32 %17
}