#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*/ opt;
typedef int /*<<< orphan*/ local_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
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*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int make_request_socket(int port) {
  int socket_desc;
  struct sockaddr_in local_addr;
  int opt = 1;
  local_addr.sin_family = AF_INET;
  local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  local_addr.sin_port = htons(port);
  socket_desc = socket(AF_INET, SOCK_STREAM, 0);
  if (socket_desc == -1) {
    goto failure;
  }
  setsockopt(socket_desc, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  if (bind(socket_desc, (struct sockaddr *)&local_addr, sizeof(local_addr)) !=
      0) {
    close(socket_desc);
    socket_desc = -1;
    goto failure;
  }
  goto success;
failure:
  perror("ERROR: Failed on binding socket");
success:
  return socket_desc;
}


x86 assembly (clang, -O0):
<make_request_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11d5 <make_request_socket+0x65>
jmp    122d <make_request_socket+0xbd>
mov    -0x8(%rbp),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    0x2dd8(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
je     1228 <make_request_socket+0xb8>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x8(%rbp)
jmp    122d <make_request_socket+0xbd>
jmp    1239 <make_request_socket+0xc9>
lea    0xdcc(%rip),%rdi
call   1080 <perror@plt>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_request_socket>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
movl   $0x1,0x4(%rsp)
mov    0x2e58(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    %ebx,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    (%r14),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     1211 <make_request_socket+0xa1>
mov    %eax,%ebx
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
mov    0x2dea(%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    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
jne    120a <make_request_socket+0x9a>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
mov    %ebx,%edi
call   1060 <close@plt>
lea    0xde8(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1200 <make_request_socket+0x90>

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
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str = [32 x i8] c"ERROR: Failed on binding socket\00"
define i32 @make_request_socket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
store i32 %0, i32* %2
store i32 1, i32* %5
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %6, i32* %7
%8 = load i32, i32* @INADDR_ANY
%9 = call i32 @htonl(i32 %8)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%11 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %10, i32 0, i32 0
store i32 %9, i32* %11
%12 = load i32, i32* %2
%13 = call i32 @htons(i32 %12)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %13, i32* %14
%15 = load i32, i32* @AF_INET
%16 = load i32, i32* @SOCK_STREAM
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %1
br label %34
21: ; preds = %1
%22 = load i32, i32* %3
%23 = load i32, i32* @SOL_SOCKET
%24 = load i32, i32* @SO_REUSEADDR
%25 = call i32 @setsockopt(i32 %22, i32 %23, i32 %24, i32* %5, i32 4)
%26 = load i32, i32* %3
%27 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%28 = call i64 @bind(i32 %26, %struct.sockaddr* %27, i32 12)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %21
%31 = load i32, i32* %3
%32 = call i32 @close(i32 %31)
store i32 -1, i32* %3
br label %34
33: ; preds = %21
br label %36
34: ; preds = %30, %20
%35 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %36
36: ; preds = %34, %33
%37 = load i32, i32* %3
ret i32 %37
}

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
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str = [32 x i8] c"ERROR: Failed on binding socket\00"
define i32 @make_request_socket(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 1, i32* %3
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %6, i32* %7
%8 = load i32, i32* @INADDR_ANY
%9 = tail call i32 @htonl(i32 %8)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %9, i32* %10
%11 = tail call i32 @htons(i32 %0)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %11, i32* %12
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = icmp eq i32 %15, -1
br i1 %16, label %26, label %17
17: ; preds = %1
%18 = load i32, i32* @SOL_SOCKET
%19 = load i32, i32* @SO_REUSEADDR
%20 = call i32 @setsockopt(i32 %15, i32 %18, i32 %19, i32* %3, i32 4)
%21 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%22 = call i64 @bind(i32 %15, %struct.sockaddr* %21, i32 12)
%23 = icmp eq i64 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %17
%25 = call i32 @close(i32 %15)
br label %26
26: ; preds = %1, %24
%27 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %17, %26
%29 = phi i32 [ -1, %26 ], [ %15, %17 ]
ret i32 %29
}