#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 tolower(char);
char *xstrdup(char const *);
char *lowercase(const char *astring) {
  char *newstring, *ptr;
  if (astring == NULL) {
    return NULL;
  } else {
    newstring = xstrdup(astring);
    for (ptr = newstring; *ptr != '\0'; ptr++) {
      *ptr = tolower(*ptr);
    }
    return newstring;
  }
}


x86 assembly (clang, -O0):
<lowercase>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1144 <lowercase+0x24>
movq   $0x0,-0x8(%rbp)
jmp    1196 <lowercase+0x76>
mov    -0x10(%rbp),%rdi
call   1030 <xstrdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     118e <lowercase+0x6e>
mov    -0x20(%rbp),%rax
movsbl (%rax),%edi
call   1040 <tolower@plt>
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1159 <lowercase+0x39>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lowercase>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     1158 <lowercase+0x38>
call   1030 <xstrdup@plt>
mov    %rax,%r14
mov    (%rax),%al
test   %al,%al
je     115b <lowercase+0x3b>
mov    %r14,%rbx
add    $0x1,%rbx
xchg   %ax,%ax
movsbl %al,%edi
call   1040 <tolower@plt>
mov    %al,-0x1(%rbx)
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
jne    1140 <lowercase+0x20>
jmp    115b <lowercase+0x3b>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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