#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;
int /*<<< orphan*/ exit(int);
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 | 0x040000, "-ERR", "can't create socket: %s",
               strerror(errno));
    exit(1);
  } 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 | 0x040000, "-ERR", "can't lookup %s", interface);
      exit(1);
    }
    memcpy(&saddr.sin_addr, ifp->h_addr, ifp->h_length);
  }
  if (bind(sock, (struct sockaddr *)&saddr, sizeof(saddr))) {
    printerror(1 | 0x040000, "-ERR", "can't bind to %s:%u", interface, port);
    exit(1);
  }
  if (listen(sock, 5) < 0) {
    printerror(1 | 0x040000, "-ERR", "listen error: %s", strerror(errno));
    exit(1);
  }
  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    0x2e1a(%rip),%rax
mov    (%rax),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1219 <bind_to_port+0x69>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rcx
mov    $0x40001,%edi
lea    0xdff(%rip),%rsi
lea    0xdfd(%rip),%rdx
mov    $0x0,%al
call   1030 <printerror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
movl   $0x1,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
mov    0x2d8d(%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    0x2d85(%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     1286 <bind_to_port+0xd6>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1296 <bind_to_port+0xe6>
lea    0xd90(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    12ec <bind_to_port+0x13c>
mov    -0x8(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12d6 <bind_to_port+0x126>
mov    -0x8(%rbp),%rcx
mov    $0x40001,%edi
lea    0xd42(%rip),%rsi
lea    0xd60(%rip),%rdx
mov    $0x0,%al
call   1030 <printerror@plt>
mov    $0x1,%edi
call   10b0 <exit@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     1333 <bind_to_port+0x183>
mov    -0x8(%rbp),%rcx
mov    -0xc(%rbp),%r8d
mov    $0x40001,%edi
lea    0xce5(%rip),%rsi
lea    0xd13(%rip),%rdx
mov    $0x0,%al
call   1030 <printerror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
jge    137f <bind_to_port+0x1cf>
mov    0x2c77(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rcx
mov    $0x40001,%edi
lea    0xc99(%rip),%rsi
lea    0xcdb(%rip),%rdx
mov    $0x0,%al
call   1030 <printerror@plt>
mov    $0x1,%edi
call   10b0 <exit@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,%rbx
mov    0x2e19(%rip),%r15
mov    (%r15),%edi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     129c <bind_to_port+0xec>
mov    %eax,%ebp
movl   $0x1,0x8(%rsp)
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
mov    0x2dc4(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    %r14d,%edi
call   1050 <htons@plt>
mov    %eax,0xc(%rsp)
lea    0xde2(%rip),%r15
test   %rbx,%rbx
je     1268 <bind_to_port+0xb8>
cmpb   $0x0,(%rbx)
je     1268 <bind_to_port+0xb8>
mov    %rbx,%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     1319 <bind_to_port+0x169>
mov    (%rax),%edx
mov    0x4(%rax),%esi
lea    0x8(%rsp),%rdi
call   1080 <memcpy@plt>
mov    %rbx,%r15
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
jne    12ba <bind_to_port+0x10a>
mov    %ebp,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
test   %rax,%rax
js     12e4 <bind_to_port+0x134>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd4f(%rip),%rsi
lea    0xd4d(%rip),%rdx
jmp    1300 <bind_to_port+0x150>
lea    0xd3f(%rip),%rsi
lea    0xd6d(%rip),%rdx
mov    $0x40001,%edi
mov    %r15,%rcx
mov    %r14d,%r8d
xor    %eax,%eax
call   1030 <printerror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2cdd(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd07(%rip),%rsi
lea    0xd49(%rip),%rdx
mov    $0x40001,%edi
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <printerror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xce0(%rip),%rsi
lea    0xcfe(%rip),%rdx
mov    $0x40001,%edi
mov    %rbx,%rcx
jmp    1308 <bind_to_port+0x158>

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 = [17 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 %18
13: ; preds = %2
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 (i32, i8*, i8*, i8*, ...) @printerror(i32 262145, 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)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
store i32 1, i32* %7
%19 = load i32, i32* %6
%20 = load i32, i32* @SOL_SOCKET
%21 = load i32, i32* @SO_REUSEADDR
%22 = call i32 @setsockopt(i32 %19, i32 %20, i32 %21, i32* %7, i32 4)
br label %23
23: ; preds = %18
%24 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %25, i32* %26
%27 = load i32, i32* %4
%28 = call i32 @htons(i32 %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %28, i32* %29
%30 = load i8*, i8** %3
%31 = icmp eq i8* %30, null
br i1 %31, label %37, label %32
32: ; preds = %23
%33 = load i8*, i8** %3
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %32, %23
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8** %3
br label %56
38: ; preds = %32
%39 = load i8*, i8** %3
%40 = call %struct.hostent* @gethostbyname(i8* %39)
store %struct.hostent* %40, %struct.hostent** %8
%41 = load %struct.hostent*, %struct.hostent** %8
%42 = icmp eq %struct.hostent* %41, null
br i1 %42, label %43, label %47
43: ; preds = %38
%44 = load i8*, i8** %3
%45 = call i32 (i32, i8*, i8*, i8*, ...) @printerror(i32 262145, 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* %44)
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %38
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%49 = load %struct.hostent*, %struct.hostent** %8
%50 = getelementptr inbounds %struct.hostent, %struct.hostent* %49, i32 0, i32 1
%51 = load i32, i32* %50
%52 = load %struct.hostent*, %struct.hostent** %8
%53 = getelementptr inbounds %struct.hostent, %struct.hostent* %52, i32 0, i32 0
%54 = load i32, i32* %53
%55 = call i32 @memcpy(i32* %48, i32 %51, i32 %54)
br label %56
56: ; preds = %47, %37
%57 = load i32, i32* %6
%58 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%59 = call i64 @bind(i32 %57, %struct.sockaddr* %58, i32 12)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %66
61: ; preds = %56
%62 = load i8*, i8** %3
%63 = load i32, i32* %4
%64 = call i32 (i32, i8*, i8*, i8*, ...) @printerror(i32 262145, 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* %62, i32 %63)
%65 = call i32 @exit(i32 1)
unreachable
66: ; preds = %56
%67 = load i32, i32* %6
%68 = call i64 @listen(i32 %67, i32 5)
%69 = icmp slt i64 %68, 0
br i1 %69, label %70, label %75
70: ; preds = %66
%71 = load i32, i32* @errno
%72 = call i8* @strerror(i32 %71)
%73 = call i32 (i32, i8*, i8*, i8*, ...) @printerror(i32 262145, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* %72)
%74 = call i32 @exit(i32 1)
unreachable
75: ; preds = %66
%76 = load i32, i32* %6
ret i32 %76
}

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 = [17 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 %15
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 262145, 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)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%17 = load i32, i32* @SOL_SOCKET
%18 = load i32, i32* @SO_REUSEADDR
%19 = call i32 @setsockopt(i32 %8, i32 %17, i32 %18, i32* %4, i32 4)
%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 %42, label %26
26: ; preds = %15
%27 = load i8, i8* %0
%28 = icmp eq i8 %27, 0
br i1 %28, label %42, 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 %35
32: ; preds = %29
%33 = call i32 (i32, i8*, i8*, i8*, ...) @printerror(i32 262145, 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)
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %29
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%37 = getelementptr inbounds %struct.hostent, %struct.hostent* %30, i64 0, i32 1
%38 = load i32, i32* %37
%39 = getelementptr inbounds %struct.hostent, %struct.hostent* %30, i64 0, i32 0
%40 = load i32, i32* %39
%41 = call i32 @memcpy(i32* %36, i32 %38, i32 %40)
br label %42
42: ; preds = %15, %26, %35
%43 = phi i8* [ %0, %35 ], [ 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), %15 ]
%44 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%45 = call i64 @bind(i32 %8, %struct.sockaddr* %44, i32 12)
%46 = icmp eq i64 %45, 0
br i1 %46, label %50, label %47
47: ; preds = %42
%48 = call i32 (i32, i8*, i8*, i8*, ...) @printerror(i32 262145, 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* %43, i32 %1)
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %42
%51 = call i64 @listen(i32 %8, i32 5)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %58
53: ; preds = %50
%54 = load i32, i32* @errno
%55 = call i8* @strerror(i32 %54)
%56 = call i32 (i32, i8*, i8*, i8*, ...) @printerror(i32 262145, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* %55)
%57 = call i32 @exit(i32 1)
unreachable
58: ; preds = %50
ret i32 %8
}