#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 double clock_t;

/* Variables and functions */
scalar_t__ CLOCKS_PER_SEC;
double clock();
size_t cntbb(char *, int);
size_t cntbr(char *, int);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int rand();
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/);
int main() {
  srand(time(0));
  char *string = malloc(sizeof(char) * (1 << 28));
  for (size_t i = 0; i < (1 << 28); i++) {
    string[i] = 'a' + (rand() & 25);
  }
  for (size_t i = 0; i < (1 << 28); i++) {
    if ((rand() & 3) == 0) {
      string[i] = ' ';
    }
  }
  clock_t start = clock();
  size_t bb_words = cntbb(string, (1 << 28));
  clock_t end = clock();
  double byte_time = (end - start) / (double)CLOCKS_PER_SEC;
  printf("Words: %ld (time: %lf)\n", bb_words, byte_time);
  start = clock();
  size_t br_words = cntbr(string, (1 << 28));
  end = clock();
  double reg_time = (end - start) / (double)CLOCKS_PER_SEC;
  printf("Words: %ld (time: %lf)\n", br_words, reg_time);
  if (bb_words == br_words) {
    printf("Test OK: nubers are equal\n");
  } else {
    printf("Test FAIL: numbers are not equal\n");
  }
  free(string);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
xor    %edi,%edi
call   1090 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
mov    $0x10000000,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x10000000,-0x18(%rbp)
10
jae    11fc <main+0x6c>
mov    $0x0,%al
call   10b0 <rand@plt>
and    $0x19,%eax
add    $0x61,%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11c3 <main+0x33>
movq   $0x0,-0x20(%rbp)
cmpq   $0x10000000,-0x20(%rbp)
10
jae    1247 <main+0xb7>
mov    $0x0,%al
call   10b0 <rand@plt>
and    $0x3,%eax
cmp    $0x0,%eax
jne    1231 <main+0xa1>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x20,(%rax,%rcx,1)
jmp    1236 <main+0xa6>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1204 <main+0x74>
mov    $0x0,%al
call   1050 <clock@plt>
movsd  %xmm0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x10000000,%esi
call   1040 <cntbb@plt>
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1050 <clock@plt>
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
subsd  -0x28(%rbp),%xmm0
mov    0x2d66(%rip),%rax
cvtsi2sdq (%rax),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x30(%rbp),%rsi
movsd  -0x40(%rbp),%xmm0
lea    0xd60(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1050 <clock@plt>
movsd  %xmm0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x10000000,%esi
call   1080 <cntbr@plt>
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1050 <clock@plt>
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
subsd  -0x28(%rbp),%xmm0
mov    0x2d06(%rip),%rax
cvtsi2sdq (%rax),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x50(%rbp)
mov    -0x48(%rbp),%rsi
movsd  -0x50(%rbp),%xmm0
lea    0xd00(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
mov    -0x30(%rbp),%rax
cmp    -0x48(%rbp),%rax
jne    1328 <main+0x198>
lea    0xcfc(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1336 <main+0x1a6>
lea    0xd04(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %edi,%edi
call   10a0 <time@plt>
mov    %eax,%edi
call   1080 <srand@plt>
mov    $0x10000000,%edi
call   10b0 <malloc@plt>
mov    %rax,%rbp
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   10c0 <rand@plt>
and    $0x19,%al
add    $0x61,%al
mov    %al,0x0(%rbp,%rbx,1)
add    $0x1,%rbx
cmp    $0x10000000,%rbx
jne    11d0 <main+0x30>
xor    %ebx,%ebx
jmp    11fd <main+0x5d>
add    $0x1,%rbx
cmp    $0x10000000,%rbx
je     120f <main+0x6f>
xor    %eax,%eax
call   10c0 <rand@plt>
test   $0x3,%al
jne    11f0 <main+0x50>
movb   $0x20,0x0(%rbp,%rbx,1)
jmp    11f0 <main+0x50>
xor    %eax,%eax
call   1060 <clock@plt>
movsd  %xmm0,(%rsp)
mov    %rbp,%rdi
mov    $0x10000000,%esi
call   1040 <cntbb@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1060 <clock@plt>
subsd  (%rsp),%xmm0
mov    0x2daa(%rip),%r13
cvtsi2sdq 0x0(%r13),%xmm1
divsd  %xmm1,%xmm0
lea    0xdb1(%rip),%r15
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x1,%al
call   1070 <printf@plt>
xor    %eax,%eax
call   1060 <clock@plt>
movsd  %xmm0,(%rsp)
mov    %rbp,%rdi
mov    $0x10000000,%esi
call   1090 <cntbr@plt>
mov    %rax,%r12
xor    %eax,%eax
call   1060 <clock@plt>
subsd  (%rsp),%xmm0
xorps  %xmm1,%xmm1
cvtsi2sdq 0x0(%r13),%xmm1
divsd  %xmm1,%xmm0
mov    %r15,%rdi
mov    %r12,%rsi
mov    $0x1,%al
call   1070 <printf@plt>
cmp    %r12,%r14
lea    0xd91(%rip),%rax
lea    0xd69(%rip),%rdi
cmove  %rax,%rdi
call   1050 <puts@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@CLOCKS_PER_SEC = global i64 0
@.str = [24 x i8] c"Words: %ld (time: %lf)\0A\00"
@.str.1 = [27 x i8] c"Test OK: nubers are equal\0A\00"
@.str.2 = [34 x i8] c"Test FAIL: numbers are not equal\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
%5 = alloca double
%6 = alloca i64
%7 = alloca double
%8 = alloca double
%9 = alloca i64
%10 = alloca double
store i32 0, i32* %1
%11 = call i32 @time(i32 0)
%12 = call i32 @srand(i32 %11)
%13 = call i8* @malloc(i32 268435456)
store i8* %13, i8** %2
store i64 0, i64* %3
br label %14
14: ; preds = %25, %0
%15 = load i64, i64* %3
%16 = icmp ult i64 %15, 268435456
br i1 %16, label %17, label %28
17: ; preds = %14
%18 = call i32 (...) @rand()
%19 = and i32 %18, 25
%20 = add nsw i32 97, %19
%21 = trunc i32 %20 to i8
%22 = load i8*, i8** %2
%23 = load i64, i64* %3
%24 = getelementptr inbounds i8, i8* %22, i64 %23
store i8 %21, i8* %24
br label %25
25: ; preds = %17
%26 = load i64, i64* %3
%27 = add i64 %26, 1
store i64 %27, i64* %3
br label %14
28: ; preds = %14
store i64 0, i64* %4
br label %29
29: ; preds = %41, %28
%30 = load i64, i64* %4
%31 = icmp ult i64 %30, 268435456
br i1 %31, label %32, label %44
32: ; preds = %29
%33 = call i32 (...) @rand()
%34 = and i32 %33, 3
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %32
%37 = load i8*, i8** %2
%38 = load i64, i64* %4
%39 = getelementptr inbounds i8, i8* %37, i64 %38
store i8 32, i8* %39
br label %40
40: ; preds = %36, %32
br label %41
41: ; preds = %40
%42 = load i64, i64* %4
%43 = add i64 %42, 1
store i64 %43, i64* %4
br label %29
44: ; preds = %29
%45 = call double (...) @clock()
store double %45, double* %5
%46 = load i8*, i8** %2
%47 = call i64 @cntbb(i8* %46, i32 268435456)
store i64 %47, i64* %6
%48 = call double (...) @clock()
store double %48, double* %7
%49 = load double, double* %7
%50 = load double, double* %5
%51 = fsub double %49, %50
%52 = load i64, i64* @CLOCKS_PER_SEC
%53 = sitofp i64 %52 to double
%54 = fdiv double %51, %53
store double %54, double* %8
%55 = load i64, i64* %6
%56 = load double, double* %8
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i64 %55, double %56)
%58 = call double (...) @clock()
store double %58, double* %5
%59 = load i8*, i8** %2
%60 = call i64 @cntbr(i8* %59, i32 268435456)
store i64 %60, i64* %9
%61 = call double (...) @clock()
store double %61, double* %7
%62 = load double, double* %7
%63 = load double, double* %5
%64 = fsub double %62, %63
%65 = load i64, i64* @CLOCKS_PER_SEC
%66 = sitofp i64 %65 to double
%67 = fdiv double %64, %66
store double %67, double* %10
%68 = load i64, i64* %9
%69 = load double, double* %10
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i64 %68, double %69)
%71 = load i64, i64* %6
%72 = load i64, i64* %9
%73 = icmp eq i64 %71, %72
br i1 %73, label %74, label %76
74: ; preds = %44
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %78
76: ; preds = %44
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %78
78: ; preds = %76, %74
%79 = load i8*, i8** %2
%80 = call i32 @free(i8* %79)
ret i32 0
}

LLVM IR (-O2):
@CLOCKS_PER_SEC = global i64 0
@.str = [24 x i8] c"Words: %ld (time: %lf)\0A\00"
@str = [33 x i8] c"Test FAIL: numbers are not equal\00"
@str.3 = [26 x i8] c"Test OK: nubers are equal\00"
define i32 @main() {
%1 = tail call i32 @time(i32 0)
%2 = tail call i32 @srand(i32 %1)
%3 = tail call dereferenceable_or_null(268435456) i8* @malloc(i32 268435456)
br label %4
4: ; preds = %0, %4
%5 = phi i64 [ 0, %0 ], [ %11, %4 ]
%6 = tail call i32 (...) @rand()
%7 = trunc i32 %6 to i8
%8 = and i8 %7, 25
%9 = add nuw nsw i8 %8, 97
%10 = getelementptr inbounds i8, i8* %3, i64 %5
store i8 %9, i8* %10
%11 = add nuw nsw i64 %5, 1
%12 = icmp eq i64 %11, 268435456
br i1 %12, label %34, label %4
13: ; preds = %41
%14 = tail call double (...) @clock()
%15 = tail call i64 @cntbb(i8* %3, i32 268435456)
%16 = tail call double (...) @clock()
%17 = fsub double %16, %14
%18 = load i64, i64* @CLOCKS_PER_SEC
%19 = sitofp i64 %18 to double
%20 = fdiv double %17, %19
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i64 %15, double %20)
%22 = tail call double (...) @clock()
%23 = tail call i64 @cntbr(i8* %3, i32 268435456)
%24 = tail call double (...) @clock()
%25 = fsub double %24, %22
%26 = load i64, i64* @CLOCKS_PER_SEC
%27 = sitofp i64 %26 to double
%28 = fdiv double %25, %27
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i64 %23, double %28)
%30 = icmp eq i64 %15, %23
%31 = select i1 %30, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @str.3, i64 0, i64 0), i8* getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0)
%32 = tail call i32 @puts(i8* dereferenceable(1) %31)
%33 = tail call i32 @free(i8* %3)
ret i32 0
34: ; preds = %4, %41
%35 = phi i64 [ %42, %41 ], [ 0, %4 ]
%36 = tail call i32 (...) @rand()
%37 = and i32 %36, 3
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = getelementptr inbounds i8, i8* %3, i64 %35
store i8 32, i8* %40
br label %41
41: ; preds = %34, %39
%42 = add nuw nsw i64 %35, 1
%43 = icmp eq i64 %42, 268435456
br i1 %43, label %13, label %34
}