#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 compress_string(char *);
int matching_char_position(char *, char, int);
int strlen(char *);
int string_reduce(char *string) {
  char *iter = string;
  int i = 0, j = 0;
  int found = 1;
  while (found == 1) {
    found = 0;
    iter = string;
    i = 0;
    while (i < strlen(string)) {
      char c = iter[i];
      if (c != '$') {
        int j = matching_char_position(string, c, i + 1);
        if (j != -1) {
          iter[i] = iter[j] = '$';
          found = 1;
          i = j + 1;
        } else
          i++;
      } else
        i++;
    }
  }
  return compress_string(string);
}


x86 assembly (clang, -O0):
<string_reduce>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jne    1220 <string_reduce+0xf0>
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
cmp    %ecx,%eax
jge    121b <string_reduce+0xeb>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x1d(%rbp)
movsbl -0x1d(%rbp),%eax
cmp    $0x24,%eax
je     120d <string_reduce+0xdd>
mov    -0x8(%rbp),%rdi
mov    -0x1d(%rbp),%al
mov    -0x14(%rbp),%edx
add    $0x1,%edx
movsbl %al,%esi
call   1050 <matching_char_position@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
je     11ff <string_reduce+0xcf>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x24,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x24,(%rax,%rcx,1)
movl   $0x1,-0x1c(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1208 <string_reduce+0xd8>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1216 <string_reduce+0xe6>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1179 <string_reduce+0x49>
jmp    1159 <string_reduce+0x29>
mov    -0x8(%rbp),%rdi
call   1030 <compress_string@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_reduce>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
jmp    1146 <string_reduce+0x16>
nopl   0x0(%rax)
cmp    $0x1,%r14d
jne    11a7 <string_reduce+0x77>
mov    %r15,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    11a7 <string_reduce+0x77>
xor    %ebp,%ebp
xor    %r14d,%r14d
jmp    116c <string_reduce+0x3c>
nopl   0x0(%rax)
mov    %r15,%rdi
call   1040 <strlen@plt>
cmp    %eax,%ebp
jge    1140 <string_reduce+0x10>
movslq %ebp,%rbx
movzbl (%r15,%rbx,1),%eax
lea    0x1(%rbx),%ebp
cmp    $0x24,%al
je     1160 <string_reduce+0x30>
movsbl %al,%esi
mov    %r15,%rdi
mov    %ebp,%edx
call   1050 <matching_char_position@plt>
cmp    $0xffffffff,%eax
je     1160 <string_reduce+0x30>
movslq %eax,%rcx
movb   $0x24,(%r15,%rcx,1)
movb   $0x24,(%r15,%rbx,1)
add    $0x1,%eax
mov    $0x1,%r14d
mov    %eax,%ebp
jmp    1160 <string_reduce+0x30>
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <compress_string@plt>

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

LLVM IR (-O2):
define i32 @string_reduce(i8* %0) {
br label %4
2: ; preds = %22
%3 = icmp eq i32 %23, 1
br i1 %3, label %4, label %27
4: ; preds = %1, %2
%5 = tail call i32 @strlen(i8* %0)
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %27
7: ; preds = %4, %22
%8 = phi i32 [ %24, %22 ], [ 0, %4 ]
%9 = phi i32 [ %23, %22 ], [ 0, %4 ]
%10 = sext i32 %8 to i64
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 36
%14 = add nsw i32 %8, 1
br i1 %13, label %22, label %15
15: ; preds = %7
%16 = tail call i32 @matching_char_position(i8* %0, i8 signext %12, i32 %14)
%17 = icmp eq i32 %16, -1
br i1 %17, label %22, label %18
18: ; preds = %15
%19 = sext i32 %16 to i64
%20 = getelementptr inbounds i8, i8* %0, i64 %19
store i8 36, i8* %20
store i8 36, i8* %11
%21 = add nuw nsw i32 %16, 1
br label %22
22: ; preds = %7, %18, %15
%23 = phi i32 [ 1, %18 ], [ %9, %15 ], [ %9, %7 ]
%24 = phi i32 [ %21, %18 ], [ %14, %15 ], [ %14, %7 ]
%25 = tail call i32 @strlen(i8* %0)
%26 = icmp slt i32 %24, %25
br i1 %26, label %7, label %2
27: ; preds = %4, %2
%28 = tail call i32 @compress_string(i8* %0)
ret i32 %28
}