#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
int /*<<< orphan*/ xfree(char *);
char *xmalloc(int);
char *find_gpsbabel_inifile(const char *path) {
  FILE *test;
  char *buff;
  int len;
  if (path == NULL)
    return NULL;
  len = strlen(path);
  buff = xmalloc(len + 1 + strlen("gpsbabel.ini") + 1);
  strcpy(buff, path);
  if (len > 0) {
    char test = buff[len - 1];
    if (test != '/')
      strcat(buff, "/");
  }
  strcat(buff, "gpsbabel.ini");
  test = fopen(buff, "rb");
  if (test) {
    fclose(test);
    return buff;
  }
  xfree(buff);
  return NULL;
}


x86 assembly (clang, -O0):
<find_gpsbabel_inifile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1194 <find_gpsbabel_inifile+0x24>
movq   $0x0,-0x8(%rbp)
jmp    1268 <find_gpsbabel_inifile+0xf8>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
lea    0xe50(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x2c(%rbp),%edi
add    %eax,%edi
add    $0x1,%edi
call   1070 <xmalloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
cmpl   $0x0,-0x24(%rbp)
jle    1212 <find_gpsbabel_inifile+0xa2>
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x25(%rbp)
movsbl -0x25(%rbp),%eax
cmp    $0x2f,%eax
je     120d <find_gpsbabel_inifile+0x9d>
mov    -0x20(%rbp),%rdi
lea    0xe05(%rip),%rsi
call   1090 <strcat@plt>
jmp    1212 <find_gpsbabel_inifile+0xa2>
mov    -0x20(%rbp),%rdi
lea    0xde3(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x20(%rbp),%rdi
lea    0xde2(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1257 <find_gpsbabel_inifile+0xe7>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1268 <find_gpsbabel_inifile+0xf8>
mov    -0x20(%rbp),%rdi
call   1060 <xfree@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_gpsbabel_inifile>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     1203 <find_gpsbabel_inifile+0x93>
mov    %rdi,%rbx
call   1050 <strlen@plt>
mov    %eax,%r14d
lea    0xe70(%rip),%rdi
call   1050 <strlen@plt>
lea    (%r14,%rax,1),%edi
add    $0x2,%edi
call   1070 <xmalloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
test   %r14d,%r14d
jle    11ce <find_gpsbabel_inifile+0x5e>
add    $0xffffffff,%r14d
cmpb   $0x2f,(%r15,%r14,1)
je     11ce <find_gpsbabel_inifile+0x5e>
lea    0xe47(%rip),%rsi
mov    %r15,%rdi
call   1090 <strcat@plt>
lea    0xe2b(%rip),%rsi
mov    %r15,%rdi
call   1090 <strcat@plt>
lea    0xe2b(%rip),%rsi
mov    %r15,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     11fb <find_gpsbabel_inifile+0x8b>
mov    %rax,%rdi
call   1040 <fclose@plt>
jmp    1206 <find_gpsbabel_inifile+0x96>
mov    %r15,%rdi
call   1060 <xfree@plt>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [13 x i8] c"gpsbabel.ini\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [3 x i8] c"rb\00"
define i8* @find_gpsbabel_inifile(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %1
store i8* null, i8** %2
br label %53
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = add nsw i32 %14, 1
%16 = call i32 @strlen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%17 = add nsw i32 %15, %16
%18 = add nsw i32 %17, 1
%19 = call i8* @xmalloc(i32 %18)
store i8* %19, i8** %5
%20 = load i8*, i8** %5
%21 = load i8*, i8** %3
%22 = call i32 @strcpy(i8* %20, i8* %21)
%23 = load i32, i32* %6
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %39
25: ; preds = %11
%26 = load i8*, i8** %5
%27 = load i32, i32* %6
%28 = sub nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %26, i64 %29
%31 = load i8, i8* %30
store i8 %31, i8* %7
%32 = load i8, i8* %7
%33 = sext i8 %32 to i32
%34 = icmp ne i32 %33, 47
br i1 %34, label %35, label %38
35: ; preds = %25
%36 = load i8*, i8** %5
%37 = call i32 @strcat(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %38
38: ; preds = %35, %25
br label %39
39: ; preds = %38, %11
%40 = load i8*, i8** %5
%41 = call i32 @strcat(i8* %40, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%42 = load i8*, i8** %5
%43 = call i32* @fopen(i8* %42, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %43, i32** %4
%44 = load i32*, i32** %4
%45 = icmp ne i32* %44, null
br i1 %45, label %46, label %50
46: ; preds = %39
%47 = load i32*, i32** %4
%48 = call i32 @fclose(i32* %47)
%49 = load i8*, i8** %5
store i8* %49, i8** %2
br label %53
50: ; preds = %39
%51 = load i8*, i8** %5
%52 = call i32 @xfree(i8* %51)
store i8* null, i8** %2
br label %53
53: ; preds = %50, %46, %10
%54 = load i8*, i8** %2
ret i8* %54
}

LLVM IR (-O2):
@.str = [13 x i8] c"gpsbabel.ini\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [3 x i8] c"rb\00"
define i8* @find_gpsbabel_inifile(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %27, label %3
3: ; preds = %1
%4 = tail call i32 @strlen(i8* %0)
%5 = tail call i32 @strlen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%6 = add i32 %4, 2
%7 = add i32 %6, %5
%8 = tail call i8* @xmalloc(i32 %7)
%9 = tail call i32 @strcpy(i8* %8, i8* %0)
%10 = icmp sgt i32 %4, 0
br i1 %10, label %11, label %19
11: ; preds = %3
%12 = add nsw i32 %4, -1
%13 = zext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %8, i64 %13
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 47
br i1 %16, label %19, label %17
17: ; preds = %11
%18 = tail call i32 @strcat(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %11, %17, %3
%20 = tail call i32 @strcat(i8* %8, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%21 = tail call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %25, label %23
23: ; preds = %19
%24 = tail call i32 @fclose(i32* %21)
br label %27
25: ; preds = %19
%26 = tail call i32 @xfree(i8* %8)
br label %27
27: ; preds = %1, %25, %23
%28 = phi i8* [ %8, %23 ], [ null, %25 ], [ null, %1 ]
ret i8* %28
}