#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ saddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EADDRINUSE;
scalar_t__ EADDRNOTAVAIL;
scalar_t__ EINTR;
scalar_t__ EINVAL;
int /*<<< orphan*/ INADDR_ANY;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
scalar_t__ errno;
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int bind_sock(int sockd, int service) {
  struct sockaddr_in saddr;
  int port = service == -1 ? 1023 : service;
next_port:
  bzero((char *)&saddr, sizeof(saddr));
  saddr.sin_family = AF_INET;
  saddr.sin_port = htons((u_short)port);
  saddr.sin_addr.s_addr = INADDR_ANY;
bind_sock_restart:
  if (bind(sockd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
    if (errno == EINTR)
      goto bind_sock_restart;
    if (service == -1 &&
        (errno == EINVAL || errno == EADDRINUSE || errno == EADDRNOTAVAIL)) {
      if (--port > 512)
        goto next_port;
    }
    return (-1);
  }
  return (sockd);
}


x86 assembly (clang, -O0):
<bind_sock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1155 <bind_sock+0x25>
mov    $0x3ff,%eax
mov    %eax,-0x20(%rbp)
jmp    115b <bind_sock+0x2b>
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   1050 <bzero@plt>
mov    0x2e72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1040 <bind@plt>
cmp    $0x0,%rax
jge    124e <bind_sock+0x11e>
mov    0x2e0c(%rip),%rax
mov    (%rax),%rax
mov    0x2dfa(%rip),%rcx
cmp    (%rcx),%rax
jne    11cc <bind_sock+0x9c>
jmp    1192 <bind_sock+0x62>
cmpl   $0xffffffff,-0xc(%rbp)
jne    1242 <bind_sock+0x112>
mov    0x2de3(%rip),%rax
mov    (%rax),%rax
mov    0x2dc9(%rip),%rcx
cmp    (%rcx),%rax
je     1224 <bind_sock+0xf4>
mov    0x2dc9(%rip),%rax
mov    (%rax),%rax
mov    0x2dc7(%rip),%rcx
cmp    (%rcx),%rax
je     1224 <bind_sock+0xf4>
mov    0x2daf(%rip),%rax
mov    (%rax),%rax
mov    0x2dc5(%rip),%rcx
cmp    (%rcx),%rax
jne    1242 <bind_sock+0x112>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
cmp    $0x200,%eax
jle    123d <bind_sock+0x10d>
jmp    1161 <bind_sock+0x31>
jmp    1242 <bind_sock+0x112>
movl   $0xffffffff,-0x4(%rbp)
jmp    1254 <bind_sock+0x124>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bind_sock>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
cmp    $0xffffffff,%esi
mov    $0x3ff,%r15d
mov    %esi,0x4(%rsp)
cmovne %esi,%r15d
lea    0x8(%rsp),%rbp
mov    0x2e8b(%rip),%r14
mov    0x2e5c(%rip),%r12
mov    0x2e4d(%rip),%r13
mov    %rbp,%rdi
mov    $0xc,%esi
call   1050 <bzero@plt>
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
mov    %r15d,%r14d
mov    %r15d,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1040 <bind@plt>
test   %rax,%rax
jns    1218 <bind_sock+0xe8>
mov    (%r12),%rdx
cmp    0x0(%r13),%rdx
je     11a0 <bind_sock+0x70>
mov    $0xffffffff,%eax
cmpl   $0xffffffff,0x4(%rsp)
jne    121a <bind_sock+0xea>
mov    0x2ddf(%rip),%rcx
cmp    (%rcx),%rdx
sete   %cl
mov    0x2dea(%rip),%rsi
cmp    (%rsi),%rdx
sete   %sil
mov    0x2df4(%rip),%rdi
cmp    (%rdi),%rdx
sete   %dl
cmp    $0x202,%r14d
jl     121a <bind_sock+0xea>
mov    %r14d,%r15d
or     %sil,%cl
or     %dl,%cl
add    $0xffffffff,%r15d
test   %cl,%cl
mov    0x2dd8(%rip),%r14
jne    116b <bind_sock+0x3b>
jmp    121a <bind_sock+0xea>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@EINVAL = global i64 0
@EADDRINUSE = global i64 0
@EADDRNOTAVAIL = global i64 0
define i32 @bind_sock(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %5
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %11
10: ; preds = %2
br label %13
11: ; preds = %2
%12 = load i32, i32* %5
br label %13
13: ; preds = %11, %10
%14 = phi i32 [ 1023, %10 ], [ %12, %11 ]
store i32 %14, i32* %7
br label %15
15: ; preds = %55, %13
%16 = bitcast %struct.sockaddr_in* %6 to i8*
%17 = call i32 @bzero(i8* %16, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* %7
%21 = call i32 @htons(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %21, i32* %22
%23 = load i32, i32* @INADDR_ANY
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
store i32 %23, i32* %25
br label %26
26: ; preds = %35, %15
%27 = load i32, i32* %4
%28 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%29 = call i64 @bind(i32 %27, %struct.sockaddr* %28, i32 12)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %58
31: ; preds = %26
%32 = load i64, i64* @errno
%33 = load i64, i64* @EINTR
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %36
35: ; preds = %31
br label %26
36: ; preds = %31
%37 = load i32, i32* %5
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %57
39: ; preds = %36
%40 = load i64, i64* @errno
%41 = load i64, i64* @EINVAL
%42 = icmp eq i64 %40, %41
br i1 %42, label %51, label %43
43: ; preds = %39
%44 = load i64, i64* @errno
%45 = load i64, i64* @EADDRINUSE
%46 = icmp eq i64 %44, %45
br i1 %46, label %51, label %47
47: ; preds = %43
%48 = load i64, i64* @errno
%49 = load i64, i64* @EADDRNOTAVAIL
%50 = icmp eq i64 %48, %49
br i1 %50, label %51, label %57
51: ; preds = %47, %43, %39
%52 = load i32, i32* %7
%53 = add nsw i32 %52, -1
store i32 %53, i32* %7
%54 = icmp sgt i32 %53, 512
br i1 %54, label %55, label %56
55: ; preds = %51
br label %15
56: ; preds = %51
br label %57
57: ; preds = %56, %47, %36
store i32 -1, i32* %3
br label %60
58: ; preds = %26
%59 = load i32, i32* %4
store i32 %59, i32* %3
br label %60
60: ; preds = %58, %57
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@EINVAL = global i64 0
@EADDRINUSE = global i64 0
@EADDRNOTAVAIL = global i64 0
define i32 @bind_sock(i32 %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = icmp eq i32 %1, -1
%6 = select i1 %5, i32 1023, i32 %1
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
%10 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %11
11: ; preds = %25, %2
%12 = phi i32 [ %6, %2 ], [ %34, %25 ]
%13 = call i32 @bzero(i8* %4, i32 12)
%14 = load i32, i32* @AF_INET
store i32 %14, i32* %7
%15 = call i32 @htons(i32 %12)
store i32 %15, i32* %8
%16 = load i32, i32* @INADDR_ANY
store i32 %16, i32* %9
br label %17
17: ; preds = %20, %11
%18 = call i64 @bind(i32 %0, %struct.sockaddr* %10, i32 12)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %37
20: ; preds = %17
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
br i1 %23, label %17, label %24
24: ; preds = %20
br i1 %5, label %25, label %37
25: ; preds = %24
%26 = load i64, i64* @EINVAL
%27 = icmp eq i64 %21, %26
%28 = load i64, i64* @EADDRINUSE
%29 = icmp eq i64 %21, %28
%30 = select i1 %27, i1 true, i1 %29
%31 = load i64, i64* @EADDRNOTAVAIL
%32 = icmp eq i64 %21, %31
%33 = select i1 %30, i1 true, i1 %32
%34 = add nsw i32 %12, -1
%35 = icmp sgt i32 %12, 513
%36 = and i1 %35, %33
br i1 %36, label %11, label %37
37: ; preds = %24, %25, %17
%38 = phi i32 [ %0, %17 ], [ -1, %25 ], [ -1, %24 ]
ret i32 %38
}