#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 /*<<< orphan*/ printf(char *, char const, int);
void encode(const char *msg, size_t len) {
  int run = 1;
  int index = 1;
  while (index < len) {
    if (msg[index] == msg[index - 1]) {
      run++;
    } else {
      printf("%c%d", msg[index - 1], run);
      run = 1;
    }
    index++;
  }
  printf("%c%d", msg[len - 1], run);
}


x86 assembly (clang, -O0):
<encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
movslq -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11a6 <encode+0x96>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    -0x8(%rbp),%rcx
mov    -0x18(%rbp),%edx
sub    $0x1,%edx
movslq %edx,%rdx
movsbl (%rcx,%rdx,1),%ecx
cmp    %ecx,%eax
jne    116f <encode+0x5f>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1198 <encode+0x88>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    -0x14(%rbp),%edx
lea    0xe77(%rip),%rdi
movsbl %al,%esi
call   1030 <printf@plt>
movl   $0x1,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    112e <encode+0x1e>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
mov    (%rax,%rcx,1),%al
mov    -0x14(%rbp),%edx
lea    0xe41(%rip),%rdi
movsbl %al,%esi
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encode>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
mov    $0x1,%edx
cmp    $0x2,%rsi
jb     116a <encode+0x5a>
mov    $0x1,%edx
mov    $0x1,%ebx
lea    0xec6(%rip),%r15
jmp    1159 <encode+0x49>
nopl   0x0(%rax)
movsbl %al,%esi
mov    %r15,%rdi
call   1030 <printf@plt>
mov    $0x1,%edx
add    $0x1,%rbx
cmp    %rbx,%r14
je     116a <encode+0x5a>
movzbl -0x1(%r12,%rbx,1),%eax
cmp    %al,(%r12,%rbx,1)
jne    1140 <encode+0x30>
add    $0x1,%edx
jmp    1150 <encode+0x40>
movsbl -0x1(%r14,%r12,1),%esi
lea    0xe89(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <printf@plt>

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

LLVM IR (-O2):
@.str = [5 x i8] c"%c%d\00"
define void @encode(i8* nocapture %0, i64 %1) {
%3 = icmp ugt i64 %1, 1
br i1 %3, label %4, label %21
4: ; preds = %2, %17
%5 = phi i64 [ %19, %17 ], [ 1, %2 ]
%6 = phi i32 [ %18, %17 ], [ 1, %2 ]
%7 = getelementptr inbounds i8, i8* %0, i64 %5
%8 = load i8, i8* %7
%9 = add nsw i64 %5, -1
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = load i8, i8* %10
%12 = icmp eq i8 %8, %11
br i1 %12, label %13, label %15
13: ; preds = %4
%14 = add nsw i32 %6, 1
br label %17
15: ; preds = %4
%16 = tail call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 signext %11, i32 %6)
br label %17
17: ; preds = %15, %13
%18 = phi i32 [ %14, %13 ], [ 1, %15 ]
%19 = add nuw i64 %5, 1
%20 = icmp eq i64 %19, %1
br i1 %20, label %21, label %4
21: ; preds = %17, %2
%22 = phi i32 [ 1, %2 ], [ %18, %17 ]
%23 = add i64 %1, -1
%24 = getelementptr inbounds i8, i8* %0, i64 %23
%25 = load i8, i8* %24
%26 = tail call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 signext %25, i32 %22)
ret void
}