#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;
};
typedef int /*<<< orphan*/ me;

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


x86 assembly (clang, -O0):
<bindresvport>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    118f <bindresvport+0x4f>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
lea    -0x20(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    0x2e68(%rip),%rax
mov    (%rax),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    11c9 <bindresvport+0x89>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e4b(%rip),%rcx
cmp    (%rcx),%rax
je     11c4 <bindresvport+0x84>
mov    0x2e33(%rip),%rax
mov    (%rax),%ecx
mov    0x2e1a(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12a5 <bindresvport+0x165>
jmp    11c9 <bindresvport+0x89>
movswl 0x2e78(%rip),%eax
cmp    $0x0,%eax
jne    1202 <bindresvport+0xc2>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    0x2de1(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x300,%ecx
cltd
idiv   %ecx
add    $0x300,%edx
mov    %dx,%ax
mov    %ax,0x2e46(%rip)
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    0x2db5(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x300,%ecx
cmp    %ecx,%eax
jge    129f <bindresvport+0x15f>
movswl 0x2e1e(%rip),%eax
mov    0x2d97(%rip),%rcx
cmp    (%rcx),%eax
jne    1242 <bindresvport+0x102>
movw   $0x300,0x2e06(%rip)
movswl 0x2dff(%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,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
je     127a <bindresvport+0x13a>
jmp    129f <bindresvport+0x15f>
jmp    127f <bindresvport+0x13f>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    0x2db9(%rip),%ax
add    $0x1,%ax
mov    %ax,0x2dae(%rip)
jmp    1209 <bindresvport+0xc9>
mov    -0x24(%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   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
test   %rsi,%rsi
je     117b <bindresvport+0x3b>
mov    %rsi,%rbx
mov    (%rsi),%rax
mov    0x2e89(%rip),%rcx
cmp    (%rcx),%rax
je     119e <bindresvport+0x5e>
mov    0x2e75(%rip),%rax
mov    (%rax),%eax
mov    0x2e5c(%rip),%rcx
mov    %eax,(%rcx)
jmp    1245 <bindresvport+0x105>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e52(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
movzwl 0x2ea3(%rip),%edx
test   %dx,%dx
je     11b5 <bindresvport+0x75>
mov    0x2e17(%rip),%rax
mov    (%rax),%ecx
jmp    11db <bindresvport+0x9b>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    0x2e05(%rip),%rcx
mov    (%rcx),%ecx
lea    -0x300(%rcx),%esi
cltd
idiv   %esi
add    $0x300,%edx
mov    %dx,0x2e6d(%rip)
xor    %eax,%eax
cmp    $0x301,%ecx
jl     124a <bindresvport+0x10a>
xor    %ebp,%ebp
mov    0x2dda(%rip),%r15
xchg   %ax,%ax
movswl %dx,%eax
cmp    %eax,%ecx
jne    1204 <bindresvport+0xc4>
movw   $0x300,0x2e48(%rip)
mov    $0x300,%dx
movswl %dx,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rbx)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
jne    124a <bindresvport+0x10a>
add    $0x1,%ebp
movzwl 0x2e1a(%rip),%edx
add    $0x1,%edx
mov    %dx,0x2e10(%rip)
mov    (%r15),%ecx
lea    -0x300(%rcx),%eax
cmp    %eax,%ebp
jl     11f0 <bindresvport+0xb0>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
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 i32 0
@errno = global i32 0
@IPPORT_RESERVED = global i32 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
store i32 0, i32* %7
%9 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%10 = icmp eq %struct.sockaddr_in* %9, null
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 16)
store %struct.sockaddr_in* %6, %struct.sockaddr_in** %5
%13 = load i64, i64* @AF_INET
%14 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 0
store i64 %13, i64* %15
br label %25
16: ; preds = %2
%17 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %17, i32 0, i32 0
%19 = load i64, i64* %18
%20 = load i64, i64* @AF_INET
%21 = icmp ne i64 %19, %20
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = load i32, i32* @EPFNOSUPPORT
store i32 %23, i32* @errno
store i32 -1, i32* %3
br label %68
24: ; preds = %16
br label %25
25: ; preds = %24, %11
%26 = load i16, i16* @bindresvport.port
%27 = sext i16 %26 to i32
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %36
29: ; preds = %25
%30 = call i32 (...) @getpid()
%31 = load i32, i32* @IPPORT_RESERVED
%32 = sub nsw i32 %31, 768
%33 = srem i32 %30, %32
%34 = add nsw i32 768, %33
%35 = trunc i32 %34 to i16
store i16 %35, i16* @bindresvport.port
br label %36
36: ; preds = %29, %25
store i32 0, i32* %8
br label %37
37: ; preds = %61, %36
%38 = load i32, i32* %8
%39 = load i32, i32* @IPPORT_RESERVED
%40 = sub nsw i32 %39, 768
%41 = icmp slt i32 %38, %40
br i1 %41, label %42, label %66
42: ; preds = %37
%43 = load i16, i16* @bindresvport.port
%44 = sext i16 %43 to i32
%45 = load i32, i32* @IPPORT_RESERVED
%46 = icmp eq i32 %44, %45
br i1 %46, label %47, label %48
47: ; preds = %42
store i16 768, i16* @bindresvport.port
br label %48
48: ; preds = %47, %42
%49 = load i16, i16* @bindresvport.port
%50 = call i32 @htons(i16 signext %49)
%51 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %51, i32 0, i32 1
store i32 %50, i32* %52
%53 = load i32, i32* %4
%54 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%55 = bitcast %struct.sockaddr_in* %54 to %struct.sockaddr*
%56 = call i32 @bind(i32 %53, %struct.sockaddr* %55, i32 16)
store i32 %56, i32* %7
%57 = load i32, i32* %7
%58 = icmp ne i32 %57, -1
br i1 %58, label %59, label %60
59: ; preds = %48
br label %66
60: ; preds = %48
br label %61
61: ; preds = %60
%62 = load i32, i32* %8
%63 = add nsw i32 %62, 1
store i32 %63, i32* %8
%64 = load i16, i16* @bindresvport.port
%65 = add i16 %64, 1
store i16 %65, i16* @bindresvport.port
br label %37
66: ; preds = %59, %37
%67 = load i32, i32* %7
store i32 %67, i32* %3
br label %68
68: ; preds = %66, %22
%69 = load i32, i32* %3
ret i32 %69
}

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 i32 0
@errno = global i32 0
@IPPORT_RESERVED = global i32 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 i32, i32* @EPFNOSUPPORT
store i32 %16, i32* @errno
br label %56
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, -768
%27 = srem i32 %24, %26
%28 = trunc i32 %27 to i16
%29 = add i16 %28, 768
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, 768
br i1 %33, label %34, label %56
34: ; preds = %30
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %18, i64 0, i32 1
%36 = bitcast %struct.sockaddr_in* %18 to %struct.sockaddr*
br label %37
37: ; preds = %34, %49
%38 = phi i16 [ %31, %34 ], [ %52, %49 ]
%39 = phi i32 [ %32, %34 ], [ %53, %49 ]
%40 = phi i32 [ 0, %34 ], [ %50, %49 ]
%41 = sext i16 %38 to i32
%42 = icmp eq i32 %39, %41
br i1 %42, label %43, label %44
43: ; preds = %37
store i16 768, i16* @bindresvport.port
br label %44
44: ; preds = %43, %37
%45 = phi i16 [ 768, %43 ], [ %38, %37 ]
%46 = call i32 @htons(i16 signext %45)
store i32 %46, i32* %35
%47 = call i32 @bind(i32 %0, %struct.sockaddr* %36, i32 16)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %56
49: ; preds = %44
%50 = add nuw nsw i32 %40, 1
%51 = load i16, i16* @bindresvport.port
%52 = add i16 %51, 1
store i16 %52, i16* @bindresvport.port
%53 = load i32, i32* @IPPORT_RESERVED
%54 = add nsw i32 %53, -768
%55 = icmp slt i32 %50, %54
br i1 %55, label %37, label %56
56: ; preds = %44, %49, %30, %15
%57 = phi i32 [ -1, %15 ], [ 0, %30 ], [ %47, %44 ], [ -1, %49 ]
ret i32 %57
}