#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 *deblank(char *);
char *malloc(int);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *undertaker(const char *str, char source, char dest) {
  int length = strlen(str) + 1;
  char *word = malloc(length);
  strcpy(word, str);
  word[length] = '\0';
  unsigned int i;
  for (i = 0; word[i] != '\0' && i < length; i++) {
    if (word[i] == source) {
      word[i] = dest;
    }
  }
  return deblank(word);
}


x86 assembly (clang, -O0):
<undertaker>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dl,%al
mov    %sil,%cl
mov    %rdi,-0x8(%rbp)
mov    %cl,-0x9(%rbp)
mov    %al,-0xa(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
movsbl (%rax,%rcx,1),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x1d(%rbp)
je     11b7 <undertaker+0x77>
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
setb   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    11c7 <undertaker+0x87>
jmp    11fe <undertaker+0xbe>
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
movsbl (%rax,%rcx,1),%eax
movsbl -0x9(%rbp),%ecx
cmp    %ecx,%eax
jne    11eb <undertaker+0xab>
mov    -0xa(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
jmp    11f0 <undertaker+0xb0>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1192 <undertaker+0x52>
mov    -0x18(%rbp),%rdi
call   1060 <deblank@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<undertaker>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%r12
call   1040 <strlen@plt>
movslq %eax,%r13
lea    0x1(%r13),%r15
mov    %r15d,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
movb   $0x0,0x1(%rbx,%r13,1)
mov    (%rbx),%al
test   %al,%al
je     11ae <undertaker+0x6e>
test   %r15d,%r15d
je     11ae <undertaker+0x6e>
mov    %r15d,%ecx
mov    $0x1,%edx
xchg   %ax,%ax
cmp    %bpl,%al
jne    119a <undertaker+0x5a>
mov    %r14b,-0x1(%rbx,%rdx,1)
movzbl (%rbx,%rdx,1),%eax
test   %al,%al
je     11ae <undertaker+0x6e>
lea    0x1(%rdx),%rsi
cmp    %rcx,%rdx
mov    %rsi,%rdx
jb     1190 <undertaker+0x50>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <deblank@plt>

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

LLVM IR (-O2):
define i8* @undertaker(i8* %0, i8 signext %1, i8 signext %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = add nsw i32 %4, 1
%6 = tail call i8* @malloc(i32 %5)
%7 = tail call i32 @strcpy(i8* %6, i8* %0)
%8 = sext i32 %5 to i64
%9 = getelementptr inbounds i8, i8* %6, i64 %8
store i8 0, i8* %9
%10 = load i8, i8* %6
%11 = icmp ne i8 %10, 0
%12 = icmp ne i32 %5, 0
%13 = and i1 %11, %12
br i1 %13, label %14, label %29
14: ; preds = %3
%15 = zext i32 %5 to i64
br label %16
16: ; preds = %14, %22
%17 = phi i64 [ 0, %14 ], [ %23, %22 ]
%18 = phi i8 [ %10, %14 ], [ %25, %22 ]
%19 = phi i8* [ %6, %14 ], [ %24, %22 ]
%20 = icmp eq i8 %18, %1
br i1 %20, label %21, label %22
21: ; preds = %16
store i8 %2, i8* %19
br label %22
22: ; preds = %16, %21
%23 = add nuw nsw i64 %17, 1
%24 = getelementptr inbounds i8, i8* %6, i64 %23
%25 = load i8, i8* %24
%26 = icmp ne i8 %25, 0
%27 = icmp ult i64 %23, %15
%28 = select i1 %26, i1 %27, i1 false
br i1 %28, label %16, label %29
29: ; preds = %22, %3
%30 = tail call i8* @deblank(i8* %6)
ret i8* %30
}