#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;
};
struct evhttp_bound_socket {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ saddr;
typedef int /*<<< orphan*/ evutil_socket_t;

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int /*<<< orphan*/ evhttp_bound_socket_get_fd(struct evhttp_bound_socket *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int getsockname(int /*<<< orphan*/, struct sockaddr *, int *);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int lport(struct evhttp_bound_socket *sock) {
  struct sockaddr_storage saddr;
  socklen_t alen;
  evutil_socket_t fd;
  int port;
  fd = evhttp_bound_socket_get_fd(sock);
  alen = sizeof(saddr);
  if (getsockname(fd, (struct sockaddr *)&saddr, &alen) == -1) {
    perror("getsockname()");
    return -1;
  }
  switch (saddr.ss_family) {
  case AF_INET:
    port = ntohs(((struct sockaddr_in *)&saddr)->sin_port);
    break;
  case AF_INET6:
    port = ntohs(((struct sockaddr_in6 *)&saddr)->sin6_port);
    break;
  default:
    fprintf(stderr, "Unsupported address family: %d\n", saddr.ss_family);
    port = -1;
    break;
  }
  return port;
}


x86 assembly (clang, -O0):
<lport>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <evhttp_bound_socket_get_fd@plt>
mov    %eax,-0x20(%rbp)
movl   $0x4,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1070 <getsockname@plt>
cmp    $0xffffffff,%eax
jne    11a0 <lport+0x50>
lea    0xe71(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120e <lport+0xbe>
mov    -0x18(%rbp),%eax
mov    %eax,-0x28(%rbp)
sub    $0x80,%eax
je     11d9 <lport+0x89>
jmp    11b6 <lport+0x66>
mov    -0x28(%rbp),%eax
sub    $0x81,%eax
jne    11e9 <lport+0x99>
jmp    11c9 <lport+0x79>
mov    -0x18(%rbp),%edi
call   1050 <ntohs@plt>
mov    %eax,-0x24(%rbp)
jmp    1208 <lport+0xb8>
mov    -0x18(%rbp),%edi
call   1050 <ntohs@plt>
mov    %eax,-0x24(%rbp)
jmp    1208 <lport+0xb8>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
lea    0xe12(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lport>:
push   %rax
call   1040 <evhttp_bound_socket_get_fd@plt>
movl   $0x4,0x4(%rsp)
mov    %rsp,%rsi
lea    0x4(%rsp),%rdx
mov    %eax,%edi
call   1070 <getsockname@plt>
cmp    $0xffffffff,%eax
je     11b4 <lport+0x64>
mov    (%rsp),%edx
cmp    $0x80,%edx
je     118c <lport+0x3c>
cmp    $0x81,%edx
jne    1198 <lport+0x48>
mov    $0x81,%edi
jmp    1191 <lport+0x41>
mov    $0x80,%edi
call   1050 <ntohs@plt>
pop    %rcx
ret
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
lea    0xe66(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret
lea    0xe45(%rip),%rdi
call   1060 <perror@plt>
jmp    11ad <lport+0x5d>

LLVM IR (-O0):
%struct.evhttp_bound_socket = type { i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@.str = [14 x i8] c"getsockname()\00"
@stderr = global i32 0
@.str.1 = [32 x i8] c"Unsupported address family: %d\0A\00"
define i32 @lport(%struct.evhttp_bound_socket* %0) {
%2 = alloca i32
%3 = alloca %struct.evhttp_bound_socket*
%4 = alloca %struct.sockaddr_storage
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store %struct.evhttp_bound_socket* %0, %struct.evhttp_bound_socket** %3
%8 = load %struct.evhttp_bound_socket*, %struct.evhttp_bound_socket** %3
%9 = call i32 @evhttp_bound_socket_get_fd(%struct.evhttp_bound_socket* %8)
store i32 %9, i32* %6
store i32 4, i32* %5
%10 = load i32, i32* %6
%11 = bitcast %struct.sockaddr_storage* %4 to %struct.sockaddr*
%12 = call i32 @getsockname(i32 %10, %struct.sockaddr* %11, i32* %5)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %36
16: ; preds = %1
%17 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %4, i32 0, i32 0
%18 = load i32, i32* %17
switch i32 %18, label %29 [
i32 129, label %19
i32 128, label %24
]
19: ; preds = %16
%20 = bitcast %struct.sockaddr_storage* %4 to %struct.sockaddr_in*
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %20, i32 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @ntohs(i32 %22)
store i32 %23, i32* %7
br label %34
24: ; preds = %16
%25 = bitcast %struct.sockaddr_storage* %4 to %struct.sockaddr_in6*
%26 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %25, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @ntohs(i32 %27)
store i32 %28, i32* %7
br label %34
29: ; preds = %16
%30 = load i32, i32* @stderr
%31 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %4, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %32)
store i32 -1, i32* %7
br label %34
34: ; preds = %29, %24, %19
%35 = load i32, i32* %7
store i32 %35, i32* %2
br label %36
36: ; preds = %34, %14
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
%struct.evhttp_bound_socket = type { i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [14 x i8] c"getsockname()\00"
@stderr = global i32 0
@.str.1 = [32 x i8] c"Unsupported address family: %d\0A\00"
define i32 @lport(%struct.evhttp_bound_socket* %0) {
%2 = alloca %struct.sockaddr_storage
%3 = alloca i32
%4 = bitcast %struct.sockaddr_storage* %2 to i8*
%5 = bitcast i32* %3 to i8*
%6 = tail call i32 @evhttp_bound_socket_get_fd(%struct.evhttp_bound_socket* %0)
store i32 4, i32* %3
%7 = bitcast %struct.sockaddr_storage* %2 to %struct.sockaddr*
%8 = call i32 @getsockname(i32 %6, %struct.sockaddr* %7, i32* %3)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %22
12: ; preds = %1
%13 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %2, i64 0, i32 0
%14 = load i32, i32* %13
switch i32 %14, label %19 [
i32 129, label %15
i32 128, label %17
]
15: ; preds = %12
%16 = call i32 @ntohs(i32 129)
br label %22
17: ; preds = %12
%18 = call i32 @ntohs(i32 128)
br label %22
19: ; preds = %12
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %14)
br label %22
22: ; preds = %15, %17, %19, %10
%23 = phi i32 [ -1, %10 ], [ -1, %19 ], [ %18, %17 ], [ %16, %15 ]
ret i32 %23
}