#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  scalar_t__ s_addr;
};
struct sockaddr_in {
  scalar_t__ sin_port;
  int /*<<< orphan*/ sin_family;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ _sin;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EADDRINUSE;
scalar_t__ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
scalar_t__ errno;
int getsockname(int, struct sockaddr *, int *);
scalar_t__ htons(int);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int getPort() {
  struct sockaddr_in sin;
  int _socket, port = 0;
  _socket = socket(AF_INET, SOCK_STREAM, 0);
  if (_socket == -1)
    return -1;
  sin.sin_port = htons(port);
  sin.sin_addr.s_addr = 0;
  sin.sin_addr.s_addr = INADDR_ANY;
  sin.sin_family = AF_INET;
  if (bind(_socket, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) ==
      -1) {
    if (errno == EADDRINUSE)
      printf("Port in use");
    return -1;
  }
  struct sockaddr_in _sin;
  socklen_t len = sizeof(_sin);
  if (getsockname(_socket, (struct sockaddr *)&_sin, &len) != -1) {
    return (int)_sin.sin_port;
  }
  return -1;
}


x86 assembly (clang, -O0):
<getPort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x28(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1191 <getPort+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    123e <getPort+0xee>
mov    -0x28(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x10(%rbp)
mov    0x2e34(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x18,%edx
call   1050 <bind@plt>
cmp    $0xffffffff,%eax
jne    120b <getPort+0xbb>
mov    0x2de0(%rip),%rax
mov    (%rax),%rax
mov    0x2dde(%rip),%rcx
cmp    (%rcx),%rax
jne    11ff <getPort+0xaf>
lea    0xe06(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123e <getPort+0xee>
movl   $0x18,-0x44(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x40(%rbp),%rsi
lea    -0x44(%rbp),%rdx
call   1060 <getsockname@plt>
cmp    $0xffffffff,%eax
je     1237 <getPort+0xe7>
mov    -0x40(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    123e <getPort+0xee>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getPort>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    0x2e89(%rip),%r14
mov    (%r14),%edi
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     1202 <getPort+0xb2>
mov    %eax,%ebp
xor    %edi,%edi
call   1030 <htons@plt>
mov    %rax,0x10(%rsp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x20(%rsp)
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x18,%edx
call   1050 <bind@plt>
cmp    $0xffffffff,%eax
je     11e0 <getPort+0x90>
movl   $0x18,0xc(%rsp)
lea    0x28(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebp,%edi
call   1060 <getsockname@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1202 <getPort+0xb2>
mov    0x28(%rsp),%ebx
jmp    1202 <getPort+0xb2>
mov    0x2dd9(%rip),%rax
mov    (%rax),%rax
mov    0x2dd7(%rip),%rcx
cmp    (%rcx),%rax
jne    1202 <getPort+0xb2>
lea    0xe03(%rip),%rdi
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i64, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i64 0
@errno = global i64 0
@EADDRINUSE = global i64 0
@.str = [12 x i8] c"Port in use\00"
define i32 @getPort() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
store i32 0, i32* %4
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %0
store i32 -1, i32* %1
br label %45
13: ; preds = %0
%14 = load i32, i32* %4
%15 = call i64 @htons(i32 %14)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
store i64 %15, i64* %16
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
%18 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %17, i32 0, i32 0
store i64 0, i64* %18
%19 = load i64, i64* @INADDR_ANY
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
store i64 %19, i64* %21
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
store i32 %22, i32* %23
%24 = load i32, i32* %3
%25 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%26 = call i32 @bind(i32 %24, %struct.sockaddr* %25, i32 24)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %35
28: ; preds = %13
%29 = load i64, i64* @errno
%30 = load i64, i64* @EADDRINUSE
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %34
34: ; preds = %32, %28
store i32 -1, i32* %1
br label %45
35: ; preds = %13
store i32 24, i32* %6
%36 = load i32, i32* %3
%37 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%38 = call i32 @getsockname(i32 %36, %struct.sockaddr* %37, i32* %6)
%39 = icmp ne i32 %38, -1
br i1 %39, label %40, label %44
40: ; preds = %35
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%42 = load i64, i64* %41
%43 = trunc i64 %42 to i32
store i32 %43, i32* %1
br label %45
44: ; preds = %35
store i32 -1, i32* %1
br label %45
45: ; preds = %44, %40, %34, %12
%46 = load i32, i32* %1
ret i32 %46
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i64, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i64 0
@errno = global i64 0
@EADDRINUSE = global i64 0
@.str = [12 x i8] c"Port in use\00"
define i32 @getPort() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %1 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %35, label %9
9: ; preds = %0
%10 = tail call i64 @htons(i32 0)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i64 %10, i64* %11
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2, i32 0
%13 = load i64, i64* @INADDR_ANY
store i64 %13, i64* %12
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %14, i32* %15
%16 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%17 = call i32 @bind(i32 %7, %struct.sockaddr* %16, i32 24)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %25
19: ; preds = %9
%20 = load i64, i64* @errno
%21 = load i64, i64* @EADDRINUSE
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %35
23: ; preds = %19
%24 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %35
25: ; preds = %9
%26 = bitcast %struct.sockaddr_in* %2 to i8*
%27 = bitcast i32* %3 to i8*
store i32 24, i32* %3
%28 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%29 = call i32 @getsockname(i32 %7, %struct.sockaddr* %28, i32* %3)
%30 = icmp eq i32 %29, -1
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%32 = load i64, i64* %31
%33 = trunc i64 %32 to i32
%34 = select i1 %30, i32 -1, i32 %33
br label %35
35: ; preds = %19, %23, %0, %25
%36 = phi i32 [ %34, %25 ], [ -1, %0 ], [ -1, %23 ], [ -1, %19 ]
ret i32 %36
}