#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__ EADDRINUSE;
scalar_t__ EPFNOSUPPORT;
int IPPORT_RESERVED;
int /*<<< orphan*/ __set_errno(scalar_t__);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
scalar_t__ errno;
int getpid();
int /*<<< orphan*/ htons(short);
int bindresvport(int sd, struct sockaddr_in *sin) {
  int res;
  static short port;
  struct sockaddr_in myaddr;
  int i;
  if (sin == (struct sockaddr_in *)0) {
    sin = &myaddr;
    bzero(sin, sizeof(*sin));
    sin->sin_family = AF_INET;
  } else if (sin->sin_family != AF_INET) {
    __set_errno(EPFNOSUPPORT);
    return -1;
  }
  if (port == 0) {
    port = (getpid() % ((IPPORT_RESERVED - 1) - 600 + 1)) + 600;
  }
  res = -1;
  __set_errno(EADDRINUSE);
  for (i = 0;
       i < ((IPPORT_RESERVED - 1) - 600 + 1) && res < 0 && errno == EADDRINUSE;
       ++i) {
    sin->sin_port = htons(port);
    if (++port > (IPPORT_RESERVED - 1)) {
      port = 600;
    }
    res = bind(sd, (struct sockaddr *)sin, sizeof(struct sockaddr_in));
  }
  return res;
}


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    1196 <bindresvport+0x46>
lea    -0x28(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x10,%esi
call   1070 <bzero@plt>
mov    0x2e61(%rip),%rax
mov    (%rax),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    11cd <bindresvport+0x7d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e44(%rip),%rcx
cmp    (%rcx),%rax
je     11c8 <bindresvport+0x78>
mov    0x2e2c(%rip),%rax
mov    (%rax),%rdi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f4 <bindresvport+0x1a4>
jmp    11cd <bindresvport+0x7d>
movswl 0x2e7c(%rip),%eax
cmp    $0x0,%eax
jne    120c <bindresvport+0xbc>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    0x2dd5(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
sub    $0x258,%ecx
add    $0x1,%ecx
cltd
idiv   %ecx
add    $0x258,%edx
mov    %dx,%ax
mov    %ax,0x2e44(%rip)
movl   $0xffffffff,-0x14(%rbp)
mov    0x2db6(%rip),%rax
mov    (%rax),%rdi
call   1030 <__set_errno@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%ecx
mov    0x2d8d(%rip),%rax
mov    (%rax),%edx
sub    $0x1,%edx
sub    $0x258,%edx
add    $0x1,%edx
xor    %eax,%eax
cmp    %edx,%ecx
mov    %al,-0x2d(%rbp)
jge    1277 <bindresvport+0x127>
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x2d(%rbp)
jge    1277 <bindresvport+0x127>
mov    0x2d64(%rip),%rax
mov    (%rax),%rax
mov    0x2d62(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    1287 <bindresvport+0x137>
jmp    12ee <bindresvport+0x19e>
movswl 0x2dc2(%rip),%edi
call   1050 <htons@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2dad(%rip),%ax
add    $0x1,%ax
mov    %ax,0x2da2(%rip)
cwtl
mov    0x2d0a(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    12cc <bindresvport+0x17c>
movw   $0x258,0x2d84(%rip)
58 02
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x10,%edx
call   1060 <bind@plt>
mov    %eax,-0x14(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1229 <bindresvport+0xd9>
mov    -0x14(%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,%r15d
test   %rsi,%rsi
je     1191 <bindresvport+0x41>
mov    %rsi,%rbx
mov    (%rsi),%rax
mov    0x2e75(%rip),%rcx
cmp    (%rcx),%rax
je     11b2 <bindresvport+0x62>
mov    0x2e61(%rip),%rax
mov    (%rax),%rdi
call   1030 <__set_errno@plt>
mov    $0xffffffff,%eax
jmp    127e <bindresvport+0x12e>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x10,%esi
call   1070 <bzero@plt>
mov    0x2e3e(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
cmpw   $0x0,0x2e96(%rip)
jne    11e1 <bindresvport+0x91>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    0x2df6(%rip),%rcx
mov    $0xfffffda8,%esi
add    (%rcx),%esi
cltd
idiv   %esi
add    $0x258,%edx
mov    %dx,0x2e6f(%rip)
mov    0x2de8(%rip),%r14
mov    (%r14),%rdi
call   1030 <__set_errno@plt>
mov    0x2dc9(%rip),%r13
mov    $0xffffffff,%eax
cmpl   $0x259,0x0(%r13)
jl     127e <bindresvport+0x12e>
mov    0x2dbb(%rip),%r12
mov    (%r12),%rcx
cmp    (%r14),%rcx
jne    127e <bindresvport+0x12e>
mov    $0x1,%ebp
nopl   0x0(%rax,%rax,1)
movswl 0x2e29(%rip),%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rbx)
movzwl 0x2e1a(%rip),%eax
add    $0x1,%eax
movswl %ax,%ecx
cmp    %ecx,0x0(%r13)
mov    $0x258,%ecx
cmovle %ecx,%eax
mov    %ax,0x2e01(%rip)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1060 <bind@plt>
mov    0x0(%r13),%ecx
mov    $0xfffffda8,%edx
add    %edx,%ecx
cmp    %ecx,%ebp
jge    127e <bindresvport+0x12e>
test   %eax,%eax
jns    127e <bindresvport+0x12e>
mov    (%r12),%rcx
add    $0x1,%ebp
cmp    (%r14),%rcx
je     1220 <bindresvport+0xd0>
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
@IPPORT_RESERVED = global i32 0
@EADDRINUSE = global i64 0
@errno = 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 i32
%7 = alloca %struct.sockaddr_in
%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* %7, %struct.sockaddr_in** %5
%12 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%13 = call i32 @bzero(%struct.sockaddr_in* %12, 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 %27
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 %26
23: ; preds = %17
%24 = load i64, i64* @EPFNOSUPPORT
%25 = call i32 @__set_errno(i64 %24)
store i32 -1, i32* %3
br label %81
26: ; preds = %17
br label %27
27: ; preds = %26, %11
%28 = load i16, i16* @bindresvport.port
%29 = sext i16 %28 to i32
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %40
31: ; preds = %27
%32 = call i32 (...) @getpid()
%33 = load i32, i32* @IPPORT_RESERVED
%34 = sub nsw i32 %33, 1
%35 = sub nsw i32 %34, 600
%36 = add nsw i32 %35, 1
%37 = srem i32 %32, %36
%38 = add nsw i32 %37, 600
%39 = trunc i32 %38 to i16
store i16 %39, i16* @bindresvport.port
br label %40
40: ; preds = %31, %27
store i32 -1, i32* %6
%41 = load i64, i64* @EADDRINUSE
%42 = call i32 @__set_errno(i64 %41)
store i32 0, i32* %8
br label %43
43: ; preds = %76, %40
%44 = load i32, i32* %8
%45 = load i32, i32* @IPPORT_RESERVED
%46 = sub nsw i32 %45, 1
%47 = sub nsw i32 %46, 600
%48 = add nsw i32 %47, 1
%49 = icmp slt i32 %44, %48
br i1 %49, label %50, label %57
50: ; preds = %43
%51 = load i32, i32* %6
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %50
%54 = load i64, i64* @errno
%55 = load i64, i64* @EADDRINUSE
%56 = icmp eq i64 %54, %55
br label %57
57: ; preds = %53, %50, %43
%58 = phi i1 [ false, %50 ], [ false, %43 ], [ %56, %53 ]
br i1 %58, label %59, label %79
59: ; preds = %57
%60 = load i16, i16* @bindresvport.port
%61 = call i32 @htons(i16 signext %60)
%62 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%63 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %62, i32 0, i32 1
store i32 %61, i32* %63
%64 = load i16, i16* @bindresvport.port
%65 = add i16 %64, 1
store i16 %65, i16* @bindresvport.port
%66 = sext i16 %65 to i32
%67 = load i32, i32* @IPPORT_RESERVED
%68 = sub nsw i32 %67, 1
%69 = icmp sgt i32 %66, %68
br i1 %69, label %70, label %71
70: ; preds = %59
store i16 600, i16* @bindresvport.port
br label %71
71: ; preds = %70, %59
%72 = load i32, i32* %4
%73 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%74 = bitcast %struct.sockaddr_in* %73 to %struct.sockaddr*
%75 = call i32 @bind(i32 %72, %struct.sockaddr* %74, i32 16)
store i32 %75, i32* %6
br label %76
76: ; preds = %71
%77 = load i32, i32* %8
%78 = add nsw i32 %77, 1
store i32 %78, i32* %8
br label %43
79: ; preds = %57
%80 = load i32, i32* %6
store i32 %80, i32* %3
br label %81
81: ; preds = %79, %23
%82 = load i32, i32* %3
ret i32 %82
}

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
@IPPORT_RESERVED = global i32 0
@EADDRINUSE = global i64 0
@errno = 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 @bzero(%struct.sockaddr_in* %3, 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 %18
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 %18, label %15
15: ; preds = %10
%16 = load i64, i64* @EPFNOSUPPORT
%17 = tail call i32 @__set_errno(i64 %16)
br label %61
18: ; preds = %10, %6
%19 = phi %struct.sockaddr_in* [ %3, %6 ], [ %1, %10 ]
%20 = load i16, i16* @bindresvport.port
%21 = icmp eq i16 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %18
%23 = tail call i32 (...) @getpid()
%24 = load i32, i32* @IPPORT_RESERVED
%25 = add nsw i32 %24, -600
%26 = srem i32 %23, %25
%27 = trunc i32 %26 to i16
%28 = add i16 %27, 600
store i16 %28, i16* @bindresvport.port
br label %29
29: ; preds = %22, %18
%30 = load i64, i64* @EADDRINUSE
%31 = tail call i32 @__set_errno(i64 %30)
%32 = load i32, i32* @IPPORT_RESERVED
%33 = icmp sgt i32 %32, 600
br i1 %33, label %34, label %61
34: ; preds = %29
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %19, i64 0, i32 1
%36 = bitcast %struct.sockaddr_in* %19 to %struct.sockaddr*
%37 = load i64, i64* @errno
%38 = load i64, i64* @EADDRINUSE
%39 = icmp eq i64 %37, %38
br i1 %39, label %44, label %61
40: ; preds = %44
%41 = load i64, i64* @errno
%42 = load i64, i64* @EADDRINUSE
%43 = icmp eq i64 %41, %42
br i1 %43, label %44, label %61
44: ; preds = %34, %40
%45 = phi i32 [ %55, %40 ], [ 0, %34 ]
%46 = load i16, i16* @bindresvport.port
%47 = call i32 @htons(i16 signext %46)
store i32 %47, i32* %35
%48 = load i16, i16* @bindresvport.port
%49 = add i16 %48, 1
%50 = sext i16 %49 to i32
%51 = load i32, i32* @IPPORT_RESERVED
%52 = icmp sgt i32 %51, %50
%53 = select i1 %52, i16 %49, i16 600
store i16 %53, i16* @bindresvport.port
%54 = call i32 @bind(i32 %0, %struct.sockaddr* %36, i32 16)
%55 = add nuw nsw i32 %45, 1
%56 = load i32, i32* @IPPORT_RESERVED
%57 = add nsw i32 %56, -600
%58 = icmp slt i32 %55, %57
%59 = icmp slt i32 %54, 0
%60 = select i1 %58, i1 %59, i1 false
br i1 %60, label %40, label %61
61: ; preds = %40, %44, %34, %29, %15
%62 = phi i32 [ -1, %15 ], [ -1, %29 ], [ -1, %34 ], [ %54, %44 ], [ %54, %40 ]
ret i32 %62
}