#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 */
int /*<<< orphan*/ STRERROR(int /*<<< orphan*/, char *);
scalar_t__ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ listen(int, size_t);
int /*<<< orphan*/ memcpy(char *, char *, int);
struct addrinfo *prepare_addrinfo_tcp(char const *, char const *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tcp_server(const char *bind_addr, const char *port, size_t backlog,
               char *errorstr) {
  struct addrinfo *ai;
  int sockfd, retsockfd = -1;
  if ((ai = prepare_addrinfo_tcp(bind_addr, port, errorstr)) == NULL)
    return -1;
  if ((sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) < 0) {
    do {
      if (errorstr != NULL) {
        memcpy(errorstr,
               "socket"
               ": ",
               (sizeof("socket"
                       ": ") /
                    sizeof(char) -
                1));
        STRERROR(errno, errorstr + (sizeof("socket"
                                           ": ") /
                                        sizeof(char) -
                                    1));
      }
    } while (0);
    goto done;
  }
  if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
    do {
      if (errorstr != NULL) {
        memcpy(errorstr,
               "bind"
               ": ",
               (sizeof("bind"
                       ": ") /
                    sizeof(char) -
                1));
        STRERROR(errno, errorstr + (sizeof("bind"
                                           ": ") /
                                        sizeof(char) -
                                    1));
      }
    } while (0);
    close(sockfd);
    goto done;
  }
  if (listen(sockfd, backlog) < 0) {
    do {
      if (errorstr != NULL) {
        memcpy(errorstr,
               "listen"
               ": ",
               (sizeof("listen"
                       ": ") /
                    sizeof(char) -
                1));
        STRERROR(errno, errorstr + (sizeof("listen"
                                           ": ") /
                                        sizeof(char) -
                                    1));
      }
    } while (0);
    close(sockfd);
    goto done;
  }
  retsockfd = sockfd;
done:
  freeaddrinfo(ai);
  return retsockfd;
}


x86 assembly (clang, -O0):
<tcp_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0xffffffff,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <prepare_addrinfo_tcp@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11ca <tcp_server+0x4a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1319 <tcp_server+0x199>
mov    -0x30(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x30(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    1235 <tcp_server+0xb5>
jmp    11f5 <tcp_server+0x75>
cmpq   $0x0,-0x28(%rbp)
je     122b <tcp_server+0xab>
mov    -0x28(%rbp),%rdi
lea    0xdf5(%rip),%rsi
mov    $0x8,%edx
call   1050 <memcpy@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rsi
add    $0x8,%rsi
call   1080 <STRERROR@plt>
jmp    1230 <tcp_server+0xb0>
jmp    130a <tcp_server+0x18a>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    12a1 <tcp_server+0x121>
jmp    1259 <tcp_server+0xd9>
cmpq   $0x0,-0x28(%rbp)
je     128f <tcp_server+0x10f>
mov    -0x28(%rbp),%rdi
lea    0xd9a(%rip),%rsi
mov    $0x6,%edx
call   1050 <memcpy@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rsi
add    $0x6,%rsi
call   1080 <STRERROR@plt>
jmp    1294 <tcp_server+0x114>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
jmp    130a <tcp_server+0x18a>
mov    -0x34(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1060 <listen@plt>
cmp    $0x0,%rax
jge    1304 <tcp_server+0x184>
jmp    12bc <tcp_server+0x13c>
cmpq   $0x0,-0x28(%rbp)
je     12f2 <tcp_server+0x172>
mov    -0x28(%rbp),%rdi
lea    0xd3e(%rip),%rsi
mov    $0x8,%edx
call   1050 <memcpy@plt>
mov    0x2cfd(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rsi
add    $0x8,%rsi
call   1080 <STRERROR@plt>
jmp    12f7 <tcp_server+0x177>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
jmp    130a <tcp_server+0x18a>
mov    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_server>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%rbp
mov    %rdx,%r15
mov    %rcx,%rdx
call   1030 <prepare_addrinfo_tcp@plt>
test   %rax,%rax
je     1203 <tcp_server+0x83>
mov    %rax,%rbx
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   10a0 <socket@plt>
test   %eax,%eax
js     120b <tcp_server+0x8b>
mov    %eax,%r14d
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1070 <bind@plt>
test   %rax,%rax
js     1241 <tcp_server+0xc1>
mov    %r14d,%edi
mov    %r15,%rsi
call   1060 <listen@plt>
test   %rax,%rax
jns    127d <tcp_server+0xfd>
test   %rbp,%rbp
je     126f <tcp_server+0xef>
lea    0xe29(%rip),%rsi
mov    %rbp,%rdi
mov    $0x8,%edx
call   1050 <memcpy@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
add    $0x8,%rbp
jmp    1267 <tcp_server+0xe7>
mov    $0xffffffff,%r14d
jmp    1285 <tcp_server+0x105>
mov    $0xffffffff,%r14d
test   %rbp,%rbp
je     127d <tcp_server+0xfd>
lea    0xde3(%rip),%rsi
mov    %rbp,%rdi
mov    $0x8,%edx
call   1050 <memcpy@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
add    $0x8,%rbp
mov    %rbp,%rsi
call   1080 <STRERROR@plt>
jmp    127d <tcp_server+0xfd>
test   %rbp,%rbp
je     126f <tcp_server+0xef>
lea    0xdbc(%rip),%rsi
mov    %rbp,%rdi
mov    $0x6,%edx
call   1050 <memcpy@plt>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
add    $0x6,%rbp
mov    %rbp,%rsi
call   1080 <STRERROR@plt>
mov    %r14d,%edi
call   1040 <close@plt>
mov    $0xffffffff,%r14d
mov    %rbx,%rdi
call   1090 <freeaddrinfo@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@.str = [9 x i8] c"socket: \00"
@errno = global i32 0
@.str.1 = [7 x i8] c"bind: \00"
@.str.2 = [9 x i8] c"listen: \00"
define i32 @tcp_server(i8* %0, i8* %1, i64 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca %struct.addrinfo*
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i8* %3, i8** %9
store i32 -1, i32* %12
%13 = load i8*, i8** %6
%14 = load i8*, i8** %7
%15 = load i8*, i8** %9
%16 = call %struct.addrinfo* @prepare_addrinfo_tcp(i8* %13, i8* %14, i8* %15)
store %struct.addrinfo* %16, %struct.addrinfo** %10
%17 = icmp eq %struct.addrinfo* %16, null
br i1 %17, label %18, label %19
18: ; preds = %4
store i32 -1, i32* %5
br label %95
19: ; preds = %4
%20 = load %struct.addrinfo*, %struct.addrinfo** %10
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i32 0, i32 4
%22 = load i32, i32* %21
%23 = load %struct.addrinfo*, %struct.addrinfo** %10
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i32 0, i32 3
%25 = load i32, i32* %24
%26 = load %struct.addrinfo*, %struct.addrinfo** %10
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i32 0, i32 2
%28 = load i32, i32* %27
%29 = call i32 @socket(i32 %22, i32 %25, i32 %28)
store i32 %29, i32* %11
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %44
31: ; preds = %19
br label %32
32: ; preds = %31
%33 = load i8*, i8** %9
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %42
35: ; preds = %32
%36 = load i8*, i8** %9
%37 = call i32 @memcpy(i8* %36, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 8)
%38 = load i32, i32* @errno
%39 = load i8*, i8** %9
%40 = getelementptr inbounds i8, i8* %39, i64 8
%41 = call i32 @STRERROR(i32 %38, i8* %40)
br label %42
42: ; preds = %35, %32
br label %43
43: ; preds = %42
br label %91
44: ; preds = %19
%45 = load i32, i32* %11
%46 = load %struct.addrinfo*, %struct.addrinfo** %10
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %46, i32 0, i32 1
%48 = load i32, i32* %47
%49 = load %struct.addrinfo*, %struct.addrinfo** %10
%50 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %49, i32 0, i32 0
%51 = load i32, i32* %50
%52 = call i64 @bind(i32 %45, i32 %48, i32 %51)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %69
54: ; preds = %44
br label %55
55: ; preds = %54
%56 = load i8*, i8** %9
%57 = icmp ne i8* %56, null
br i1 %57, label %58, label %65
58: ; preds = %55
%59 = load i8*, i8** %9
%60 = call i32 @memcpy(i8* %59, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 6)
%61 = load i32, i32* @errno
%62 = load i8*, i8** %9
%63 = getelementptr inbounds i8, i8* %62, i64 6
%64 = call i32 @STRERROR(i32 %61, i8* %63)
br label %65
65: ; preds = %58, %55
br label %66
66: ; preds = %65
%67 = load i32, i32* %11
%68 = call i32 @close(i32 %67)
br label %91
69: ; preds = %44
%70 = load i32, i32* %11
%71 = load i64, i64* %8
%72 = call i64 @listen(i32 %70, i64 %71)
%73 = icmp slt i64 %72, 0
br i1 %73, label %74, label %89
74: ; preds = %69
br label %75
75: ; preds = %74
%76 = load i8*, i8** %9
%77 = icmp ne i8* %76, null
br i1 %77, label %78, label %85
78: ; preds = %75
%79 = load i8*, i8** %9
%80 = call i32 @memcpy(i8* %79, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 8)
%81 = load i32, i32* @errno
%82 = load i8*, i8** %9
%83 = getelementptr inbounds i8, i8* %82, i64 8
%84 = call i32 @STRERROR(i32 %81, i8* %83)
br label %85
85: ; preds = %78, %75
br label %86
86: ; preds = %85
%87 = load i32, i32* %11
%88 = call i32 @close(i32 %87)
br label %91
89: ; preds = %69
%90 = load i32, i32* %11
store i32 %90, i32* %12
br label %91
91: ; preds = %89, %86, %66, %43
%92 = load %struct.addrinfo*, %struct.addrinfo** %10
%93 = call i32 @freeaddrinfo(%struct.addrinfo* %92)
%94 = load i32, i32* %12
store i32 %94, i32* %5
br label %95
95: ; preds = %91, %18
%96 = load i32, i32* %5
ret i32 %96
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@.str = [9 x i8] c"socket: \00"
@errno = global i32 0
@.str.1 = [7 x i8] c"bind: \00"
@.str.2 = [9 x i8] c"listen: \00"
define i32 @tcp_server(i8* %0, i8* %1, i64 %2, i8* %3) {
%5 = tail call %struct.addrinfo* @prepare_addrinfo_tcp(i8* %0, i8* %1, i8* %3)
%6 = icmp eq %struct.addrinfo* %5, null
br i1 %6, label %54, label %7
7: ; preds = %4
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 4
%9 = load i32, i32* %8
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 3
%11 = load i32, i32* %10
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 2
%13 = load i32, i32* %12
%14 = tail call i32 @socket(i32 %9, i32 %11, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %23
16: ; preds = %7
%17 = icmp eq i8* %3, null
br i1 %17, label %51, label %18
18: ; preds = %16
%19 = tail call i32 @memcpy(i8* %3, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 8)
%20 = load i32, i32* @errno
%21 = getelementptr inbounds i8, i8* %3, i64 8
%22 = tail call i32 @STRERROR(i32 %20, i8* %21)
br label %51
23: ; preds = %7
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 1
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 0
%27 = load i32, i32* %26
%28 = tail call i64 @bind(i32 %14, i32 %25, i32 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %39
30: ; preds = %23
%31 = icmp eq i8* %3, null
br i1 %31, label %37, label %32
32: ; preds = %30
%33 = tail call i32 @memcpy(i8* %3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 6)
%34 = load i32, i32* @errno
%35 = getelementptr inbounds i8, i8* %3, i64 6
%36 = tail call i32 @STRERROR(i32 %34, i8* %35)
br label %37
37: ; preds = %32, %30
%38 = tail call i32 @close(i32 %14)
br label %51
39: ; preds = %23
%40 = tail call i64 @listen(i32 %14, i64 %2)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %51
42: ; preds = %39
%43 = icmp eq i8* %3, null
br i1 %43, label %49, label %44
44: ; preds = %42
%45 = tail call i32 @memcpy(i8* %3, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 8)
%46 = load i32, i32* @errno
%47 = getelementptr inbounds i8, i8* %3, i64 8
%48 = tail call i32 @STRERROR(i32 %46, i8* %47)
br label %49
49: ; preds = %44, %42
%50 = tail call i32 @close(i32 %14)
br label %51
51: ; preds = %39, %16, %18, %49, %37
%52 = phi i32 [ -1, %18 ], [ -1, %16 ], [ -1, %37 ], [ -1, %49 ], [ %14, %39 ]
%53 = tail call i32 @freeaddrinfo(%struct.addrinfo* %5)
br label %54
54: ; preds = %4, %51
%55 = phi i32 [ %52, %51 ], [ -1, %4 ]
ret i32 %55
}