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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int nsStreamSocketServerStart(int port) {
  int ret;
  int sock;
  printf("nsStreamSocketServerStart port:%d\n", port);
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (-1 == sock) {
    perror("\n nsStreamSocketServerStart create socket failed :");
    return -1;
  }
  struct sockaddr_in addr;
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
  addr.sin_port = htons(port);
  ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
  if (ret < 0) {
    perror("\n nsStreamSocketServerStart bind failed :");
    close(sock);
    return -1;
  }
  ret = listen(sock, 5);
  if (ret < 0) {
    perror("\n nsStreamSocketServerStart socket listen failed :");
    close(sock);
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<nsStreamSocketServerStart>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe5b(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x10(%rbp)
mov    $0xffffffff,%eax
cmp    -0x10(%rbp),%eax
jne    11ec <nsStreamSocketServerStart+0x5c>
lea    0xe48(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a2 <nsStreamSocketServerStart+0x112>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1262 <nsStreamSocketServerStart+0xd2>
lea    0xe0d(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a2 <nsStreamSocketServerStart+0x112>
mov    -0x10(%rbp),%edi
mov    $0x5,%esi
call   1080 <listen@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    129c <nsStreamSocketServerStart+0x10c>
lea    0xdfd(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a2 <nsStreamSocketServerStart+0x112>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nsStreamSocketServerStart>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
lea    0xe5d(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
mov    0x2e37(%rip),%r15
mov    (%r15),%edi
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     1233 <nsStreamSocketServerStart+0xa3>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %eax,%eax
js     1241 <nsStreamSocketServerStart+0xb1>
mov    %ebx,%edi
mov    $0x5,%esi
call   1080 <listen@plt>
test   %eax,%eax
js     124a <nsStreamSocketServerStart+0xba>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde9(%rip),%rdi
call   10a0 <perror@plt>
jmp    125d <nsStreamSocketServerStart+0xcd>
lea    0xe0e(%rip),%rdi
jmp    1251 <nsStreamSocketServerStart+0xc1>
lea    0xe2f(%rip),%rdi
call   10a0 <perror@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0xffffffff,%ebx
jmp    1226 <nsStreamSocketServerStart+0x96>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [35 x i8] c"nsStreamSocketServerStart port:%d\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [51 x i8] c"\0A nsStreamSocketServerStart create socket failed :\00"
@INADDR_ANY = global i32 0
@.str.2 = [42 x i8] c"\0A nsStreamSocketServerStart bind failed :\00"
@.str.3 = [51 x i8] c"\0A nsStreamSocketServerStart socket listen failed :\00"
define i32 @nsStreamSocketServerStart(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp eq i32 -1, %12
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %47
16: ; preds = %1
%17 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_ANY
%21 = call i32 @htonl(i32 %20)
%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 i32, i32* %3
%25 = call i32 @htons(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %25, i32* %26
%27 = load i32, i32* %5
%28 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%29 = call i32 @bind(i32 %27, %struct.sockaddr* %28, i32 12)
store i32 %29, i32* %4
%30 = load i32, i32* %4
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %16
%33 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
%34 = load i32, i32* %5
%35 = call i32 @close(i32 %34)
store i32 -1, i32* %2
br label %47
36: ; preds = %16
%37 = load i32, i32* %5
%38 = call i32 @listen(i32 %37, i32 5)
store i32 %38, i32* %4
%39 = load i32, i32* %4
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %36
%42 = call i32 @perror(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0))
%43 = load i32, i32* %5
%44 = call i32 @close(i32 %43)
store i32 -1, i32* %2
br label %47
45: ; preds = %36
%46 = load i32, i32* %5
store i32 %46, i32* %2
br label %47
47: ; preds = %45, %41, %32, %14
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [35 x i8] c"nsStreamSocketServerStart port:%d\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [51 x i8] c"\0A nsStreamSocketServerStart create socket failed :\00"
@INADDR_ANY = global i32 0
@.str.2 = [42 x i8] c"\0A nsStreamSocketServerStart bind failed :\00"
@.str.3 = [51 x i8] c"\0A nsStreamSocketServerStart socket listen failed :\00"
define i32 @nsStreamSocketServerStart(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %0)
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0))
br label %32
10: ; preds = %1
%11 = bitcast %struct.sockaddr_in* %2 to i8*
%12 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %13, i32* %14
%15 = load i32, i32* @INADDR_ANY
%16 = call i32 @htonl(i32 %15)
%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(i32 %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 %6, %struct.sockaddr* %20, i32 12)
%22 = icmp slt i32 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %10
%24 = call i32 @listen(i32 %6, i32 5)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %23, %10
%27 = phi i8* [ getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), %10 ], [ getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), %23 ]
%28 = call i32 @perror(i8* %27)
%29 = call i32 @close(i32 %6)
br label %30
30: ; preds = %26, %23
%31 = phi i32 [ %6, %23 ], [ -1, %26 ]
br label %32
32: ; preds = %30, %8
%33 = phi i32 [ -1, %8 ], [ %31, %30 ]
ret i32 %33
}