#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 sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ address;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ htons(int);
int listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int open_port(int port) {
  int sockfd;
  struct sockaddr_in address;
  if ((sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
    goto open_port_error;
  memset(&address, 0, sizeof(address));
  address.sin_family = AF_INET;
  address.sin_port = htons(port);
  if (bind(sockfd, (struct sockaddr *)&address, sizeof(address)) == -1 ||
      listen(sockfd, 0) == -1)
    goto open_port_error;
  return sockfd;
open_port_error:
  fprintf(stderr, "Cannot open port %d.\n", port);
  exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<open_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
mov    0x2e4c(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11ac <open_port+0x3c>
jmp    120e <open_port+0x9e>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
je     1200 <open_port+0x90>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
jne    1205 <open_port+0x95>
jmp    120e <open_port+0x9e>
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xddf(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<open_port>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
mov    0x2e4c(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     11fc <open_port+0x8c>
mov    %eax,%ebp
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
je     11fc <open_port+0x8c>
mov    %ebp,%edi
xor    %esi,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
je     11fc <open_port+0x8c>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
lea    0xdf4(%rip),%rsi
mov    %r14d,%edx
call   1050 <fprintf@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@AF_INET = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"Cannot open port %d.\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @open_port(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
store i32 %0, i32* %2
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = load i32, i32* @IPPROTO_TCP
%8 = call i32 @socket(i32 %5, i32 %6, i32 %7)
store i32 %8, i32* %3
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %11
10: ; preds = %1
br label %29
11: ; preds = %1
%12 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 8)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %13, i32* %14
%15 = load i32, i32* %2
%16 = call i32 @htons(i32 %15)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* %3
%19 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%20 = call i32 @bind(i32 %18, %struct.sockaddr* %19, i32 8)
%21 = icmp eq i32 %20, -1
br i1 %21, label %26, label %22
22: ; preds = %11
%23 = load i32, i32* %3
%24 = call i32 @listen(i32 %23, i32 0)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %27
26: ; preds = %22, %11
br label %29
27: ; preds = %22
%28 = load i32, i32* %3
ret i32 %28
29: ; preds = %26, %10
%30 = load i32, i32* @stderr
%31 = load i32, i32* %2
%32 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %31)
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@AF_INET = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"Cannot open port %d.\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @open_port(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = load i32, i32* @IPPROTO_TCP
%7 = tail call i32 @socket(i32 %4, i32 %5, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %22, label %9
9: ; preds = %1
%10 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 8)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %11, i32* %12
%13 = call i32 @htons(i32 %0)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %13, i32* %14
%15 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%16 = call i32 @bind(i32 %7, %struct.sockaddr* %15, i32 8)
%17 = icmp eq i32 %16, -1
br i1 %17, label %22, label %18
18: ; preds = %9
%19 = call i32 @listen(i32 %7, i32 0)
%20 = icmp eq i32 %19, -1
br i1 %20, label %22, label %21
21: ; preds = %18
ret i32 %7
22: ; preds = %9, %18, %1
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %0)
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
}