#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 */
scalar_t__ getsockname(int, struct sockaddr *, int *);
scalar_t__ ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int sockport(int s) {
  socklen_t namelen;
  struct sockaddr_in sin;
  namelen = sizeof(sin);
  if (getsockname(s, (struct sockaddr *)&sin, &namelen) < 0) {
    perror("getsockname");
    return (-1);
  }
  return ((int)ntohs(sin.sin_port));
}


x86 assembly (clang, -O0):
<sockport>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x4,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0xc(%rbp),%rdx
call   1050 <getsockname@plt>
cmp    $0x0,%rax
jge    1174 <sockport+0x44>
lea    0xe9d(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    117f <sockport+0x4f>
mov    -0x10(%rbp),%edi
call   1030 <ntohs@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

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

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [12 x i8] c"getsockname\00"
define i32 @sockport(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = bitcast i32* %2 to i8*
%5 = bitcast %struct.sockaddr_in* %3 to i8*
store i32 4, i32* %2
%6 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%7 = call i64 @getsockname(i32 %0, %struct.sockaddr* %6, i32* %2)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %16
11: ; preds = %1
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, 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 [ -1, %9 ], [ %15, %11 ]
ret i32 %17
}