#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_in {
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ sin;

/* Variables and functions */
int /*<<< orphan*/ errno;
scalar_t__ getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ log_message(char *, char *, char *, int /*<<< orphan*/,
                               int /*<<< orphan*/);
unsigned short ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
unsigned short get_local_port(int fd) {
  struct sockaddr_in sin;
  socklen_t sinsize = sizeof(sin);
  if (getsockname(fd, (struct sockaddr *)&sin, &sinsize)) {
    log_message("%s: %s: %s (errno %d)", "get_local_port", "getsockname",
                strerror(errno), errno);
    return 0;
  }
  return (ntohs(sin.sin_port));
}


x86 assembly (clang, -O0):
<get_local_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   1050 <getsockname@plt>
cmp    $0x0,%rax
je     11ab <get_local_port+0x6b>
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %eax,%ecx
mov    0x2e5d(%rip),%rax
mov    (%rax),%r8d
lea    0xe73(%rip),%rdi
lea    0xe82(%rip),%rsi
lea    0xe8a(%rip),%rdx
call   1040 <log_message@plt>
movw   $0x0,-0x2(%rbp)
jmp    11b7 <get_local_port+0x77>
mov    -0x10(%rbp),%edi
call   1030 <ntohs@plt>
mov    %ax,-0x2(%rbp)
movzwl -0x2(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_local_port>:
push   %rbx
sub    $0x10,%rsp
movl   $0x4,0xc(%rsp)
lea    0x8(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1050 <getsockname@plt>
test   %rax,%rax
je     1192 <get_local_port+0x52>
mov    0x2e78(%rip),%rbx
mov    (%rbx),%edi
call   1060 <strerror@plt>
mov    (%rbx),%r8d
lea    0xe87(%rip),%rdi
lea    0xe96(%rip),%rsi
lea    0xe9e(%rip),%rdx
mov    %eax,%ecx
call   1040 <log_message@plt>
xor    %eax,%eax
jmp    119b <get_local_port+0x5b>
mov    0x8(%rsp),%edi
call   1030 <ntohs@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [22 x i8] c"%s: %s: %s (errno %d)\00"
@.str.1 = [15 x i8] c"get_local_port\00"
@.str.2 = [12 x i8] c"getsockname\00"
@errno = global i32 0
define zeroext i16 @get_local_port(i32 %0) {
%2 = alloca i16
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
store i32 %0, i32* %3
store i32 4, i32* %5
%6 = load i32, i32* %3
%7 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%8 = call i64 @getsockname(i32 %6, %struct.sockaddr* %7, i32* %5)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = load i32, i32* @errno
%12 = call i32 @strerror(i32 %11)
%13 = load i32, i32* @errno
%14 = call i32 @log_message(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %12, i32 %13)
store i16 0, i16* %2
br label %19
15: ; preds = %1
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call zeroext i16 @ntohs(i32 %17)
store i16 %18, i16* %2
br label %19
19: ; preds = %15, %10
%20 = load i16, i16* %2
ret i16 %20
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [22 x i8] c"%s: %s: %s (errno %d)\00"
@.str.1 = [15 x i8] c"get_local_port\00"
@.str.2 = [12 x i8] c"getsockname\00"
@errno = global i32 0
define zeroext i16 @get_local_port(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = bitcast i32* %3 to i8*
store i32 4, i32* %3
%6 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%7 = call i64 @getsockname(i32 %0, %struct.sockaddr* %6, i32* %3)
%8 = icmp eq i64 %7, 0
br i1 %8, label %14, label %9
9: ; preds = %1
%10 = load i32, i32* @errno
%11 = call i32 @strerror(i32 %10)
%12 = load i32, i32* @errno
%13 = call i32 @log_message(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %11, i32 %12)
br label %18
14: ; preds = %1
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%16 = load i32, i32* %15
%17 = call zeroext i16 @ntohs(i32 %16)
br label %18
18: ; preds = %14, %9
%19 = phi i16 [ 0, %9 ], [ %17, %14 ]
ret i16 %19
}