#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*/ cleanchr(char **, unsigned char const);
unsigned char *realloc(unsigned char *, int);
char *cleanstr(const unsigned char *s, int l) {
  static unsigned char *tmp = NULL;
  static int tmplen = 0;
  if (tmplen < l * 4 + 1)
    tmp = realloc(tmp, tmplen = l * 4 + 1);
  if (tmp == NULL) {
    tmplen = 0;
    return (char *)"(mem alloc error)";
  } else {
    int i = 0;
    char *p = tmp;
    while (i < l)
      cleanchr(&p, s[i++]);
    *p = '\0';
  }
  return tmp;
}


x86 assembly (clang, -O0):
<cleanstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2f0b(%rip),%eax
mov    -0x14(%rbp),%ecx
shl    $0x2,%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    1168 <cleanstr+0x48>
mov    0x2eeb(%rip),%rdi
mov    -0x14(%rbp),%esi
shl    $0x2,%esi
add    $0x1,%esi
mov    %esi,0x2ee4(%rip)
call   1030 <realloc@plt>
mov    %rax,0x2ed0(%rip)
cmpq   $0x0,0x2ec8(%rip)
jne    1190 <cleanstr+0x70>
movl   $0x0,0x2ec0(%rip)
lea    0xe79(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11e4 <cleanstr+0xc4>
movl   $0x0,-0x18(%rbp)
mov    0x2e9a(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11d2 <cleanstr+0xb2>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x18(%rbp)
movslq %ecx,%rcx
lea    -0x20(%rbp),%rdi
movzbl (%rax,%rcx,1),%esi
call   1040 <cleanchr@plt>
jmp    11a2 <cleanstr+0x82>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    0x2e58(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cleanstr>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
lea    0x1(,%r14,4),%esi
mov    0x2efb(%rip),%rax
cmp    %esi,0x2efd(%rip)
jge    115a <cleanstr+0x3a>
mov    %esi,0x2ef5(%rip)
mov    %rax,%rdi
call   1030 <realloc@plt>
mov    %rax,0x2ede(%rip)
test   %rax,%rax
je     1196 <cleanstr+0x76>
mov    %rax,(%rsp)
test   %r14d,%r14d
jle    118a <cleanstr+0x6a>
mov    %r14d,%r12d
xor    %ebx,%ebx
mov    %rsp,%r14
movzbl (%r15,%rbx,1),%esi
add    $0x1,%rbx
mov    %r14,%rdi
call   1040 <cleanchr@plt>
cmp    %rbx,%r12
jne    1170 <cleanstr+0x50>
mov    (%rsp),%rax
movb   $0x0,(%rax)
mov    0x2ea4(%rip),%rax
jmp    11a7 <cleanstr+0x87>
movl   $0x0,0x2ea0(%rip)
lea    0xe59(%rip),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@cleanstr.tmp = internal global i8* null
@cleanstr.tmplen = internal global i32 0
@.str = [18 x i8] c"(mem alloc error)\00"
define i8* @cleanstr(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i32, i32* @cleanstr.tmplen
%9 = load i32, i32* %5
%10 = mul nsw i32 %9, 4
%11 = add nsw i32 %10, 1
%12 = icmp slt i32 %8, %11
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i8*, i8** @cleanstr.tmp
%15 = load i32, i32* %5
%16 = mul nsw i32 %15, 4
%17 = add nsw i32 %16, 1
store i32 %17, i32* @cleanstr.tmplen
%18 = call i8* @realloc(i8* %14, i32 %17)
store i8* %18, i8** @cleanstr.tmp
br label %19
19: ; preds = %13, %2
%20 = load i8*, i8** @cleanstr.tmp
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %23
22: ; preds = %19
store i32 0, i32* @cleanstr.tmplen
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8** %3
br label %41
23: ; preds = %19
store i32 0, i32* %6
%24 = load i8*, i8** @cleanstr.tmp
store i8* %24, i8** %7
br label %25
25: ; preds = %29, %23
%26 = load i32, i32* %6
%27 = load i32, i32* %5
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %37
29: ; preds = %25
%30 = load i8*, i8** %4
%31 = load i32, i32* %6
%32 = add nsw i32 %31, 1
store i32 %32, i32* %6
%33 = sext i32 %31 to i64
%34 = getelementptr inbounds i8, i8* %30, i64 %33
%35 = load i8, i8* %34
%36 = call i32 @cleanchr(i8** %7, i8 zeroext %35)
br label %25
37: ; preds = %25
%38 = load i8*, i8** %7
store i8 0, i8* %38
br label %39
39: ; preds = %37
%40 = load i8*, i8** @cleanstr.tmp
store i8* %40, i8** %3
br label %41
41: ; preds = %39, %22
%42 = load i8*, i8** %3
ret i8* %42
}

LLVM IR (-O2):
@cleanstr.tmp = internal unnamed_addr global i8* null
@cleanstr.tmplen = internal unnamed_addr global i32 0
@.str = [18 x i8] c"(mem alloc error)\00"
define i8* @cleanstr(i8* nocapture %0, i32 %1) {
%3 = alloca i8*
%4 = load i32, i32* @cleanstr.tmplen
%5 = shl nsw i32 %1, 2
%6 = or i32 %5, 1
%7 = icmp slt i32 %4, %6
%8 = load i8*, i8** @cleanstr.tmp
br i1 %7, label %9, label %11
9: ; preds = %2
store i32 %6, i32* @cleanstr.tmplen
%10 = tail call i8* @realloc(i8* %8, i32 %6)
store i8* %10, i8** @cleanstr.tmp
br label %11
11: ; preds = %2, %9
%12 = phi i8* [ %10, %9 ], [ %8, %2 ]
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %11
store i32 0, i32* @cleanstr.tmplen
br label %32
15: ; preds = %11
%16 = bitcast i8** %3 to i8*
store i8* %12, i8** %3
%17 = icmp sgt i32 %1, 0
br i1 %17, label %18, label %29
18: ; preds = %15
%19 = zext i32 %1 to i64
br label %20
20: ; preds = %18, %20
%21 = phi i64 [ 0, %18 ], [ %22, %20 ]
%22 = add nuw nsw i64 %21, 1
%23 = getelementptr inbounds i8, i8* %0, i64 %21
%24 = load i8, i8* %23
%25 = call i32 @cleanchr(i8** %3, i8 zeroext %24)
%26 = icmp eq i64 %22, %19
br i1 %26, label %27, label %20
27: ; preds = %20
%28 = load i8*, i8** %3
br label %29
29: ; preds = %27, %15
%30 = phi i8* [ %28, %27 ], [ %12, %15 ]
store i8 0, i8* %30
%31 = load i8*, i8** @cleanstr.tmp
br label %32
32: ; preds = %29, %14
%33 = phi i8* [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %14 ], [ %31, %29 ]
ret i8* %33
}