#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 */
typedef int uint16_t;
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*/ socklen_t;
typedef int /*<<< orphan*/ servAddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EADDRINUSE;
int /*<<< orphan*/ INADDR_ANY;
scalar_t__ bind(int, struct sockaddr *, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
uint16_t bind_open_port(int sock) {
  struct sockaddr_in servAddr;
  uint16_t port;
  for (port = 8081; port <= 8090; port++) {
    memset(&servAddr, 0, sizeof(servAddr));
    servAddr.sin_family = AF_INET;
    servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servAddr.sin_port = htons(port);
    if (bind(sock, (struct sockaddr *)&servAddr, (socklen_t)sizeof(servAddr)) ==
        0) {
      return port;
    }
    if (errno == EADDRINUSE) {
      printf("Port %d is in use\n", port);
      continue;
    }
    perror("Failed to bind");
  }
  printf("Unable to find a valid port");
  return 0;
}


x86 assembly (clang, -O0):
<bind_open_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x1f91,-0x1c(%rbp)
cmpl   $0x1f9a,-0x1c(%rbp)
jg     1227 <bind_open_port+0xc7>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jne    11dd <bind_open_port+0x7d>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    123c <bind_open_port+0xdc>
mov    0x2de4(%rip),%rax
mov    (%rax),%rax
mov    0x2de2(%rip),%rcx
cmp    (%rcx),%rax
jne    120d <bind_open_port+0xad>
mov    -0x1c(%rbp),%esi
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1219 <bind_open_port+0xb9>
lea    0xdff(%rip),%rdi
call   1080 <perror@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1172 <bind_open_port+0x12>
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bind_open_port>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
mov    $0x1f91,%ebx
lea    0x8(%rsp),%r14
mov    0x2e67(%rip),%r13
mov    0x2e58(%rip),%r12
mov    0x2e41(%rip),%r15
nop
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
mov    (%r12),%edi
call   1050 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
je     121d <bind_open_port+0xbd>
mov    0x2dee(%rip),%rax
mov    (%rax),%rax
cmp    (%r15),%rax
jne    11ff <bind_open_port+0x9f>
lea    0xe17(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%ebx
cmp    $0x1f9b,%ebx
jne    1190 <bind_open_port+0x30>
jmp    120d <bind_open_port+0xad>
lea    0xe0d(%rip),%rdi
call   1080 <perror@plt>
jmp    11f2 <bind_open_port+0x92>
lea    0xe0e(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
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
@INADDR_ANY = global i32 0
@errno = global i64 0
@EADDRINUSE = global i64 0
@.str = [19 x i8] c"Port %d is in use\0A\00"
@.str.1 = [15 x i8] c"Failed to bind\00"
@.str.2 = [28 x i8] c"Unable to find a valid port\00"
define i32 @bind_open_port(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
store i32 %0, i32* %3
store i32 8081, i32* %5
br label %6
6: ; preds = %35, %1
%7 = load i32, i32* %5
%8 = icmp sle i32 %7, 8090
br i1 %8, label %9, label %38
9: ; preds = %6
%10 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %11, i32* %12
%13 = load i32, i32* @INADDR_ANY
%14 = call i32 @htonl(i32 %13)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%16 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %15, i32 0, i32 0
store i32 %14, i32* %16
%17 = load i32, i32* %5
%18 = call i32 @htons(i32 %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* %3
%21 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%22 = call i64 @bind(i32 %20, %struct.sockaddr* %21, i32 12)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %9
%25 = load i32, i32* %5
store i32 %25, i32* %2
br label %40
26: ; preds = %9
%27 = load i64, i64* @errno
%28 = load i64, i64* @EADDRINUSE
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = load i32, i32* %5
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %31)
br label %35
33: ; preds = %26
%34 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %33, %30
%36 = load i32, i32* %5
%37 = add nsw i32 %36, 1
store i32 %37, i32* %5
br label %6
38: ; preds = %6
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %2
br label %40
40: ; preds = %38, %24
%41 = load i32, i32* %2
ret i32 %41
}

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
@INADDR_ANY = global i32 0
@errno = global i64 0
@EADDRINUSE = global i64 0
@.str = [19 x i8] c"Port %d is in use\0A\00"
@.str.1 = [15 x i8] c"Failed to bind\00"
@.str.2 = [28 x i8] c"Unable to find a valid port\00"
define i32 @bind_open_port(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
%5 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%7 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
br label %8
8: ; preds = %1, %25
%9 = phi i32 [ 8081, %1 ], [ %26, %25 ]
%10 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%11 = load i32, i32* @AF_INET
store i32 %11, i32* %4
%12 = load i32, i32* @INADDR_ANY
%13 = call i32 @htonl(i32 %12)
store i32 %13, i32* %5
%14 = call i32 @htons(i32 %9)
store i32 %14, i32* %6
%15 = call i64 @bind(i32 %0, %struct.sockaddr* %7, i32 12)
%16 = icmp eq i64 %15, 0
br i1 %16, label %30, label %17
17: ; preds = %8
%18 = load i64, i64* @errno
%19 = load i64, i64* @EADDRINUSE
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %9)
br label %25
23: ; preds = %17
%24 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %21
%26 = add nuw nsw i32 %9, 1
%27 = icmp eq i32 %26, 8091
br i1 %27, label %28, label %8
28: ; preds = %25
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %8, %28
%31 = phi i32 [ 0, %28 ], [ %9, %8 ]
ret i32 %31
}