#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__ EINTR;
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 BMI_sockio_bind_sock(int sockd, int service) {
  struct sockaddr_in saddr;
  bzero((char *)&saddr, sizeof(saddr));
  saddr.sin_family = AF_INET;
  saddr.sin_port = htons((u_short)service);
  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;
    return (-1);
  }
  return (sockd);
}


x86 assembly (clang, -O0):
<BMI_sockio_bind_sock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   1050 <bzero@plt>
mov    0x2e95(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e76(%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    11b5 <BMI_sockio_bind_sock+0x85>
mov    0x2e3f(%rip),%rax
mov    (%rax),%rax
mov    0x2e2d(%rip),%rcx
cmp    (%rcx),%rax
jne    11a9 <BMI_sockio_bind_sock+0x79>
jmp    116f <BMI_sockio_bind_sock+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bb <BMI_sockio_bind_sock+0x8b>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BMI_sockio_bind_sock>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   1050 <bzero@plt>
mov    0x2e91(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e54(%rip),%r15
mov    0x2e45(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1040 <bind@plt>
test   %rax,%rax
jns    11b2 <BMI_sockio_bind_sock+0x82>
mov    (%r15),%rax
cmp    0x0(%rbp),%rax
je     1190 <BMI_sockio_bind_sock+0x60>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
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
define i32 @BMI_sockio_bind_sock(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
store i32 %0, i32* %4
store i32 %1, i32* %5
%7 = bitcast %struct.sockaddr_in* %6 to i8*
%8 = call i32 @bzero(i8* %7, i32 12)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %9, i32* %10
%11 = load i32, i32* %5
%12 = call i32 @htons(i32 %11)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %12, i32* %13
%14 = load i32, i32* @INADDR_ANY
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%16 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %15, i32 0, i32 0
store i32 %14, i32* %16
br label %17
17: ; preds = %26, %2
%18 = load i32, i32* %4
%19 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%20 = call i64 @bind(i32 %18, %struct.sockaddr* %19, i32 12)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %17
%23 = load i64, i64* @errno
%24 = load i64, i64* @EINTR
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22
br label %17
27: ; preds = %22
store i32 -1, i32* %3
br label %30
28: ; preds = %17
%29 = load i32, i32* %4
store i32 %29, i32* %3
br label %30
30: ; preds = %28, %27
%31 = load i32, i32* %3
ret i32 %31
}

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
define i32 @BMI_sockio_bind_sock(i32 %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = call i32 @bzero(i8* %4, i32 12)
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %6, i32* %7
%8 = tail call i32 @htons(i32 %1)
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %8, i32* %9
%10 = load i32, i32* @INADDR_ANY
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %10, i32* %11
%12 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %13
13: ; preds = %16, %2
%14 = call i64 @bind(i32 %0, %struct.sockaddr* %12, i32 12)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINTR
%19 = icmp eq i64 %17, %18
br i1 %19, label %13, label %20
20: ; preds = %13, %16
%21 = phi i32 [ -1, %16 ], [ %0, %13 ]
ret i32 %21
}