#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++;
      i++;
    }
  }
  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     1210 <_ds_compute_sparse+0xf0>
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    11fd <_ds_compute_sparse+0xdd>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1202 <_ds_compute_sparse+0xe2>
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   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
lea    0xed3(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
mov    %eax,%r14d
xor    %ebp,%ebp
test   %eax,%eax
sete   %bpl
mov    %r15,%rdi
call   1040 <strlen@plt>
cmp    $0x2,%eax
jl     1187 <_ds_compute_sparse+0x67>
mov    %r15,%rdi
call   1040 <strlen@plt>
cltq
lea    (%r15,%rax,1),%rdi
add    $0xfffffffffffffffe,%rdi
lea    0xe94(%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,(%r15)
je     11c9 <_ds_compute_sparse+0xa9>
xor    %ebx,%ebx
lea    0xe6d(%rip),%r14
mov    %r15,%rdi
nopl   0x0(%rax)
mov    %r14,%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
xor    %ecx,%ecx
test   %eax,%eax
sete   %cl
add    %ecx,%ebp
add    %ecx,%ebx
add    $0x1,%ebx
movslq %ebx,%rax
lea    (%r15,%rax,1),%rdi
cmpb   $0x0,(%r15,%rax,1)
jne    11a0 <_ds_compute_sparse+0x80>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
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 = %48, %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 %51
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 %47, label %42
42: ; preds = %35
%43 = load i32, i32* %3
%44 = add nsw i32 %43, 1
store i32 %44, i32* %3
%45 = load i32, i32* %4
%46 = add nsw i32 %45, 1
store i32 %46, i32* %4
br label %47
47: ; preds = %42, %35
br label %48
48: ; preds = %47
%49 = load i32, i32* %4
%50 = add nsw i32 %49, 1
store i32 %50, i32* %4
br label %28
51: ; preds = %28
%52 = load i32, i32* %3
ret i32 %52
}

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 %34, label %20
20: ; preds = %16, %20
%21 = phi i8* [ %31, %20 ], [ %0, %16 ]
%22 = phi i32 [ %29, %20 ], [ 0, %16 ]
%23 = phi i32 [ %27, %20 ], [ %17, %16 ]
%24 = tail call i32 @strncmp(i8* %21, 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 i32 %22, 1
%29 = add i32 %28, %26
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %0, i64 %30
%32 = load i8, i8* %31
%33 = icmp eq i8 %32, 0
br i1 %33, label %34, label %20
34: ; preds = %20, %16
%35 = phi i32 [ %17, %16 ], [ %27, %20 ]
ret i32 %35
}