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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(unsigned int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printerror(int, char *, char *, char *, ...);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
int bind_to_port(char *interface, unsigned int port) {
  struct sockaddr_in saddr;
  int sock;
  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    printerror(1, "-ERR", "can't create socket: %s", strerror(errno));
  else {
    int opt;
    opt = 1;
    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  }
  memset(&saddr, 0, sizeof(saddr));
  saddr.sin_family = AF_INET;
  saddr.sin_port = htons(port);
  if (interface == NULL || *interface == 0)
    interface = "0.0.0.0";
  else {
    struct hostent *ifp;
    ifp = gethostbyname(interface);
    if (ifp == NULL)
      printerror(1, "-ERR", "can't lookup %s", interface);
    memcpy(&saddr.sin_addr, ifp->h_addr, ifp->h_length);
  }
  if (bind(sock, (struct sockaddr *)&saddr, sizeof(saddr)))
    printerror(1, "-ERR", "can't bind to %s:%u", interface, port);
  if (listen(sock, 5) < 0)
    printerror(1, "-ERR", "listen error:  %s", strerror(errno));
  return (sock);
}


x86 assembly (clang, -O0):
<bind_to_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1204 <bind_to_port+0x64>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rcx
mov    $0x1,%edi
lea    0xe0f(%rip),%rsi
lea    0xe0d(%rip),%rdx
mov    $0x0,%al
call   1030 <printerror@plt>
jmp    122f <bind_to_port+0x8f>
movl   $0x1,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
mov    0x2da2(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x14(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1271 <bind_to_port+0xd1>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1281 <bind_to_port+0xe1>
lea    0xda5(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    12cd <bind_to_port+0x12d>
mov    -0x8(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12b7 <bind_to_port+0x117>
mov    -0x8(%rbp),%rcx
mov    $0x1,%edi
lea    0xd57(%rip),%rsi
lea    0xd75(%rip),%rdx
mov    $0x0,%al
call   1030 <printerror@plt>
lea    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1080 <memcpy@plt>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
je     130a <bind_to_port+0x16a>
mov    -0x8(%rbp),%rcx
mov    -0xc(%rbp),%r8d
mov    $0x1,%edi
lea    0xd04(%rip),%rsi
lea    0xd32(%rip),%rdx
mov    $0x0,%al
call   1030 <printerror@plt>
mov    -0x1c(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
jge    134c <bind_to_port+0x1ac>
mov    0x2ca0(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rcx
mov    $0x1,%edi
lea    0xcc2(%rip),%rsi
lea    0xd04(%rip),%rdx
mov    $0x0,%al
call   1030 <printerror@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bind_to_port>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
mov    0x2e29(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11fe <bind_to_port+0x5e>
movl   $0x1,0x8(%rsp)
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    0x2dd8(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
jmp    1229 <bind_to_port+0x89>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xded(%rip),%rsi
lea    0xdeb(%rip),%rdx
mov    $0x1,%edi
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <printerror@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    %r14d,%edi
call   1050 <htons@plt>
mov    %eax,0xc(%rsp)
lea    0xdc9(%rip),%rbp
test   %r15,%r15
je     129f <bind_to_port+0xff>
cmpb   $0x0,(%r15)
je     129f <bind_to_port+0xff>
mov    %r15,%rdi
call   1070 <gethostbyname@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    128c <bind_to_port+0xec>
lea    0xd8a(%rip),%rsi
lea    0xda8(%rip),%rdx
mov    $0x1,%edi
mov    %r15,%rcx
xor    %eax,%eax
call   1030 <printerror@plt>
mov    0x0(%rbp),%edx
mov    0x4(%rbp),%esi
lea    0x8(%rsp),%rdi
call   1080 <memcpy@plt>
mov    %r15,%rbp
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
je     12d5 <bind_to_port+0x135>
lea    0xd44(%rip),%rsi
lea    0xd72(%rip),%rdx
mov    $0x1,%edi
mov    %rbp,%rcx
mov    %r14d,%r8d
xor    %eax,%eax
call   1030 <printerror@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
test   %rax,%rax
jns    1311 <bind_to_port+0x171>
mov    0x2cdb(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd05(%rip),%rsi
lea    0xd47(%rip),%rdx
mov    $0x1,%edi
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <printerror@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [5 x i8] c"-ERR\00"
@.str.1 = [24 x i8] c"can't create socket: %s\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [8 x i8] c"0.0.0.0\00"
@.str.3 = [16 x i8] c"can't lookup %s\00"
@.str.4 = [20 x i8] c"can't bind to %s:%u\00"
@.str.5 = [18 x i8] c"listen error: %s\00"
define i32 @bind_to_port(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.hostent*
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 (i32, i8*, i8*, i8*, ...) @printerror(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %15)
br label %22
17: ; preds = %2
store i32 1, i32* %7
%18 = load i32, i32* %6
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_REUSEADDR
%21 = call i32 @setsockopt(i32 %18, i32 %19, i32 %20, i32* %7, i32 4)
br label %22
22: ; preds = %17, %13
%23 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %24, i32* %25
%26 = load i32, i32* %4
%27 = call i32 @htons(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %27, i32* %28
%29 = load i8*, i8** %3
%30 = icmp eq i8* %29, null
br i1 %30, label %36, label %31
31: ; preds = %22
%32 = load i8*, i8** %3
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %31, %22
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8** %3
br label %54
37: ; preds = %31
%38 = load i8*, i8** %3
%39 = call %struct.hostent* @gethostbyname(i8* %38)
store %struct.hostent* %39, %struct.hostent** %8
%40 = load %struct.hostent*, %struct.hostent** %8
%41 = icmp eq %struct.hostent* %40, null
br i1 %41, label %42, label %45
42: ; preds = %37
%43 = load i8*, i8** %3
%44 = call i32 (i32, i8*, i8*, i8*, ...) @printerror(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %43)
br label %45
45: ; preds = %42, %37
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%47 = load %struct.hostent*, %struct.hostent** %8
%48 = getelementptr inbounds %struct.hostent, %struct.hostent* %47, i32 0, i32 1
%49 = load i32, i32* %48
%50 = load %struct.hostent*, %struct.hostent** %8
%51 = getelementptr inbounds %struct.hostent, %struct.hostent* %50, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call i32 @memcpy(i32* %46, i32 %49, i32 %52)
br label %54
54: ; preds = %45, %36
%55 = load i32, i32* %6
%56 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%57 = call i64 @bind(i32 %55, %struct.sockaddr* %56, i32 12)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %63
59: ; preds = %54
%60 = load i8*, i8** %3
%61 = load i32, i32* %4
%62 = call i32 (i32, i8*, i8*, i8*, ...) @printerror(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %60, i32 %61)
br label %63
63: ; preds = %59, %54
%64 = load i32, i32* %6
%65 = call i64 @listen(i32 %64, i32 5)
%66 = icmp slt i64 %65, 0
br i1 %66, label %67, label %71
67: ; preds = %63
%68 = load i32, i32* @errno
%69 = call i8* @strerror(i32 %68)
%70 = call i32 (i32, i8*, i8*, i8*, ...) @printerror(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* %69)
br label %71
71: ; preds = %67, %63
%72 = load i32, i32* %6
ret i32 %72
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [5 x i8] c"-ERR\00"
@.str.1 = [24 x i8] c"can't create socket: %s\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [8 x i8] c"0.0.0.0\00"
@.str.3 = [16 x i8] c"can't lookup %s\00"
@.str.4 = [20 x i8] c"can't bind to %s:%u\00"
@.str.5 = [18 x i8] c"listen error: %s\00"
define i32 @bind_to_port(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
%13 = tail call i32 (i32, i8*, i8*, i8*, ...) @printerror(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %12)
br label %19
14: ; preds = %2
%15 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%16 = load i32, i32* @SOL_SOCKET
%17 = load i32, i32* @SO_REUSEADDR
%18 = call i32 @setsockopt(i32 %8, i32 %16, i32 %17, i32* %4, i32 4)
br label %19
19: ; preds = %14, %10
%20 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %21, i32* %22
%23 = call i32 @htons(i32 %1)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %23, i32* %24
%25 = icmp eq i8* %0, null
br i1 %25, label %41, label %26
26: ; preds = %19
%27 = load i8, i8* %0
%28 = icmp eq i8 %27, 0
br i1 %28, label %41, label %29
29: ; preds = %26
%30 = call %struct.hostent* @gethostbyname(i8* %0)
%31 = icmp eq %struct.hostent* %30, null
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 (i32, i8*, i8*, i8*, ...) @printerror(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %34
34: ; preds = %32, %29
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%36 = getelementptr inbounds %struct.hostent, %struct.hostent* %30, i64 0, i32 1
%37 = load i32, i32* %36
%38 = getelementptr inbounds %struct.hostent, %struct.hostent* %30, i64 0, i32 0
%39 = load i32, i32* %38
%40 = call i32 @memcpy(i32* %35, i32 %37, i32 %39)
br label %41
41: ; preds = %19, %26, %34
%42 = phi i8* [ %0, %34 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), %26 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), %19 ]
%43 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%44 = call i64 @bind(i32 %8, %struct.sockaddr* %43, i32 12)
%45 = icmp eq i64 %44, 0
br i1 %45, label %48, label %46
46: ; preds = %41
%47 = call i32 (i32, i8*, i8*, i8*, ...) @printerror(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %42, i32 %1)
br label %48
48: ; preds = %46, %41
%49 = call i64 @listen(i32 %8, i32 5)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %48
%52 = load i32, i32* @errno
%53 = call i8* @strerror(i32 %52)
%54 = call i32 (i32, i8*, i8*, i8*, ...) @printerror(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* %53)
br label %55
55: ; preds = %51, %48
ret i32 %8
}