#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*/ sa;

/* Variables and functions */
int errno;
scalar_t__ getsockname(int, struct sockaddr *, int *);
scalar_t__ ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int get_port_by_sock(int fd) {
  int err = 0;
  int port = 0;
  struct sockaddr_in sa;
  socklen_t sa_len = sizeof(sa);
  if (0 != getsockname(fd, (struct sockaddr *)&sa, &sa_len)) {
    perror("getsockname:");
    err = errno;
  } else {
    port = (int)ntohs(sa.sin_port);
  }
  return port;
}


x86 assembly (clang, -O0):
<get_port_by_sock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x4,-0x14(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1050 <getsockname@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     118b <get_port_by_sock+0x5b>
lea    0xe8b(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
jmp    1196 <get_port_by_sock+0x66>
mov    -0x10(%rbp),%edi
call   1030 <ntohs@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_port_by_sock>:
push   %rax
movl   $0x4,0x4(%rsp)
mov    %rsp,%rsi
lea    0x4(%rsp),%rdx
call   1050 <getsockname@plt>
test   %rax,%rax
jne    1155 <get_port_by_sock+0x25>
mov    (%rsp),%edi
call   1030 <ntohs@plt>
pop    %rcx
ret
lea    0xea4(%rip),%rdi
call   1040 <perror@plt>
xor    %eax,%eax
pop    %rcx
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [13 x i8] c"getsockname:\00"
@errno = global i32 0
define i32 @get_port_by_sock(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 %11, label %9
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %16
11: ; preds = %1
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%13 = load i32, i32* %12
%14 = call i64 @ntohs(i32 %13)
%15 = trunc i64 %14 to i32
br label %16
16: ; preds = %11, %9
%17 = phi i32 [ 0, %9 ], [ %15, %11 ]
ret i32 %17
}