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

/* Variables and functions */
int strlen(char const *);
int /*<<< orphan*/ strncmp(char const *, char *, int);
int _ds_compute_sparse(const char *token) {
  int sparse = 0, i;
  if (!strncmp(token, "#+", 2))
    sparse++;
  if (strlen(token) >= 2 && !strncmp((token + strlen(token)) - 2, "+#", 2))
    sparse++;
  for (i = 0; token[i]; i++) {
    if (!strncmp(token + i, "+#+", 3))
      sparse++;
  }
  return sparse;
}


x86 assembly (clang, -O0):
<_ds_compute_sparse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xec6(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    115a <_ds_compute_sparse+0x3a>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x2,%eax
jl     11ad <_ds_compute_sparse+0x8d>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x18(%rbp),%rdi
cltq
add    %rax,%rdi
add    $0xfffffffffffffffe,%rdi
lea    0xe6f(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    11ad <_ds_compute_sparse+0x8d>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     1207 <_ds_compute_sparse+0xe7>
mov    -0x8(%rbp),%rdi
movslq -0x10(%rbp),%rax
add    %rax,%rdi
lea    0xe2b(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    11f4 <_ds_compute_sparse+0xd4>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11f9 <_ds_compute_sparse+0xd9>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11b4 <_ds_compute_sparse+0x94>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_ds_compute_sparse>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
lea    0xed6(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
mov    %eax,%r14d
xor    %ebp,%ebp
test   %eax,%eax
sete   %bpl
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    $0x2,%eax
jl     1184 <_ds_compute_sparse+0x64>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
lea    (%rbx,%rax,1),%rdi
add    $0xfffffffffffffffe,%rdi
lea    0xe97(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
cmp    $0x1,%r14d
mov    $0x1,%ecx
adc    $0x0,%ecx
test   %eax,%eax
cmove  %ecx,%ebp
cmpb   $0x0,(%rbx)
je     11b0 <_ds_compute_sparse+0x90>
lea    0xe73(%rip),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
cmp    $0x1,%eax
adc    $0x0,%ebp
cmpb   $0x0,0x1(%rbx)
lea    0x1(%rbx),%rbx
jne    1190 <_ds_compute_sparse+0x70>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"#+\00"
@.str.1 = [3 x i8] c"+#\00"
@.str.2 = [4 x i8] c"+#+\00"
define i32 @_ds_compute_sparse(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %3
%5 = load i8*, i8** %2
%6 = call i32 @strncmp(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%7 = icmp ne i32 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %1
%9 = load i32, i32* %3
%10 = add nsw i32 %9, 1
store i32 %10, i32* %3
br label %11
11: ; preds = %8, %1
%12 = load i8*, i8** %2
%13 = call i32 @strlen(i8* %12)
%14 = icmp sge i32 %13, 2
br i1 %14, label %15, label %27
15: ; preds = %11
%16 = load i8*, i8** %2
%17 = load i8*, i8** %2
%18 = call i32 @strlen(i8* %17)
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %16, i64 %19
%21 = getelementptr inbounds i8, i8* %20, i64 -2
%22 = call i32 @strncmp(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%23 = icmp ne i32 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %15
%25 = load i32, i32* %3
%26 = add nsw i32 %25, 1
store i32 %26, i32* %3
br label %27
27: ; preds = %24, %15, %11
store i32 0, i32* %4
br label %28
28: ; preds = %46, %27
%29 = load i8*, i8** %2
%30 = load i32, i32* %4
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = load i8, i8* %32
%34 = icmp ne i8 %33, 0
br i1 %34, label %35, label %49
35: ; preds = %28
%36 = load i8*, i8** %2
%37 = load i32, i32* %4
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = call i32 @strncmp(i8* %39, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%41 = icmp ne i32 %40, 0
br i1 %41, label %45, label %42
42: ; preds = %35
%43 = load i32, i32* %3
%44 = add nsw i32 %43, 1
store i32 %44, i32* %3
br label %45
45: ; preds = %42, %35
br label %46
46: ; preds = %45
%47 = load i32, i32* %4
%48 = add nsw i32 %47, 1
store i32 %48, i32* %4
br label %28
49: ; preds = %28
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
@.str = [3 x i8] c"#+\00"
@.str.1 = [3 x i8] c"+#\00"
@.str.2 = [4 x i8] c"+#+\00"
define i32 @_ds_compute_sparse(i8* %0) {
%2 = tail call i32 @strncmp(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%3 = icmp eq i32 %2, 0
%4 = zext i1 %3 to i32
%5 = tail call i32 @strlen(i8* %0)
%6 = icmp sgt i32 %5, 1
br i1 %6, label %7, label %16
7: ; preds = %1
%8 = tail call i32 @strlen(i8* %0)
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = getelementptr inbounds i8, i8* %10, i64 -2
%12 = tail call i32 @strncmp(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%13 = icmp eq i32 %12, 0
%14 = select i1 %3, i32 2, i32 1
%15 = select i1 %13, i32 %14, i32 %4
br label %16
16: ; preds = %7, %1
%17 = phi i32 [ %4, %1 ], [ %15, %7 ]
%18 = load i8, i8* %0
%19 = icmp eq i8 %18, 0
br i1 %19, label %32, label %20
20: ; preds = %16, %20
%21 = phi i64 [ %28, %20 ], [ 0, %16 ]
%22 = phi i8* [ %29, %20 ], [ %0, %16 ]
%23 = phi i32 [ %27, %20 ], [ %17, %16 ]
%24 = tail call i32 @strncmp(i8* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%25 = icmp eq i32 %24, 0
%26 = zext i1 %25 to i32
%27 = add nsw i32 %23, %26
%28 = add nuw i64 %21, 1
%29 = getelementptr inbounds i8, i8* %0, i64 %28
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 0
br i1 %31, label %32, label %20
32: ; preds = %20, %16
%33 = phi i32 [ %17, %16 ], [ %27, %20 ]
ret i32 %33
}