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

/* Type definitions */
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int bin_search_first(int *, size_t, int);
int bin_search_last(int *, size_t, int);
ssize_t occurrences(int arr[], size_t n, int val) {
  ssize_t first = bin_search_first(arr, n, val);
  if (first == -1) {
    return -1;
  }
  ssize_t last = bin_search_last(arr, n, val);
  assert(last != -1);
  return last - first + 1;
}


x86 assembly (clang, -O0):
<occurrences>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <bin_search_first@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    116c <occurrences+0x3c>
movl   $0xffffffff,-0x4(%rbp)
jmp    119c <occurrences+0x6c>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <bin_search_last@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x24(%rbp),%eax
sub    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<occurrences>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%rbp
call   1050 <bin_search_first@plt>
cmp    $0xffffffff,%eax
je     1173 <occurrences+0x43>
mov    %eax,%r14d
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1040 <bin_search_last@plt>
mov    %eax,%ebp
xor    %edi,%edi
cmp    $0xffffffff,%eax
setne  %dil
call   1030 <assert@plt>
sub    %r14d,%ebp
add    $0x1,%ebp
jmp    1178 <occurrences+0x48>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @occurrences(i32* %0, i64 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %5
store i64 %1, i64* %6
store i32 %2, i32* %7
%10 = load i32*, i32** %5
%11 = load i64, i64* %6
%12 = load i32, i32* %7
%13 = call i32 @bin_search_first(i32* %10, i64 %11, i32 %12)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 -1, i32* %4
br label %30
17: ; preds = %3
%18 = load i32*, i32** %5
%19 = load i64, i64* %6
%20 = load i32, i32* %7
%21 = call i32 @bin_search_last(i32* %18, i64 %19, i32 %20)
store i32 %21, i32* %9
%22 = load i32, i32* %9
%23 = icmp ne i32 %22, -1
%24 = zext i1 %23 to i32
%25 = call i32 @assert(i32 %24)
%26 = load i32, i32* %9
%27 = load i32, i32* %8
%28 = sub nsw i32 %26, %27
%29 = add nsw i32 %28, 1
store i32 %29, i32* %4
br label %30
30: ; preds = %17, %16
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
define i32 @occurrences(i32* %0, i64 %1, i32 %2) {
%4 = tail call i32 @bin_search_first(i32* %0, i64 %1, i32 %2)
%5 = icmp eq i32 %4, -1
br i1 %5, label %13, label %6
6: ; preds = %3
%7 = tail call i32 @bin_search_last(i32* %0, i64 %1, i32 %2)
%8 = icmp ne i32 %7, -1
%9 = zext i1 %8 to i32
%10 = tail call i32 @assert(i32 %9)
%11 = sub i32 1, %4
%12 = add i32 %11, %7
br label %13
13: ; preds = %3, %6
%14 = phi i32 [ %12, %6 ], [ -1, %3 ]
ret i32 %14
}