#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*/ *fopen(char *, char *);
char *getenv(char *);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
FILE *open_cfgfile(char *filename) {
  FILE *fp;
  static char searchPaths[5][256];
  static char searchPathsReady = 0;
  int i;
  if (!searchPathsReady) {
    strcpy(searchPaths[0], "./");
    strncpy(searchPaths[1], getenv("HOME"), 256);
    searchPaths[1][255] = '\0';
    if (searchPaths[1][0] == '\0' || strlen(searchPaths[1]) > 150)
      strcpy(searchPaths[1], "~/");
    if (searchPaths[1][strlen(searchPaths[1])] != '/')
      strncat(searchPaths[1], "/", 256);
    strncat(searchPaths[1], ".avrxtool32/", 256);
    strcpy(searchPaths[2], "/usr/share/avrxtool32/");
    strcpy(searchPaths[3], "/etc/avrxtool32/");
    searchPaths[4][0] = '\0';
    searchPathsReady = 1;
  }
  i = 0;
  while (searchPaths[i][0] != '\0') {
    char path[256];
    strncpy(path, searchPaths[i++], 256);
    strncat(path, filename, 256);
    fp = fopen(path, "rt");
    if (fp != NULL)
      return fp;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<open_cfgfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x10(%rbp)
cmpb   $0x0,0x33ea(%rip)
jne    1291 <open_cfgfile+0x131>
lea    0x2edd(%rip),%rdi
lea    0xe76(%rip),%rsi
call   1050 <strcpy@plt>
lea    0xe6d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rsi
lea    0x2ebb(%rip),%rdi
add    $0x100,%rdi
mov    $0x100,%edx
call   1040 <strncpy@plt>
movb   $0x0,0x30a2(%rip)
movsbl 0x2f9c(%rip),%eax
cmp    $0x0,%eax
je     11e5 <open_cfgfile+0x85>
lea    0x2f8c(%rip),%rdi
call   1060 <strlen@plt>
cmp    $0x96,%rax
jbe    11f8 <open_cfgfile+0x98>
lea    0x2f74(%rip),%rdi
lea    0xe15(%rip),%rsi
call   1050 <strcpy@plt>
lea    0x2f61(%rip),%rdi
call   1060 <strlen@plt>
mov    %rax,%rcx
lea    0x2e52(%rip),%rax
movsbl 0x100(%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     123e <open_cfgfile+0xde>
lea    0x2e3a(%rip),%rdi
add    $0x100,%rdi
lea    0xdcd(%rip),%rsi
mov    $0x100,%edx
call   1070 <strncat@plt>
lea    0x2e1b(%rip),%rdi
add    $0x100,%rdi
lea    0xdb8(%rip),%rsi
mov    $0x100,%edx
call   1070 <strncat@plt>
lea    0x2ffc(%rip),%rdi
lea    0xdad(%rip),%rsi
call   1050 <strcpy@plt>
lea    0x30e9(%rip),%rdi
lea    0xdb1(%rip),%rsi
call   1050 <strcpy@plt>
movb   $0x0,0x31d6(%rip)
movb   $0x1,0x32cf(%rip)
movl   $0x0,-0x1c(%rbp)
movslq -0x1c(%rbp),%rcx
lea    0x2dbd(%rip),%rax
shl    $0x8,%rcx
add    %rcx,%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     132b <open_cfgfile+0x1cb>
lea    -0x120(%rbp),%rdi
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x1c(%rbp)
cltq
lea    0x2d8f(%rip),%rsi
shl    $0x8,%rax
add    %rax,%rsi
mov    $0x100,%edx
call   1040 <strncpy@plt>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x100,%edx
call   1070 <strncat@plt>
lea    -0x120(%rbp),%rdi
lea    0xd3b(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1326 <open_cfgfile+0x1c6>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1333 <open_cfgfile+0x1d3>
jmp    1298 <open_cfgfile+0x138>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_cfgfile>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x100,%rsp
mov    %rdi,%r15
cmpb   $0x0,0x33e6(%rip)
jne    1268 <open_cfgfile+0x108>
lea    0x2ed9(%rip),%r14
lea    0xe72(%rip),%rsi
mov    %r14,%rdi
call   1050 <strcpy@plt>
lea    0xe66(%rip),%rdi
call   1030 <getenv@plt>
lea    0x2fb7(%rip),%rdi
mov    %rax,%rsi
mov    $0x100,%edx
call   1040 <strncpy@plt>
movb   $0x0,0x30a2(%rip)
cmpb   $0x0,0x2f9c(%rip)
je     11da <open_cfgfile+0x7a>
lea    0x2f93(%rip),%rdi
call   1060 <strlen@plt>
cmp    $0x97,%rax
jb     11ed <open_cfgfile+0x8d>
lea    0x2f7f(%rip),%rdi
lea    0xe20(%rip),%rsi
call   1050 <strcpy@plt>
lea    0x2f6c(%rip),%rdi
call   1060 <strlen@plt>
cmpb   $0x2f,0x100(%rax,%r14,1)
je     121c <open_cfgfile+0xbc>
lea    0x2f55(%rip),%rdi
lea    0xdef(%rip),%rsi
mov    $0x100,%edx
call   1070 <strncat@plt>
lea    0x2f3d(%rip),%rdi
lea    0xde1(%rip),%rsi
mov    $0x100,%edx
call   1070 <strncat@plt>
lea    0x3025(%rip),%rdi
lea    0xdd6(%rip),%rsi
call   1050 <strcpy@plt>
lea    0x3112(%rip),%rdi
lea    0xdda(%rip),%rsi
call   1050 <strcpy@plt>
movb   $0x0,0x31ff(%rip)
movb   $0x1,0x32f8(%rip)
lea    0x2df1(%rip),%rbx
xor    %r14d,%r14d
mov    %rsp,%r12
lea    0xdc4(%rip),%r13
nopl   0x0(%rax)
cmpb   $0x0,(%rbx)
je     12bf <open_cfgfile+0x15f>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1040 <strncpy@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    $0x100,%edx
call   1070 <strncat@plt>
mov    %r12,%rdi
mov    %r13,%rsi
call   1080 <fopen@plt>
add    $0x100,%rbx
test   %rax,%rax
je     1280 <open_cfgfile+0x120>
mov    %rax,%r14
mov    %r14,%rax
add    $0x100,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@open_cfgfile.searchPaths = internal global [5 x [256 x i8]] zeroinitializer
@open_cfgfile.searchPathsReady = internal global i8 0
@.str = [3 x i8] c"./\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [3 x i8] c"~/\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [13 x i8] c".avrxtool32/\00"
@.str.5 = [23 x i8] c"/usr/share/avrxtool32/\00"
@.str.6 = [17 x i8] c"/etc/avrxtool32/\00"
@.str.7 = [3 x i8] c"rt\00"
define i32* @open_cfgfile(i8* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca [256 x i8]
store i8* %0, i8** %3
%7 = load i8, i8* @open_cfgfile.searchPathsReady
%8 = icmp ne i8 %7, 0
br i1 %8, label %33, label %9
9: ; preds = %1
%10 = call i32 @strcpy(i8* getelementptr inbounds ([5 x [256 x i8]], [5 x [256 x i8]]* @open_cfgfile.searchPaths, i64 0, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%11 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @strncpy(i8* getelementptr inbounds ([5 x [256 x i8]], [5 x [256 x i8]]* @open_cfgfile.searchPaths, i64 0, i64 1, i64 0), i8* %11, i32 256)
store i8 0, i8* getelementptr inbounds ([5 x [256 x i8]], [5 x [256 x i8]]* @open_cfgfile.searchPaths, i64 0, i64 1, i64 255)
%13 = load i8, i8* getelementptr inbounds ([5 x [256 x i8]], [5 x [256 x i8]]* @open_cfgfile.searchPaths, i64 0, i64 1, i64 0)
%14 = sext i8 %13 to i32
%15 = icmp eq i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %9
%17 = call i64 @strlen(i8* getelementptr inbounds ([5 x [256 x i8]], [5 x [256 x i8]]* @open_cfgfile.searchPaths, i64 0, i64 1, i64 0))
%18 = icmp ugt i64 %17, 150
br i1 %18, label %19, label %21
19: ; preds = %16, %9
%20 = call i32 @strcpy(i8* getelementptr inbounds ([5 x [256 x i8]], [5 x [256 x i8]]* @open_cfgfile.searchPaths, i64 0, i64 1, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
%22 = call i64 @strlen(i8* getelementptr inbounds ([5 x [256 x i8]], [5 x [256 x i8]]* @open_cfgfile.searchPaths, i64 0, i64 1, i64 0))
%23 = getelementptr inbounds [256 x i8], [256 x i8]* getelementptr inbounds ([5 x [256 x i8]], [5 x [256 x i8]]* @open_cfgfile.searchPaths, i64 0, i64 1), i64 0, i64 %22
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %25, 47
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = call i32 @strncat(i8* getelementptr inbounds ([5 x [256 x i8]], [5 x [256 x i8]]* @open_cfgfile.searchPaths, i64 0, i64 1, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 256)
br label %29
29: ; preds = %27, %21
%30 = call i32 @strncat(i8* getelementptr inbounds ([5 x [256 x i8]], [5 x [256 x i8]]* @open_cfgfile.searchPaths, i64 0, i64 1, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 256)
%31 = call i32 @strcpy(i8* getelementptr inbounds ([5 x [256 x i8]], [5 x [256 x i8]]* @open_cfgfile.searchPaths, i64 0, i64 2, i64 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
%32 = call i32 @strcpy(i8* getelementptr inbounds ([5 x [256 x i8]], [5 x [256 x i8]]* @open_cfgfile.searchPaths, i64 0, i64 3, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0))
store i8 0, i8* getelementptr inbounds ([5 x [256 x i8]], [5 x [256 x i8]]* @open_cfgfile.searchPaths, i64 0, i64 4, i64 0)
store i8 1, i8* @open_cfgfile.searchPathsReady
br label %33
33: ; preds = %29, %1
store i32 0, i32* %5
br label %34
34: ; preds = %59, %33
%35 = load i32, i32* %5
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [5 x [256 x i8]], [5 x [256 x i8]]* @open_cfgfile.searchPaths, i64 0, i64 %36
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %37, i64 0, i64 0
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %60
42: ; preds = %34
%43 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%44 = load i32, i32* %5
%45 = add nsw i32 %44, 1
store i32 %45, i32* %5
%46 = sext i32 %44 to i64
%47 = getelementptr inbounds [5 x [256 x i8]], [5 x [256 x i8]]* @open_cfgfile.searchPaths, i64 0, i64 %46
%48 = getelementptr inbounds [256 x i8], [256 x i8]* %47, i64 0, i64 0
%49 = call i32 @strncpy(i8* %43, i8* %48, i32 256)
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%51 = load i8*, i8** %3
%52 = call i32 @strncat(i8* %50, i8* %51, i32 256)
%53 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%54 = call i32* @fopen(i8* %53, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
store i32* %54, i32** %4
%55 = load i32*, i32** %4
%56 = icmp ne i32* %55, null
br i1 %56, label %57, label %59
57: ; preds = %42
%58 = load i32*, i32** %4
store i32* %58, i32** %2
br label %61
59: ; preds = %42
br label %34
60: ; preds = %34
store i32* null, i32** %2
br label %61
61: ; preds = %60, %57
%62 = load i32*, i32** %2
ret i32* %62
}

LLVM IR (-O2):
@open_cfgfile.searchPaths = internal global [5 x [256 x i8]] zeroinitializer
@open_cfgfile.searchPathsReady = internal unnamed_addr global i1 false
@.str = [3 x i8] c"./\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [3 x i8] c"~/\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [13 x i8] c".avrxtool32/\00"
@.str.5 = [23 x i8] c"/usr/share/avrxtool32/\00"
@.str.6 = [17 x i8] c"/etc/avrxtool32/\00"
@.str.7 = [3 x i8] c"rt\00"
define noalias i32* @open_cfgfile(i8* %0) {
%2 = alloca [256 x i8]
%3 = load i1, i1* @open_cfgfile.searchPathsReady
br i1 %3, label %26, label %4
4: ; preds = %1
%5 = tail call i32 @strcpy(i8* getelementptr inbounds ([5 x [256 x i8]], [5 x [256 x i8]]* @open_cfgfile.searchPaths, i64 0, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%7 = tail call i32 @strncpy(i8* getelementptr inbounds ([5 x [256 x i8]], [5 x [256 x i8]]* @open_cfgfile.searchPaths, i64 0, i64 1, i64 0), i8* %6, i32 256)
store i8 0, i8* getelementptr inbounds ([5 x [256 x i8]], [5 x [256 x i8]]* @open_cfgfile.searchPaths, i64 0, i64 1, i64 255)
%8 = load i8, i8* getelementptr inbounds ([5 x [256 x i8]], [5 x [256 x i8]]* @open_cfgfile.searchPaths, i64 0, i64 1, i64 0)
%9 = icmp eq i8 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %4
%11 = tail call i64 @strlen(i8* dereferenceable(1) getelementptr inbounds ([5 x [256 x i8]], [5 x [256 x i8]]* @open_cfgfile.searchPaths, i64 0, i64 1, i64 0))
%12 = icmp ugt i64 %11, 150
br i1 %12, label %13, label %15
13: ; preds = %10, %4
%14 = tail call i32 @strcpy(i8* getelementptr inbounds ([5 x [256 x i8]], [5 x [256 x i8]]* @open_cfgfile.searchPaths, i64 0, i64 1, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
%16 = tail call i64 @strlen(i8* dereferenceable(1) getelementptr inbounds ([5 x [256 x i8]], [5 x [256 x i8]]* @open_cfgfile.searchPaths, i64 0, i64 1, i64 0))
%17 = getelementptr inbounds [5 x [256 x i8]], [5 x [256 x i8]]* @open_cfgfile.searchPaths, i64 0, i64 1, i64 %16
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 47
br i1 %19, label %22, label %20
20: ; preds = %15
%21 = tail call i32 @strncat(i8* getelementptr inbounds ([5 x [256 x i8]], [5 x [256 x i8]]* @open_cfgfile.searchPaths, i64 0, i64 1, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 256)
br label %22
22: ; preds = %20, %15
%23 = tail call i32 @strncat(i8* getelementptr inbounds ([5 x [256 x i8]], [5 x [256 x i8]]* @open_cfgfile.searchPaths, i64 0, i64 1, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 256)
%24 = tail call i32 @strcpy(i8* getelementptr inbounds ([5 x [256 x i8]], [5 x [256 x i8]]* @open_cfgfile.searchPaths, i64 0, i64 2, i64 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
%25 = tail call i32 @strcpy(i8* getelementptr inbounds ([5 x [256 x i8]], [5 x [256 x i8]]* @open_cfgfile.searchPaths, i64 0, i64 3, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0))
store i8 0, i8* getelementptr inbounds ([5 x [256 x i8]], [5 x [256 x i8]]* @open_cfgfile.searchPaths, i64 0, i64 4, i64 0)
store i1 true, i1* @open_cfgfile.searchPathsReady
br label %26
26: ; preds = %22, %1
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
br label %28
28: ; preds = %33, %26
%29 = phi i64 [ %34, %33 ], [ 0, %26 ]
%30 = getelementptr inbounds [5 x [256 x i8]], [5 x [256 x i8]]* @open_cfgfile.searchPaths, i64 0, i64 %29, i64 0
%31 = load i8, i8* %30
%32 = icmp eq i8 %31, 0
br i1 %32, label %39, label %33
33: ; preds = %28
%34 = add nuw i64 %29, 1
%35 = call i32 @strncpy(i8* %27, i8* %30, i32 256)
%36 = call i32 @strncat(i8* %27, i8* %0, i32 256)
%37 = call i32* @fopen(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
%38 = icmp eq i32* %37, null
br i1 %38, label %28, label %39
39: ; preds = %28, %33
%40 = phi i32* [ %37, %33 ], [ null, %28 ]
ret i32* %40
}