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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
tcp_serv_init(int serv_port) {
  int port = serv_port;
  int servfd;
  servfd = socket(AF_INET, SOCK_STREAM, 0);
  struct sockaddr_in servaddr;
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(port);
  int ret;
  ret = bind(servfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
  if (ret < 0) {
    printf("bind error\n");
    exit(1);
  }
  listen(servfd, 5);
  return servfd;
}


x86 assembly (clang, -O0):
<tcp_serv_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0xc(%rbp)
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11f9 <tcp_serv_init+0x89>
lea    0xe16(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%edi
mov    $0x5,%esi
call   1060 <listen@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_serv_init>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
mov    0x2e67(%rip),%r14
mov    (%r14),%edi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0x8(%rsp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x4(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %eax,%eax
js     11e3 <tcp_serv_init+0x73>
mov    %ebx,%edi
mov    $0x5,%esi
call   1060 <listen@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe16(%rip),%rdi
call   1030 <puts@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 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str = [12 x i8] c"bind error\0A\00"
define i32 @tcp_serv_init(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
store i32 %0, i32* %2
%7 = load i32, i32* %2
store i32 %7, i32* %3
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %4
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %11, i32* %12
%13 = load i32, i32* @INADDR_ANY
%14 = call i32 @htonl(i32 %13)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%16 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %15, i32 0, i32 0
store i32 %14, i32* %16
%17 = load i32, i32* %3
%18 = call i32 @htons(i32 %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* %4
%21 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%22 = call i32 @bind(i32 %20, %struct.sockaddr* %21, i32 12)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %1
%26 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %1
%29 = load i32, i32* %4
%30 = call i32 @listen(i32 %29, i32 5)
%31 = load i32, i32* %4
ret i32 %31
}

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
@INADDR_ANY = global i32 0
@str = [11 x i8] c"bind error\00"
define i32 @tcp_serv_init(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %7, i32* %8
%9 = load i32, i32* @INADDR_ANY
%10 = tail call i32 @htonl(i32 %9)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %10, i32* %11
%12 = tail call i32 @htons(i32 %0)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %12, i32* %13
%14 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%15 = call i32 @bind(i32 %5, %struct.sockaddr* %14, i32 12)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %1
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %1
%21 = call i32 @listen(i32 %5, i32 5)
ret i32 %5
}