#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(int /*<<< orphan*/);
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    12f9 <bindresvport+0x1a9>
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    12f3 <bindresvport+0x1a3>
mov    0x2dc2(%rip),%ax
mov    %ax,%cx
add    $0x1,%cx
mov    %cx,0x2db4(%rip)
movswl %ax,%edi
call   1050 <htons@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
movswl 0x2d9c(%rip),%eax
mov    0x2d05(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    12d1 <bindresvport+0x181>
movw   $0x258,0x2d7f(%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
lea    0x1(%rdi),%eax
mov    %ax,0x2e1f(%rip)
call   1050 <htons@plt>
mov    %eax,0x8(%rbx)
movswl 0x2e10(%rip),%eax
cmp    %eax,0x0(%r13)
jg     124f <bindresvport+0xff>
movw   $0x258,0x2e01(%rip)
58 02
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 %82
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 = %77, %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 %80
59: ; preds = %57
%60 = load i16, i16* @bindresvport.port
%61 = add i16 %60, 1
store i16 %61, i16* @bindresvport.port
%62 = sext i16 %60 to i32
%63 = call i32 @htons(i32 %62)
%64 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%65 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %64, i32 0, i32 1
store i32 %63, i32* %65
%66 = load i16, i16* @bindresvport.port
%67 = sext i16 %66 to i32
%68 = load i32, i32* @IPPORT_RESERVED
%69 = sub nsw i32 %68, 1
%70 = icmp sgt i32 %67, %69
br i1 %70, label %71, label %72
71: ; preds = %59
store i16 600, i16* @bindresvport.port
br label %72
72: ; preds = %71, %59
%73 = load i32, i32* %4
%74 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%75 = bitcast %struct.sockaddr_in* %74 to %struct.sockaddr*
%76 = call i32 @bind(i32 %73, %struct.sockaddr* %75, i32 16)
store i32 %76, i32* %6
br label %77
77: ; preds = %72
%78 = load i32, i32* %8
%79 = add nsw i32 %78, 1
store i32 %79, i32* %8
br label %43
80: ; preds = %57
%81 = load i32, i32* %6
store i32 %81, i32* %3
br label %82
82: ; preds = %80, %23
%83 = load i32, i32* %3
ret i32 %83
}

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 %63
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 %63
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 %63
40: ; preds = %55
%41 = load i64, i64* @errno
%42 = load i64, i64* @EADDRINUSE
%43 = icmp eq i64 %41, %42
br i1 %43, label %44, label %63
44: ; preds = %34, %40
%45 = phi i32 [ %57, %40 ], [ 0, %34 ]
%46 = load i16, i16* @bindresvport.port
%47 = add i16 %46, 1
store i16 %47, i16* @bindresvport.port
%48 = sext i16 %46 to i32
%49 = call i32 @htons(i32 %48)
store i32 %49, i32* %35
%50 = load i16, i16* @bindresvport.port
%51 = sext i16 %50 to i32
%52 = load i32, i32* @IPPORT_RESERVED
%53 = icmp sgt i32 %52, %51
br i1 %53, label %55, label %54
54: ; preds = %44
store i16 600, i16* @bindresvport.port
br label %55
55: ; preds = %54, %44
%56 = call i32 @bind(i32 %0, %struct.sockaddr* %36, i32 16)
%57 = add nuw nsw i32 %45, 1
%58 = load i32, i32* @IPPORT_RESERVED
%59 = add nsw i32 %58, -600
%60 = icmp slt i32 %57, %59
%61 = icmp slt i32 %56, 0
%62 = select i1 %60, i1 %61, i1 false
br i1 %62, label %40, label %63
63: ; preds = %40, %55, %34, %29, %15
%64 = phi i32 [ -1, %15 ], [ -1, %29 ], [ -1, %34 ], [ %56, %55 ], [ %56, %40 ]
ret i32 %64
}