#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 /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int, ...);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strlen(char *);
int __mb_bits_to_string(char *bits, int nbits, char *ret) {
  int i;
  int start_idx, end_idx;
  char buf[1024];
  start_idx = end_idx = -1;
  bzero(buf, sizeof(buf));
  for (i = 0; i < nbits; i++) {
    int bits_idx, bits_ofst;
    bits_idx = i / (sizeof(char) * 8);
    bits_ofst = i - (sizeof(char) * 8) * bits_idx;
    if (start_idx < 0) {
      if ((bits[bits_idx] & (1 << bits_ofst)) > 0) {
        start_idx = i;
      }
    } else {
      if ((bits[bits_idx] & (1 << bits_ofst)) > 0) {
      } else {
        end_idx = i - 1;
        if (strlen(ret) > 0)
          strcat(ret, ",");
        if (start_idx == end_idx) {
          sprintf(buf, "%d", start_idx);
        } else {
          sprintf(buf, "%d-%d", start_idx, end_idx);
        }
        strcat(ret, buf);
        start_idx = -1;
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<__mb_bits_to_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0xffffffff,-0x24(%rbp)
movl   $0xffffffff,-0x20(%rbp)
lea    -0x430(%rbp),%rdi
mov    $0x400,%esi
call   1040 <bzero@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    12b9 <__mb_bits_to_string+0x179>
movslq -0x1c(%rbp),%rax
shr    $0x3,%rax
mov    %eax,-0x434(%rbp)
movslq -0x1c(%rbp),%rax
movslq -0x434(%rbp),%rcx
shl    $0x3,%rcx
sub    %rcx,%rax
mov    %eax,-0x438(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11ec <__mb_bits_to_string+0xac>
mov    -0x8(%rbp),%rax
movslq -0x434(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    -0x438(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
jle    11e7 <__mb_bits_to_string+0xa7>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    12a6 <__mb_bits_to_string+0x166>
mov    -0x8(%rbp),%rax
movslq -0x434(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    -0x438(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
jle    121a <__mb_bits_to_string+0xda>
jmp    12a1 <__mb_bits_to_string+0x161>
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%rax
jle    1246 <__mb_bits_to_string+0x106>
mov    -0x18(%rbp),%rdi
lea    0xdbf(%rip),%rsi
call   1050 <strcat@plt>
mov    -0x20(%rbp),%eax
cmp    -0x24(%rbp),%eax
jne    126f <__mb_bits_to_string+0x12f>
lea    -0x430(%rbp),%rdi
mov    -0x20(%rbp),%edx
lea    0xda2(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
jmp    128a <__mb_bits_to_string+0x14a>
lea    -0x430(%rbp),%rdi
mov    -0x20(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    0xd7f(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
mov    -0x18(%rbp),%rdi
lea    -0x430(%rbp),%rsi
call   1050 <strcat@plt>
movl   $0xffffffff,-0x20(%rbp)
jmp    12a6 <__mb_bits_to_string+0x166>
jmp    12ab <__mb_bits_to_string+0x16b>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    117c <__mb_bits_to_string+0x3c>
xor    %eax,%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__mb_bits_to_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdx,%r15
mov    %esi,%ebx
mov    %rdi,%rbp
lea    0x10(%rsp),%rdi
mov    $0x400,%esi
call   1040 <bzero@plt>
test   %ebx,%ebx
jle    123c <__mb_bits_to_string+0xfc>
mov    $0xffffffff,%r12d
lea    0x10(%rsp),%r14
xor    %r13d,%r13d
mov    %ebx,0xc(%rsp)
jmp    11a3 <__mb_bits_to_string+0x63>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
test   %eax,%eax
cmovg  %r13d,%r12d
add    $0x1,%r13d
cmp    %r13d,%ebx
je     123c <__mb_bits_to_string+0xfc>
mov    %r13d,%edx
mov    %r13d,%ecx
and    $0x7,%cl
mov    $0x1,%eax
shl    %cl,%eax
shr    $0x3,%rdx
movsbl 0x0(%rbp,%rdx,1),%ecx
and    %ecx,%eax
test   %r12d,%r12d
js     1190 <__mb_bits_to_string+0x50>
test   %eax,%eax
jg     1196 <__mb_bits_to_string+0x56>
mov    %r14,%rbx
lea    -0x1(%r13),%r14d
mov    %r15,%rdi
call   1030 <strlen@plt>
test   %rax,%rax
jle    11ea <__mb_bits_to_string+0xaa>
mov    %r15,%rdi
lea    0xe1b(%rip),%rsi
call   1050 <strcat@plt>
cmp    %r12d,%r14d
jne    1208 <__mb_bits_to_string+0xc8>
mov    %rbx,%r14
mov    %rbx,%rdi
lea    0xe09(%rip),%rsi
mov    %r12d,%edx
xor    %eax,%eax
call   1060 <sprintf@plt>
jmp    1222 <__mb_bits_to_string+0xe2>
mov    %rbx,%rdi
lea    0xdf0(%rip),%rsi
mov    %r12d,%edx
mov    %r14d,%ecx
mov    %rbx,%r14
xor    %eax,%eax
call   1060 <sprintf@plt>
mov    0xc(%rsp),%ebx
mov    %r15,%rdi
mov    %r14,%rsi
call   1050 <strcat@plt>
mov    $0xffffffff,%r12d
jmp    1196 <__mb_bits_to_string+0x56>
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c",\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [6 x i8] c"%d-%d\00"
define i32 @__mb_bits_to_string(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [1024 x i8]
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
store i32 -1, i32* %9
store i32 -1, i32* %8
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%14 = call i32 @bzero(i8* %13, i32 1024)
store i32 0, i32* %7
br label %15
15: ; preds = %87, %3
%16 = load i32, i32* %7
%17 = load i32, i32* %5
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %90
19: ; preds = %15
%20 = load i32, i32* %7
%21 = sext i32 %20 to i64
%22 = udiv i64 %21, 8
%23 = trunc i64 %22 to i32
store i32 %23, i32* %11
%24 = load i32, i32* %7
%25 = sext i32 %24 to i64
%26 = load i32, i32* %11
%27 = sext i32 %26 to i64
%28 = mul i64 8, %27
%29 = sub i64 %25, %28
%30 = trunc i64 %29 to i32
store i32 %30, i32* %12
%31 = load i32, i32* %8
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %47
33: ; preds = %19
%34 = load i8*, i8** %4
%35 = load i32, i32* %11
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = load i32, i32* %12
%41 = shl i32 1, %40
%42 = and i32 %39, %41
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %33
%45 = load i32, i32* %7
store i32 %45, i32* %8
br label %46
46: ; preds = %44, %33
br label %86
47: ; preds = %19
%48 = load i8*, i8** %4
%49 = load i32, i32* %11
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = load i32, i32* %12
%55 = shl i32 1, %54
%56 = and i32 %53, %55
%57 = icmp sgt i32 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %47
br label %85
59: ; preds = %47
%60 = load i32, i32* %7
%61 = sub nsw i32 %60, 1
store i32 %61, i32* %9
%62 = load i8*, i8** %6
%63 = call i64 @strlen(i8* %62)
%64 = icmp sgt i64 %63, 0
br i1 %64, label %65, label %68
65: ; preds = %59
%66 = load i8*, i8** %6
%67 = call i32 @strcat(i8* %66, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %68
68: ; preds = %65, %59
%69 = load i32, i32* %8
%70 = load i32, i32* %9
%71 = icmp eq i32 %69, %70
br i1 %71, label %72, label %76
72: ; preds = %68
%73 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%74 = load i32, i32* %8
%75 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %73, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %74)
br label %81
76: ; preds = %68
%77 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%78 = load i32, i32* %8
%79 = load i32, i32* %9
%80 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %77, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %78, i32 %79)
br label %81
81: ; preds = %76, %72
%82 = load i8*, i8** %6
%83 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%84 = call i32 @strcat(i8* %82, i8* %83)
store i32 -1, i32* %8
br label %85
85: ; preds = %81, %58
br label %86
86: ; preds = %85, %46
br label %87
87: ; preds = %86
%88 = load i32, i32* %7
%89 = add nsw i32 %88, 1
store i32 %89, i32* %7
br label %15
90: ; preds = %15
ret i32 0
}

LLVM IR (-O2):
@.str = [2 x i8] c",\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [6 x i8] c"%d-%d\00"
define i32 @__mb_bits_to_string(i8* nocapture %0, i32 %1, i8* %2) {
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = call i32 @bzero(i8* %5, i32 1024)
%7 = icmp sgt i32 %1, 0
br i1 %7, label %8, label %43
8: ; preds = %3, %39
%9 = phi i32 [ %41, %39 ], [ 0, %3 ]
%10 = phi i32 [ %40, %39 ], [ -1, %3 ]
%11 = lshr i32 %9, 3
%12 = zext i32 %11 to i64
%13 = mul nsw i32 %11, -8
%14 = add i32 %13, %9
%15 = icmp slt i32 %10, 0
%16 = getelementptr inbounds i8, i8* %0, i64 %12
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = shl nuw i32 1, %14
%20 = and i32 %19, %18
%21 = icmp sgt i32 %20, 0
br i1 %15, label %22, label %24
22: ; preds = %8
%23 = select i1 %21, i32 %9, i32 %10
br label %39
24: ; preds = %8
br i1 %21, label %39, label %25
25: ; preds = %24
%26 = add nsw i32 %9, -1
%27 = call i64 @strlen(i8* dereferenceable(1) %2)
%28 = icmp sgt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @strcat(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %31
31: ; preds = %29, %25
%32 = icmp eq i32 %10, %26
br i1 %32, label %33, label %35
33: ; preds = %31
%34 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %10)
br label %37
35: ; preds = %31
%36 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %10, i32 %26)
br label %37
37: ; preds = %35, %33
%38 = call i32 @strcat(i8* %2, i8* %5)
br label %39
39: ; preds = %22, %37, %24
%40 = phi i32 [ %10, %24 ], [ -1, %37 ], [ %23, %22 ]
%41 = add nuw nsw i32 %9, 1
%42 = icmp eq i32 %41, %1
br i1 %42, label %43, label %8
43: ; preds = %39, %3
ret i32 0
}