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

/* Variables and functions */
scalar_t__ getsockname(int, struct sockaddr *, int *);
int htons(int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int SockAPI_TCPGetLocalPort(int sd) {
  struct sockaddr_in addr;
  socklen_t len = sizeof(addr);
  memset(&addr, 0, sizeof(addr));
  if (getsockname(sd, (struct sockaddr *)&addr, &len) != 0) {
    return -1;
  }
  return htons(addr.sin_port);
}


x86 assembly (clang, -O0):
<SockAPI_TCPGetLocalPort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x4,-0x14(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1050 <getsockname@plt>
cmp    $0x0,%rax
je     1178 <SockAPI_TCPGetLocalPort+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    1183 <SockAPI_TCPGetLocalPort+0x53>
mov    -0x10(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SockAPI_TCPGetLocalPort>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
movl   $0x4,0x4(%rsp)
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
mov    %r14,%rsi
call   1050 <getsockname@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    1174 <SockAPI_TCPGetLocalPort+0x44>
mov    (%rsp),%edi
call   1030 <htons@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
define i32 @SockAPI_TCPGetLocalPort(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
store i32 %0, i32* %3
store i32 4, i32* %5
%6 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 4)
%7 = load i32, i32* %3
%8 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%9 = call i64 @getsockname(i32 %7, %struct.sockaddr* %8, i32* %5)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %16
12: ; preds = %1
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%14 = load i32, i32* %13
%15 = call i32 @htons(i32 %14)
store i32 %15, i32* %2
br label %16
16: ; preds = %12, %11
%17 = load i32, i32* %2
ret i32 %17
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
define i32 @SockAPI_TCPGetLocalPort(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 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 4)
%7 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%8 = call i64 @getsockname(i32 %0, %struct.sockaddr* %7, i32* %3)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %1
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%12 = load i32, i32* %11
%13 = call i32 @htons(i32 %12)
br label %14
14: ; preds = %1, %10
%15 = phi i32 [ %13, %10 ], [ -1, %1 ]
ret i32 %15
}