#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 */
typedef int /*<<< orphan*/ uint16_t;
struct sockaddr_storage {
  int dummy;
};
struct sockaddr_in6 {
  int /*<<< orphan*/ sin6_port;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  scalar_t__ sa_family;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ EAFNOSUPPORT;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ PF_INET6;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ errno;
scalar_t__ getsockname(int, struct sockaddr *, int *);
scalar_t__ listen(int, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int s;
  struct sockaddr_storage sa;
  socklen_t salen;
  uint16_t port;
  if ((s = socket(PF_INET6, SOCK_STREAM, 0)) < 0) {
    if (errno == EAFNOSUPPORT)
      s = socket(PF_INET, SOCK_STREAM, 0);
    if (s < 0) {
      perror("socket()");
      return 1;
    }
  }
  if (listen(s, 2) < 0) {
    perror("listen()");
    return 2;
  }
  salen = sizeof(sa);
  if (getsockname(s, (struct sockaddr *)&sa, &salen) < 0) {
    perror("getsockname()");
    return 3;
  }
  if (((struct sockaddr *)&sa)->sa_family == AF_INET)
    port = ((struct sockaddr_in *)&sa)->sin_port;
  else
    port = ((struct sockaddr_in6 *)&sa)->sin6_port;
  printf("Selected port number %u\n", ntohs(port));
  sleep(60);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1208 <main+0x98>
mov    0x2e16(%rip),%rax
mov    (%rax),%rax
mov    0x2dfc(%rip),%rcx
cmp    (%rcx),%rax
jne    11e1 <main+0x71>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1203 <main+0x93>
lea    0xe0e(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bc <main+0x14c>
jmp    1208 <main+0x98>
mov    -0x14(%rbp),%edi
mov    $0x2,%esi
call   1040 <listen@plt>
cmp    $0x0,%rax
jge    1237 <main+0xc7>
lea    0xde3(%rip),%rdi
call   1060 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12bc <main+0x14c>
movl   $0x4,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1070 <getsockname@plt>
cmp    $0x0,%rax
jge    1270 <main+0x100>
lea    0xdb3(%rip),%rdi
call   1060 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    12bc <main+0x14c>
mov    -0x18(%rbp),%rax
mov    0x2d65(%rip),%rcx
cmp    (%rcx),%rax
jne    128f <main+0x11f>
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1295 <main+0x125>
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
call   1050 <ntohs@plt>
mov    %eax,%esi
lea    0xd7a(%rip),%rdi
call   1030 <printf@plt>
mov    $0x3c,%edi
call   1080 <sleep@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
mov    0x2e55(%rip),%rbp
mov    0x0(%rbp),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11c6 <main+0x56>
mov    0x2e36(%rip),%rax
mov    (%rax),%rax
mov    0x2e1c(%rip),%rcx
cmp    (%rcx),%rax
jne    1244 <main+0xd4>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1244 <main+0xd4>
mov    $0x2,%ebp
mov    %ebx,%edi
mov    $0x2,%esi
call   1040 <listen@plt>
test   %rax,%rax
js     1223 <main+0xb3>
movl   $0x4,0x4(%rsp)
mov    %rsp,%rsi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1070 <getsockname@plt>
test   %rax,%rax
js     1231 <main+0xc1>
mov    (%rsp),%edi
call   1050 <ntohs@plt>
lea    0xe19(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    $0x3c,%edi
call   1080 <sleep@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xddf(%rip),%rdi
call   1060 <perror@plt>
jmp    121a <main+0xaa>
lea    0xdda(%rip),%rdi
call   1060 <perror@plt>
mov    $0x3,%ebp
jmp    121a <main+0xaa>
lea    0xdb5(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%ebp
jmp    121a <main+0xaa>

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i64 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@PF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
@errno = global i64 0
@EAFNOSUPPORT = global i64 0
@PF_INET = global i32 0
@.str = [9 x i8] c"socket()\00"
@.str.1 = [9 x i8] c"listen()\00"
@.str.2 = [14 x i8] c"getsockname()\00"
@AF_INET = global i64 0
@.str.3 = [25 x i8] c"Selected port number %u\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_storage
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @PF_INET6
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %28
14: ; preds = %2
%15 = load i64, i64* @errno
%16 = load i64, i64* @EAFNOSUPPORT
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = load i32, i32* @PF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %6
br label %22
22: ; preds = %18, %14
%23 = load i32, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %60
27: ; preds = %22
br label %28
28: ; preds = %27, %2
%29 = load i32, i32* %6
%30 = call i64 @listen(i32 %29, i32 2)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %3
br label %60
34: ; preds = %28
store i32 4, i32* %8
%35 = load i32, i32* %6
%36 = bitcast %struct.sockaddr_storage* %7 to %struct.sockaddr*
%37 = call i64 @getsockname(i32 %35, %struct.sockaddr* %36, i32* %8)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
store i32 3, i32* %3
br label %60
41: ; preds = %34
%42 = bitcast %struct.sockaddr_storage* %7 to %struct.sockaddr*
%43 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %42, i32 0, i32 0
%44 = load i64, i64* %43
%45 = load i64, i64* @AF_INET
%46 = icmp eq i64 %44, %45
br i1 %46, label %47, label %51
47: ; preds = %41
%48 = bitcast %struct.sockaddr_storage* %7 to %struct.sockaddr_in*
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %48, i32 0, i32 0
%50 = load i32, i32* %49
store i32 %50, i32* %9
br label %55
51: ; preds = %41
%52 = bitcast %struct.sockaddr_storage* %7 to %struct.sockaddr_in6*
%53 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %52, i32 0, i32 0
%54 = load i32, i32* %53
store i32 %54, i32* %9
br label %55
55: ; preds = %51, %47
%56 = load i32, i32* %9
%57 = call i32 @ntohs(i32 %56)
%58 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %57)
%59 = call i32 @sleep(i32 60)
store i32 0, i32* %3
br label %60
60: ; preds = %55, %39, %32, %25
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i64 }
@PF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
@errno = global i64 0
@EAFNOSUPPORT = global i64 0
@PF_INET = global i32 0
@.str = [9 x i8] c"socket()\00"
@.str.1 = [9 x i8] c"listen()\00"
@.str.2 = [14 x i8] c"getsockname()\00"
@AF_INET = global i64 0
@.str.3 = [25 x i8] c"Selected port number %u\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_storage
%4 = alloca i32
%5 = bitcast %struct.sockaddr_storage* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = load i32, i32* @PF_INET6
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %22
11: ; preds = %2
%12 = load i64, i64* @errno
%13 = load i64, i64* @EAFNOSUPPORT
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %20
15: ; preds = %11
%16 = load i32, i32* @PF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = tail call i32 @socket(i32 %16, i32 %17, i32 0)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %11, %15
%21 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %40
22: ; preds = %15, %2
%23 = phi i32 [ %18, %15 ], [ %9, %2 ]
%24 = tail call i64 @listen(i32 %23, i32 2)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %40
28: ; preds = %22
store i32 4, i32* %4
%29 = bitcast %struct.sockaddr_storage* %3 to %struct.sockaddr*
%30 = call i64 @getsockname(i32 %23, %struct.sockaddr* %29, i32* %4)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %40
34: ; preds = %28
%35 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %3, i64 0, i32 0
%36 = load i32, i32* %35
%37 = call i32 @ntohs(i32 %36)
%38 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %37)
%39 = call i32 @sleep(i32 60)
br label %40
40: ; preds = %34, %32, %26, %20
%41 = phi i32 [ 1, %20 ], [ 2, %26 ], [ 3, %32 ], [ 0, %34 ]
ret i32 %41
}