#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 sscanf(char *, char *, int *, char *);
int strlen(char *);
char *rle_decode(char *str) {
  char *newstr = calloc(2 * strlen(str), sizeof(char)), c, itmp[3];
  if (!newstr)
    return NULL;
  int i, j;
  while (sscanf(str, "%d%c", &i, &c) == 2) {
    sprintf(itmp, "%d", i);
    str += strlen(itmp) + 1;
    for (j = 0; j < i; j++)
      sprintf(newstr, "%s%c", newstr, c);
  }
  return newstr;
}


x86 assembly (clang, -O0):
<rle_decode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%edi
shl    $0x1,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1180 <rle_decode+0x40>
movq   $0x0,-0x8(%rbp)
jmp    121f <rle_decode+0xdf>
jmp    1185 <rle_decode+0x45>
mov    -0x10(%rbp),%rdi
lea    0xe70(%rip),%rsi
lea    -0x20(%rbp),%rdx
lea    -0x19(%rbp),%rcx
call   1050 <sscanf@plt>
cmp    $0x2,%eax
jne    1217 <rle_decode+0xd7>
lea    -0x1c(%rbp),%rdi
mov    -0x20(%rbp),%edx
lea    0xe51(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
lea    -0x1c(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
add    $0x1,%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1212 <rle_decode+0xd2>
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rdx
movsbl -0x19(%rbp),%ecx
lea    0xe0b(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11de <rle_decode+0x9e>
jmp    1185 <rle_decode+0x45>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rle_decode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
call   1030 <strlen@plt>
lea    (%rax,%rax,1),%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1220 <rle_decode+0xe0>
lea    0xe8a(%rip),%rsi
lea    0x10(%rsp),%rdx
lea    0xf(%rsp),%rcx
mov    %r14,%rdi
call   1050 <sscanf@plt>
cmp    $0x2,%eax
jne    1220 <rle_decode+0xe0>
lea    0x15(%rsp),%r12
lea    0xe6b(%rip),%r13
jmp    11c8 <rle_decode+0x88>
nop
movslq %r15d,%rax
add    %rax,%r14
add    $0x1,%r14
mov    %r14,%rdi
lea    0xe4c(%rip),%rsi
lea    0x10(%rsp),%rdx
lea    0xf(%rsp),%rcx
call   1050 <sscanf@plt>
cmp    $0x2,%eax
jne    1220 <rle_decode+0xe0>
mov    0x10(%rsp),%edx
mov    %r12,%rdi
lea    0xe2f(%rip),%rsi
xor    %eax,%eax
call   1060 <sprintf@plt>
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %eax,%r15d
cmpl   $0x0,0x10(%rsp)
jle    11a0 <rle_decode+0x60>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsbl 0xf(%rsp),%ecx
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <sprintf@plt>
add    $0x1,%ebp
cmp    0x10(%rsp),%ebp
jl     1200 <rle_decode+0xc0>
jmp    11a0 <rle_decode+0x60>
mov    %rbx,%rax
add    $0x18,%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"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [5 x i8] c"%s%c\00"
define i8* @rle_decode(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8
%6 = alloca [3 x i8]
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = call i32 @strlen(i8* %9)
%11 = mul nsw i32 2, %10
%12 = call i8* @calloc(i32 %11, i32 1)
store i8* %12, i8** %4
%13 = load i8*, i8** %4
%14 = icmp ne i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %1
store i8* null, i8** %2
br label %47
16: ; preds = %1
br label %17
17: ; preds = %44, %16
%18 = load i8*, i8** %3
%19 = call i32 @sscanf(i8* %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32* %7, i8* %5)
%20 = icmp eq i32 %19, 2
br i1 %20, label %21, label %45
21: ; preds = %17
%22 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 0
%23 = load i32, i32* %7
%24 = call i32 (i8*, i8*, ...) @sprintf(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 0
%26 = call i32 @strlen(i8* %25)
%27 = add nsw i32 %26, 1
%28 = load i8*, i8** %3
%29 = sext i32 %27 to i64
%30 = getelementptr inbounds i8, i8* %28, i64 %29
store i8* %30, i8** %3
store i32 0, i32* %8
br label %31
31: ; preds = %41, %21
%32 = load i32, i32* %8
%33 = load i32, i32* %7
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %44
35: ; preds = %31
%36 = load i8*, i8** %4
%37 = load i8*, i8** %4
%38 = load i8, i8* %5
%39 = sext i8 %38 to i32
%40 = call i32 (i8*, i8*, ...) @sprintf(i8* %36, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %37, i32 %39)
br label %41
41: ; preds = %35
%42 = load i32, i32* %8
%43 = add nsw i32 %42, 1
store i32 %43, i32* %8
br label %31
44: ; preds = %31
br label %17
45: ; preds = %17
%46 = load i8*, i8** %4
store i8* %46, i8** %2
br label %47
47: ; preds = %45, %15
%48 = load i8*, i8** %2
ret i8* %48
}

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