#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 */
char *calloc(int, int);
int /*<<< orphan*/ sprintf(char *, char *, int, char);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
char *rle_encode(char *str) {
  int cnt = 0;
  char smb, *encode = calloc(strlen(str), sizeof(char)),
            *tmp = calloc(strlen(str), sizeof(char));
  if ((!encode) || (!tmp))
    return NULL;
  smb = str[0];
  for (int i = 0; i <= strlen(str); i++) {
    if (str[i] == smb)
      cnt++;
    else {
      sprintf(tmp, "%d%c", cnt, smb);
      strcat(encode, tmp);
      smb = str[i];
      cnt = 1;
    }
  }
  return encode;
}


x86 assembly (clang, -O0):
<rle_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     119b <rle_encode+0x5b>
cmpq   $0x0,-0x28(%rbp)
jne    11a8 <rle_encode+0x68>
movq   $0x0,-0x8(%rbp)
jmp    124e <rle_encode+0x10e>
mov    -0x10(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x15(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
cmp    %ecx,%eax
jg     1246 <rle_encode+0x106>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
movsbl -0x15(%rbp),%ecx
cmp    %ecx,%eax
jne    11fa <rle_encode+0xba>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1233 <rle_encode+0xf3>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xdf8(%rip),%rsi
movsbl -0x15(%rbp),%ecx
call   1060 <sprintf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <strcat@plt>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x15(%rbp)
movl   $0x1,-0x14(%rbp)
jmp    1238 <rle_encode+0xf8>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11b8 <rle_encode+0x78>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rle_encode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r13
call   1030 <strlen@plt>
mov    %eax,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,%r14
mov    %r13,%rdi
call   1030 <strlen@plt>
mov    %eax,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,%r15
xor    %eax,%eax
test   %r14,%r14
je     11ed <rle_encode+0xad>
test   %r15,%r15
je     11ed <rle_encode+0xad>
mov    0x0(%r13),%r12b
mov    %r13,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
js     11ea <rle_encode+0xaa>
xor    %ebp,%ebp
mov    $0xffffffffffffffff,%rbx
jmp    11de <rle_encode+0x9e>
movsbl %r12b,%ecx
mov    %r15,%rdi
lea    0xe52(%rip),%rsi
mov    %ebp,%edx
call   1060 <sprintf@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1050 <strcat@plt>
movzbl 0x1(%r13,%rbx,1),%r12d
mov    $0x1,%ebp
mov    %r13,%rdi
call   1030 <strlen@plt>
cltq
add    $0x1,%rbx
cmp    %rax,%rbx
jge    11ea <rle_encode+0xaa>
cmp    %r12b,0x1(%r13,%rbx,1)
jne    11a0 <rle_encode+0x60>
add    $0x1,%ebp
jmp    11cb <rle_encode+0x8b>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%d%c\00"
define i8* @rle_encode(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %4
%9 = load i8*, i8** %3
%10 = call i32 @strlen(i8* %9)
%11 = call i8* @calloc(i32 %10, i32 1)
store i8* %11, i8** %6
%12 = load i8*, i8** %3
%13 = call i32 @strlen(i8* %12)
%14 = call i8* @calloc(i32 %13, i32 1)
store i8* %14, i8** %7
%15 = load i8*, i8** %6
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %1
%18 = load i8*, i8** %7
%19 = icmp ne i8* %18, null
br i1 %19, label %21, label %20
20: ; preds = %17, %1
store i8* null, i8** %2
br label %62
21: ; preds = %17
%22 = load i8*, i8** %3
%23 = getelementptr inbounds i8, i8* %22, i64 0
%24 = load i8, i8* %23
store i8 %24, i8* %5
store i32 0, i32* %8
br label %25
25: ; preds = %57, %21
%26 = load i32, i32* %8
%27 = load i8*, i8** %3
%28 = call i32 @strlen(i8* %27)
%29 = icmp sle i32 %26, %28
br i1 %29, label %30, label %60
30: ; preds = %25
%31 = load i8*, i8** %3
%32 = load i32, i32* %8
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = load i8, i8* %5
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %36, %38
br i1 %39, label %40, label %43
40: ; preds = %30
%41 = load i32, i32* %4
%42 = add nsw i32 %41, 1
store i32 %42, i32* %4
br label %56
43: ; preds = %30
%44 = load i8*, i8** %7
%45 = load i32, i32* %4
%46 = load i8, i8* %5
%47 = call i32 @sprintf(i8* %44, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %45, i8 signext %46)
%48 = load i8*, i8** %6
%49 = load i8*, i8** %7
%50 = call i32 @strcat(i8* %48, i8* %49)
%51 = load i8*, i8** %3
%52 = load i32, i32* %8
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
%55 = load i8, i8* %54
store i8 %55, i8* %5
store i32 1, i32* %4
br label %56
56: ; preds = %43, %40
br label %57
57: ; preds = %56
%58 = load i32, i32* %8
%59 = add nsw i32 %58, 1
store i32 %59, i32* %8
br label %25
60: ; preds = %25
%61 = load i8*, i8** %6
store i8* %61, i8** %2
br label %62
62: ; preds = %60, %20
%63 = load i8*, i8** %2
ret i8* %63
}

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