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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_ERROR;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int /*<<< orphan*/ *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char const *);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, char, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tcpsocket_listen(const char *serveraddress, int port) {
  int nRet = -1;
  int socketfd = socket(AF_INET, SOCK_STREAM, 0);
  if (socketfd > 0) {
    int reuseon = 1;
    setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR, &reuseon, sizeof(reuseon));
    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(serveraddress);
    serv_addr.sin_port = htons(port);
    int nBind =
        bind(socketfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
    if (nBind == 0) {
      int backlog = 128;
      int nListen = listen(socketfd, backlog);
      if (nListen == 0) {
        int error = 0;
        int errlen = sizeof(error);
        getsockopt(socketfd, SOL_SOCKET, SO_ERROR, &error,
                   (socklen_t *)&errlen);
        if (0 == error) {
          nRet = socketfd;
        } else {
          printf("tcpsocket_listen.listen socket fail with error:%d \n", error);
          nRet = -5;
        }
      } else {
        printf("tcpsocket_listen.listen socket fail!\n");
        nRet = -4;
      }
    } else {
      printf("tcpsocket_listen.bind socket fail!\n");
      nRet = -3;
    }
  } else {
    printf("tcpsocket_listen.create socket fail!\n");
    nRet = -2;
  }
  return nRet;
}


x86 assembly (clang, -O0):
<tcpsocket_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    12fe <tcpsocket_listen+0x16e>
movl   $0x1,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
mov    0x2dda(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <inet_addr@plt>
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    12e4 <tcpsocket_listen+0x154>
movl   $0x80,-0x30(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x30(%rbp),%esi
call   1090 <listen@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    12ca <tcpsocket_listen+0x13a>
movl   $0x0,-0x38(%rbp)
movl   $0x4,-0x3c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d69(%rip),%rax
mov    (%rax),%esi
mov    0x2d40(%rip),%rax
mov    (%rax),%edx
lea    -0x38(%rbp),%rcx
lea    -0x3c(%rbp),%r8
call   1070 <getsockopt@plt>
xor    %eax,%eax
cmp    -0x38(%rbp),%eax
jne    12ad <tcpsocket_listen+0x11d>
mov    -0x14(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    12c5 <tcpsocket_listen+0x135>
mov    -0x38(%rbp),%esi
lea    0xd49(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xfffffffb,-0x10(%rbp)
jmp    12df <tcpsocket_listen+0x14f>
lea    0xd63(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xfffffffc,-0x10(%rbp)
jmp    12f9 <tcpsocket_listen+0x169>
lea    0xd6f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xfffffffd,-0x10(%rbp)
jmp    1313 <tcpsocket_listen+0x183>
lea    0xd79(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xfffffffe,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcpsocket_listen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e25(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
jle    1261 <tcpsocket_listen+0xc1>
mov    %eax,%ebx
movl   $0x1,0x14(%rsp)
mov    0x2e00(%rip),%r12
mov    (%r12),%esi
mov    0x2dcd(%rip),%rax
mov    (%rax),%edx
lea    0x14(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
lea    0x18(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x0(%r13),%eax
mov    %eax,0x20(%rsp)
mov    %rbp,%rdi
call   1090 <inet_addr@plt>
mov    %eax,0x1c(%rsp)
mov    %r14d,%edi
call   1050 <htons@plt>
mov    %eax,0x18(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
test   %eax,%eax
je     1274 <tcpsocket_listen+0xd4>
lea    0xe04(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffffd,%ebx
jmp    12e0 <tcpsocket_listen+0x140>
lea    0xdcc(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffffe,%ebx
jmp    12e0 <tcpsocket_listen+0x140>
mov    %ebx,%edi
mov    $0x80,%esi
call   10a0 <listen@plt>
test   %eax,%eax
je     1297 <tcpsocket_listen+0xf7>
lea    0xdf1(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffffc,%ebx
jmp    12e0 <tcpsocket_listen+0x140>
movl   $0x0,0xc(%rsp)
movl   $0x4,0x10(%rsp)
mov    (%r12),%esi
mov    0x2d16(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
lea    0x10(%rsp),%r8
mov    %ebx,%edi
call   1080 <getsockopt@plt>
mov    0xc(%rsp),%esi
test   %esi,%esi
je     12e0 <tcpsocket_listen+0x140>
lea    0xd2c(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xfffffffb,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
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_ERROR = global i32 0
@.str = [52 x i8] c"tcpsocket_listen.listen socket fail with error:%d \0A\00"
@.str.1 = [38 x i8] c"tcpsocket_listen.listen socket fail!\0A\00"
@.str.2 = [36 x i8] c"tcpsocket_listen.bind socket fail!\0A\00"
@.str.3 = [38 x i8] c"tcpsocket_listen.create socket fail!\0A\00"
define i32 @tcpsocket_listen(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 -1, i32* %5
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %64
19: ; preds = %2
store i32 1, i32* %7
%20 = load i32, i32* %6
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_REUSEADDR
%23 = call i32 @setsockopt(i32 %20, i32 %21, i32 %22, i32* %7, i32 4)
%24 = call i32 @memset(%struct.sockaddr_in* %8, i8 signext 0, i32 12)
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %25, i32* %26
%27 = load i8*, i8** %3
%28 = call i32 @inet_addr(i8* %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
store i32 %28, i32* %30
%31 = load i32, i32* %4
%32 = call i32 @htons(i32 %31)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %32, i32* %33
%34 = load i32, i32* %6
%35 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%36 = call i32 @bind(i32 %34, %struct.sockaddr* %35, i32 12)
store i32 %36, i32* %9
%37 = load i32, i32* %9
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %61
39: ; preds = %19
store i32 128, i32* %10
%40 = load i32, i32* %6
%41 = load i32, i32* %10
%42 = call i32 @listen(i32 %40, i32 %41)
store i32 %42, i32* %11
%43 = load i32, i32* %11
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %58
45: ; preds = %39
store i32 0, i32* %12
store i32 4, i32* %13
%46 = load i32, i32* %6
%47 = load i32, i32* @SOL_SOCKET
%48 = load i32, i32* @SO_ERROR
%49 = call i32 @getsockopt(i32 %46, i32 %47, i32 %48, i32* %12, i32* %13)
%50 = load i32, i32* %12
%51 = icmp eq i32 0, %50
br i1 %51, label %52, label %54
52: ; preds = %45
%53 = load i32, i32* %6
store i32 %53, i32* %5
br label %57
54: ; preds = %45
%55 = load i32, i32* %12
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i32 %55)
store i32 -5, i32* %5
br label %57
57: ; preds = %54, %52
br label %60
58: ; preds = %39
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
store i32 -4, i32* %5
br label %60
60: ; preds = %58, %57
br label %63
61: ; preds = %19
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
store i32 -3, i32* %5
br label %63
63: ; preds = %61, %60
br label %66
64: ; preds = %2
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
store i32 -2, i32* %5
br label %66
66: ; preds = %64, %63
%67 = load i32, i32* %5
ret i32 %67
}

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_ERROR = global i32 0
@.str = [52 x i8] c"tcpsocket_listen.listen socket fail with error:%d \0A\00"
@str = [37 x i8] c"tcpsocket_listen.create socket fail!\00"
@str.4 = [35 x i8] c"tcpsocket_listen.bind socket fail!\00"
@str.5 = [37 x i8] c"tcpsocket_listen.listen socket fail!\00"
define i32 @tcpsocket_listen(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %48
11: ; preds = %2
%12 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%13 = load i32, i32* @SOL_SOCKET
%14 = load i32, i32* @SO_REUSEADDR
%15 = call i32 @setsockopt(i32 %9, i32 %13, i32 %14, i32* %3, i32 4)
%16 = bitcast %struct.sockaddr_in* %4 to i8*
%17 = call i32 @memset(%struct.sockaddr_in* %4, i8 signext 0, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %18, i32* %19
%20 = call i32 @inet_addr(i8* %0)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %20, i32* %21
%22 = call i32 @htons(i32 %1)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %22, i32* %23
%24 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%25 = call i32 @bind(i32 %9, %struct.sockaddr* %24, i32 12)
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %44
27: ; preds = %11
%28 = call i32 @listen(i32 %9, i32 128)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %42
30: ; preds = %27
%31 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%32 = bitcast i32* %6 to i8*
store i32 4, i32* %6
%33 = load i32, i32* @SOL_SOCKET
%34 = load i32, i32* @SO_ERROR
%35 = call i32 @getsockopt(i32 %9, i32 %33, i32 %34, i32* %5, i32* %6)
%36 = load i32, i32* %5
%37 = icmp eq i32 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %30
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i32 %36)
br label %40
40: ; preds = %30, %38
%41 = phi i32 [ -5, %38 ], [ %9, %30 ]
br label %46
42: ; preds = %27
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.5, i64 0, i64 0))
br label %46
44: ; preds = %11
%45 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.4, i64 0, i64 0))
br label %46
46: ; preds = %40, %42, %44
%47 = phi i32 [ -3, %44 ], [ %41, %40 ], [ -4, %42 ]
br label %50
48: ; preds = %2
%49 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str, i64 0, i64 0))
br label %50
50: ; preds = %48, %46
%51 = phi i32 [ %47, %46 ], [ -2, %48 ]
ret i32 %51
}