#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 *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int strlen(char const *);
char *scram_escape(const char *str) {
  char *out = malloc(strlen(str) * 3 + 1);
  char *p = out;
  if (!out)
    return NULL;
  while (*str) {
    if (*str == ',') {
      memcpy(p, "=2C", 3);
      p += 3;
    } else if (*str == '=') {
      memcpy(p, "=3D", 3);
      p += 3;
    } else {
      *p = *str;
      p++;
    }
    str++;
  }
  *p = '\0';
  return out;
}


x86 assembly (clang, -O0):
<scram_escape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
imul   $0x3,%eax,%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1174 <scram_escape+0x44>
movq   $0x0,-0x8(%rbp)
jmp    122f <scram_escape+0xff>
jmp    1179 <scram_escape+0x49>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1220 <scram_escape+0xf0>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2c,%eax
jne    11bc <scram_escape+0x8c>
mov    -0x20(%rbp),%rdi
lea    0xe5f(%rip),%rsi
mov    $0x3,%edx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x20(%rbp)
jmp    120f <scram_escape+0xdf>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3d,%eax
jne    11f2 <scram_escape+0xc2>
mov    -0x20(%rbp),%rdi
lea    0xe2d(%rip),%rsi
mov    $0x3,%edx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x20(%rbp)
jmp    120a <scram_escape+0xda>
mov    -0x10(%rbp),%rax
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    120f <scram_escape+0xdf>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1179 <scram_escape+0x49>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scram_escape>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r13
call   1030 <strlen@plt>
lea    (%rax,%rax,2),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     11bb <scram_escape+0x8b>
lea    0xea5(%rip),%r15
lea    0xea2(%rip),%r12
mov    %r14,%rbx
jmp    118c <scram_escape+0x5c>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x3,%edx
call   1040 <memcpy@plt>
mov    $0x3,%eax
add    %rax,%rbx
add    $0x1,%r13
movzbl 0x0(%r13),%eax
cmp    $0x2c,%al
je     11b0 <scram_escape+0x80>
cmp    $0x3d,%al
je     1170 <scram_escape+0x40>
test   %al,%al
je     11b8 <scram_escape+0x88>
mov    %al,(%rbx)
mov    $0x1,%eax
jmp    1185 <scram_escape+0x55>
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r15,%rsi
jmp    1176 <scram_escape+0x46>
movb   $0x0,(%rbx)
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [4 x i8] c"=2C\00"
@.str.1 = [4 x i8] c"=3D\00"
define i8* @scram_escape(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = mul nsw i32 %2, 3
%4 = add nsw i32 %3, 1
%5 = tail call i8* @malloc(i32 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %21, label %7
7: ; preds = %1, %16
%8 = phi i8* [ %19, %16 ], [ %0, %1 ]
%9 = phi i8* [ %18, %16 ], [ %5, %1 ]
%10 = load i8, i8* %8
switch i8 %10, label %15 [
i8 0, label %20
i8 44, label %11
i8 61, label %13
]
11: ; preds = %7
%12 = tail call i32 @memcpy(i8* %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
br label %16
13: ; preds = %7
%14 = tail call i32 @memcpy(i8* %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
br label %16
15: ; preds = %7
store i8 %10, i8* %9
br label %16
16: ; preds = %13, %15, %11
%17 = phi i64 [ 3, %13 ], [ 1, %15 ], [ 3, %11 ]
%18 = getelementptr inbounds i8, i8* %9, i64 %17
%19 = getelementptr inbounds i8, i8* %8, i64 1
br label %7
20: ; preds = %7
store i8 0, i8* %9
br label %21
21: ; preds = %1, %20
ret i8* %5
}