#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*/ opt;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ htons(int const);
int /*<<< orphan*/ puts(char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int make_socket(const int port, struct sockaddr_in *name) {
  int sock, opt = 1;
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock == -1)
    error("ERROR opening socket");
  puts("Socket created...");
  name->sin_family = AF_INET;
  name->sin_port = htons(port);
  name->sin_addr.s_addr = INADDR_ANY;
  if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)) < 0)
    error("ERROR setsockopt");
  if (bind(sock, (struct sockaddr *)name, sizeof(struct sockaddr_in)) == -1)
    error("ERROR binding");
  puts("Binding done...");
  return sock;
}


x86 assembly (clang, -O0):
<make_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x18(%rbp)
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11a8 <make_socket+0x48>
lea    0xe5d(%rip),%rdi
call   1070 <error@plt>
lea    0xe66(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x4(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    0x2dfc(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%edi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
mov    0x2dc9(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0x0,%rax
jge    121e <make_socket+0xbe>
lea    0xe0e(%rip),%rdi
call   1070 <error@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
jne    1244 <make_socket+0xe4>
lea    0xdf9(%rip),%rdi
call   1070 <error@plt>
lea    0xdfb(%rip),%rdi
call   1030 <puts@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%r14d
movl   $0x1,0x4(%rsp)
mov    0x2e64(%rip),%r15
mov    (%r15),%edi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11a2 <make_socket+0x42>
lea    0xe63(%rip),%rdi
call   1070 <error@plt>
lea    0xe6c(%rip),%rdi
call   1030 <puts@plt>
mov    (%r15),%eax
mov    %eax,0x8(%rbx)
mov    %r14d,%edi
call   1050 <htons@plt>
mov    %eax,0x4(%rbx)
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rbx)
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
mov    0x2de6(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
test   %rax,%rax
jns    11ff <make_socket+0x9f>
lea    0xe2d(%rip),%rdi
call   1070 <error@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
jne    121f <make_socket+0xbf>
lea    0xe1e(%rip),%rdi
call   1070 <error@plt>
lea    0xe20(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"ERROR opening socket\00"
@.str.1 = [18 x i8] c"Socket created...\00"
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [17 x i8] c"ERROR setsockopt\00"
@.str.3 = [14 x i8] c"ERROR binding\00"
@.str.4 = [16 x i8] c"Binding done...\00"
define i32 @make_socket(i32 %0, %struct.sockaddr_in* %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in*
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %4
store i32 1, i32* %6
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = call i32 @puts(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @AF_INET
%17 = load %struct.sockaddr_in*, %struct.sockaddr_in** %4
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %17, i32 0, i32 2
store i32 %16, i32* %18
%19 = load i32, i32* %3
%20 = call i32 @htons(i32 %19)
%21 = load %struct.sockaddr_in*, %struct.sockaddr_in** %4
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %21, i32 0, i32 1
store i32 %20, i32* %22
%23 = load i32, i32* @INADDR_ANY
%24 = load %struct.sockaddr_in*, %struct.sockaddr_in** %4
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %24, i32 0, i32 0
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
store i32 %23, i32* %26
%27 = load i32, i32* %5
%28 = load i32, i32* @SOL_SOCKET
%29 = load i32, i32* @SO_REUSEADDR
%30 = bitcast i32* %6 to i8*
%31 = call i64 @setsockopt(i32 %27, i32 %28, i32 %29, i8* %30, i32 4)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %14
%34 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %33, %14
%36 = load i32, i32* %5
%37 = load %struct.sockaddr_in*, %struct.sockaddr_in** %4
%38 = bitcast %struct.sockaddr_in* %37 to %struct.sockaddr*
%39 = call i32 @bind(i32 %36, %struct.sockaddr* %38, i32 12)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %35
%42 = call i32 @error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %41, %35
%44 = call i32 @puts(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%45 = load i32, i32* %5
ret i32 %45
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"ERROR opening socket\00"
@.str.1 = [18 x i8] c"Socket created...\00"
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [17 x i8] c"ERROR setsockopt\00"
@.str.3 = [14 x i8] c"ERROR binding\00"
@.str.4 = [16 x i8] c"Binding done...\00"
define i32 @make_socket(i32 %0, %struct.sockaddr_in* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %13, i32* %14
%15 = tail call i32 @htons(i32 %0)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %15, i32* %16
%17 = load i32, i32* @INADDR_ANY
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_REUSEADDR
%21 = call i64 @setsockopt(i32 %7, i32 %19, i32 %20, i8* %4, i32 4)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %11
%24 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %23, %11
%26 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%27 = call i32 @bind(i32 %7, %struct.sockaddr* %26, i32 12)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %31
31: ; preds = %29, %25
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
ret i32 %7
}