#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__ isupper(char);
char *strdup(char const *);
char tolower(char);
char *toLowcase(const char *str) {
  char *newstr = strdup(str);
  char *p = newstr;
  while (*p != '\0') {
    if (isupper(*p)) {
      *p = tolower(*p);
    }
    p++;
  }
  return newstr;
}


x86 assembly (clang, -O0):
<toLowcase>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strdup@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     119c <toLowcase+0x6c>
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isupper@plt>
cmp    $0x0,%rax
je     118b <toLowcase+0x5b>
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1030 <tolower@plt>
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1151 <toLowcase+0x21>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<toLowcase>:
push   %r14
push   %rbx
push   %rax
call   1050 <strdup@plt>
mov    %rax,%r14
mov    (%rax),%al
test   %al,%al
je     1175 <toLowcase+0x45>
mov    %r14,%rbx
jmp    115c <toLowcase+0x2c>
nopw   0x0(%rax,%rax,1)
movzbl 0x1(%rbx),%eax
add    $0x1,%rbx
test   %al,%al
je     1175 <toLowcase+0x45>
movsbl %al,%edi
call   1040 <isupper@plt>
test   %rax,%rax
je     1150 <toLowcase+0x20>
movsbl (%rbx),%edi
call   1030 <tolower@plt>
mov    %al,(%rbx)
jmp    1150 <toLowcase+0x20>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @toLowcase(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i8* @strdup(i8* %5)
store i8* %6, i8** %3
%7 = load i8*, i8** %3
store i8* %7, i8** %4
br label %8
8: ; preds = %23, %1
%9 = load i8*, i8** %4
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %26
13: ; preds = %8
%14 = load i8*, i8** %4
%15 = load i8, i8* %14
%16 = call i64 @isupper(i8 signext %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %13
%19 = load i8*, i8** %4
%20 = load i8, i8* %19
%21 = call signext i8 @tolower(i8 signext %20)
%22 = load i8*, i8** %4
store i8 %21, i8* %22
br label %23
23: ; preds = %18, %13
%24 = load i8*, i8** %4
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %4
br label %8
26: ; preds = %8
%27 = load i8*, i8** %3
ret i8* %27
}

LLVM IR (-O2):
define noalias i8* @toLowcase(i8* nocapture %0) {
%2 = tail call i8* @strdup(i8* %0)
%3 = load i8, i8* %2
%4 = icmp eq i8 %3, 0
br i1 %4, label %17, label %5
5: ; preds = %1, %13
%6 = phi i8 [ %15, %13 ], [ %3, %1 ]
%7 = phi i8* [ %14, %13 ], [ %2, %1 ]
%8 = tail call i64 @isupper(i8 signext %6)
%9 = icmp eq i64 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %5
%11 = load i8, i8* %7
%12 = tail call signext i8 @tolower(i8 signext %11)
store i8 %12, i8* %7
br label %13
13: ; preds = %10, %5
%14 = getelementptr inbounds i8, i8* %7, i64 1
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 0
br i1 %16, label %17, label %5
17: ; preds = %13, %1
ret i8* %2
}