#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ on;
typedef int /*<<< orphan*/ my_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int make_server_socket() {
  int sockfd = socket(PF_INET, SOCK_STREAM, 0);
  struct sockaddr_in my_addr;
  memset(&my_addr, 0, sizeof(my_addr));
  my_addr.sin_family = AF_INET;
  my_addr.sin_port = htons(21453);
  my_addr.sin_addr.s_addr = INADDR_ANY;
  if (sockfd > 0) {
    int on = 1;
    setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
    if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) ==
        0) {
      if (listen(sockfd, 0) == 0) {
      } else {
        close(sockfd);
        sockfd = -3;
      }
    } else {
      close(sockfd);
      sockfd = -2;
    }
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<make_server_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x4(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    $0x53cd,%edi
call   1040 <htons@plt>
mov    %eax,-0xc(%rbp)
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x4(%rbp)
jle    125a <make_server_socket+0xea>
movl   $0x1,-0x14(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
mov    0x2dcb(%rip),%rax
mov    (%rax),%edx
lea    -0x14(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jne    1246 <make_server_socket+0xd6>
mov    -0x4(%rbp),%edi
xor    %esi,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jne    1232 <make_server_socket+0xc2>
jmp    1241 <make_server_socket+0xd1>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1255 <make_server_socket+0xe5>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    125a <make_server_socket+0xea>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_server_socket>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    $0x53cd,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
test   %ebx,%ebx
jle    1231 <make_server_socket+0xc1>
movl   $0x1,0x4(%rsp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1080 <bind@plt>
mov    $0xfffffffe,%ebp
test   %rax,%rax
jne    1228 <make_server_socket+0xb8>
mov    %ebx,%edi
xor    %esi,%esi
call   1070 <listen@plt>
mov    $0xfffffffd,%ebp
test   %rax,%rax
je     1231 <make_server_socket+0xc1>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32 @make_server_socket() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = call i32 @socket(i32 %4, i32 %5, i32 0)
store i32 %6, i32* %1
%7 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %8, i32* %9
%10 = call i32 @htons(i32 21453)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
store i32 %10, i32* %11
%12 = load i32, i32* @INADDR_ANY
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
%14 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %13, i32 0, i32 0
store i32 %12, i32* %14
%15 = load i32, i32* %1
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %39
17: ; preds = %0
store i32 1, i32* %3
%18 = load i32, i32* %1
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_REUSEADDR
%21 = call i32 @setsockopt(i32 %18, i32 %19, i32 %20, i32* %3, i32 4)
%22 = load i32, i32* %1
%23 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%24 = call i64 @bind(i32 %22, %struct.sockaddr* %23, i32 4)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %35
26: ; preds = %17
%27 = load i32, i32* %1
%28 = call i64 @listen(i32 %27, i32 0)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %26
br label %34
31: ; preds = %26
%32 = load i32, i32* %1
%33 = call i32 @close(i32 %32)
store i32 -3, i32* %1
br label %34
34: ; preds = %31, %30
br label %38
35: ; preds = %17
%36 = load i32, i32* %1
%37 = call i32 @close(i32 %36)
store i32 -2, i32* %1
br label %38
38: ; preds = %35, %34
br label %39
39: ; preds = %38, %0
%40 = load i32, i32* %1
ret i32 %40
}

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