#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 {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, const char *argv[]) {
  int sckfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  struct sockaddr_in addr;
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
  addr.sin_port = htons(0);
  if (bind(sckfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    printf("-1\n");
    return 1;
  }
  int len = sizeof(addr);
  getsockname(sckfd, (struct sockaddr *)&addr, &len);
  printf("%d\n", ntohs(addr.sin_port));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x1c(%rbp)
xor    %edi,%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    1205 <main+0x95>
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    123b <main+0xcb>
movl   $0xc,-0x24(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1080 <getsockname@plt>
mov    -0x20(%rbp),%edi
call   1060 <ntohs@plt>
mov    %eax,%esi
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    0x2e5b(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0xc(%rsp)
xor    %ebx,%ebx
xor    %edi,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     1217 <main+0x97>
movl   $0xc,0x14(%rsp)
lea    0x8(%rsp),%rsi
lea    0x14(%rsp),%rdx
mov    %ebp,%edi
call   1090 <getsockname@plt>
mov    0x8(%rsp),%edi
call   1070 <ntohs@plt>
lea    0xdf4(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1228 <main+0xa8>
lea    0xde6(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
@str = [3 x i8] c"-1\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = load i32, i32* @IPPROTO_TCP
%8 = tail call i32 @socket(i32 %5, i32 %6, i32 %7)
%9 = bitcast %struct.sockaddr_in* %3 to i8*
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %10, i32* %11
%12 = load i32, i32* @INADDR_ANY
%13 = tail call i32 @htonl(i32 %12)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %13, i32* %14
%15 = tail call i32 @htons(i32 0)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %15, i32* %16
%17 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%18 = call i64 @bind(i32 %8, %struct.sockaddr* %17, i32 12)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %2
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str, i64 0, i64 0))
br label %28
22: ; preds = %2
%23 = bitcast i32* %4 to i8*
store i32 12, i32* %4
%24 = call i32 @getsockname(i32 %8, %struct.sockaddr* %17, i32* %4)
%25 = load i32, i32* %16
%26 = call i32 @ntohs(i32 %25)
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %22, %20
%29 = phi i32 [ 1, %20 ], [ 0, %22 ]
ret i32 %29
}