#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__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  scalar_t__ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ getsockname(int, struct sockaddr *, size_t *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int get_free_port(void) {
  int sh;
  struct sockaddr_in myaddr;
  size_t namelen = sizeof(struct sockaddr);
  myaddr.sin_family = AF_INET;
  myaddr.sin_port = 0;
  myaddr.sin_addr.s_addr = INADDR_ANY;
  sh = socket(AF_INET, SOCK_STREAM, 0);
  bind(sh, (struct sockaddr *)&myaddr, sizeof(struct sockaddr_in));
  getsockname(sh, (struct sockaddr *)&myaddr, &namelen);
  close(sh);
  return (int)myaddr.sin_port;
}


x86 assembly (clang, -O0):
<get_free_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x4,-0x20(%rbp)
mov    0x2e91(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   1040 <bind@plt>
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1050 <getsockname@plt>
mov    -0x4(%rbp),%edi
call   1030 <close@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_free_port>:
push   %r14
push   %rbx
sub    $0x18,%rsp
movq   $0x4,0x10(%rsp)
mov    0x2e91(%rip),%rax
mov    (%rax),%edi
mov    %edi,0xc(%rsp)
movq   $0x0,(%rsp)
mov    0x2e74(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x10,%edx
call   1040 <bind@plt>
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
mov    %r14,%rsi
call   1050 <getsockname@plt>
mov    %ebx,%edi
call   1030 <close@plt>
mov    (%rsp),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i64, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
define i32 @get_free_port() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca i64
store i64 4, i64* %3
%4 = load i32, i32* @AF_INET
%5 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %4, i32* %5
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
store i64 0, i64* %6
%7 = load i32, i32* @INADDR_ANY
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
%9 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %8, i32 0, i32 0
store i32 %7, i32* %9
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %1
%13 = load i32, i32* %1
%14 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%15 = call i32 @bind(i32 %13, %struct.sockaddr* %14, i32 16)
%16 = load i32, i32* %1
%17 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%18 = call i32 @getsockname(i32 %16, %struct.sockaddr* %17, i64* %3)
%19 = load i32, i32* %1
%20 = call i32 @close(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
%22 = load i64, i64* %21
%23 = trunc i64 %22 to i32
ret i32 %23
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i64, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
define i32 @get_free_port() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i64
%3 = bitcast %struct.sockaddr_in* %1 to i8*
%4 = bitcast i64* %2 to i8*
store i64 4, i64* %2
%5 = load i32, i32* @AF_INET
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %5, i32* %6
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i64 0, i64* %7
%8 = load i32, i32* @INADDR_ANY
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %8, i32* %9
%10 = load i32, i32* @SOCK_STREAM
%11 = tail call i32 @socket(i32 %5, i32 %10, i32 0)
%12 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%13 = call i32 @bind(i32 %11, %struct.sockaddr* %12, i32 16)
%14 = call i32 @getsockname(i32 %11, %struct.sockaddr* %12, i64* %2)
%15 = call i32 @close(i32 %11)
%16 = load i64, i64* %7
%17 = trunc i64 %16 to i32
ret i32 %17
}