#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__ {
  scalar_t__ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct in_addr {
  scalar_t__ s_addr;
};
struct hostent {
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
scalar_t__ INADDR_NONE;
int bind(int, struct sockaddr *, int);
struct hostent *gethostbyname(char const *);
scalar_t__ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(unsigned short);
scalar_t__ inet_addr(char const *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strlen(char const *);
int sock_bind(int *fd, unsigned short port, const char *addr) {
  struct sockaddr_in sa;
  struct hostent *host = NULL;
  memset(&sa, 0, sizeof(sa));
  sa.sin_family = AF_INET;
  if (!addr || !strlen(addr))
    sa.sin_addr.s_addr = htonl(INADDR_ANY);
  else {
    sa.sin_addr.s_addr = inet_addr(addr);
    if (sa.sin_addr.s_addr == INADDR_NONE) {
      host = gethostbyname(addr);
      if (host) {
        sa.sin_addr.s_addr = ((struct in_addr *)host->h_addr)->s_addr;
        host = NULL;
      } else
        return -1;
    }
  }
  sa.sin_port = htons(port);
  return bind((*fd), (struct sockaddr *)&sa, sizeof(sa));
}


x86 assembly (clang, -O0):
<sock_bind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x40(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11c8 <sock_bind+0x58>
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%eax
jne    11df <sock_bind+0x6f>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %rax,-0x30(%rbp)
jmp    1249 <sock_bind+0xd9>
mov    -0x20(%rbp),%rdi
call   1070 <inet_addr@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2de9(%rip),%rcx
cmp    (%rcx),%rax
jne    1244 <sock_bind+0xd4>
mov    -0x20(%rbp),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1233 <sock_bind+0xc3>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x40(%rbp)
jmp    123f <sock_bind+0xcf>
movl   $0xffffffff,-0x4(%rbp)
jmp    126c <sock_bind+0xfc>
jmp    1244 <sock_bind+0xd4>
jmp    1249 <sock_bind+0xd9>
movzwl -0x12(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
mov    $0x18,%edx
call   1090 <bind@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_bind>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%rbx
mov    %esi,%ebp
mov    %rdi,%r14
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
test   %rbx,%rbx
je     11dd <sock_bind+0x6d>
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
je     11dd <sock_bind+0x6d>
mov    %rbx,%rdi
call   1070 <inet_addr@plt>
mov    %rax,0x10(%rsp)
mov    0x2e1d(%rip),%rcx
cmp    (%rcx),%rax
jne    11f0 <sock_bind+0x80>
mov    %rbx,%rdi
call   1080 <gethostbyname@plt>
test   %rax,%rax
je     1217 <sock_bind+0xa7>
mov    (%rax),%rax
mov    (%rax),%rax
jmp    11eb <sock_bind+0x7b>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %rax,0x10(%rsp)
movzwl %bp,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
mov    $0x18,%edx
call   1090 <bind@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0xffffffff,%eax
jmp    120e <sock_bind+0x9e>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.hostent = type { i64 }
%struct.in_addr = type { i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@INADDR_NONE = global i64 0
define i32 @sock_bind(i32* %0, i16 zeroext %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i16
%7 = alloca i8*
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.hostent*
store i32* %0, i32** %5
store i16 %1, i16* %6
store i8* %2, i8** %7
store %struct.hostent* null, %struct.hostent** %9
%10 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 24)
%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 i8*, i8** %7
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %19
15: ; preds = %3
%16 = load i8*, i8** %7
%17 = call i32 @strlen(i8* %16)
%18 = icmp ne i32 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %15, %3
%20 = load i32, i32* @INADDR_ANY
%21 = call i64 @htonl(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
store i64 %21, i64* %23
br label %51
24: ; preds = %15
%25 = load i8*, i8** %7
%26 = call i64 @inet_addr(i8* %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i64 %26, i64* %28
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
%31 = load i64, i64* %30
%32 = load i64, i64* @INADDR_NONE
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %50
34: ; preds = %24
%35 = load i8*, i8** %7
%36 = call %struct.hostent* @gethostbyname(i8* %35)
store %struct.hostent* %36, %struct.hostent** %9
%37 = load %struct.hostent*, %struct.hostent** %9
%38 = icmp ne %struct.hostent* %37, null
br i1 %38, label %39, label %48
39: ; preds = %34
%40 = load %struct.hostent*, %struct.hostent** %9
%41 = getelementptr inbounds %struct.hostent, %struct.hostent* %40, i32 0, i32 0
%42 = load i64, i64* %41
%43 = inttoptr i64 %42 to %struct.in_addr*
%44 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %43, i32 0, i32 0
%45 = load i64, i64* %44
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%47 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %46, i32 0, i32 0
store i64 %45, i64* %47
store %struct.hostent* null, %struct.hostent** %9
br label %49
48: ; preds = %34
store i32 -1, i32* %4
br label %59
49: ; preds = %39
br label %50
50: ; preds = %49, %24
br label %51
51: ; preds = %50, %19
%52 = load i16, i16* %6
%53 = call i32 @htons(i16 zeroext %52)
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %53, i32* %54
%55 = load i32*, i32** %5
%56 = load i32, i32* %55
%57 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%58 = call i32 @bind(i32 %56, %struct.sockaddr* %57, i32 24)
store i32 %58, i32* %4
br label %59
59: ; preds = %51, %48
%60 = load i32, i32* %4
ret i32 %60
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.hostent = type { i64 }
%struct.in_addr = type { i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@INADDR_NONE = global i64 0
define i32 @sock_bind(i32* nocapture %0, i16 zeroext %1, i8* %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 24)
%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 = icmp eq i8* %2, null
br i1 %9, label %13, label %10
10: ; preds = %3
%11 = call i32 @strlen(i8* %2)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %10, %3
%14 = load i32, i32* @INADDR_ANY
%15 = call i64 @htonl(i32 %14)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i64 %15, i64* %16
br label %31
17: ; preds = %10
%18 = call i64 @inet_addr(i8* %2)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i64 %18, i64* %19
%20 = load i64, i64* @INADDR_NONE
%21 = icmp eq i64 %18, %20
br i1 %21, label %22, label %31
22: ; preds = %17
%23 = call %struct.hostent* @gethostbyname(i8* %2)
%24 = icmp eq %struct.hostent* %23, null
br i1 %24, label %37, label %25
25: ; preds = %22
%26 = getelementptr inbounds %struct.hostent, %struct.hostent* %23, i64 0, i32 0
%27 = load i64, i64* %26
%28 = inttoptr i64 %27 to %struct.in_addr*
%29 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %28, i64 0, i32 0
%30 = load i64, i64* %29
store i64 %30, i64* %19
br label %31
31: ; preds = %17, %25, %13
%32 = call i32 @htons(i16 zeroext %1)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %32, i32* %33
%34 = load i32, i32* %0
%35 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%36 = call i32 @bind(i32 %34, %struct.sockaddr* %35, i32 24)
br label %37
37: ; preds = %22, %31
%38 = phi i32 [ %36, %31 ], [ -1, %22 ]
ret i32 %38
}