#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__ malloc(int);
char tolower(char);
char *str2lower(char *str, int len) {
  char *res = (char *)malloc(len + 1);
  char *p;
  for (p = res; p < res + len; p++, str++) {
    *p = tolower(*str);
  }
  *p = 0;
  return res;
}


x86 assembly (clang, -O0):
<str2lower>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movslq -0xc(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jae    118f <str2lower+0x6f>
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1030 <tolower@plt>
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1146 <str2lower+0x26>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str2lower>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r12
lea    0x1(%r15),%edi
call   1040 <malloc@plt>
mov    %rax,%r14
test   %r15d,%r15d
jle    116b <str2lower+0x4b>
movslq %r15d,%rax
mov    %r14,%r15
add    %rax,%r15
mov    %r14,%rbx
nopl   0x0(%rax,%rax,1)
movsbl (%r12),%edi
call   1030 <tolower@plt>
mov    %al,(%rbx)
add    $0x1,%rbx
add    $0x1,%r12
cmp    %r15,%rbx
jb     1150 <str2lower+0x30>
jmp    116e <str2lower+0x4e>
mov    %r14,%rbx
movb   $0x0,(%rbx)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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