#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 */
scalar_t__ strchr(char *, int /*<<< orphan*/);
scalar_t__ strlen(char *);
int /*<<< orphan*/ toupper(char);
char *xmalloc(scalar_t__);
char *m315_cleanse(char *istring) {
  char *rstring = xmalloc(strlen(istring) + 1);
  char *i, *o;
  static char m315_valid_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789";
  for (o = rstring, i = istring; *i; i++) {
    if (strchr(m315_valid_chars, toupper(*i))) {
      *o++ = toupper(*i);
    }
  }
  *o = 0;
  return rstring;
}


x86 assembly (clang, -O0):
<m315_cleanse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1060 <xmalloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     11db <m315_cleanse+0x9b>
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1030 <toupper@plt>
mov    %eax,%esi
lea    0x2eb9(%rip),%rdi
call   1050 <strchr@plt>
cmp    $0x0,%rax
je     11c5 <m315_cleanse+0x85>
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1030 <toupper@plt>
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
jmp    11ca <m315_cleanse+0x8a>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1175 <m315_cleanse+0x35>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<m315_cleanse>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
call   1060 <xmalloc@plt>
mov    %rax,%r14
mov    (%rbx),%al
mov    %r14,%r12
test   %al,%al
je     11b5 <m315_cleanse+0x75>
add    $0x1,%rbx
lea    0x2ee0(%rip),%r15
mov    %r14,%r12
jmp    118b <m315_cleanse+0x4b>
cs nopw 0x0(%rax,%rax,1)
nop
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
je     11b5 <m315_cleanse+0x75>
movsbl %al,%edi
call   1030 <toupper@plt>
mov    %r15,%rdi
mov    %eax,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1180 <m315_cleanse+0x40>
movsbl -0x1(%rbx),%edi
call   1030 <toupper@plt>
mov    %al,(%r12)
add    $0x1,%r12
jmp    1180 <m315_cleanse+0x40>
movb   $0x0,(%r12)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@m315_cleanse.m315_valid_chars = internal global [38 x i8] c"ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789\00"
define i8* @m315_cleanse(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i64 @strlen(i8* %6)
%8 = add nsw i64 %7, 1
%9 = call i8* @xmalloc(i64 %8)
store i8* %9, i8** %3
%10 = load i8*, i8** %3
store i8* %10, i8** %5
%11 = load i8*, i8** %2
store i8* %11, i8** %4
br label %12
12: ; preds = %30, %1
%13 = load i8*, i8** %4
%14 = load i8, i8* %13
%15 = icmp ne i8 %14, 0
br i1 %15, label %16, label %33
16: ; preds = %12
%17 = load i8*, i8** %4
%18 = load i8, i8* %17
%19 = call i32 @toupper(i8 signext %18)
%20 = call i64 @strchr(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @m315_cleanse.m315_valid_chars, i64 0, i64 0), i32 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %16
%23 = load i8*, i8** %4
%24 = load i8, i8* %23
%25 = call i32 @toupper(i8 signext %24)
%26 = trunc i32 %25 to i8
%27 = load i8*, i8** %5
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %5
store i8 %26, i8* %27
br label %29
29: ; preds = %22, %16
br label %30
30: ; preds = %29
%31 = load i8*, i8** %4
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %4
br label %12
33: ; preds = %12
%34 = load i8*, i8** %5
store i8 0, i8* %34
%35 = load i8*, i8** %3
ret i8* %35
}

LLVM IR (-O2):
@m315_cleanse.m315_valid_chars = internal global [38 x i8] c"ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789\00"
define i8* @m315_cleanse(i8* nocapture %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = add nsw i64 %2, 1
%4 = tail call i8* @xmalloc(i64 %3)
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 0
br i1 %6, label %24, label %7
7: ; preds = %1, %19
%8 = phi i8 [ %22, %19 ], [ %5, %1 ]
%9 = phi i8* [ %20, %19 ], [ %4, %1 ]
%10 = phi i8* [ %21, %19 ], [ %0, %1 ]
%11 = tail call i32 @toupper(i8 signext %8)
%12 = tail call i64 @strchr(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @m315_cleanse.m315_valid_chars, i64 0, i64 0), i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %19, label %14
14: ; preds = %7
%15 = load i8, i8* %10
%16 = tail call i32 @toupper(i8 signext %15)
%17 = trunc i32 %16 to i8
%18 = getelementptr inbounds i8, i8* %9, i64 1
store i8 %17, i8* %9
br label %19
19: ; preds = %7, %14
%20 = phi i8* [ %18, %14 ], [ %9, %7 ]
%21 = getelementptr inbounds i8, i8* %10, i64 1
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 0
br i1 %23, label %24, label %7
24: ; preds = %19, %1
%25 = phi i8* [ %4, %1 ], [ %20, %19 ]
store i8 0, i8* %25
ret i8* %4
}