#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 dummy;
};

/* Variables and functions */
int bind_socket(struct addrinfo *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int listen(int, int);
struct addrinfo *prepare_addrinfo(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ stderr;
int listen_on_port(char *port) {
  int socketfd;
  struct addrinfo *res;
  res = prepare_addrinfo(NULL, port);
  socketfd = bind_socket(res);
  freeaddrinfo(res);
  if (socketfd != -1) {
    if (listen(socketfd, 20) == -1) {
      fprintf(stderr, "ERROR: listen to port %s failed: ", port);
      close(socketfd);
      socketfd = -1;
    }
  }
  return socketfd;
}


x86 assembly (clang, -O0):
<listen_on_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1060 <prepare_addrinfo@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <bind_socket@plt>
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
cmpl   $0xffffffff,-0xc(%rbp)
je     11df <listen_on_port+0x7f>
mov    -0xc(%rbp),%edi
mov    $0x14,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
jne    11da <listen_on_port+0x7a>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe3a(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0xc(%rbp)
jmp    11df <listen_on_port+0x7f>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listen_on_port>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
xor    %edi,%edi
mov    %r14,%rsi
call   1060 <prepare_addrinfo@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1030 <bind_socket@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1080 <freeaddrinfo@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%ebp
je     11c4 <listen_on_port+0x64>
mov    %ebp,%edi
mov    $0x14,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
je     11a5 <listen_on_port+0x45>
mov    %ebp,%ebx
jmp    11c4 <listen_on_port+0x64>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
lea    0xe4b(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32 }
@stderr = global i32 0
@.str = [34 x i8] c"ERROR: listen to port %s failed: \00"
define i32 @listen_on_port(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.addrinfo*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call %struct.addrinfo* @prepare_addrinfo(i32* null, i8* %5)
store %struct.addrinfo* %6, %struct.addrinfo** %4
%7 = load %struct.addrinfo*, %struct.addrinfo** %4
%8 = call i32 @bind_socket(%struct.addrinfo* %7)
store i32 %8, i32* %3
%9 = load %struct.addrinfo*, %struct.addrinfo** %4
%10 = call i32 @freeaddrinfo(%struct.addrinfo* %9)
%11 = load i32, i32* %3
%12 = icmp ne i32 %11, -1
br i1 %12, label %13, label %24
13: ; preds = %1
%14 = load i32, i32* %3
%15 = call i32 @listen(i32 %14, i32 20)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %23
17: ; preds = %13
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %2
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = load i32, i32* %3
%22 = call i32 @close(i32 %21)
store i32 -1, i32* %3
br label %23
23: ; preds = %17, %13
br label %24
24: ; preds = %23, %1
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
%struct.addrinfo = type { i32 }
@stderr = global i32 0
@.str = [34 x i8] c"ERROR: listen to port %s failed: \00"
define i32 @listen_on_port(i8* %0) {
%2 = tail call %struct.addrinfo* @prepare_addrinfo(i32* null, i8* %0)
%3 = tail call i32 @bind_socket(%struct.addrinfo* %2)
%4 = tail call i32 @freeaddrinfo(%struct.addrinfo* %2)
%5 = icmp eq i32 %3, -1
br i1 %5, label %13, label %6
6: ; preds = %1
%7 = tail call i32 @listen(i32 %3, i32 20)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %13
9: ; preds = %6
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %0)
%12 = tail call i32 @close(i32 %3)
br label %13
13: ; preds = %6, %9, %1
%14 = phi i32 [ -1, %9 ], [ %3, %6 ], [ -1, %1 ]
ret i32 %14
}