#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 */

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bindToUnusedPort(int const, unsigned short *,
                                    char const **const);
int /*<<< orphan*/ close(int const);
int /*<<< orphan*/ errno;
int listen(int const, int);
int /*<<< orphan*/ pm_asprintf(char const **const, char *, int /*<<< orphan*/,
                               int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void CreateListeningSocket(int *const socketP, int *const portNumP,
                           const char **const errorP) {
  int rc;
  rc = socket(AF_INET, SOCK_STREAM, 0);
  if (rc < 0)
    pm_asprintf(errorP,
                "Unable to create socket.  "
                "socket() failed with errno %d (%s)",
                errno, strerror(errno));
  else {
    int const socketFd = rc;
    unsigned short portNum;
    *socketP = socketFd;
    bindToUnusedPort(socketFd, &portNum, errorP);
    if (!*errorP) {
      int rc;
      *portNumP = portNum;
      rc = listen(socketFd, 5);
      if (rc != 0)
        pm_asprintf(errorP,
                    "Unable to listen on TCP socket.  "
                    "listen() fails with errno %d (%s)",
                    errno, strerror(errno));
    }
    if (*errorP)
      close(socketFd);
  }
}


x86 assembly (clang, -O0):
<CreateListeningSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11d6 <CreateListeningSocket+0x76>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    %eax,%ecx
lea    0xe34(%rip),%rsi
call   1030 <pm_asprintf@plt>
jmp    127e <CreateListeningSocket+0x11e>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rdx
lea    -0x22(%rbp),%rsi
call   1060 <bindToUnusedPort@plt>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1263 <CreateListeningSocket+0x103>
movzwl -0x22(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%edi
mov    $0x5,%esi
call   1050 <listen@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     125e <CreateListeningSocket+0xfe>
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d9a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x48(%rbp),%rdi
mov    -0x3c(%rbp),%edx
mov    %eax,%ecx
lea    0xde4(%rip),%rsi
call   1030 <pm_asprintf@plt>
jmp    1263 <CreateListeningSocket+0x103>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
je     1279 <CreateListeningSocket+0x119>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
jmp    127e <CreateListeningSocket+0x11e>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CreateListeningSocket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11f9 <CreateListeningSocket+0x99>
mov    %eax,%ebp
mov    %eax,(%rbx)
lea    0x6(%rsp),%rsi
mov    %eax,%edi
mov    %r15,%rdx
call   1060 <bindToUnusedPort@plt>
cmpq   $0x0,(%r15)
jne    11e7 <CreateListeningSocket+0x87>
movzwl 0x6(%rsp),%eax
mov    %eax,(%r14)
mov    %ebp,%edi
mov    $0x5,%esi
call   1050 <listen@plt>
test   %eax,%eax
je     11e1 <CreateListeningSocket+0x81>
mov    0x2e0b(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1070 <strerror@plt>
lea    0xe68(%rip),%rsi
mov    %r15,%rdi
mov    %ebx,%edx
mov    %eax,%ecx
call   1030 <pm_asprintf@plt>
cmpq   $0x0,(%r15)
je     11ee <CreateListeningSocket+0x8e>
mov    %ebp,%edi
call   1040 <close@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dd0(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1070 <strerror@plt>
lea    0xdf0(%rip),%rsi
mov    %r15,%rdi
mov    %ebx,%edx
mov    %eax,%ecx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <pm_asprintf@plt>

LLVM IR (-O0):
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [61 x i8] c"Unable to create socket. socket() failed with errno %d (%s)\00"
@errno = global i32 0
@.str.1 = [67 x i8] c"Unable to listen on TCP socket. listen() fails with errno %d (%s)\00"
define void @CreateListeningSocket(i32* %0, i32* %1, i8** %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i32
%9 = alloca i16
%10 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i8** %2, i8*** %6
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %3
%17 = load i8**, i8*** %6
%18 = load i32, i32* @errno
%19 = load i32, i32* @errno
%20 = call i32 @strerror(i32 %19)
%21 = call i32 @pm_asprintf(i8** %17, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0), i32 %18, i32 %20)
br label %55
22: ; preds = %3
%23 = load i32, i32* %7
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = load i32*, i32** %4
store i32 %24, i32* %25
%26 = load i32, i32* %8
%27 = load i8**, i8*** %6
%28 = call i32 @bindToUnusedPort(i32 %26, i16* %9, i8** %27)
%29 = load i8**, i8*** %6
%30 = load i8*, i8** %29
%31 = icmp ne i8* %30, null
br i1 %31, label %47, label %32
32: ; preds = %22
%33 = load i16, i16* %9
%34 = zext i16 %33 to i32
%35 = load i32*, i32** %5
store i32 %34, i32* %35
%36 = load i32, i32* %8
%37 = call i32 @listen(i32 %36, i32 5)
store i32 %37, i32* %10
%38 = load i32, i32* %10
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %46
40: ; preds = %32
%41 = load i8**, i8*** %6
%42 = load i32, i32* @errno
%43 = load i32, i32* @errno
%44 = call i32 @strerror(i32 %43)
%45 = call i32 @pm_asprintf(i8** %41, i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.1, i64 0, i64 0), i32 %42, i32 %44)
br label %46
46: ; preds = %40, %32
br label %47
47: ; preds = %46, %22
%48 = load i8**, i8*** %6
%49 = load i8*, i8** %48
%50 = icmp ne i8* %49, null
br i1 %50, label %51, label %54
51: ; preds = %47
%52 = load i32, i32* %8
%53 = call i32 @close(i32 %52)
br label %54
54: ; preds = %51, %47
br label %55
55: ; preds = %54, %16
ret void
}

LLVM IR (-O2):
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [61 x i8] c"Unable to create socket. socket() failed with errno %d (%s)\00"
@errno = global i32 0
@.str.1 = [67 x i8] c"Unable to listen on TCP socket. listen() fails with errno %d (%s)\00"
define void @CreateListeningSocket(i32* nocapture writeonly %0, i32* nocapture writeonly %1, i8** %2) {
%4 = alloca i16
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %3
%10 = load i32, i32* @errno
%11 = tail call i32 @strerror(i32 %10)
%12 = tail call i32 @pm_asprintf(i8** %2, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %11)
br label %33
13: ; preds = %3
%14 = bitcast i16* %4 to i8*
store i32 %7, i32* %0
%15 = call i32 @bindToUnusedPort(i32 %7, i16* %4, i8** %2)
%16 = load i8*, i8** %2
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %30
18: ; preds = %13
%19 = load i16, i16* %4
%20 = zext i16 %19 to i32
store i32 %20, i32* %1
%21 = call i32 @listen(i32 %7, i32 5)
%22 = icmp eq i32 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %18
%24 = load i32, i32* @errno
%25 = call i32 @strerror(i32 %24)
%26 = call i32 @pm_asprintf(i8** %2, i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.1, i64 0, i64 0), i32 %24, i32 %25)
br label %27
27: ; preds = %18, %23
%28 = load i8*, i8** %2
%29 = icmp eq i8* %28, null
br i1 %29, label %32, label %30
30: ; preds = %13, %27
%31 = call i32 @close(i32 %7)
br label %32
32: ; preds = %30, %27
br label %33
33: ; preds = %32, %9
ret void
}