#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(unsigned int);
int /*<<< orphan*/ inet_addr(char *);
scalar_t__ listen(int, unsigned short);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tcpsock_bind(char *host, unsigned int port, unsigned short backlog) {
  struct sockaddr_in addr;
  int sfd;
  sfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (sfd < 0) {
    perror("socket() ");
    return -1;
  }
  memset((char *)&addr, 0, sizeof addr);
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = inet_addr(host);
  if (bind(sfd, (struct sockaddr *)&addr, sizeof addr) < 0) {
    perror("bind() ");
    close(sfd);
    return -1;
  }
  if (listen(sfd, backlog) < 0) {
    perror("listen() ");
    close(sfd);
    return -1;
  }
  return sfd;
}


x86 assembly (clang, -O0):
<tcpsock_bind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dx,%ax
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %ax,-0x16(%rbp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11db <tcpsock_bind+0x5b>
lea    0xe36(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1285 <tcpsock_bind+0x105>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    1249 <tcpsock_bind+0xc9>
lea    0xdda(%rip),%rdi
call   1090 <perror@plt>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1285 <tcpsock_bind+0x105>
mov    -0x2c(%rbp),%edi
movzwl -0x16(%rbp),%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    127f <tcpsock_bind+0xff>
lea    0xdac(%rip),%rdi
call   1090 <perror@plt>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1285 <tcpsock_bind+0x105>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcpsock_bind>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%r15
mov    0x2e4b(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
mov    0x2e2f(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1226 <tcpsock_bind+0xa6>
mov    %eax,%ebx
lea    0x8(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %r15,%rdi
call   1060 <inet_addr@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     1234 <tcpsock_bind+0xb4>
movzwl %r14w,%esi
mov    %ebx,%edi
call   1070 <listen@plt>
test   %rax,%rax
js     123d <tcpsock_bind+0xbd>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd3(%rip),%rdi
call   1090 <perror@plt>
jmp    1250 <tcpsock_bind+0xd0>
lea    0xdcf(%rip),%rdi
jmp    1244 <tcpsock_bind+0xc4>
lea    0xdce(%rip),%rdi
call   1090 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
jmp    1215 <tcpsock_bind+0x95>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [10 x i8] c"socket() \00"
@.str.1 = [8 x i8] c"bind() \00"
@.str.2 = [10 x i8] c"listen() \00"
define i32 @tcpsock_bind(i8* %0, i32 %1, i16 zeroext %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = load i32, i32* @IPPROTO_TCP
%9 = tail call i32 @socket(i32 %6, i32 %7, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %33
13: ; preds = %3
%14 = call i32 @memset(i8* %5, i32 0, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %15, i32* %16
%17 = call i32 @htons(i32 %1)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %17, i32* %18
%19 = call i32 @inet_addr(i8* %0)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %19, i32* %20
%21 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%22 = call i64 @bind(i32 %9, %struct.sockaddr* %21, i32 12)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %13
%25 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%26 = call i32 @close(i32 %9)
br label %33
27: ; preds = %13
%28 = call i64 @listen(i32 %9, i16 zeroext %2)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @close(i32 %9)
br label %33
33: ; preds = %27, %30, %24, %11
%34 = phi i32 [ -1, %11 ], [ -1, %24 ], [ -1, %30 ], [ %9, %27 ]
ret i32 %34
}