#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 */
struct stat {
  int st_mode;
};

/* Variables and functions */
int S_IFDIR;
int /*<<< orphan*/ elg_error_msg(char *);
scalar_t__ epk_archive_is_epik(char *);
char *malloc(int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *epk_get_elgfilename(const char *path) {
  struct stat statbuf;
  int isDir;
  int pathlen = strlen(path);
  char *efile = malloc(pathlen + 10);
  if (efile == NULL)
    elg_error_msg("Can't allocate ELG filename buffer");
  strcpy(efile, path);
  if (path[pathlen - 1] == '/') {
    efile[pathlen - 1] = '\0';
    pathlen--;
  }
  isDir = ((stat(efile, &statbuf) == 0) && (statbuf.st_mode & S_IFDIR));
  if (isDir && epk_archive_is_epik(efile)) {
    strcat(efile, "/epik.elg");
  }
  return efile;
}


x86 assembly (clang, -O0):
<epk_get_elgfilename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
add    $0xa,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ae <epk_get_elgfilename+0x3e>
lea    0xe57(%rip),%rdi
call   1050 <elg_error_msg@plt>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    11ef <epk_get_elgfilename+0x7f>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1060 <stat@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x21(%rbp)
jne    1223 <epk_get_elgfilename+0xb3>
mov    -0x10(%rbp),%eax
mov    0x2dc0(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     125b <epk_get_elgfilename+0xeb>
mov    -0x20(%rbp),%rdi
call   1080 <epk_archive_is_epik@plt>
cmp    $0x0,%rax
je     125b <epk_get_elgfilename+0xeb>
mov    -0x20(%rbp),%rdi
lea    0xdcd(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<epk_get_elgfilename>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
call   1040 <strlen@plt>
mov    %eax,%r15d
lea    0xa(%r15),%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11a1 <epk_get_elgfilename+0x31>
lea    0xe64(%rip),%rdi
call   1050 <elg_error_msg@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
movslq %r15d,%rax
cmpb   $0x2f,-0x1(%rax,%r14,1)
jne    11bc <epk_get_elgfilename+0x4c>
movb   $0x0,-0x1(%rbx,%rax,1)
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1060 <stat@plt>
test   %rax,%rax
jne    11f9 <epk_get_elgfilename+0x89>
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
and    0x8(%rsp),%eax
je     11f9 <epk_get_elgfilename+0x89>
mov    %rbx,%rdi
call   1080 <epk_archive_is_epik@plt>
test   %rax,%rax
je     11f9 <epk_get_elgfilename+0x89>
lea    0xe32(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcat@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [35 x i8] c"Can't allocate ELG filename buffer\00"
@S_IFDIR = global i32 0
@.str.1 = [10 x i8] c"/epik.elg\00"
define i8* @epk_get_elgfilename(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.stat
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32 @strlen(i8* %7)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = add nsw i32 %9, 10
%11 = call i8* @malloc(i32 %10)
store i8* %11, i8** %6
%12 = load i8*, i8** %6
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 @elg_error_msg(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %1
%17 = load i8*, i8** %6
%18 = load i8*, i8** %2
%19 = call i32 @strcpy(i8* %17, i8* %18)
%20 = load i8*, i8** %2
%21 = load i32, i32* %5
%22 = sub nsw i32 %21, 1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %20, i64 %23
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 47
br i1 %27, label %28, label %36
28: ; preds = %16
%29 = load i8*, i8** %6
%30 = load i32, i32* %5
%31 = sub nsw i32 %30, 1
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %29, i64 %32
store i8 0, i8* %33
%34 = load i32, i32* %5
%35 = add nsw i32 %34, -1
store i32 %35, i32* %5
br label %36
36: ; preds = %28, %16
%37 = load i8*, i8** %6
%38 = call i64 @stat(i8* %37, %struct.stat* %3)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %46
40: ; preds = %36
%41 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%42 = load i32, i32* %41
%43 = load i32, i32* @S_IFDIR
%44 = and i32 %42, %43
%45 = icmp ne i32 %44, 0
br label %46
46: ; preds = %40, %36
%47 = phi i1 [ false, %36 ], [ %45, %40 ]
%48 = zext i1 %47 to i32
store i32 %48, i32* %4
%49 = load i32, i32* %4
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %58
51: ; preds = %46
%52 = load i8*, i8** %6
%53 = call i64 @epk_archive_is_epik(i8* %52)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %51
%56 = load i8*, i8** %6
%57 = call i32 @strcat(i8* %56, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %58
58: ; preds = %55, %51, %46
%59 = load i8*, i8** %6
ret i8* %59
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [35 x i8] c"Can't allocate ELG filename buffer\00"
@S_IFDIR = global i32 0
@.str.1 = [10 x i8] c"/epik.elg\00"
define i8* @epk_get_elgfilename(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = tail call i32 @strlen(i8* %0)
%5 = add nsw i32 %4, 10
%6 = tail call i8* @malloc(i32 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @elg_error_msg(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %1
%11 = tail call i32 @strcpy(i8* %6, i8* %0)
%12 = add nsw i32 %4, -1
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 47
br i1 %16, label %17, label %19
17: ; preds = %10
%18 = getelementptr inbounds i8, i8* %6, i64 %13
store i8 0, i8* %18
br label %19
19: ; preds = %17, %10
%20 = call i64 @stat(i8* %6, %struct.stat* %2)
%21 = icmp eq i64 %20, 0
%22 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%23 = load i32, i32* %22
%24 = load i32, i32* @S_IFDIR
%25 = and i32 %24, %23
%26 = icmp ne i32 %25, 0
%27 = select i1 %21, i1 %26, i1 false
br i1 %27, label %28, label %33
28: ; preds = %19
%29 = call i64 @epk_archive_is_epik(i8* %6)
%30 = icmp eq i64 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %28
%32 = call i32 @strcat(i8* %6, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %33
33: ; preds = %31, %28, %19
ret i8* %6
}