#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 */
int /*<<< orphan*/ LC_CTYPE;
scalar_t__ realloc(char *, size_t);
char *setlocale(int /*<<< orphan*/, char const *);
size_t strlen(char const *);
char tolower(char const);
const char *lowercase(const char *x, const char *locale) {
  static char *result = NULL;
  static size_t ressize = 0;
  size_t len = strlen(x);
  if (ressize <= len) {
    ressize = len + 1;
    result = (char *)realloc(result, ressize);
  }
  char *r;
  const char *prev_locale = setlocale(LC_CTYPE, locale);
  for (r = result; *x; x++, r++)
    *r = tolower(*x);
  setlocale(LC_CTYPE, prev_locale);
  *r = '\0';
  return result;
}


x86 assembly (clang, -O0):
<lowercase>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    0x2eec(%rip),%rax
cmp    -0x18(%rbp),%rax
ja     1197 <lowercase+0x57>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,0x2ed3(%rip)
mov    0x2ec4(%rip),%rdi
mov    0x2ec5(%rip),%rsi
call   1050 <realloc@plt>
mov    %rax,0x2eb1(%rip)
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
call   1060 <setlocale@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e94(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     11f6 <lowercase+0xb6>
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1040 <tolower@plt>
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11b8 <lowercase+0x78>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rsi
call   1060 <setlocale@plt>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    0x2e32(%rip),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lowercase>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
call   1030 <strlen@plt>
cmp    %rax,0x2ef6(%rip)
ja     117d <lowercase+0x3d>
add    $0x1,%rax
mov    %rax,0x2ee9(%rip)
mov    0x2eda(%rip),%rdi
mov    %rax,%rsi
call   1050 <realloc@plt>
mov    %rax,0x2ecb(%rip)
mov    0x2e64(%rip),%r15
mov    (%r15),%edi
mov    %r14,%rsi
call   1060 <setlocale@plt>
mov    %rax,%r14
mov    0x2eaf(%rip),%rbx
mov    (%r12),%al
test   %al,%al
je     11cb <lowercase+0x8b>
add    $0x1,%r12
cs nopw 0x0(%rax,%rax,1)
nop
movsbl %al,%edi
call   1040 <tolower@plt>
mov    %al,(%rbx)
add    $0x1,%rbx
movzbl (%r12),%eax
add    $0x1,%r12
test   %al,%al
jne    11b0 <lowercase+0x70>
mov    (%r15),%edi
mov    %r14,%rsi
call   1060 <setlocale@plt>
movb   $0x0,(%rbx)
mov    0x2e68(%rip),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@lowercase.result = internal unnamed_addr global i8* null
@lowercase.ressize = internal unnamed_addr global i64 0
@LC_CTYPE = global i32 0
define i8* @lowercase(i8* nocapture %0, i8* %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = load i64, i64* @lowercase.ressize
%5 = icmp ugt i64 %4, %3
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = add i64 %3, 1
store i64 %7, i64* @lowercase.ressize
%8 = load i8*, i8** @lowercase.result
%9 = tail call i64 @realloc(i8* %8, i64 %7)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** @lowercase.result
br label %11
11: ; preds = %6, %2
%12 = load i32, i32* @LC_CTYPE
%13 = tail call i8* @setlocale(i32 %12, i8* %1)
%14 = load i8*, i8** @lowercase.result
%15 = load i8, i8* %0
%16 = icmp eq i8 %15, 0
br i1 %16, label %26, label %17
17: ; preds = %11, %17
%18 = phi i8 [ %24, %17 ], [ %15, %11 ]
%19 = phi i8* [ %22, %17 ], [ %0, %11 ]
%20 = phi i8* [ %23, %17 ], [ %14, %11 ]
%21 = tail call signext i8 @tolower(i8 signext %18)
store i8 %21, i8* %20
%22 = getelementptr inbounds i8, i8* %19, i64 1
%23 = getelementptr inbounds i8, i8* %20, i64 1
%24 = load i8, i8* %22
%25 = icmp eq i8 %24, 0
br i1 %25, label %26, label %17
26: ; preds = %17, %11
%27 = phi i8* [ %14, %11 ], [ %23, %17 ]
%28 = load i32, i32* @LC_CTYPE
%29 = tail call i8* @setlocale(i32 %28, i8* %13)
store i8 0, i8* %27
%30 = load i8*, i8** @lowercase.result
ret i8* %30
}