#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 *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int strlen(char const *);
char tolower(char const);
char *str_tolower(const char *str) {
  char *lower_str;
  int len;
  int i;
  len = strlen(str) + 1;
  lower_str = malloc(len);
  memset(lower_str, 0, len);
  if (lower_str == NULL) {
    return NULL;
  }
  for (i = 0; str[i]; i++) {
    lower_str[i] = tolower(str[i]);
  }
  return lower_str;
}


x86 assembly (clang, -O0):
<str_tolower>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
xor    %esi,%esi
call   1040 <memset@plt>
cmpq   $0x0,-0x18(%rbp)
jne    118d <str_tolower+0x4d>
movq   $0x0,-0x8(%rbp)
jmp    11da <str_tolower+0x9a>
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     11d2 <str_tolower+0x92>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1050 <tolower@plt>
mov    %al,%dl
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1194 <str_tolower+0x54>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_tolower>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
call   1030 <strlen@plt>
lea    0x1(%rax),%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1040 <memset@plt>
test   %rbx,%rbx
je     1199 <str_tolower+0x59>
mov    (%r14),%al
test   %al,%al
je     1199 <str_tolower+0x59>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbl %al,%edi
call   1050 <tolower@plt>
mov    %al,(%rbx,%rbp,1)
movzbl 0x1(%r14,%rbp,1),%eax
add    $0x1,%rbp
test   %al,%al
jne    1180 <str_tolower+0x40>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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