#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*/ serv_addr;
typedef int /*<<< orphan*/ reuseon;
typedef int /*<<< orphan*/ keepalive;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_KEEPALIVE;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char const *);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, char, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int ytcpsocket_listen(const char *addr, int port) {
  int socketfd = socket(AF_INET, SOCK_STREAM, 0);
  int reuseon = 1;
  setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR, &reuseon, sizeof(reuseon));
  int keepalive = 1;
  setsockopt(socketfd, SOL_SOCKET, SO_KEEPALIVE, &keepalive, sizeof(keepalive));
  struct sockaddr_in serv_addr;
  memset(&serv_addr, '\0', sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = inet_addr(addr);
  serv_addr.sin_port = htons(port);
  int r = bind(socketfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
  if (r == 0) {
    if (listen(socketfd, 128) == 0) {
      return socketfd;
    } else {
      return -2;
    }
  } else {
    return -1;
  }
}


x86 assembly (clang, -O0):
<ytcpsocket_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
mov    0x2e13(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
movl   $0x1,-0x20(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
mov    0x2dd8(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    1270 <ytcpsocket_listen+0x100>
mov    -0x18(%rbp),%edi
mov    $0x80,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jne    1264 <ytcpsocket_listen+0xf4>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1277 <ytcpsocket_listen+0x107>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1277 <ytcpsocket_listen+0x107>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ytcpsocket_listen>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
mov    0x2e57(%rip),%r12
mov    (%r12),%edi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebx
movl   $0x1,0xc(%rsp)
mov    0x2e3a(%rip),%rbp
mov    0x0(%rbp),%esi
mov    0x2e10(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
movl   $0x1,0x8(%rsp)
mov    0x0(%rbp),%esi
mov    0x2dda(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
mov    %r15,%rdi
call   1060 <inet_addr@plt>
mov    %eax,0x14(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
test   %ecx,%ecx
jne    1257 <ytcpsocket_listen+0xe7>
mov    %ebx,%edi
mov    $0x80,%esi
call   1070 <listen@plt>
test   %rax,%rax
mov    $0xfffffffe,%eax
cmove  %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
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
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SO_KEEPALIVE = global i32 0
define i32 @ytcpsocket_listen(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %6
store i32 1, i32* %7
%14 = load i32, i32* %6
%15 = load i32, i32* @SOL_SOCKET
%16 = load i32, i32* @SO_REUSEADDR
%17 = call i32 @setsockopt(i32 %14, i32 %15, i32 %16, i32* %7, i32 4)
store i32 1, i32* %8
%18 = load i32, i32* %6
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_KEEPALIVE
%21 = call i32 @setsockopt(i32 %18, i32 %19, i32 %20, i32* %8, i32 4)
%22 = call i32 @memset(%struct.sockaddr_in* %9, i8 signext 0, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %23, i32* %24
%25 = load i8*, i8** %4
%26 = call i32 @inet_addr(i8* %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = load i32, i32* %5
%30 = call i32 @htons(i32 %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %30, i32* %31
%32 = load i32, i32* %6
%33 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%34 = call i32 @bind(i32 %32, %struct.sockaddr* %33, i32 12)
store i32 %34, i32* %10
%35 = load i32, i32* %10
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %44
37: ; preds = %2
%38 = load i32, i32* %6
%39 = call i64 @listen(i32 %38, i32 128)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = load i32, i32* %6
store i32 %42, i32* %3
br label %45
43: ; preds = %37
store i32 -2, i32* %3
br label %45
44: ; preds = %2
store i32 -1, i32* %3
br label %45
45: ; preds = %44, %43, %41
%46 = load i32, i32* %3
ret i32 %46
}

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
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SO_KEEPALIVE = global i32 0
define i32 @ytcpsocket_listen(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%10 = load i32, i32* @SOL_SOCKET
%11 = load i32, i32* @SO_REUSEADDR
%12 = call i32 @setsockopt(i32 %8, i32 %10, i32 %11, i32* %3, i32 4)
%13 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_KEEPALIVE
%16 = call i32 @setsockopt(i32 %8, i32 %14, i32 %15, i32* %4, i32 4)
%17 = bitcast %struct.sockaddr_in* %5 to i8*
%18 = call i32 @memset(%struct.sockaddr_in* %5, i8 signext 0, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %19, i32* %20
%21 = call i32 @inet_addr(i8* %0)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
store i32 %21, i32* %22
%23 = call i32 @htons(i32 %1)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %23, i32* %24
%25 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%26 = call i32 @bind(i32 %8, %struct.sockaddr* %25, i32 12)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %2
%29 = call i64 @listen(i32 %8, i32 128)
%30 = icmp eq i64 %29, 0
%31 = select i1 %30, i32 %8, i32 -2
br label %32
32: ; preds = %2, %28
%33 = phi i32 [ %31, %28 ], [ -1, %2 ]
ret i32 %33
}