#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 *appaserver_parameter_file_get_appaserver_error_directory();
char *environ_get_environment(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ timlib_file_exists(char *);
int /*<<< orphan*/ timlib_parse_database_string(char **, char *);
int /*<<< orphan*/ timlib_strcpy(char *, char *, int);
char *appaserver_error_get_filename(char *application_name) {
  static char filename[128] = {0};
  char local_application_name[128];
  char *database_string = {0};
  if (!application_name) {
    application_name = environ_get_environment("APPASERVER_DATABASE");
  }
  if (application_name && *application_name) {
    timlib_strcpy(local_application_name, application_name, 128);
    timlib_parse_database_string(&database_string, local_application_name);
    sprintf(filename, "%s/appaserver_%s.err",
            appaserver_parameter_file_get_appaserver_error_directory(),
            local_application_name);
    if (!timlib_file_exists(filename)) {
      strcpy(filename, "/var/log/appaserver.err");
    }
  } else {
    strcpy(filename, "/var/log/appaserver.err");
  }
  if (!timlib_file_exists(filename)) {
    strcpy(filename, "/var/appaserver.err");
  }
  return filename;
}


x86 assembly (clang, -O0):
<appaserver_error_get_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x98(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11a5 <appaserver_error_get_filename+0x35>
lea    0xe64(%rip),%rdi
call   1060 <environ_get_environment@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1239 <appaserver_error_get_filename+0xc9>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1239 <appaserver_error_get_filename+0xc9>
lea    -0x90(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x80,%edx
call   1050 <timlib_strcpy@plt>
lea    -0x90(%rbp),%rsi
lea    -0x98(%rbp),%rdi
call   1070 <timlib_parse_database_string@plt>
mov    $0x0,%al
call   1040 <appaserver_parameter_file_get_appaserver_error_directory@plt>
mov    %rax,%rdx
lea    -0x90(%rbp),%rcx
lea    0x2e70(%rip),%rdi
lea    0xe0d(%rip),%rsi
call   1090 <sprintf@plt>
lea    0x2e5d(%rip),%rdi
call   1080 <timlib_file_exists@plt>
cmp    $0x0,%eax
jne    1234 <appaserver_error_get_filename+0xc4>
lea    0x2e48(%rip),%rdi
lea    0xdfa(%rip),%rsi
call   1030 <strcpy@plt>
jmp    124c <appaserver_error_get_filename+0xdc>
lea    0x2e30(%rip),%rdi
lea    0xde2(%rip),%rsi
call   1030 <strcpy@plt>
lea    0x2e1d(%rip),%rdi
call   1080 <timlib_file_exists@plt>
cmp    $0x0,%eax
jne    1274 <appaserver_error_get_filename+0x104>
lea    0x2e08(%rip),%rdi
lea    0xdd2(%rip),%rsi
call   1030 <strcpy@plt>
lea    0x2df5(%rip),%rax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<appaserver_error_get_filename>:
push   %r14
push   %rbx
sub    $0x98,%rsp
mov    %rdi,%rsi
movq   $0x0,0x8(%rsp)
test   %rdi,%rdi
jne    119f <appaserver_error_get_filename+0x2f>
lea    0xe6e(%rip),%rdi
call   1060 <environ_get_environment@plt>
mov    %rax,%rsi
test   %rax,%rax
je     11f2 <appaserver_error_get_filename+0x82>
cmpb   $0x0,(%rsi)
je     11f2 <appaserver_error_get_filename+0x82>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x80,%edx
call   1050 <timlib_strcpy@plt>
lea    0x8(%rsp),%rdi
mov    %r14,%rsi
call   1070 <timlib_parse_database_string@plt>
xor    %eax,%eax
call   1040 <appaserver_parameter_file_get_appaserver_error_directory@plt>
lea    0x2e9f(%rip),%rbx
lea    0xe3c(%rip),%rsi
mov    %rbx,%rdi
mov    %rax,%rdx
mov    %r14,%rcx
call   1090 <sprintf@plt>
mov    %rbx,%rdi
call   1080 <timlib_file_exists@plt>
test   %eax,%eax
jne    1205 <appaserver_error_get_filename+0x95>
lea    0x2e77(%rip),%rdi
lea    0xe29(%rip),%rsi
call   1030 <strcpy@plt>
lea    0x2e64(%rip),%rdi
call   1080 <timlib_file_exists@plt>
test   %eax,%eax
jne    1228 <appaserver_error_get_filename+0xb8>
lea    0x2e54(%rip),%rdi
lea    0xe1e(%rip),%rsi
call   1030 <strcpy@plt>
lea    0x2e41(%rip),%rax
add    $0x98,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@appaserver_error_get_filename.filename = internal global [128 x i8] zeroinitializer
@.str = [20 x i8] c"APPASERVER_DATABASE\00"
@.str.1 = [21 x i8] c"%s/appaserver_%s.err\00"
@.str.2 = [24 x i8] c"/var/log/appaserver.err\00"
@.str.3 = [20 x i8] c"/var/appaserver.err\00"
define i8* @appaserver_error_get_filename(i8* %0) {
%2 = alloca i8*
%3 = alloca [128 x i8]
%4 = alloca i8*
store i8* %0, i8** %2
store i8* null, i8** %4
%5 = load i8*, i8** %2
%6 = icmp ne i8* %5, null
br i1 %6, label %9, label %7
7: ; preds = %1
%8 = call i8* @environ_get_environment(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i8* %8, i8** %2
br label %9
9: ; preds = %7, %1
%10 = load i8*, i8** %2
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %31
12: ; preds = %9
%13 = load i8*, i8** %2
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %31
17: ; preds = %12
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%19 = load i8*, i8** %2
%20 = call i32 @timlib_strcpy(i8* %18, i8* %19, i32 128)
%21 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%22 = call i32 @timlib_parse_database_string(i8** %4, i8* %21)
%23 = call i8* (...) @appaserver_parameter_file_get_appaserver_error_directory()
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%25 = call i32 @sprintf(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @appaserver_error_get_filename.filename, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %23, i8* %24)
%26 = call i32 @timlib_file_exists(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @appaserver_error_get_filename.filename, i64 0, i64 0))
%27 = icmp ne i32 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %17
%29 = call i32 @strcpy(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @appaserver_error_get_filename.filename, i64 0, i64 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %28, %17
br label %33
31: ; preds = %12, %9
%32 = call i32 @strcpy(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @appaserver_error_get_filename.filename, i64 0, i64 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31, %30
%34 = call i32 @timlib_file_exists(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @appaserver_error_get_filename.filename, i64 0, i64 0))
%35 = icmp ne i32 %34, 0
br i1 %35, label %38, label %36
36: ; preds = %33
%37 = call i32 @strcpy(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @appaserver_error_get_filename.filename, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %36, %33
ret i8* getelementptr inbounds ([128 x i8], [128 x i8]* @appaserver_error_get_filename.filename, i64 0, i64 0)
}

LLVM IR (-O2):
@appaserver_error_get_filename.filename = internal global [128 x i8] zeroinitializer
@.str = [20 x i8] c"APPASERVER_DATABASE\00"
@.str.1 = [21 x i8] c"%s/appaserver_%s.err\00"
@.str.2 = [24 x i8] c"/var/log/appaserver.err\00"
@.str.3 = [20 x i8] c"/var/appaserver.err\00"
define i8* @appaserver_error_get_filename(i8* %0) {
%2 = alloca [128 x i8]
%3 = alloca i8*
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%5 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%6 = icmp eq i8* %0, null
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = tail call i8* @environ_get_environment(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i8* %8, null
br i1 %9, label %23, label %10
10: ; preds = %1, %7
%11 = phi i8* [ %8, %7 ], [ %0, %1 ]
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 0
br i1 %13, label %23, label %14
14: ; preds = %10
%15 = call i32 @timlib_strcpy(i8* %4, i8* %11, i32 128)
%16 = call i32 @timlib_parse_database_string(i8** %3, i8* %4)
%17 = call i8* (...) @appaserver_parameter_file_get_appaserver_error_directory()
%18 = call i32 @sprintf(i8* dereferenceable(1) getelementptr inbounds ([128 x i8], [128 x i8]* @appaserver_error_get_filename.filename, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %17, i8* %4)
%19 = call i32 @timlib_file_exists(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @appaserver_error_get_filename.filename, i64 0, i64 0))
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %14
%22 = call i32 @strcpy(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @appaserver_error_get_filename.filename, i64 0, i64 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %25
23: ; preds = %10, %7
%24 = tail call i32 @strcpy(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @appaserver_error_get_filename.filename, i64 0, i64 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %14, %21, %23
%26 = call i32 @timlib_file_exists(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @appaserver_error_get_filename.filename, i64 0, i64 0))
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 @strcpy(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @appaserver_error_get_filename.filename, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %30
30: ; preds = %28, %25
ret i8* getelementptr inbounds ([128 x i8], [128 x i8]* @appaserver_error_get_filename.filename, i64 0, i64 0)
}