#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int getsockname(int, struct sockaddr *, int /*<<< orphan*/ *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int cf_socket_server(int port) {
  struct sockaddr_in serv_addr;
  int result;
  int size = sizeof(serv_addr);
  int newSocket = -1;
  bzero((char *)&serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  serv_addr.sin_port = htons(port);
  newSocket = socket(AF_INET, SOCK_STREAM, 0);
  if (newSocket == -1) {
    return -1;
  }
  result = bind(newSocket, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
  if (result == -1) {
    return -1;
  }
  result =
      getsockname(newSocket, (struct sockaddr *)&serv_addr, (socklen_t *)&size);
  if (result == -1) {
    return -1;
  }
  result = listen(newSocket, 5);
  if (result == -1) {
    return -1;
  }
  return newSocket;
}


x86 assembly (clang, -O0):
<cf_socket_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movl   $0xc,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
lea    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11f1 <cf_socket_server+0x81>
movl   $0xffffffff,-0x4(%rbp)
jmp    1270 <cf_socket_server+0x100>
mov    -0x24(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    121b <cf_socket_server+0xab>
movl   $0xffffffff,-0x4(%rbp)
jmp    1270 <cf_socket_server+0x100>
mov    -0x24(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1080 <getsockname@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1244 <cf_socket_server+0xd4>
movl   $0xffffffff,-0x4(%rbp)
jmp    1270 <cf_socket_server+0x100>
mov    -0x24(%rbp),%edi
mov    $0x5,%esi
call   1050 <listen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    126a <cf_socket_server+0xfa>
movl   $0xffffffff,-0x4(%rbp)
jmp    1270 <cf_socket_server+0x100>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cf_socket_server>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
movl   $0xc,0x14(%rsp)
lea    0x8(%rsp),%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    0x2e52(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    0x0(%rbp),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     1219 <cf_socket_server+0xa9>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
je     1219 <cf_socket_server+0xa9>
lea    0x8(%rsp),%rsi
lea    0x14(%rsp),%rdx
mov    %ebx,%edi
call   1080 <getsockname@plt>
cmp    $0xffffffff,%eax
je     1219 <cf_socket_server+0xa9>
mov    %ebx,%edi
mov    $0x5,%esi
call   1050 <listen@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebx
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
define i32 @cf_socket_server(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i32 12, i32* %6
store i32 -1, i32* %7
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = call i32 @bzero(i8* %8, i32 12)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %10, i32* %11
%12 = load i32, i32* @INADDR_ANY
%13 = call i32 @htonl(i32 %12)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%15 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %14, i32 0, i32 0
store i32 %13, i32* %15
%16 = load i32, i32* %3
%17 = call i32 @htons(i32 %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %1
store i32 -1, i32* %2
br label %47
25: ; preds = %1
%26 = load i32, i32* %7
%27 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%28 = call i32 @bind(i32 %26, %struct.sockaddr* %27, i32 12)
store i32 %28, i32* %5
%29 = load i32, i32* %5
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %25
store i32 -1, i32* %2
br label %47
32: ; preds = %25
%33 = load i32, i32* %7
%34 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%35 = call i32 @getsockname(i32 %33, %struct.sockaddr* %34, i32* %6)
store i32 %35, i32* %5
%36 = load i32, i32* %5
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %39
38: ; preds = %32
store i32 -1, i32* %2
br label %47
39: ; preds = %32
%40 = load i32, i32* %7
%41 = call i32 @listen(i32 %40, i32 5)
store i32 %41, i32* %5
%42 = load i32, i32* %5
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %45
44: ; preds = %39
store i32 -1, i32* %2
br label %47
45: ; preds = %39
%46 = load i32, i32* %7
store i32 %46, i32* %2
br label %47
47: ; preds = %45, %44, %38, %31, %24
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
define i32 @cf_socket_server(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = bitcast i32* %3 to i8*
store i32 12, i32* %3
%6 = call i32 @bzero(i8* %4, i32 12)
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %7, i32* %8
%9 = load i32, i32* @INADDR_ANY
%10 = tail call i32 @htonl(i32 %9)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %10, i32* %11
%12 = tail call i32 @htons(i32 %0)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %12, i32* %13
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = tail call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = icmp eq i32 %16, -1
br i1 %17, label %29, label %18
18: ; preds = %1
%19 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%20 = call i32 @bind(i32 %16, %struct.sockaddr* %19, i32 12)
%21 = icmp eq i32 %20, -1
br i1 %21, label %29, label %22
22: ; preds = %18
%23 = call i32 @getsockname(i32 %16, %struct.sockaddr* %19, i32* %3)
%24 = icmp eq i32 %23, -1
br i1 %24, label %29, label %25
25: ; preds = %22
%26 = call i32 @listen(i32 %16, i32 5)
%27 = icmp eq i32 %26, -1
%28 = select i1 %27, i32 -1, i32 %16
br label %29
29: ; preds = %25, %22, %18, %1
%30 = phi i32 [ -1, %1 ], [ -1, %18 ], [ -1, %22 ], [ %28, %25 ]
ret i32 %30
}