#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 *getenv(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char const *);
int strlen(char *);
char tolower(char);
int fs_storage_path(const char *appname, char *path, int max) {
  char *home = getenv("HOME");
  int i;
  if (!home)
    return -1;
  snprintf(path, max, "%s/.%s", home, appname);
  for (i = strlen(home) + 2; path[i]; i++)
    path[i] = tolower(path[i]);
  return 0;
}


x86 assembly (clang, -O0):
<fs_storage_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    0xea6(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    117a <fs_storage_path+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e9 <fs_storage_path+0xa9>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xe75(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x2,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     11e2 <fs_storage_path+0xa2>
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1060 <tolower@plt>
mov    %al,%dl
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11a4 <fs_storage_path+0x64>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fs_storage_path>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xea9(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11b8 <fs_storage_path+0x78>
mov    %rax,%rbp
lea    0xe9a(%rip),%rdx
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %rax,%rcx
mov    %r14,%r8
call   1050 <snprintf@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
movslq %eax,%rcx
mov    0x2(%rbx,%rcx,1),%al
xor    %ebp,%ebp
test   %al,%al
je     11bd <fs_storage_path+0x7d>
add    $0x2,%rcx
add    %rcx,%rbx
add    $0x1,%rbx
nopl   0x0(%rax)
movsbl %al,%edi
call   1060 <tolower@plt>
mov    %al,-0x1(%rbx)
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
jne    11a0 <fs_storage_path+0x60>
jmp    11bd <fs_storage_path+0x7d>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [7 x i8] c"%s/.%s\00"
define i32 @fs_storage_path(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %10, i8** %8
%11 = load i8*, i8** %8
%12 = icmp ne i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %3
store i32 -1, i32* %4
br label %45
14: ; preds = %3
%15 = load i8*, i8** %6
%16 = load i32, i32* %7
%17 = load i8*, i8** %8
%18 = load i8*, i8** %5
%19 = call i32 @snprintf(i8* %15, i32 %16, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %17, i8* %18)
%20 = load i8*, i8** %8
%21 = call i32 @strlen(i8* %20)
%22 = add nsw i32 %21, 2
store i32 %22, i32* %9
br label %23
23: ; preds = %41, %14
%24 = load i8*, i8** %6
%25 = load i32, i32* %9
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i8, i8* %27
%29 = icmp ne i8 %28, 0
br i1 %29, label %30, label %44
30: ; preds = %23
%31 = load i8*, i8** %6
%32 = load i32, i32* %9
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i8, i8* %34
%36 = call signext i8 @tolower(i8 signext %35)
%37 = load i8*, i8** %6
%38 = load i32, i32* %9
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 %36, i8* %40
br label %41
41: ; preds = %30
%42 = load i32, i32* %9
%43 = add nsw i32 %42, 1
store i32 %43, i32* %9
br label %23
44: ; preds = %23
store i32 0, i32* %4
br label %45
45: ; preds = %44, %13
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [7 x i8] c"%s/.%s\00"
define i32 @fs_storage_path(i8* %0, i8* %1, i32 %2) {
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %23, label %6
6: ; preds = %3
%7 = tail call i32 @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %4, i8* %0)
%8 = tail call i32 @strlen(i8* %4)
%9 = add nsw i32 %8, 2
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %1, i64 %10
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 0
br i1 %13, label %23, label %14
14: ; preds = %6, %14
%15 = phi i64 [ %19, %14 ], [ %10, %6 ]
%16 = phi i8 [ %21, %14 ], [ %12, %6 ]
%17 = phi i8* [ %20, %14 ], [ %11, %6 ]
%18 = tail call signext i8 @tolower(i8 signext %16)
store i8 %18, i8* %17
%19 = add i64 %15, 1
%20 = getelementptr inbounds i8, i8* %1, i64 %19
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 0
br i1 %22, label %23, label %14
23: ; preds = %14, %6, %3
%24 = phi i32 [ -1, %3 ], [ 0, %6 ], [ 0, %14 ]
ret i32 %24
}