#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 */
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);
scalar_t__ errno;
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char const *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int sock_bind(int sock, const char *hostname, int port) {
  struct sockaddr_in saddr;
  memset((char *)&saddr, 0, sizeof(saddr));
  saddr.sin_family = AF_INET;
  saddr.sin_port = htons(port);
  if (hostname)
    saddr.sin_addr.s_addr = inet_addr(hostname);
  else
    saddr.sin_addr.s_addr = htonl(INADDR_ANY);
bind_sock_restart:
  if (bind(sock, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
    if (errno == EINTR)
      goto bind_sock_restart;
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<sock_bind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11a5 <sock_bind+0x55>
mov    -0x10(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %eax,-0x20(%rbp)
jmp    11b6 <sock_bind+0x66>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,-0x20(%rbp)
jmp    11bb <sock_bind+0x6b>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    1201 <sock_bind+0xb1>
mov    0x2df3(%rip),%rax
mov    (%rax),%rax
mov    0x2de1(%rip),%rcx
cmp    (%rcx),%rax
jne    11f5 <sock_bind+0xa5>
jmp    11bb <sock_bind+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1207 <sock_bind+0xb7>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_bind>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r15d
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
test   %r14,%r14
je     119a <sock_bind+0x4a>
mov    %r14,%rdi
call   1060 <inet_addr@plt>
jmp    11a8 <sock_bind+0x58>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%r14
mov    0x2e18(%rip),%rbp
mov    0x2e09(%rip),%rbx
nop
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
jns    11e4 <sock_bind+0x94>
mov    0x0(%rbp),%rax
cmp    (%rbx),%rax
je     11c0 <sock_bind+0x70>
mov    $0xffffffff,%r15d
mov    %r15d,%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 @sock_bind(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = bitcast %struct.sockaddr_in* %8 to i8*
%10 = call i32 @memset(i8* %9, i32 0, i32 12)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %11, i32* %12
%13 = load i32, i32* %7
%14 = call i32 @htons(i32 %13)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %14, i32* %15
%16 = load i8*, i8** %6
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %23
18: ; preds = %3
%19 = load i8*, i8** %6
%20 = call i32 @inet_addr(i8* %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
store i32 %20, i32* %22
br label %28
23: ; preds = %3
%24 = load i32, i32* @INADDR_ANY
%25 = call i32 @htonl(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %26, i32 0, i32 0
store i32 %25, i32* %27
br label %28
28: ; preds = %23, %18
br label %29
29: ; preds = %38, %28
%30 = load i32, i32* %5
%31 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%32 = call i64 @bind(i32 %30, %struct.sockaddr* %31, i32 12)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %29
%35 = load i64, i64* @errno
%36 = load i64, i64* @EINTR
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %39
38: ; preds = %34
br label %29
39: ; preds = %34
store i32 -1, i32* %4
br label %42
40: ; preds = %29
%41 = load i32, i32* %5
store i32 %41, i32* %4
br label %42
42: ; preds = %40, %39
%43 = load i32, i32* %4
ret i32 %43
}

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