#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 /*<<< orphan*/ u_short;
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*/ sin;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EADDRINUSE;
int /*<<< orphan*/ EAGAIN;
int /*<<< orphan*/ INADDR_ANY;
int IPPORT_RESERVED;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ __set_errno(int /*<<< orphan*/);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int rresvport(int *alport) {
  struct sockaddr_in sin;
  int s;
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = INADDR_ANY;
  s = socket(AF_INET, SOCK_STREAM, 0);
  if (s < 0)
    return -1;
  for (;;) {
    sin.sin_port = htons((u_short)*alport);
    if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
      return s;
    if (errno != EADDRINUSE) {
      (void)close(s);
      return -1;
    }
    (*alport)--;
    if (*alport == IPPORT_RESERVED / 2) {
      (void)close(s);
      __set_errno(EAGAIN);
      return -1;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<rresvport>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11a6 <rresvport+0x56>
movl   $0xffffffff,-0x4(%rbp)
jmp    1266 <rresvport+0x116>
jmp    11ab <rresvport+0x5b>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jl     11df <rresvport+0x8f>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1266 <rresvport+0x116>
mov    0x2dda(%rip),%rax
mov    (%rax),%rax
mov    0x2dd8(%rip),%rcx
cmp    (%rcx),%rax
je     120d <rresvport+0xbd>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1266 <rresvport+0x116>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
add    $0xffffffff,%ecx
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d90(%rip),%rax
mov    (%rax),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
cmp    %ecx,%eax
jne    1261 <rresvport+0x111>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1266 <rresvport+0x116>
jmp    11ab <rresvport+0x5b>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rresvport>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
mov    0x2e80(%rip),%rax
mov    (%rax),%edi
mov    %edi,0x10(%rsp)
mov    0x2e6b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ebp
mov    $0xffffffff,%eax
test   %ebp,%ebp
js     1225 <rresvport+0xd5>
mov    (%rbx),%edi
lea    0x8(%rsp),%r15
mov    0x2e18(%rip),%r12
mov    0x2e19(%rip),%r13
mov    0x2e02(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
test   %rax,%rax
jns    1215 <rresvport+0xc5>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
jne    1219 <rresvport+0xc9>
mov    (%rbx),%edi
add    $0xffffffff,%edi
mov    %edi,(%rbx)
mov    (%r14),%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
cmp    %ecx,%edi
jne    11c0 <rresvport+0x70>
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
jmp    1220 <rresvport+0xd0>
mov    %ebp,%eax
jmp    1225 <rresvport+0xd5>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0xffffffff,%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
@SOCK_STREAM = global i32 0
@errno = global i64 0
@EADDRINUSE = global i64 0
@IPPORT_RESERVED = global i32 0
@EAGAIN = global i32 0
define i32 @rresvport(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
store i32* %0, i32** %3
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %6, i32* %7
%8 = load i32, i32* @INADDR_ANY
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%10 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %9, i32 0, i32 0
store i32 %8, i32* %10
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %1
store i32 -1, i32* %2
br label %51
17: ; preds = %1
br label %18
18: ; preds = %50, %17
%19 = load i32*, i32** %3
%20 = load i32, i32* %19
%21 = call i32 @htons(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* %5
%24 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%25 = call i64 @bind(i32 %23, %struct.sockaddr* %24, i32 12)
%26 = icmp sge i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %18
%28 = load i32, i32* %5
store i32 %28, i32* %2
br label %51
29: ; preds = %18
%30 = load i64, i64* @errno
%31 = load i64, i64* @EADDRINUSE
%32 = icmp ne i64 %30, %31
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = load i32, i32* %5
%35 = call i32 @close(i32 %34)
store i32 -1, i32* %2
br label %51
36: ; preds = %29
%37 = load i32*, i32** %3
%38 = load i32, i32* %37
%39 = add nsw i32 %38, -1
store i32 %39, i32* %37
%40 = load i32*, i32** %3
%41 = load i32, i32* %40
%42 = load i32, i32* @IPPORT_RESERVED
%43 = sdiv i32 %42, 2
%44 = icmp eq i32 %41, %43
br i1 %44, label %45, label %50
45: ; preds = %36
%46 = load i32, i32* %5
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* @EAGAIN
%49 = call i32 @__set_errno(i32 %48)
store i32 -1, i32* %2
br label %51
50: ; preds = %36
br label %18
51: ; preds = %45, %33, %27, %16
%52 = load i32, i32* %2
ret i32 %52
}

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
@SOCK_STREAM = global i32 0
@errno = global i64 0
@EADDRINUSE = global i64 0
@IPPORT_RESERVED = global i32 0
@EAGAIN = global i32 0
define i32 @rresvport(i32* nocapture %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = load i32, i32* @AF_INET
%5 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %4, i32* %5
%6 = load i32, i32* @INADDR_ANY
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %6, i32* %7
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %4, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %36, label %11
11: ; preds = %1
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%13 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%14 = load i32, i32* %0
br label %15
15: ; preds = %11, %26
%16 = phi i32 [ %14, %11 ], [ %28, %26 ]
%17 = call i32 @htons(i32 %16)
store i32 %17, i32* %12
%18 = call i64 @bind(i32 %9, %struct.sockaddr* %13, i32 12)
%19 = icmp sgt i64 %18, -1
br i1 %19, label %36, label %20
20: ; preds = %15
%21 = load i64, i64* @errno
%22 = load i64, i64* @EADDRINUSE
%23 = icmp eq i64 %21, %22
br i1 %23, label %26, label %24
24: ; preds = %20
%25 = call i32 @close(i32 %9)
br label %36
26: ; preds = %20
%27 = load i32, i32* %0
%28 = add nsw i32 %27, -1
store i32 %28, i32* %0
%29 = load i32, i32* @IPPORT_RESERVED
%30 = sdiv i32 %29, 2
%31 = icmp eq i32 %28, %30
br i1 %31, label %32, label %15
32: ; preds = %26
%33 = call i32 @close(i32 %9)
%34 = load i32, i32* @EAGAIN
%35 = call i32 @__set_errno(i32 %34)
br label %36
36: ; preds = %15, %1, %32, %24
%37 = phi i32 [ -1, %24 ], [ -1, %32 ], [ -1, %1 ], [ %9, %15 ]
ret i32 %37
}