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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(short);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, char, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char tcp_create_socket(short int s_port) {
  int err;
  int listen_sock;
  int sock;
  struct sockaddr_in sa_serv;
  struct sockaddr_in sa_cli;
  listen_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  if ((listen_sock) == -1) {
    perror("socket");
    exit(1);
  };
  memset(&sa_serv, '\0', sizeof(sa_serv));
  sa_serv.sin_family = AF_INET;
  sa_serv.sin_addr.s_addr = INADDR_ANY;
  sa_serv.sin_port = htons(s_port);
  err = bind(listen_sock, (struct sockaddr *)&sa_serv, sizeof(sa_serv));
  if ((err) == -1) {
    perror("bind");
    exit(1);
  };
  err = listen(listen_sock, 500000);
  return listen_sock;
}


x86 assembly (clang, -O0):
<tcp_create_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %di,%ax
mov    %ax,-0x2(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    0x2e40(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11c2 <tcp_create_socket+0x52>
lea    0xe4d(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
movswl -0x2(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    122a <tcp_create_socket+0xba>
lea    0xdec(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%edi
mov    $0x7a120,%esi
call   1050 <listen@plt>
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
movsbl %al,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_create_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
mov    0x2e45(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     1203 <tcp_create_socket+0x93>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
movswl %bp,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
je     120c <tcp_create_socket+0x9c>
mov    %ebx,%edi
mov    $0x7a120,%esi
call   1050 <listen@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdf6(%rip),%rdi
jmp    1213 <tcp_create_socket+0xa3>
lea    0xdf4(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [7 x i8] c"socket\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
define signext i8 @tcp_create_socket(i16 signext %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 %9, label %12
9: ; preds = %1
%10 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %1
%13 = call i32 @memset(%struct.sockaddr_in* %2, i8 signext 0, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %14, i32* %15
%16 = load i32, i32* @INADDR_ANY
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %16, i32* %17
%18 = call i32 @htons(i16 signext %0)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %18, i32* %19
%20 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%21 = call i32 @bind(i32 %7, %struct.sockaddr* %20, i32 12)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %12
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %12
%27 = call i32 @listen(i32 %7, i32 500000)
%28 = trunc i32 %7 to i8
ret i8 %28
}