#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 */
typedef int /*<<< orphan*/ uint16_t;
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*/ INADDR_ANY;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
int bind_listen(int sock, uint16_t port) {
  struct sockaddr_in addr;
  int len = sizeof(struct sockaddr_in);
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(sock, (struct sockaddr *)&addr, len) < 0) {
    perror("Could not bind socket");
    return 0;
  }
  if (listen(sock, 10) < 0) {
    perror("listen");
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<bind_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xc,-0x1c(%rbp)
mov    0x2e7c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    11be <bind_listen+0x6e>
lea    0xe53(%rip),%rdi
call   1070 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f4 <bind_listen+0xa4>
mov    -0x8(%rbp),%edi
mov    $0xa,%esi
call   1050 <listen@plt>
cmp    $0x0,%rax
jge    11ed <bind_listen+0x9d>
lea    0xe3a(%rip),%rdi
call   1070 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f4 <bind_listen+0xa4>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bind_listen>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    0x2e8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %esi,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1060 <bind@plt>
test   %rax,%rax
js     11b3 <bind_listen+0x63>
mov    %ebx,%edi
mov    $0xa,%esi
call   1050 <listen@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
js     11bc <bind_listen+0x6c>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe46(%rip),%rdi
jmp    11c3 <bind_listen+0x73>
lea    0xe53(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
jmp    11ad <bind_listen+0x5d>

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
@INADDR_ANY = global i32 0
@.str = [22 x i8] c"Could not bind socket\00"
@.str.1 = [7 x i8] c"listen\00"
define i32 @bind_listen(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 12, i32* %7
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %8, i32* %9
%10 = load i32, i32* %5
%11 = call i32 @htons(i32 %10)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
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* %6, i32 0, i32 0
%16 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %15, i32 0, i32 0
store i32 %14, i32* %16
%17 = load i32, i32* %4
%18 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%19 = load i32, i32* %7
%20 = call i64 @bind(i32 %17, %struct.sockaddr* %18, i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %2
%23 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %31
24: ; preds = %2
%25 = load i32, i32* %4
%26 = call i64 @listen(i32 %25, i32 10)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %31
30: ; preds = %24
store i32 1, i32* %3
br label %31
31: ; preds = %30, %28, %22
%32 = load i32, i32* %3
ret i32 %32
}

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