#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;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
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 socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int _conn_event_port(char *host, int port) {
  int 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);
  if (bind(sock_fd, (struct sockaddr *)&wiki_addr, sizeof(wiki_addr))) {
    printf("WARNING: bind to port %i failed, may not be real error\n", port);
    return -1;
  }
  listen(sock_fd, 1);
  return sock_fd;
}


x86 assembly (clang, -O0):
<_conn_event_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11dd <_conn_event_port+0x3d>
lea    0xe32(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
mov    0x2dea(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    121f <_conn_event_port+0x7f>
lea    0xdfe(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x24(%rbp)
lea    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
je     1290 <_conn_event_port+0xf0>
mov    -0x14(%rbp),%esi
lea    0xd96(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a3 <_conn_event_port+0x103>
mov    -0x18(%rbp),%edi
mov    $0x1,%esi
call   1070 <listen@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_conn_event_port>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     1262 <_conn_event_port+0xc2>
mov    %rax,%rbx
mov    0x2e21(%rip),%r12
mov    (%r12),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     126b <_conn_event_port+0xcb>
mov    %eax,%ebp
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %r15,%rdi
call   1060 <memcpy@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
je     1247 <_conn_event_port+0xa7>
lea    0xddd(%rip),%rdi
mov    %r14d,%esi
call   1040 <printf@plt>
mov    $0xffffffff,%ebp
jmp    1253 <_conn_event_port+0xb3>
mov    %ebp,%edi
mov    $0x1,%esi
call   1070 <listen@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd97(%rip),%rdi
jmp    1272 <_conn_event_port+0xd2>
lea    0xd9c(%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"
@.str.2 = [56 x i8] c"WARNING: bind to port %i failed, may not be real error\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 %struct.sockaddr_in
%8 = alloca %struct.hostent*
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = call %struct.hostent* @gethostbyname(i8* %9)
store %struct.hostent* %10, %struct.hostent** %8
%11 = load %struct.hostent*, %struct.hostent** %8
%12 = icmp eq %struct.hostent* %11, null
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = load i32, i32* @IPPROTO_TCP
%20 = call i32 @socket(i32 %17, i32 %18, i32 %19)
store i32 %20, i32* %6
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %16
%26 = bitcast %struct.sockaddr_in* %7 to i8*
%27 = call i32 @bzero(i8* %26, i32 12)
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %28, i32* %29
%30 = load i32, i32* %5
%31 = call i32 @htons(i32 %30)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %31, i32* %32
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%34 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %33, i32 0, i32 0
%35 = load %struct.hostent*, %struct.hostent** %8
%36 = getelementptr inbounds %struct.hostent, %struct.hostent* %35, i32 0, i32 1
%37 = load i32, i32* %36
%38 = load %struct.hostent*, %struct.hostent** %8
%39 = getelementptr inbounds %struct.hostent, %struct.hostent* %38, i32 0, i32 0
%40 = load i32, i32* %39
%41 = call i32 @memcpy(i32* %34, i32 %37, i32 %40)
%42 = load i32, i32* %6
%43 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%44 = call i64 @bind(i32 %42, %struct.sockaddr* %43, i32 12)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %25
%47 = load i32, i32* %5
%48 = call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0), i32 %47)
store i32 -1, i32* %3
br label %53
49: ; preds = %25
%50 = load i32, i32* %6
%51 = call i32 @listen(i32 %50, i32 1)
%52 = load i32, i32* %6
store i32 %52, i32* %3
br label %53
53: ; preds = %49, %46
%54 = load i32, i32* %3
ret i32 %54
}

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"
@.str.2 = [56 x i8] c"WARNING: bind to port %i failed, may not be real error\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*
%32 = call i64 @bind(i32 %14, %struct.sockaddr* %31, i32 12)
%33 = icmp eq i64 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %19
%35 = call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0), i32 %1)
br label %38
36: ; preds = %19
%37 = call i32 @listen(i32 %14, i32 1)
br label %38
38: ; preds = %36, %34
%39 = phi i32 [ -1, %34 ], [ %14, %36 ]
ret i32 %39
}