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

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int /*<<< orphan*/ EADDRINUSE;
int const STDERR_FILENO;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ bind(int const, struct sockaddr const *, scalar_t__ const);
int /*<<< orphan*/ errno;
void *htons(int);
int rand();
int /*<<< orphan*/ srand(unsigned int);
scalar_t__ time(int /*<<< orphan*/ *);
bool _pick_tid(const int s, const struct sockaddr *local_address,
               const socklen_t address_size) {
  unsigned int attempts = 0;
  assert(STDERR_FILENO < s);
  assert(NULL != local_address);
  assert(0 < address_size);
  srand((unsigned int)time(NULL));
  switch (local_address->sa_family) {
  case AF_INET:
    for (; (2) > attempts; ++attempts) {
      ((struct sockaddr_in *)local_address)->sin_port =
          htons((6996) + (rand() % (4)));
      if (0 == bind(s, local_address, address_size)) {
        return true;
      } else {
        if (EADDRINUSE != errno) {
          return false;
        }
      }
    }
    break;
  case AF_INET6:
    for (; (2) > attempts; ++attempts) {
      ((struct sockaddr_in6 *)local_address)->sin6_port =
          htons((6996) + (rand() % (4)));
      if (0 == bind(s, local_address, address_size)) {
        return true;
      } else {
        if (EADDRINUSE != errno) {
          return false;
        }
      }
    }
    break;
  }
  return false;
}


x86 assembly (clang, -O0):
<_pick_tid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
mov    -0x8(%rbp),%ecx
sub    %ecx,%eax
setl   %al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rax
test   %rax,%rax
setne  %al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x18(%rbp),%rax
test   %rax,%rax
setg   %al
movzbl %al,%edi
call   1050 <assert@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %eax,%edi
call   1040 <srand@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
sub    $0x80,%eax
je     1295 <_pick_tid+0x135>
jmp    11e0 <_pick_tid+0x80>
mov    -0x20(%rbp),%eax
sub    $0x81,%eax
jne    1337 <_pick_tid+0x1d7>
jmp    11f3 <_pick_tid+0x93>
jmp    11f8 <_pick_tid+0x98>
mov    $0x2,%eax
cmp    -0x1c(%rbp),%eax
jbe    1290 <_pick_tid+0x130>
mov    $0x0,%al
call   1080 <rand@plt>
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
add    $0x1b54,%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1070 <bind@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    %ecx,%eax
jne    1254 <_pick_tid+0xf4>
movl   $0x1,-0x4(%rbp)
jmp    133e <_pick_tid+0x1de>
mov    0x2d85(%rip),%rax
mov    (%rax),%eax
mov    0x2d74(%rip),%rcx
cmp    (%rcx),%eax
je     1278 <_pick_tid+0x118>
movl   $0x0,-0x4(%rbp)
jmp    133e <_pick_tid+0x1de>
jmp    127d <_pick_tid+0x11d>
jmp    1282 <_pick_tid+0x122>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11f8 <_pick_tid+0x98>
jmp    1337 <_pick_tid+0x1d7>
jmp    129a <_pick_tid+0x13a>
mov    $0x2,%eax
cmp    -0x1c(%rbp),%eax
jbe    1332 <_pick_tid+0x1d2>
mov    $0x0,%al
call   1080 <rand@plt>
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
add    $0x1b54,%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1070 <bind@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    %ecx,%eax
jne    12f6 <_pick_tid+0x196>
movl   $0x1,-0x4(%rbp)
jmp    133e <_pick_tid+0x1de>
mov    0x2ce3(%rip),%rax
mov    (%rax),%eax
mov    0x2cd2(%rip),%rcx
cmp    (%rcx),%eax
je     131a <_pick_tid+0x1ba>
movl   $0x0,-0x4(%rbp)
jmp    133e <_pick_tid+0x1de>
jmp    131f <_pick_tid+0x1bf>
jmp    1324 <_pick_tid+0x1c4>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    129a <_pick_tid+0x13a>
jmp    1337 <_pick_tid+0x1d7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_pick_tid>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%r15d
xor    %edi,%edi
test   %r15d,%r15d
setg   %dil
call   1050 <assert@plt>
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1050 <assert@plt>
xor    %edi,%edi
test   %r14,%r14
setg   %dil
call   1050 <assert@plt>
xor    %ebp,%ebp
xor    %edi,%edi
call   1060 <time@plt>
mov    %eax,%edi
call   1040 <srand@plt>
mov    (%rbx),%eax
cmp    $0x81,%eax
je     11be <_pick_tid+0x5e>
cmp    $0x80,%eax
jne    124d <_pick_tid+0xed>
xor    %ebp,%ebp
xor    %eax,%eax
call   1080 <rand@plt>
lea    0x3(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xfffffffc,%ecx
neg    %ecx
lea    (%rax,%rcx,1),%edi
add    $0x1b54,%edi
call   1030 <htons@plt>
mov    %rax,(%rbx)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1070 <bind@plt>
test   %eax,%eax
je     1248 <_pick_tid+0xe8>
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    0x2dd1(%rip),%rcx
cmp    (%rcx),%eax
jne    124d <_pick_tid+0xed>
xor    %eax,%eax
call   1080 <rand@plt>
lea    0x3(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xfffffffc,%ecx
neg    %ecx
lea    (%rax,%rcx,1),%edi
add    $0x1b54,%edi
call   1030 <htons@plt>
mov    %rax,(%rbx)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1070 <bind@plt>
xor    %ebp,%ebp
test   %eax,%eax
sete   %bpl
jmp    124d <_pick_tid+0xed>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i8* }
%struct.sockaddr_in6 = type { i8* }
@STDERR_FILENO = constant i32 0
@EADDRINUSE = global i32 0
@errno = global i32 0
define i32 @_pick_tid(i32 %0, %struct.sockaddr* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr*
%7 = alloca i64
%8 = alloca i32
store i32 %0, i32* %5
store %struct.sockaddr* %1, %struct.sockaddr** %6
store i64 %2, i64* %7
store i32 0, i32* %8
%9 = load i32, i32* @STDERR_FILENO
%10 = load i32, i32* %5
%11 = icmp slt i32 %9, %10
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
%14 = load %struct.sockaddr*, %struct.sockaddr** %6
%15 = icmp ne %struct.sockaddr* null, %14
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
%18 = load i64, i64* %7
%19 = icmp slt i64 0, %18
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = call i64 @time(i32* null)
%23 = trunc i64 %22 to i32
%24 = call i32 @srand(i32 %23)
%25 = load %struct.sockaddr*, %struct.sockaddr** %6
%26 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %25, i32 0, i32 0
%27 = load i32, i32* %26
switch i32 %27, label %86 [
i32 129, label %28
i32 128, label %57
]
28: ; preds = %3
br label %29
29: ; preds = %53, %28
%30 = load i32, i32* %8
%31 = icmp ugt i32 2, %30
br i1 %31, label %32, label %56
32: ; preds = %29
%33 = call i32 (...) @rand()
%34 = srem i32 %33, 4
%35 = add nsw i32 6996, %34
%36 = call i8* @htons(i32 %35)
%37 = load %struct.sockaddr*, %struct.sockaddr** %6
%38 = bitcast %struct.sockaddr* %37 to %struct.sockaddr_in*
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %38, i32 0, i32 0
store i8* %36, i8** %39
%40 = load i32, i32* %5
%41 = load %struct.sockaddr*, %struct.sockaddr** %6
%42 = load i64, i64* %7
%43 = call i32 @bind(i32 %40, %struct.sockaddr* %41, i64 %42)
%44 = icmp eq i32 0, %43
br i1 %44, label %45, label %46
45: ; preds = %32
store i32 1, i32* %4
br label %87
46: ; preds = %32
%47 = load i32, i32* @EADDRINUSE
%48 = load i32, i32* @errno
%49 = icmp ne i32 %47, %48
br i1 %49, label %50, label %51
50: ; preds = %46
store i32 0, i32* %4
br label %87
51: ; preds = %46
br label %52
52: ; preds = %51
br label %53
53: ; preds = %52
%54 = load i32, i32* %8
%55 = add i32 %54, 1
store i32 %55, i32* %8
br label %29
56: ; preds = %29
br label %86
57: ; preds = %3
br label %58
58: ; preds = %82, %57
%59 = load i32, i32* %8
%60 = icmp ugt i32 2, %59
br i1 %60, label %61, label %85
61: ; preds = %58
%62 = call i32 (...) @rand()
%63 = srem i32 %62, 4
%64 = add nsw i32 6996, %63
%65 = call i8* @htons(i32 %64)
%66 = load %struct.sockaddr*, %struct.sockaddr** %6
%67 = bitcast %struct.sockaddr* %66 to %struct.sockaddr_in6*
%68 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %67, i32 0, i32 0
store i8* %65, i8** %68
%69 = load i32, i32* %5
%70 = load %struct.sockaddr*, %struct.sockaddr** %6
%71 = load i64, i64* %7
%72 = call i32 @bind(i32 %69, %struct.sockaddr* %70, i64 %71)
%73 = icmp eq i32 0, %72
br i1 %73, label %74, label %75
74: ; preds = %61
store i32 1, i32* %4
br label %87
75: ; preds = %61
%76 = load i32, i32* @EADDRINUSE
%77 = load i32, i32* @errno
%78 = icmp ne i32 %76, %77
br i1 %78, label %79, label %80
79: ; preds = %75
store i32 0, i32* %4
br label %87
80: ; preds = %75
br label %81
81: ; preds = %80
br label %82
82: ; preds = %81
%83 = load i32, i32* %8
%84 = add i32 %83, 1
store i32 %84, i32* %8
br label %58
85: ; preds = %58
br label %86
86: ; preds = %3, %85, %56
store i32 0, i32* %4
br label %87
87: ; preds = %86, %79, %74, %50, %45
%88 = load i32, i32* %4
ret i32 %88
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@STDERR_FILENO = constant i32 0
@EADDRINUSE = global i32 0
@errno = global i32 0
define i32 @_pick_tid(i32 %0, %struct.sockaddr* %1, i64 %2) {
%4 = icmp sgt i32 %0, 0
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = icmp ne %struct.sockaddr* %1, null
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = icmp sgt i64 %2, 0
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = tail call i64 @time(i32* null)
%14 = trunc i64 %13 to i32
%15 = tail call i32 @srand(i32 %14)
%16 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %1, i64 0, i32 0
%17 = load i32, i32* %16
switch i32 %17, label %51 [
i32 129, label %26
i32 128, label %18
]
18: ; preds = %3
%19 = bitcast %struct.sockaddr* %1 to i8**
%20 = tail call i32 (...) @rand()
%21 = srem i32 %20, 4
%22 = add nsw i32 %21, 6996
%23 = tail call i8* @htons(i32 %22)
store i8* %23, i8** %19
%24 = tail call i32 @bind(i32 %0, %struct.sockaddr* %1, i64 %2)
%25 = icmp eq i32 %24, 0
br i1 %25, label %51, label %38
26: ; preds = %3
%27 = bitcast %struct.sockaddr* %1 to i8**
%28 = tail call i32 (...) @rand()
%29 = srem i32 %28, 4
%30 = add nsw i32 %29, 6996
%31 = tail call i8* @htons(i32 %30)
store i8* %31, i8** %27
%32 = tail call i32 @bind(i32 %0, %struct.sockaddr* %1, i64 %2)
%33 = icmp eq i32 %32, 0
br i1 %33, label %51, label %34
34: ; preds = %26
%35 = load i32, i32* @EADDRINUSE
%36 = load i32, i32* @errno
%37 = icmp eq i32 %35, %36
br i1 %37, label %42, label %51
38: ; preds = %18
%39 = load i32, i32* @EADDRINUSE
%40 = load i32, i32* @errno
%41 = icmp eq i32 %39, %40
br i1 %41, label %42, label %51
42: ; preds = %38, %34
%43 = bitcast %struct.sockaddr* %1 to i8**
%44 = tail call i32 (...) @rand()
%45 = srem i32 %44, 4
%46 = add nsw i32 %45, 6996
%47 = tail call i8* @htons(i32 %46)
store i8* %47, i8** %43
%48 = tail call i32 @bind(i32 %0, %struct.sockaddr* %1, i64 %2)
%49 = icmp eq i32 %48, 0
%50 = zext i1 %49 to i32
br label %51
51: ; preds = %42, %38, %18, %34, %26, %3
%52 = phi i32 [ 0, %3 ], [ 1, %26 ], [ 0, %34 ], [ 1, %18 ], [ 0, %38 ], [ %50, %42 ]
ret i32 %52
}