#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_in {
  scalar_t__ sin_family;
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ EINTR;
scalar_t__ EPFNOSUPPORT;
int IPPORT_RESERVED;
int bind(int, struct sockaddr *, int);
scalar_t__ errno;
int getpid();
int /*<<< orphan*/ htons(short);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int bindresvport(int sd, struct sockaddr_in *sin) {
  static short port;
  struct sockaddr_in sin0;
  int nn, ret;
  if (sin == NULL) {
    sin = &sin0;
    memset(sin, 0, sizeof *sin);
    sin->sin_family = AF_INET;
  } else if (sin->sin_family != AF_INET) {
    errno = EPFNOSUPPORT;
    return -1;
  }
  if (port == 0) {
    port = 600 + (getpid() % (IPPORT_RESERVED - 600));
  }
  for (nn = (IPPORT_RESERVED - 600); nn > 0; nn--, port++) {
    if (port == IPPORT_RESERVED)
      port = 600;
    sin->sin_port = htons(port);
    do {
      ret = bind(sd, (struct sockaddr *)sin, sizeof(*sin));
    } while (ret < 0 && errno == EINTR);
    if (!ret)
      break;
  }
  return ret;
}


x86 assembly (clang, -O0):
<bindresvport>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1188 <bindresvport+0x48>
lea    -0x20(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e6f(%rip),%rax
mov    (%rax),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    11c4 <bindresvport+0x84>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e52(%rip),%rcx
cmp    (%rcx),%rax
je     11bf <bindresvport+0x7f>
mov    0x2e3a(%rip),%rax
mov    (%rax),%rcx
mov    0x2e20(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12ce <bindresvport+0x18e>
jmp    11c4 <bindresvport+0x84>
movswl 0x2e7d(%rip),%eax
cmp    $0x0,%eax
jne    11fd <bindresvport+0xbd>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    0x2dde(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x258,%ecx
cltd
idiv   %ecx
add    $0x258,%edx
mov    %dx,%ax
mov    %ax,0x2e4b(%rip)
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
sub    $0x258,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    12c8 <bindresvport+0x188>
movswl 0x2e29(%rip),%eax
mov    0x2d9a(%rip),%rcx
cmp    (%rcx),%eax
jne    1237 <bindresvport+0xf7>
movw   $0x258,0x2e11(%rip)
58 02
movswl 0x2e0a(%rip),%edi
call   1040 <htons@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x10,%edx
call   1060 <bind@plt>
mov    %eax,-0x28(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x28(%rbp)
mov    %al,-0x29(%rbp)
jge    1289 <bindresvport+0x149>
mov    0x2d5a(%rip),%rax
mov    (%rax),%rax
mov    0x2d48(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    124c <bindresvport+0x10c>
cmpl   $0x0,-0x28(%rbp)
jne    12a3 <bindresvport+0x163>
jmp    12c8 <bindresvport+0x188>
jmp    12a8 <bindresvport+0x168>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
mov    0x2d90(%rip),%ax
add    $0x1,%ax
mov    %ax,0x2d85(%rip)
jmp    120e <bindresvport+0xce>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bindresvport>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
test   %rsi,%rsi
je     1185 <bindresvport+0x45>
mov    %rsi,%rbx
mov    (%rsi),%rax
mov    0x2e86(%rip),%rcx
cmp    (%rcx),%rax
je     11a8 <bindresvport+0x68>
mov    0x2e72(%rip),%rax
mov    (%rax),%rax
mov    0x2e58(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%eax
jmp    1283 <bindresvport+0x143>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e48(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
movzwl 0x2e99(%rip),%edx
test   %dx,%dx
je     11cb <bindresvport+0x8b>
mov    0x2e05(%rip),%rax
mov    (%rax),%ecx
cmp    $0x259,%ecx
jl     1283 <bindresvport+0x143>
jmp    11fd <bindresvport+0xbd>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    0x2de7(%rip),%rcx
mov    (%rcx),%ecx
lea    -0x258(%rcx),%esi
cltd
idiv   %esi
add    $0x258,%edx
mov    %dx,0x2e57(%rip)
cmp    $0x259,%ecx
jl     1283 <bindresvport+0x143>
lea    -0x258(%rcx),%r14d
mov    0x2dc5(%rip),%r15
mov    0x2db6(%rip),%r12
mov    0x2da7(%rip),%r13
nopl   0x0(%rax)
movswl %dx,%eax
cmp    %eax,%ecx
jne    1234 <bindresvport+0xf4>
movw   $0x258,0x2e18(%rip)
58 02
mov    $0x258,%dx
movswl %dx,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rbx)
nop
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1060 <bind@plt>
test   %eax,%eax
jns    125c <bindresvport+0x11c>
mov    (%r12),%rcx
cmp    %rcx,(%r15)
je     1240 <bindresvport+0x100>
test   %eax,%eax
je     1281 <bindresvport+0x141>
movzwl 0x2de1(%rip),%edx
add    $0x1,%edx
mov    %dx,0x2dd7(%rip)
cmp    $0x2,%r14d
jl     1283 <bindresvport+0x143>
add    $0xffffffff,%r14d
mov    0x0(%r13),%ecx
jmp    1220 <bindresvport+0xe0>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i64, i32 }
%struct.sockaddr = type { i32 }
@bindresvport.port = internal global i16 0
@AF_INET = global i64 0
@EPFNOSUPPORT = global i64 0
@errno = global i64 0
@IPPORT_RESERVED = global i32 0
@EINTR = global i64 0
define i32 @bindresvport(i32 %0, %struct.sockaddr_in* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in*
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %5
%9 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%10 = icmp eq %struct.sockaddr_in* %9, null
br i1 %10, label %11, label %17
11: ; preds = %2
store %struct.sockaddr_in* %6, %struct.sockaddr_in** %5
%12 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%13 = call i32 @memset(%struct.sockaddr_in* %12, i32 0, i32 16)
%14 = load i64, i64* @AF_INET
%15 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 0
store i64 %14, i64* %16
br label %26
17: ; preds = %2
%18 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %18, i32 0, i32 0
%20 = load i64, i64* %19
%21 = load i64, i64* @AF_INET
%22 = icmp ne i64 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = load i64, i64* @EPFNOSUPPORT
store i64 %24, i64* @errno
store i32 -1, i32* %3
br label %80
25: ; preds = %17
br label %26
26: ; preds = %25, %11
%27 = load i16, i16* @bindresvport.port
%28 = sext i16 %27 to i32
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %37
30: ; preds = %26
%31 = call i32 (...) @getpid()
%32 = load i32, i32* @IPPORT_RESERVED
%33 = sub nsw i32 %32, 600
%34 = srem i32 %31, %33
%35 = add nsw i32 600, %34
%36 = trunc i32 %35 to i16
store i16 %36, i16* @bindresvport.port
br label %37
37: ; preds = %30, %26
%38 = load i32, i32* @IPPORT_RESERVED
%39 = sub nsw i32 %38, 600
store i32 %39, i32* %7
br label %40
40: ; preds = %73, %37
%41 = load i32, i32* %7
%42 = icmp sgt i32 %41, 0
br i1 %42, label %43, label %78
43: ; preds = %40
%44 = load i16, i16* @bindresvport.port
%45 = sext i16 %44 to i32
%46 = load i32, i32* @IPPORT_RESERVED
%47 = icmp eq i32 %45, %46
br i1 %47, label %48, label %49
48: ; preds = %43
store i16 600, i16* @bindresvport.port
br label %49
49: ; preds = %48, %43
%50 = load i16, i16* @bindresvport.port
%51 = call i32 @htons(i16 signext %50)
%52 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %52, i32 0, i32 1
store i32 %51, i32* %53
br label %54
54: ; preds = %66, %49
%55 = load i32, i32* %4
%56 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%57 = bitcast %struct.sockaddr_in* %56 to %struct.sockaddr*
%58 = call i32 @bind(i32 %55, %struct.sockaddr* %57, i32 16)
store i32 %58, i32* %8
br label %59
59: ; preds = %54
%60 = load i32, i32* %8
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %66
62: ; preds = %59
%63 = load i64, i64* @errno
%64 = load i64, i64* @EINTR
%65 = icmp eq i64 %63, %64
br label %66
66: ; preds = %62, %59
%67 = phi i1 [ false, %59 ], [ %65, %62 ]
br i1 %67, label %54, label %68
68: ; preds = %66
%69 = load i32, i32* %8
%70 = icmp ne i32 %69, 0
br i1 %70, label %72, label %71
71: ; preds = %68
br label %78
72: ; preds = %68
br label %73
73: ; preds = %72
%74 = load i32, i32* %7
%75 = add nsw i32 %74, -1
store i32 %75, i32* %7
%76 = load i16, i16* @bindresvport.port
%77 = add i16 %76, 1
store i16 %77, i16* @bindresvport.port
br label %40
78: ; preds = %71, %40
%79 = load i32, i32* %8
store i32 %79, i32* %3
br label %80
80: ; preds = %78, %23
%81 = load i32, i32* %3
ret i32 %81
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i64, i32 }
%struct.sockaddr = type { i32 }
@bindresvport.port = internal unnamed_addr global i16 0
@AF_INET = global i64 0
@EPFNOSUPPORT = global i64 0
@errno = global i64 0
@IPPORT_RESERVED = global i32 0
@EINTR = global i64 0
define i32 @bindresvport(i32 %0, %struct.sockaddr_in* %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = icmp eq %struct.sockaddr_in* %1, null
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 16)
%8 = load i64, i64* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i64 %8, i64* %9
br label %17
10: ; preds = %2
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%12 = load i64, i64* %11
%13 = load i64, i64* @AF_INET
%14 = icmp eq i64 %12, %13
br i1 %14, label %17, label %15
15: ; preds = %10
%16 = load i64, i64* @EPFNOSUPPORT
store i64 %16, i64* @errno
br label %64
17: ; preds = %10, %6
%18 = phi %struct.sockaddr_in* [ %3, %6 ], [ %1, %10 ]
%19 = load i16, i16* @bindresvport.port
%20 = icmp eq i16 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %17
%22 = load i32, i32* @IPPORT_RESERVED
br label %30
23: ; preds = %17
%24 = call i32 (...) @getpid()
%25 = load i32, i32* @IPPORT_RESERVED
%26 = add nsw i32 %25, -600
%27 = srem i32 %24, %26
%28 = trunc i32 %27 to i16
%29 = add i16 %28, 600
store i16 %29, i16* @bindresvport.port
br label %30
30: ; preds = %21, %23
%31 = phi i16 [ %19, %21 ], [ %29, %23 ]
%32 = phi i32 [ %22, %21 ], [ %25, %23 ]
%33 = icmp sgt i32 %32, 600
br i1 %33, label %34, label %64
34: ; preds = %30
%35 = add nsw i32 %32, -600
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %18, i64 0, i32 1
%37 = bitcast %struct.sockaddr_in* %18 to %struct.sockaddr*
br label %38
38: ; preds = %61, %34
%39 = phi i32 [ %32, %34 ], [ %63, %61 ]
%40 = phi i16 [ %31, %34 ], [ %59, %61 ]
%41 = phi i32 [ %35, %34 ], [ %62, %61 ]
%42 = sext i16 %40 to i32
%43 = icmp eq i32 %39, %42
br i1 %43, label %44, label %45
44: ; preds = %38
store i16 600, i16* @bindresvport.port
br label %45
45: ; preds = %44, %38
%46 = phi i16 [ 600, %44 ], [ %40, %38 ]
%47 = call i32 @htons(i16 signext %46)
store i32 %47, i32* %36
br label %48
48: ; preds = %48, %45
%49 = call i32 @bind(i32 %0, %struct.sockaddr* %37, i32 16)
%50 = icmp slt i32 %49, 0
%51 = load i64, i64* @errno
%52 = load i64, i64* @EINTR
%53 = icmp eq i64 %51, %52
%54 = select i1 %50, i1 %53, i1 false
br i1 %54, label %48, label %55
55: ; preds = %48
%56 = icmp eq i32 %49, 0
br i1 %56, label %64, label %57
57: ; preds = %55
%58 = load i16, i16* @bindresvport.port
%59 = add i16 %58, 1
store i16 %59, i16* @bindresvport.port
%60 = icmp sgt i32 %41, 1
br i1 %60, label %61, label %64
61: ; preds = %57
%62 = add nsw i32 %41, -1
%63 = load i32, i32* @IPPORT_RESERVED
br label %38
64: ; preds = %55, %57, %30, %15
%65 = phi i32 [ -1, %15 ], [ undef, %30 ], [ 0, %55 ], [ %49, %57 ]
ret i32 %65
}