#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;
  int /*<<< orphan*/ ai_flags;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOMAXCONN;
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ cleanuptcp(int, struct addrinfo *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(int /*<<< orphan*/ *, char const *, struct addrinfo *,
                struct addrinfo **);
int listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int tcpopen(const char *port) {
  struct addrinfo hints, *ai = NULL;
  int status;
  int fd = -1;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  if ((status = getaddrinfo(NULL, port, &hints, &ai)) < 0) {
    fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
    cleanuptcp(fd, ai);
    exit(EXIT_FAILURE);
  }
  if ((fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) < 0) {
    fprintf(stderr, "socket error: %s\n", gai_strerror(fd));
    cleanuptcp(fd, ai);
    exit(EXIT_FAILURE);
  }
  if ((status = bind(fd, ai->ai_addr, ai->ai_addrlen)) < 0) {
    fprintf(stderr, "bind error: %s\n", gai_strerror(fd));
    cleanuptcp(fd, ai);
    exit(EXIT_FAILURE);
  }
  if ((status = listen(fd, SOMAXCONN)) < 0) {
    fprintf(stderr, "listen error: %s\n", gai_strerror(fd));
    cleanuptcp(fd, ai);
    exit(EXIT_FAILURE);
  }
  freeaddrinfo(ai);
  return fd;
}


x86 assembly (clang, -O0):
<tcpopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0xffffffff,-0x30(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x20(%rbp),%rdx
lea    -0x28(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    1250 <tcpopen+0xb0>
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x2c(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xdcf(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x30(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1040 <cleanuptcp@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x28(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
jge    12b6 <tcpopen+0x116>
mov    0x2d53(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x38(%rbp),%edi
mov    %rax,%rdx
lea    0xd80(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x30(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1040 <cleanuptcp@plt>
mov    0x2d09(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x30(%rbp),%edi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1080 <bind@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    1317 <tcpopen+0x177>
mov    0x2cf2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x30(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x3c(%rbp),%edi
mov    %rax,%rdx
lea    0xd31(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x30(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1040 <cleanuptcp@plt>
mov    0x2ca8(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x30(%rbp),%edi
mov    0x2ca7(%rip),%rax
mov    (%rax),%esi
call   1070 <listen@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    1374 <tcpopen+0x1d4>
mov    0x2c95(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x30(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x40(%rbp),%edi
mov    %rax,%rdx
lea    0xce4(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x30(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1040 <cleanuptcp@plt>
mov    0x2c4b(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    -0x30(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcpopen>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
movq   $0x0,(%rsp)
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2de5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    %rsp,%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   10a0 <getaddrinfo@plt>
test   %eax,%eax
js     1256 <tcpopen+0xb6>
mov    (%rsp),%rax
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   10c0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1282 <tcpopen+0xe2>
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   1080 <bind@plt>
test   %eax,%eax
js     129b <tcpopen+0xfb>
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1070 <listen@plt>
test   %eax,%eax
js     12b4 <tcpopen+0x114>
mov    (%rsp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d73(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xd93(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    (%rsp),%rsi
mov    $0xffffffff,%edi
jmp    12db <tcpopen+0x13b>
mov    0x2d47(%rip),%rax
mov    (%rax),%ebp
mov    %ebx,%edi
call   1030 <gai_strerror@plt>
lea    0xd7e(%rip),%rsi
jmp    12cb <tcpopen+0x12b>
mov    0x2d2e(%rip),%rax
mov    (%rax),%ebp
mov    %ebx,%edi
call   1030 <gai_strerror@plt>
lea    0xd77(%rip),%rsi
jmp    12cb <tcpopen+0x12b>
mov    0x2d15(%rip),%rax
mov    (%rax),%ebp
mov    %ebx,%edi
call   1030 <gai_strerror@plt>
lea    0xd6e(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    (%rsp),%rsi
mov    %ebx,%edi
call   1040 <cleanuptcp@plt>
mov    0x2cd1(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@stderr = global i32 0
@.str = [23 x i8] c"getaddrinfo error: %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [18 x i8] c"socket error: %s\0A\00"
@.str.2 = [16 x i8] c"bind error: %s\0A\00"
@SOMAXCONN = global i32 0
@.str.3 = [18 x i8] c"listen error: %s\0A\00"
define i32 @tcpopen(i8* %0) {
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = bitcast %struct.addrinfo* %2 to i8*
%5 = bitcast %struct.addrinfo** %3 to i8*
store %struct.addrinfo* null, %struct.addrinfo** %3
%6 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 24)
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 4
store i32 %7, i32* %8
%9 = load i32, i32* @SOCK_STREAM
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 3
store i32 %9, i32* %10
%11 = load i32, i32* @AI_PASSIVE
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 5
store i32 %11, i32* %12
%13 = call i32 @getaddrinfo(i32* null, i8* %0, %struct.addrinfo* %2, %struct.addrinfo** %3)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %1
%16 = load i32, i32* @stderr
%17 = call i8* @gai_strerror(i32 %13)
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load %struct.addrinfo*, %struct.addrinfo** %3
%20 = call i32 @cleanuptcp(i32 -1, %struct.addrinfo* %19)
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %1
%24 = load %struct.addrinfo*, %struct.addrinfo** %3
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 4
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 3
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 2
%30 = load i32, i32* %29
%31 = call i32 @socket(i32 %26, i32 %28, i32 %30)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %41
33: ; preds = %23
%34 = load i32, i32* @stderr
%35 = call i8* @gai_strerror(i32 %31)
%36 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %35)
%37 = load %struct.addrinfo*, %struct.addrinfo** %3
%38 = call i32 @cleanuptcp(i32 %31, %struct.addrinfo* %37)
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %23
%42 = load %struct.addrinfo*, %struct.addrinfo** %3
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %42, i64 0, i32 1
%44 = load i32, i32* %43
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %42, i64 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @bind(i32 %31, i32 %44, i32 %46)
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %57
49: ; preds = %41
%50 = load i32, i32* @stderr
%51 = call i8* @gai_strerror(i32 %31)
%52 = call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %51)
%53 = load %struct.addrinfo*, %struct.addrinfo** %3
%54 = call i32 @cleanuptcp(i32 %31, %struct.addrinfo* %53)
%55 = load i32, i32* @EXIT_FAILURE
%56 = call i32 @exit(i32 %55)
unreachable
57: ; preds = %41
%58 = load i32, i32* @SOMAXCONN
%59 = call i32 @listen(i32 %31, i32 %58)
%60 = icmp slt i32 %59, 0
br i1 %60, label %61, label %69
61: ; preds = %57
%62 = load i32, i32* @stderr
%63 = call i8* @gai_strerror(i32 %31)
%64 = call i32 @fprintf(i32 %62, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %63)
%65 = load %struct.addrinfo*, %struct.addrinfo** %3
%66 = call i32 @cleanuptcp(i32 %31, %struct.addrinfo* %65)
%67 = load i32, i32* @EXIT_FAILURE
%68 = call i32 @exit(i32 %67)
unreachable
69: ; preds = %57
%70 = load %struct.addrinfo*, %struct.addrinfo** %3
%71 = call i32 @freeaddrinfo(%struct.addrinfo* %70)
ret i32 %31
}