#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__ {
  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_list;
};
typedef int /*<<< orphan*/ server;
typedef int /*<<< orphan*/ opt;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(short);
int listen(int, int);
int /*<<< orphan*/ memcpy(TYPE_1__ *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int create_tcp_listener(const char *host, int port) {
  int sfd;
  struct sockaddr_in server;
  struct hostent *hptr;
  int opt = 1;
  if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    return -1;
  }
  if ((hptr = gethostbyname(host)) == NULL) {
    fprintf(stderr, "Could not gethostbyname(%s)\n", host);
    return -1;
  }
  memcpy(&server.sin_addr, hptr->h_addr_list[0], hptr->h_length);
  server.sin_family = AF_INET;
  server.sin_port = htons((short)port);
  server.sin_addr.s_addr = INADDR_ANY;
  if ((bind(sfd, (struct sockaddr *)&server, sizeof(server)) == -1)) {
    perror("bind");
    close(sfd);
    return -1;
  }
  if ((setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1)) {
    perror("setsockopt");
    close(sfd);
    return -1;
  }
  if ((listen(sfd, 10) == -1)) {
    perror("listen");
    close(sfd);
    return -1;
  }
  return sfd;
}


x86 assembly (clang, -O0):
<create_tcp_listener>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x1,-0x34(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    11f3 <create_tcp_listener+0x53>
lea    0xe1e(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1331 <create_tcp_listener+0x191>
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    122f <create_tcp_listener+0x8f>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xde9(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1331 <create_tcp_listener+0x191>
lea    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1080 <memcpy@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
movswl %ax,%edi
call   1040 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2d6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12a8 <create_tcp_listener+0x108>
lea    0xd95(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1331 <create_tcp_listener+0x191>
mov    -0x18(%rbp),%edi
mov    0x2d36(%rip),%rax
mov    (%rax),%esi
mov    0x2cfd(%rip),%rax
mov    (%rax),%edx
lea    -0x34(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    12f5 <create_tcp_listener+0x155>
lea    0xd4d(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1331 <create_tcp_listener+0x191>
mov    -0x18(%rbp),%edi
mov    $0xa,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    132b <create_tcp_listener+0x18b>
lea    0xd22(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1331 <create_tcp_listener+0x191>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_tcp_listener>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
movl   $0x1,0x4(%rsp)
mov    0x2e21(%rip),%r15
mov    (%r15),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     1295 <create_tcp_listener+0xf5>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     127b <create_tcp_listener+0xdb>
mov    0x8(%rax),%rcx
mov    (%rcx),%esi
mov    (%rax),%edx
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1080 <memcpy@plt>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
movswl %r14w,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
je     12a3 <create_tcp_listener+0x103>
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
mov    0x2d70(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     12ac <create_tcp_listener+0x10c>
mov    %ebx,%edi
mov    $0xa,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    12c4 <create_tcp_listener+0x124>
lea    0xdbb(%rip),%rdi
jmp    12b3 <create_tcp_listener+0x113>
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
lea    0xd7c(%rip),%rsi
mov    %rbp,%rdx
call   1070 <fprintf@plt>
jmp    12bf <create_tcp_listener+0x11f>
lea    0xd64(%rip),%rdi
call   10b0 <perror@plt>
jmp    12bf <create_tcp_listener+0x11f>
lea    0xd7a(%rip),%rdi
jmp    12b3 <create_tcp_listener+0x113>
lea    0xd76(%rip),%rdi
call   10b0 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"Could not gethostbyname(%s)\0A\00"
@INADDR_ANY = global i32 0
@.str.2 = [5 x i8] c"bind\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.3 = [11 x i8] c"setsockopt\00"
@.str.4 = [7 x i8] c"listen\00"
define i32 @create_tcp_listener(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*
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 1, i32* %9
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %72
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = call %struct.hostent* @gethostbyname(i8* %17)
store %struct.hostent* %18, %struct.hostent** %8
%19 = icmp eq %struct.hostent* %18, null
br i1 %19, label %20, label %24
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %4
%23 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %22)
store i32 -1, i32* %3
br label %72
24: ; preds = %16
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%26 = load %struct.hostent*, %struct.hostent** %8
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %26, i32 0, i32 1
%28 = load i32*, i32** %27
%29 = getelementptr inbounds i32, i32* %28, i64 0
%30 = load i32, i32* %29
%31 = load %struct.hostent*, %struct.hostent** %8
%32 = getelementptr inbounds %struct.hostent, %struct.hostent* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @memcpy(%struct.TYPE_2__* %25, i32 %30, i32 %33)
%35 = load i32, i32* @AF_INET
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %35, i32* %36
%37 = load i32, i32* %5
%38 = trunc i32 %37 to i16
%39 = call i32 @htons(i16 signext %38)
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %39, i32* %40
%41 = load i32, i32* @INADDR_ANY
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%43 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %42, i32 0, i32 0
store i32 %41, i32* %43
%44 = load i32, i32* %6
%45 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%46 = call i32 @bind(i32 %44, %struct.sockaddr* %45, i32 12)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %52
48: ; preds = %24
%49 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%50 = load i32, i32* %6
%51 = call i32 @close(i32 %50)
store i32 -1, i32* %3
br label %72
52: ; preds = %24
%53 = load i32, i32* %6
%54 = load i32, i32* @SOL_SOCKET
%55 = load i32, i32* @SO_REUSEADDR
%56 = call i32 @setsockopt(i32 %53, i32 %54, i32 %55, i32* %9, i32 4)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %62
58: ; preds = %52
%59 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%60 = load i32, i32* %6
%61 = call i32 @close(i32 %60)
store i32 -1, i32* %3
br label %72
62: ; preds = %52
%63 = load i32, i32* %6
%64 = call i32 @listen(i32 %63, i32 10)
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %70
66: ; preds = %62
%67 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%68 = load i32, i32* %6
%69 = call i32 @close(i32 %68)
store i32 -1, i32* %3
br label %72
70: ; preds = %62
%71 = load i32, i32* %6
store i32 %71, i32* %3
br label %72
72: ; preds = %70, %66, %58, %48, %20, %14
%73 = load i32, i32* %3
ret i32 %73
}

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 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"Could not gethostbyname(%s)\0A\00"
@INADDR_ANY = global i32 0
@.str.2 = [5 x i8] c"bind\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.3 = [11 x i8] c"setsockopt\00"
@.str.4 = [7 x i8] c"listen\00"
define i32 @create_tcp_listener(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %54
13: ; preds = %2
%14 = tail call %struct.hostent* @gethostbyname(i8* %0)
%15 = icmp eq %struct.hostent* %14, null
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %54
19: ; preds = %13
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 1
%22 = load i32*, i32** %21
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @memcpy(%struct.TYPE_2__* %20, i32 %23, i32 %25)
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %27, i32* %28
%29 = trunc i32 %1 to i16
%30 = call i32 @htons(i16 signext %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %30, i32* %31
%32 = load i32, i32* @INADDR_ANY
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %32, i32* %33
%34 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%35 = call i32 @bind(i32 %9, %struct.sockaddr* %34, i32 12)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %40
37: ; preds = %19
%38 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%39 = call i32 @close(i32 %9)
br label %54
40: ; preds = %19
%41 = load i32, i32* @SOL_SOCKET
%42 = load i32, i32* @SO_REUSEADDR
%43 = call i32 @setsockopt(i32 %9, i32 %41, i32 %42, i32* %4, i32 4)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %40
%46 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%47 = call i32 @close(i32 %9)
br label %54
48: ; preds = %40
%49 = call i32 @listen(i32 %9, i32 10)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%53 = call i32 @close(i32 %9)
br label %54
54: ; preds = %48, %51, %45, %37, %16, %11
%55 = phi i32 [ -1, %11 ], [ -1, %16 ], [ -1, %37 ], [ -1, %45 ], [ -1, %51 ], [ %9, %48 ]
ret i32 %55
}