#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*/ sa;
typedef int /*<<< orphan*/ on;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(void *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int l_bind_port(int port) {
  printf("to bind port : [%d]\n", port);
  int fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd < 0) {
    printf("get socket faild\n");
    return -1;
  }
  printf("get socket success : [%d]\n", fd);
  int on = 1;
  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
    printf("setsockopt faild\n");
    return -1;
  }
  printf("setsockopt opetion success : [SO_REUSEADDR](%d)\n", SO_REUSEADDR);
  struct sockaddr_in sa;
  bzero((void *)&sa, sizeof(sa));
  sa.sin_family = AF_INET;
  sa.sin_port = htons(port);
  sa.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
    close(fd);
    printf("bind at port %d faild\n", port);
    return -1;
  }
  printf("bing at port success : [%d]\n", port);
  int maxconn = 10;
  if (listen(fd, maxconn) < 0) {
    close(fd);
    printf("listen port [%d] faild\n", port);
    return -1;
  }
  printf("listen port success : [%d]\n", port);
  return 0;
}


x86 assembly (clang, -O0):
<l_bind_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11ec <l_bind_port+0x5c>
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1343 <l_bind_port+0x1b3>
mov    -0xc(%rbp),%esi
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x10(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
mov    0x2da9(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    124c <l_bind_port+0xbc>
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1343 <l_bind_port+0x1b3>
mov    0x2d6d(%rip),%rax
mov    (%rax),%esi
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x2d68(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    12d9 <l_bind_port+0x149>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    -0x8(%rbp),%esi
lea    0xdbf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1343 <l_bind_port+0x1b3>
mov    -0x8(%rbp),%esi
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xa,-0x24(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x24(%rbp),%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
jge    132b <l_bind_port+0x19b>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    -0x8(%rbp),%esi
lea    0xda1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1343 <l_bind_port+0x1b3>
mov    -0x8(%rbp),%esi
lea    0xd9c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<l_bind_port>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edi,%r14d
lea    0xe4a(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2e19(%rip),%r12
mov    (%r12),%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     12b3 <l_bind_port+0x113>
mov    %eax,%ebp
lea    0xe29(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
movl   $0x1,0xc(%rsp)
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
mov    0x2db3(%rip),%rbx
mov    (%rbx),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
test   %rax,%rax
js     12bc <l_bind_port+0x11c>
mov    (%rbx),%esi
lea    0xdfd(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
mov    %r14d,%edi
call   1050 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
js     12cf <l_bind_port+0x12f>
lea    0xde9(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%edi
mov    $0xa,%esi
call   1090 <listen@plt>
test   %rax,%rax
js     12d8 <l_bind_port+0x138>
lea    0xdfc(%rip),%r15
jmp    12eb <l_bind_port+0x14b>
lea    0xe20(%rip),%rdi
jmp    12c3 <l_bind_port+0x123>
lea    0xe06(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
jmp    12f8 <l_bind_port+0x158>
lea    0xd8b(%rip),%r15
jmp    12df <l_bind_port+0x13f>
lea    0xdb6(%rip),%r15
mov    %ebp,%edi
call   1080 <close@plt>
mov    $0xffffffff,%ebx
mov    %r15,%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
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 }
@.str = [21 x i8] c"to bind port : [%d]\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [18 x i8] c"get socket faild\0A\00"
@.str.2 = [27 x i8] c"get socket success : [%d]\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.3 = [18 x i8] c"setsockopt faild\0A\00"
@.str.4 = [49 x i8] c"setsockopt opetion success : [SO_REUSEADDR](%d)\0A\00"
@INADDR_ANY = global i32 0
@.str.5 = [23 x i8] c"bind at port %d faild\0A\00"
@.str.6 = [29 x i8] c"bing at port success : [%d]\0A\00"
@.str.7 = [24 x i8] c"listen port [%d] faild\0A\00"
@.str.8 = [28 x i8] c"listen port success : [%d]\0A\00"
define i32 @l_bind_port(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
store i32 %0, i32* %3
%8 = load i32, i32* %3
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %65
17: ; preds = %1
%18 = load i32, i32* %4
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %18)
store i32 1, i32* %5
%20 = load i32, i32* %4
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_REUSEADDR
%23 = call i64 @setsockopt(i32 %20, i32 %21, i32 %22, i32* %5, i32 4)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %17
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %65
27: ; preds = %17
%28 = load i32, i32* @SO_REUSEADDR
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0), i32 %28)
%30 = bitcast %struct.sockaddr_in* %6 to i8*
%31 = call i32 @bzero(i8* %30, i32 12)
%32 = load i32, i32* @AF_INET
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %32, i32* %33
%34 = load i32, i32* %3
%35 = call i32 @htons(i32 %34)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %35, i32* %36
%37 = load i32, i32* @INADDR_ANY
%38 = call i32 @htonl(i32 %37)
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%40 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %39, i32 0, i32 0
store i32 %38, i32* %40
%41 = load i32, i32* %4
%42 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%43 = call i64 @bind(i32 %41, %struct.sockaddr* %42, i32 12)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %27
%46 = load i32, i32* %4
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* %3
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %48)
store i32 -1, i32* %2
br label %65
50: ; preds = %27
%51 = load i32, i32* %3
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 %51)
store i32 10, i32* %7
%53 = load i32, i32* %4
%54 = load i32, i32* %7
%55 = call i64 @listen(i32 %53, i32 %54)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %62
57: ; preds = %50
%58 = load i32, i32* %4
%59 = call i32 @close(i32 %58)
%60 = load i32, i32* %3
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i32 %60)
store i32 -1, i32* %2
br label %65
62: ; preds = %50
%63 = load i32, i32* %3
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.8, i64 0, i64 0), i32 %63)
store i32 0, i32* %2
br label %65
65: ; preds = %62, %57, %45, %25, %15
%66 = load i32, i32* %2
ret i32 %66
}

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