#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*/ free(char *);
scalar_t__ isalpha(char);
scalar_t__ strlen(char *);
char tolower(char);
char *xalloc(scalar_t__);
char *xstrdup(char *);
char *string_clean(char *str) {
  unsigned i = 0;
  char *cleaned = xalloc(strlen(str) + 1);
  char *ret;
  while (*str) {
    if (isalpha(*str))
      cleaned[i++] = tolower(*str);
    ++str;
  }
  cleaned[i] = 0;
  ret = xstrdup(cleaned);
  free(cleaned);
  return ret;
}


x86 assembly (clang, -O0):
<string_clean>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1080 <xalloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     11e2 <string_clean+0x82>
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1070 <isalpha@plt>
cmp    $0x0,%rax
je     11d1 <string_clean+0x71>
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1060 <tolower@plt>
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0xc(%rbp)
mov    %ecx,%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    118c <string_clean+0x2c>
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1050 <xstrdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_clean>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
call   1080 <xalloc@plt>
mov    %rax,%r14
mov    (%rbx),%al
test   %al,%al
je     11bc <string_clean+0x5c>
add    $0x1,%rbx
xor    %ebp,%ebp
jmp    119b <string_clean+0x3b>
cs nopw 0x0(%rax,%rax,1)
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
je     11be <string_clean+0x5e>
movsbl %al,%edi
call   1070 <isalpha@plt>
test   %rax,%rax
je     1190 <string_clean+0x30>
movsbl -0x1(%rbx),%edi
call   1060 <tolower@plt>
mov    %ebp,%ecx
add    $0x1,%ebp
mov    %al,(%r14,%rcx,1)
jmp    1190 <string_clean+0x30>
xor    %ebp,%ebp
mov    %ebp,%eax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rdi
call   1050 <xstrdup@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i8* @string_clean(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %2
store i32 0, i32* %3
%6 = load i8*, i8** %2
%7 = call i64 @strlen(i8* %6)
%8 = add nsw i64 %7, 1
%9 = call i8* @xalloc(i64 %8)
store i8* %9, i8** %4
br label %10
10: ; preds = %28, %1
%11 = load i8*, i8** %2
%12 = load i8, i8* %11
%13 = icmp ne i8 %12, 0
br i1 %13, label %14, label %31
14: ; preds = %10
%15 = load i8*, i8** %2
%16 = load i8, i8* %15
%17 = call i64 @isalpha(i8 signext %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %14
%20 = load i8*, i8** %2
%21 = load i8, i8* %20
%22 = call signext i8 @tolower(i8 signext %21)
%23 = load i8*, i8** %4
%24 = load i32, i32* %3
%25 = add i32 %24, 1
store i32 %25, i32* %3
%26 = zext i32 %24 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
store i8 %22, i8* %27
br label %28
28: ; preds = %19, %14
%29 = load i8*, i8** %2
%30 = getelementptr inbounds i8, i8* %29, i32 1
store i8* %30, i8** %2
br label %10
31: ; preds = %10
%32 = load i8*, i8** %4
%33 = load i32, i32* %3
%34 = zext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 0, i8* %35
%36 = load i8*, i8** %4
%37 = call i8* @xstrdup(i8* %36)
store i8* %37, i8** %5
%38 = load i8*, i8** %4
%39 = call i32 @free(i8* %38)
%40 = load i8*, i8** %5
ret i8* %40
}

LLVM IR (-O2):
define i8* @string_clean(i8* nocapture %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = add nsw i64 %2, 1
%4 = tail call i8* @xalloc(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* [ %21, %19 ], [ %0, %1 ]
%10 = phi i32 [ %20, %19 ], [ 0, %1 ]
%11 = tail call i64 @isalpha(i8 signext %8)
%12 = icmp eq i64 %11, 0
br i1 %12, label %19, label %13
13: ; preds = %7
%14 = load i8, i8* %9
%15 = tail call signext i8 @tolower(i8 signext %14)
%16 = add i32 %10, 1
%17 = zext i32 %10 to i64
%18 = getelementptr inbounds i8, i8* %4, i64 %17
store i8 %15, i8* %18
br label %19
19: ; preds = %13, %7
%20 = phi i32 [ %16, %13 ], [ %10, %7 ]
%21 = getelementptr inbounds i8, i8* %9, 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 i32 [ 0, %1 ], [ %20, %19 ]
%26 = zext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %4, i64 %26
store i8 0, i8* %27
%28 = tail call i8* @xstrdup(i8* %4)
%29 = tail call i32 @free(i8* %4)
ret i8* %28
}