#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 strlen(char *);
char tolower(char);
scalar_t__ wMalloc(int);
char *wCharCopyToLower(char *text) {
  int len, i;
  char *buffer;
  len = strlen(text);
  buffer = (char *)wMalloc(len + 1);
  for (i = 0; i < len; i++) {
    buffer[i] = tolower(text[i]);
  }
  buffer[len] = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<wCharCopyToLower>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
add    $0x1,%edi
call   1050 <wMalloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1196 <wCharCopyToLower+0x66>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <tolower@plt>
mov    %al,%dl
mov    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    115e <wCharCopyToLower+0x2e>
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wCharCopyToLower>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r15
call   1030 <strlen@plt>
mov    %eax,%r14d
lea    0x1(%r14),%edi
call   1050 <wMalloc@plt>
mov    %rax,%r12
test   %r14d,%r14d
jle    1177 <wCharCopyToLower+0x47>
mov    %r14d,%r13d
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
movsbl (%r15,%rbx,1),%edi
call   1040 <tolower@plt>
mov    %al,(%r12,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1160 <wCharCopyToLower+0x30>
movslq %r14d,%rax
movb   $0x0,(%r12,%rax,1)
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @wCharCopyToLower(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, 1
%4 = tail call i64 @wMalloc(i32 %3)
%5 = inttoptr i64 %4 to i8*
%6 = icmp sgt i32 %2, 0
br i1 %6, label %7, label %17
7: ; preds = %1
%8 = zext i32 %2 to i64
br label %9
9: ; preds = %7, %9
%10 = phi i64 [ 0, %7 ], [ %15, %9 ]
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = load i8, i8* %11
%13 = tail call signext i8 @tolower(i8 signext %12)
%14 = getelementptr inbounds i8, i8* %5, i64 %10
store i8 %13, i8* %14
%15 = add nuw nsw i64 %10, 1
%16 = icmp eq i64 %15, %8
br i1 %16, label %17, label %9
17: ; preds = %9, %1
%18 = sext i32 %2 to i64
%19 = getelementptr inbounds i8, i8* %5, i64 %18
store i8 0, i8* %19
ret i8* %5
}