#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__ strlen(char const *);
char tolower(char const);
void strtolower(char *target, const char *src, int len) {
  int i;
  for (i = 0; i < ((int)strlen(src) < len - 1 ? (int)strlen(src) : len - 1);
       i++)
    target[i] = tolower(src[i]);
  target[i] = '\0';
}


x86 assembly (clang, -O0):
<strtolower>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1168 <strtolower+0x48>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x20(%rbp)
jmp    1171 <strtolower+0x51>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x20(%rbp),%ecx
cmp    %ecx,%eax
jge    11ab <strtolower+0x8b>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <tolower@plt>
mov    %al,%dl
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    113a <strtolower+0x1a>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strtolower>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%r14
add    $0xffffffff,%r15d
mov    %rsi,%rdi
call   1030 <strlen@plt>
cmp    %eax,%r15d
cmovle %r15d,%eax
xor    %ebp,%ebp
test   %eax,%eax
jg     1167 <strtolower+0x47>
movb   $0x0,(%r14,%rbp,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
nopl   0x0(%rax)
cltq
cmp    %rax,%rbp
jge    1149 <strtolower+0x29>
movsbl (%rbx,%rbp,1),%edi
call   1040 <tolower@plt>
mov    %al,(%r14,%rbp,1)
add    $0x1,%rbp
mov    %rbx,%rdi
call   1030 <strlen@plt>
cmp    %eax,%r15d
jg     1160 <strtolower+0x40>
mov    %r15d,%eax
jmp    1160 <strtolower+0x40>

LLVM IR (-O0):
define void @strtolower(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
br label %8
8: ; preds = %37, %3
%9 = load i32, i32* %7
%10 = load i8*, i8** %5
%11 = call i64 @strlen(i8* %10)
%12 = trunc i64 %11 to i32
%13 = load i32, i32* %6
%14 = sub nsw i32 %13, 1
%15 = icmp slt i32 %12, %14
br i1 %15, label %16, label %20
16: ; preds = %8
%17 = load i8*, i8** %5
%18 = call i64 @strlen(i8* %17)
%19 = trunc i64 %18 to i32
br label %23
20: ; preds = %8
%21 = load i32, i32* %6
%22 = sub nsw i32 %21, 1
br label %23
23: ; preds = %20, %16
%24 = phi i32 [ %19, %16 ], [ %22, %20 ]
%25 = icmp slt i32 %9, %24
br i1 %25, label %26, label %40
26: ; preds = %23
%27 = load i8*, i8** %5
%28 = load i32, i32* %7
%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* %7
%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* %7
%39 = add nsw i32 %38, 1
store i32 %39, i32* %7
br label %8
40: ; preds = %23
%41 = load i8*, i8** %4
%42 = load i32, i32* %7
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
store i8 0, i8* %44
ret void
}

LLVM IR (-O2):
define void @strtolower(i8* nocapture writeonly %0, i8* nocapture %1, i32 %2) {
%4 = add nsw i32 %2, -1
%5 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%6 = trunc i64 %5 to i32
%7 = icmp sgt i32 %4, %6
%8 = select i1 %7, i32 %6, i32 %4
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %25
10: ; preds = %3, %10
%11 = phi i64 [ %16, %10 ], [ 0, %3 ]
%12 = getelementptr inbounds i8, i8* %1, i64 %11
%13 = load i8, i8* %12
%14 = tail call signext i8 @tolower(i8 signext %13)
%15 = getelementptr inbounds i8, i8* %0, i64 %11
store i8 %14, i8* %15
%16 = add nuw nsw i64 %11, 1
%17 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%18 = trunc i64 %17 to i32
%19 = icmp sgt i32 %4, %18
%20 = select i1 %19, i32 %18, i32 %4
%21 = sext i32 %20 to i64
%22 = icmp slt i64 %16, %21
br i1 %22, label %10, label %23
23: ; preds = %10
%24 = and i64 %16, 4294967295
br label %25
25: ; preds = %23, %3
%26 = phi i64 [ 0, %3 ], [ %24, %23 ]
%27 = getelementptr inbounds i8, i8* %0, i64 %26
store i8 0, i8* %27
ret void
}