#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 addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
scalar_t__ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
struct addrinfo *createAddrInfo(char *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ log_err(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int startListener(char *listenaddress, int listenport) {
  int listenSocket = 0;
  struct addrinfo *res = createAddrInfo(listenaddress, listenport);
  if (res == NULL) {
    log_err("clients control thread createAddrInfo error!");
    freeaddrinfo(res);
    listenSocket = -1;
    return listenSocket;
  }
  listenSocket = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  if (listenSocket == -1) {
    log_err("fail to create clients listener socket %s", strerror(errno));
    freeaddrinfo(res);
    return listenSocket;
  }
  if (bind(listenSocket, res->ai_addr, res->ai_addrlen) < 0) {
    log_err("clients listener unable to bind %s", strerror(errno));
    close(listenSocket);
    freeaddrinfo(res);
    listenSocket = -1;
    return listenSocket;
  }
  if (listen(listenSocket, 0) < 0) {
    log_err("clients listener unable to listen");
    freeaddrinfo(res);
    close(listenSocket);
    listenSocket = -1;
    return listenSocket;
  }
  freeaddrinfo(res);
  return (listenSocket);
}


x86 assembly (clang, -O0):
<startListener>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <createAddrInfo@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11da <startListener+0x5a>
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1080 <log_err@plt>
mov    -0x20(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
movl   $0xffffffff,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12e7 <startListener+0x167>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x20(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1233 <startListener+0xb3>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %eax,%esi
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1080 <log_err@plt>
mov    -0x20(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12e7 <startListener+0x167>
mov    -0x18(%rbp),%edi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1050 <bind@plt>
cmp    $0x0,%rax
jge    1293 <startListener+0x113>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %eax,%esi
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1080 <log_err@plt>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
mov    -0x20(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
movl   $0xffffffff,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12e7 <startListener+0x167>
mov    -0x18(%rbp),%edi
xor    %esi,%esi
call   1040 <listen@plt>
cmp    $0x0,%rax
jge    12d8 <startListener+0x158>
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1080 <log_err@plt>
mov    -0x20(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12e7 <startListener+0x167>
mov    -0x20(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<startListener>:
push   %rbp
push   %rbx
push   %rax
call   1060 <createAddrInfo@plt>
test   %rax,%rax
je     11ce <startListener+0x4e>
mov    %rax,%rbx
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     11e0 <startListener+0x60>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1050 <bind@plt>
test   %rax,%rax
js     1200 <startListener+0x80>
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <listen@plt>
test   %rax,%rax
js     123b <startListener+0xbb>
mov    %rbx,%rdi
call   1090 <freeaddrinfo@plt>
jmp    1232 <startListener+0xb2>
lea    0xe2b(%rip),%rdi
xor    %eax,%eax
call   1080 <log_err@plt>
xor    %edi,%edi
jmp    1228 <startListener+0xa8>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe38(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <log_err@plt>
jmp    1225 <startListener+0xa5>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe42(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <log_err@plt>
mov    %ebp,%edi
call   1030 <close@plt>
mov    %rbx,%rdi
call   1090 <freeaddrinfo@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe38(%rip),%rdi
xor    %eax,%eax
call   1080 <log_err@plt>
mov    %rbx,%rdi
call   1090 <freeaddrinfo@plt>
mov    %ebp,%edi
call   1030 <close@plt>
jmp    122d <startListener+0xad>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@.str = [45 x i8] c"clients control thread createAddrInfo error!\00"
@.str.1 = [42 x i8] c"fail to create clients listener socket %s\00"
@errno = global i32 0
@.str.2 = [35 x i8] c"clients listener unable to bind %s\00"
@.str.3 = [34 x i8] c"clients listener unable to listen\00"
define i32 @startListener(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.addrinfo*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %6
%8 = load i8*, i8** %4
%9 = load i32, i32* %5
%10 = call %struct.addrinfo* @createAddrInfo(i8* %8, i32 %9)
store %struct.addrinfo* %10, %struct.addrinfo** %7
%11 = load %struct.addrinfo*, %struct.addrinfo** %7
%12 = icmp eq %struct.addrinfo* %11, null
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = call i32 (i8*, ...) @log_err(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%15 = load %struct.addrinfo*, %struct.addrinfo** %7
%16 = call i32 @freeaddrinfo(%struct.addrinfo* %15)
store i32 -1, i32* %6
%17 = load i32, i32* %6
store i32 %17, i32* %3
br label %72
18: ; preds = %2
%19 = load %struct.addrinfo*, %struct.addrinfo** %7
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i32 0, i32 4
%21 = load i32, i32* %20
%22 = load %struct.addrinfo*, %struct.addrinfo** %7
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i32 0, i32 3
%24 = load i32, i32* %23
%25 = load %struct.addrinfo*, %struct.addrinfo** %7
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i32 0, i32 2
%27 = load i32, i32* %26
%28 = call i32 @socket(i32 %21, i32 %24, i32 %27)
store i32 %28, i32* %6
%29 = load i32, i32* %6
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %38
31: ; preds = %18
%32 = load i32, i32* @errno
%33 = call i32 @strerror(i32 %32)
%34 = call i32 (i8*, ...) @log_err(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %33)
%35 = load %struct.addrinfo*, %struct.addrinfo** %7
%36 = call i32 @freeaddrinfo(%struct.addrinfo* %35)
%37 = load i32, i32* %6
store i32 %37, i32* %3
br label %72
38: ; preds = %18
%39 = load i32, i32* %6
%40 = load %struct.addrinfo*, %struct.addrinfo** %7
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i32 0, i32 1
%42 = load i32, i32* %41
%43 = load %struct.addrinfo*, %struct.addrinfo** %7
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i64 @bind(i32 %39, i32 %42, i32 %45)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %57
48: ; preds = %38
%49 = load i32, i32* @errno
%50 = call i32 @strerror(i32 %49)
%51 = call i32 (i8*, ...) @log_err(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %50)
%52 = load i32, i32* %6
%53 = call i32 @close(i32 %52)
%54 = load %struct.addrinfo*, %struct.addrinfo** %7
%55 = call i32 @freeaddrinfo(%struct.addrinfo* %54)
store i32 -1, i32* %6
%56 = load i32, i32* %6
store i32 %56, i32* %3
br label %72
57: ; preds = %38
%58 = load i32, i32* %6
%59 = call i64 @listen(i32 %58, i32 0)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %68
61: ; preds = %57
%62 = call i32 (i8*, ...) @log_err(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%63 = load %struct.addrinfo*, %struct.addrinfo** %7
%64 = call i32 @freeaddrinfo(%struct.addrinfo* %63)
%65 = load i32, i32* %6
%66 = call i32 @close(i32 %65)
store i32 -1, i32* %6
%67 = load i32, i32* %6
store i32 %67, i32* %3
br label %72
68: ; preds = %57
%69 = load %struct.addrinfo*, %struct.addrinfo** %7
%70 = call i32 @freeaddrinfo(%struct.addrinfo* %69)
%71 = load i32, i32* %6
store i32 %71, i32* %3
br label %72
72: ; preds = %68, %61, %48, %31, %13
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@.str = [45 x i8] c"clients control thread createAddrInfo error!\00"
@.str.1 = [42 x i8] c"fail to create clients listener socket %s\00"
@errno = global i32 0
@.str.2 = [35 x i8] c"clients listener unable to bind %s\00"
@.str.3 = [34 x i8] c"clients listener unable to listen\00"
define i32 @startListener(i8* %0, i32 %1) {
%3 = tail call %struct.addrinfo* @createAddrInfo(i8* %0, i32 %1)
%4 = icmp eq %struct.addrinfo* %3, null
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i32 (i8*, ...) @log_err(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @freeaddrinfo(%struct.addrinfo* null)
br label %44
8: ; preds = %2
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
%10 = load i32, i32* %9
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
%12 = load i32, i32* %11
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
%14 = load i32, i32* %13
%15 = tail call i32 @socket(i32 %10, i32 %12, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %22
17: ; preds = %8
%18 = load i32, i32* @errno
%19 = tail call i32 @strerror(i32 %18)
%20 = tail call i32 (i8*, ...) @log_err(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = tail call i32 @freeaddrinfo(%struct.addrinfo* %3)
br label %44
22: ; preds = %8
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 1
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 0
%26 = load i32, i32* %25
%27 = tail call i64 @bind(i32 %15, i32 %24, i32 %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %22
%30 = load i32, i32* @errno
%31 = tail call i32 @strerror(i32 %30)
%32 = tail call i32 (i8*, ...) @log_err(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %31)
%33 = tail call i32 @close(i32 %15)
%34 = tail call i32 @freeaddrinfo(%struct.addrinfo* %3)
br label %44
35: ; preds = %22
%36 = tail call i64 @listen(i32 %15, i32 0)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %35
%39 = tail call i32 (i8*, ...) @log_err(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%40 = tail call i32 @freeaddrinfo(%struct.addrinfo* %3)
%41 = tail call i32 @close(i32 %15)
br label %44
42: ; preds = %35
%43 = tail call i32 @freeaddrinfo(%struct.addrinfo* %3)
br label %44
44: ; preds = %42, %38, %29, %17, %5
%45 = phi i32 [ -1, %5 ], [ -1, %17 ], [ -1, %29 ], [ -1, %38 ], [ %15, %42 ]
ret i32 %45
}