#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*/ wiki_addr;
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;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EINVAL;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(scalar_t__);
int _conn_event_port(char *host, int port) {
  int i, rc, sock_fd;
  struct sockaddr_in wiki_addr;
  struct hostent *hptr;
  hptr = gethostbyname(host);
  if (hptr == NULL) {
    perror("gethostbyname");
    exit(1);
  }
  if ((sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
    perror("socket");
    exit(1);
  }
  bzero((char *)&wiki_addr, sizeof(wiki_addr));
  wiki_addr.sin_family = AF_INET;
  wiki_addr.sin_port = htons(port);
  memcpy(&wiki_addr.sin_addr.s_addr, hptr->h_addr, hptr->h_length);
  for (i = 0;; i++) {
    if (i)
      sleep(5);
    rc = bind(sock_fd, (struct sockaddr *)&wiki_addr, sizeof(wiki_addr));
    if (rc == 0)
      break;
    if ((errno != EINVAL) || (i > 5)) {
      printf("WARNING: bind to port %i; %s\n", port, strerror(errno));
      return -1;
    }
    printf("WARNING: port %i in use, retrying\n", port);
  }
  listen(sock_fd, 1);
  return sock_fd;
}


x86 assembly (clang, -O0):
<_conn_event_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11fd <_conn_event_port+0x3d>
lea    0xe12(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
mov    0x2dca(%rip),%rax
mov    (%rax),%edx
call   10e0 <socket@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    123f <_conn_event_port+0x7f>
lea    0xdde(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x30(%rbp),%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x2d94(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x2c(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
call   1060 <memcpy@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1295 <_conn_event_port+0xd5>
mov    $0x5,%edi
call   10d0 <sleep@plt>
mov    -0x20(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    12b8 <_conn_event_port+0xf8>
jmp    1330 <_conn_event_port+0x170>
mov    0x2d09(%rip),%rax
mov    (%rax),%rax
mov    0x2cf7(%rip),%rcx
cmp    (%rcx),%rax
jne    12dc <_conn_event_port+0x11c>
cmpl   $0x5,-0x18(%rbp)
jle    1311 <_conn_event_port+0x151>
mov    -0x14(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2cdf(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    -0x3c(%rbp),%esi
mov    %rax,%rdx
lea    0xd17(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1343 <_conn_event_port+0x183>
mov    -0x14(%rbp),%esi
lea    0xd18(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1281 <_conn_event_port+0xc1>
mov    -0x20(%rbp),%edi
mov    $0x1,%esi
call   1070 <listen@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_conn_event_port>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebx
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     12e2 <_conn_event_port+0x122>
mov    %rax,%r15
mov    0x2e00(%rip),%r14
mov    (%r14),%edi
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
mov    0x2de5(%rip),%rax
mov    (%rax),%edx
call   10e0 <socket@plt>
test   %eax,%eax
js     12eb <_conn_event_port+0x12b>
mov    %eax,%ebp
lea    0x8(%rsp),%r12
mov    %r12,%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    (%r15),%edx
mov    0x4(%r15),%esi
mov    %r12,%rdi
call   1060 <memcpy@plt>
mov    0x2d82(%rip),%r12
mov    0x2d73(%rip),%r13
lea    0xddf(%rip),%r15
xor    %r14d,%r14d
test   %r14d,%r14d
je     126a <_conn_event_port+0xaa>
nopl   0x0(%rax)
mov    $0x5,%edi
call   10d0 <sleep@plt>
mov    %ebp,%edi
lea    0x8(%rsp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %eax,%eax
je     12a6 <_conn_event_port+0xe6>
mov    (%r12),%rdi
cmp    0x0(%r13),%rdi
jne    12b4 <_conn_event_port+0xf4>
cmp    $0x6,%r14d
jae    12b4 <_conn_event_port+0xf4>
mov    %r15,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%r14d
test   %r14d,%r14d
jne    1260 <_conn_event_port+0xa0>
jmp    126a <_conn_event_port+0xaa>
mov    %ebp,%edi
mov    $0x1,%esi
call   1070 <listen@plt>
jmp    12d1 <_conn_event_port+0x111>
call   10c0 <strerror@plt>
lea    0xd55(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd17(%rip),%rdi
jmp    12f2 <_conn_event_port+0x132>
lea    0xd1c(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [14 x i8] c"gethostbyname\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@errno = global i64 0
@EINVAL = global i64 0
@.str.2 = [30 x i8] c"WARNING: bind to port %i; %s\0A\00"
@.str.3 = [35 x i8] c"WARNING: port %i in use, retrying\0A\00"
define i32 @_conn_event_port(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca %struct.hostent*
store i8* %0, i8** %4
store i32 %1, i32* %5
%11 = load i8*, i8** %4
%12 = call %struct.hostent* @gethostbyname(i8* %11)
store %struct.hostent* %12, %struct.hostent** %10
%13 = load %struct.hostent*, %struct.hostent** %10
%14 = icmp eq %struct.hostent* %13, null
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = load i32, i32* @IPPROTO_TCP
%22 = call i32 @socket(i32 %19, i32 %20, i32 %21)
store i32 %22, i32* %8
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %18
%28 = bitcast %struct.sockaddr_in* %9 to i8*
%29 = call i32 @bzero(i8* %28, i32 12)
%30 = load i32, i32* @AF_INET
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %30, i32* %31
%32 = load i32, i32* %5
%33 = call i32 @htons(i32 %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %33, i32* %34
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%36 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %35, i32 0, i32 0
%37 = load %struct.hostent*, %struct.hostent** %10
%38 = getelementptr inbounds %struct.hostent, %struct.hostent* %37, i32 0, i32 1
%39 = load i32, i32* %38
%40 = load %struct.hostent*, %struct.hostent** %10
%41 = getelementptr inbounds %struct.hostent, %struct.hostent* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 @memcpy(i32* %36, i32 %39, i32 %42)
store i32 0, i32* %6
br label %44
44: ; preds = %71, %27
%45 = load i32, i32* %6
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %44
%48 = call i32 @sleep(i32 5)
br label %49
49: ; preds = %47, %44
%50 = load i32, i32* %8
%51 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%52 = call i32 @bind(i32 %50, %struct.sockaddr* %51, i32 12)
store i32 %52, i32* %7
%53 = load i32, i32* %7
%54 = icmp eq i32 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %49
br label %74
56: ; preds = %49
%57 = load i64, i64* @errno
%58 = load i64, i64* @EINVAL
%59 = icmp ne i64 %57, %58
br i1 %59, label %63, label %60
60: ; preds = %56
%61 = load i32, i32* %6
%62 = icmp sgt i32 %61, 5
br i1 %62, label %63, label %68
63: ; preds = %60, %56
%64 = load i32, i32* %5
%65 = load i64, i64* @errno
%66 = call i8* @strerror(i64 %65)
%67 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %64, i8* %66)
store i32 -1, i32* %3
br label %78
68: ; preds = %60
%69 = load i32, i32* %5
%70 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %69)
br label %71
71: ; preds = %68
%72 = load i32, i32* %6
%73 = add nsw i32 %72, 1
store i32 %73, i32* %6
br label %44
74: ; preds = %55
%75 = load i32, i32* %8
%76 = call i32 @listen(i32 %75, i32 1)
%77 = load i32, i32* %8
store i32 %77, i32* %3
br label %78
78: ; preds = %74, %63
%79 = load i32, i32* %3
ret i32 %79
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [14 x i8] c"gethostbyname\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@errno = global i64 0
@EINVAL = global i64 0
@.str.2 = [30 x i8] c"WARNING: bind to port %i; %s\0A\00"
@.str.3 = [35 x i8] c"WARNING: port %i in use, retrying\0A\00"
define i32 @_conn_event_port(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = tail call %struct.hostent* @gethostbyname(i8* %0)
%6 = icmp eq %struct.hostent* %5, null
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = load i32, i32* @IPPROTO_TCP
%14 = tail call i32 @socket(i32 %11, i32 %12, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %10
%20 = call i32 @bzero(i8* %4, 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 = tail 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 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
%26 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 1
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @memcpy(i32* %25, i32 %27, i32 %29)
%31 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %32
32: ; preds = %49, %19
%33 = phi i32 [ 0, %19 ], [ %51, %49 ]
%34 = icmp eq i32 %33, 0
br i1 %34, label %37, label %35
35: ; preds = %32
%36 = call i32 @sleep(i32 5)
br label %37
37: ; preds = %35, %32
%38 = call i32 @bind(i32 %14, %struct.sockaddr* %31, i32 12)
%39 = icmp eq i32 %38, 0
br i1 %39, label %52, label %40
40: ; preds = %37
%41 = load i64, i64* @errno
%42 = load i64, i64* @EINVAL
%43 = icmp ne i64 %41, %42
%44 = icmp ugt i32 %33, 5
%45 = select i1 %43, i1 true, i1 %44
br i1 %45, label %46, label %49
46: ; preds = %40
%47 = call i8* @strerror(i64 %41)
%48 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %1, i8* %47)
br label %54
49: ; preds = %40
%50 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %1)
%51 = add nuw nsw i32 %33, 1
br label %32
52: ; preds = %37
%53 = call i32 @listen(i32 %14, i32 1)
br label %54
54: ; preds = %52, %46
%55 = phi i32 [ %14, %52 ], [ -1, %46 ]
ret i32 %55
}