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

/* Variables and functions */
int /*<<< orphan*/ R_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ getcwd(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
FILE *xyz_conf_open(char *file) {
  FILE *fp;
  char filepath[256];
  if (file == NULL) {
    return NULL;
  }
  if (file[0] == '/' || file[0] == '.') {
    fp = fopen(file, "r");
    if (fp == NULL) {
      return NULL;
    }
    return fp;
  }
  bzero(filepath, sizeof(filepath));
  snprintf(filepath, sizeof(filepath) - 1, "/etc/%s", file);
  if (access(filepath, R_OK) == 0) {
    fp = fopen(filepath, "r");
    if (fp == NULL) {
      return NULL;
    }
    return fp;
  }
  bzero(filepath, sizeof(filepath));
  snprintf(filepath, sizeof(filepath) - 1, "/usr/local/etc/%s", file);
  if (access(filepath, R_OK) == 0) {
    fp = fopen(filepath, "r");
    if (fp == NULL) {
      return NULL;
    }
    return fp;
  }
  bzero(filepath, sizeof(filepath));
  getcwd(filepath, sizeof(filepath) - 1);
  strncat(filepath, "/", sizeof(filepath) - strlen(filepath) - 1);
  strncat(filepath, file, sizeof(filepath) - strlen(filepath) - 1);
  if (access(filepath, R_OK) == 0) {
    fp = fopen(filepath, "r");
    if (fp == NULL) {
      return NULL;
    }
    return fp;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<xyz_conf_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1197 <xyz_conf_open+0x27>
movq   $0x0,-0x8(%rbp)
jmp    140c <xyz_conf_open+0x29c>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     11b7 <xyz_conf_open+0x47>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    11f0 <xyz_conf_open+0x80>
mov    -0x10(%rbp),%rdi
lea    0xe3e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e3 <xyz_conf_open+0x73>
movq   $0x0,-0x8(%rbp)
jmp    140c <xyz_conf_open+0x29c>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    140c <xyz_conf_open+0x29c>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   1090 <bzero@plt>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0xff,%esi
lea    0xdf4(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x120(%rbp),%rdi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
call   1070 <access@plt>
cmp    $0x0,%rax
jne    1278 <xyz_conf_open+0x108>
lea    -0x120(%rbp),%rdi
lea    0xdb6(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    126b <xyz_conf_open+0xfb>
movq   $0x0,-0x8(%rbp)
jmp    140c <xyz_conf_open+0x29c>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    140c <xyz_conf_open+0x29c>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   1090 <bzero@plt>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0xff,%esi
lea    0xd62(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x120(%rbp),%rdi
mov    0x2d2d(%rip),%rax
mov    (%rax),%esi
call   1070 <access@plt>
cmp    $0x0,%rax
jne    1300 <xyz_conf_open+0x190>
lea    -0x120(%rbp),%rdi
lea    0xd2e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    12f3 <xyz_conf_open+0x183>
movq   $0x0,-0x8(%rbp)
jmp    140c <xyz_conf_open+0x29c>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    140c <xyz_conf_open+0x29c>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   1090 <bzero@plt>
lea    -0x120(%rbp),%rdi
mov    $0xff,%esi
call   1050 <getcwd@plt>
lea    -0x120(%rbp),%rax
mov    %rax,-0x138(%rbp)
lea    -0x120(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x138(%rbp),%rdi
movslq %eax,%rcx
mov    $0x100,%eax
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%edx
lea    0xcb9(%rip),%rsi
call   1060 <strncat@plt>
lea    -0x120(%rbp),%rax
mov    %rax,-0x130(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x128(%rbp)
lea    -0x120(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x130(%rbp),%rdi
mov    -0x128(%rbp),%rsi
movslq %eax,%rcx
mov    $0x100,%eax
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%edx
call   1060 <strncat@plt>
lea    -0x120(%rbp),%rdi
mov    0x2c29(%rip),%rax
mov    (%rax),%esi
call   1070 <access@plt>
cmp    $0x0,%rax
jne    1404 <xyz_conf_open+0x294>
lea    -0x120(%rbp),%rdi
lea    0xc2a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    13f7 <xyz_conf_open+0x287>
movq   $0x0,-0x8(%rbp)
jmp    140c <xyz_conf_open+0x29c>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    140c <xyz_conf_open+0x29c>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xyz_conf_open>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
test   %rdi,%rdi
je     1290 <xyz_conf_open+0x120>
mov    %rdi,%rbx
mov    (%rdi),%al
and    $0xfe,%al
cmp    $0x2e,%al
jne    11ad <xyz_conf_open+0x3d>
lea    0xe68(%rip),%rsi
mov    %rbx,%rdi
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <fopen@plt>
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x100,%esi
call   1090 <bzero@plt>
lea    0xe48(%rip),%rdx
mov    %rbp,%rdi
mov    $0xff,%esi
mov    %rbx,%rcx
call   1040 <snprintf@plt>
mov    0x2e05(%rip),%r15
mov    (%r15),%esi
mov    %rbp,%rdi
call   1070 <access@plt>
test   %rax,%rax
je     1294 <xyz_conf_open+0x124>
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x100,%esi
call   1090 <bzero@plt>
lea    0xdfc(%rip),%rdx
mov    $0xff,%r14d
mov    %rbp,%rdi
mov    $0xff,%esi
mov    %rbx,%rcx
call   1040 <snprintf@plt>
mov    (%r15),%esi
mov    %rbp,%rdi
call   1070 <access@plt>
test   %rax,%rax
je     1294 <xyz_conf_open+0x124>
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x100,%esi
call   1090 <bzero@plt>
mov    %rbp,%rdi
mov    $0xff,%esi
call   1050 <getcwd@plt>
mov    %rbp,%rdi
call   1030 <strlen@plt>
mov    $0xff,%edx
sub    %eax,%edx
lea    0xdb5(%rip),%rsi
mov    %rbp,%rdi
call   1060 <strncat@plt>
mov    %rbp,%rdi
call   1030 <strlen@plt>
sub    %eax,%r14d
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1060 <strncat@plt>
mov    (%r15),%esi
mov    %rbp,%rdi
call   1070 <access@plt>
test   %rax,%rax
je     1294 <xyz_conf_open+0x124>
xor    %eax,%eax
jmp    12a3 <xyz_conf_open+0x133>
lea    0xd65(%rip),%rsi
mov    %rsp,%rdi
call   1080 <fopen@plt>
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [8 x i8] c"/etc/%s\00"
@R_OK = global i32 0
@.str.2 = [18 x i8] c"/usr/local/etc/%s\00"
@.str.3 = [2 x i8] c"/\00"
define i32* @xyz_conf_open(i8* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [256 x i8]
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %9
8: ; preds = %1
store i32* null, i32** %2
br label %100
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = getelementptr inbounds i8, i8* %10, i64 0
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %13, 47
br i1 %14, label %21, label %15
15: ; preds = %9
%16 = load i8*, i8** %3
%17 = getelementptr inbounds i8, i8* %16, i64 0
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 46
br i1 %20, label %21, label %29
21: ; preds = %15, %9
%22 = load i8*, i8** %3
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %23, i32** %4
%24 = load i32*, i32** %4
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %27
26: ; preds = %21
store i32* null, i32** %2
br label %100
27: ; preds = %21
%28 = load i32*, i32** %4
store i32* %28, i32** %2
br label %100
29: ; preds = %15
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%31 = call i32 @bzero(i8* %30, i32 256)
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%33 = load i8*, i8** %3
%34 = call i32 @snprintf(i8* %32, i32 255, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %33)
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%36 = load i32, i32* @R_OK
%37 = call i64 @access(i8* %35, i32 %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %47
39: ; preds = %29
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%41 = call i32* @fopen(i8* %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %41, i32** %4
%42 = load i32*, i32** %4
%43 = icmp eq i32* %42, null
br i1 %43, label %44, label %45
44: ; preds = %39
store i32* null, i32** %2
br label %100
45: ; preds = %39
%46 = load i32*, i32** %4
store i32* %46, i32** %2
br label %100
47: ; preds = %29
%48 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%49 = call i32 @bzero(i8* %48, i32 256)
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%51 = load i8*, i8** %3
%52 = call i32 @snprintf(i8* %50, i32 255, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %51)
%53 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%54 = load i32, i32* @R_OK
%55 = call i64 @access(i8* %53, i32 %54)
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %65
57: ; preds = %47
%58 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%59 = call i32* @fopen(i8* %58, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %59, i32** %4
%60 = load i32*, i32** %4
%61 = icmp eq i32* %60, null
br i1 %61, label %62, label %63
62: ; preds = %57
store i32* null, i32** %2
br label %100
63: ; preds = %57
%64 = load i32*, i32** %4
store i32* %64, i32** %2
br label %100
65: ; preds = %47
%66 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%67 = call i32 @bzero(i8* %66, i32 256)
%68 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%69 = call i32 @getcwd(i8* %68, i32 255)
%70 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%71 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%72 = call i32 @strlen(i8* %71)
%73 = sext i32 %72 to i64
%74 = sub i64 256, %73
%75 = sub i64 %74, 1
%76 = trunc i64 %75 to i32
%77 = call i32 @strncat(i8* %70, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %76)
%78 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%79 = load i8*, i8** %3
%80 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%81 = call i32 @strlen(i8* %80)
%82 = sext i32 %81 to i64
%83 = sub i64 256, %82
%84 = sub i64 %83, 1
%85 = trunc i64 %84 to i32
%86 = call i32 @strncat(i8* %78, i8* %79, i32 %85)
%87 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%88 = load i32, i32* @R_OK
%89 = call i64 @access(i8* %87, i32 %88)
%90 = icmp eq i64 %89, 0
br i1 %90, label %91, label %99
91: ; preds = %65
%92 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%93 = call i32* @fopen(i8* %92, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %93, i32** %4
%94 = load i32*, i32** %4
%95 = icmp eq i32* %94, null
br i1 %95, label %96, label %97
96: ; preds = %91
store i32* null, i32** %2
br label %100
97: ; preds = %91
%98 = load i32*, i32** %4
store i32* %98, i32** %2
br label %100
99: ; preds = %65
store i32* null, i32** %2
br label %100
100: ; preds = %99, %97, %96, %63, %62, %45, %44, %27, %26, %8
%101 = load i32*, i32** %2
ret i32* %101
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [8 x i8] c"/etc/%s\00"
@R_OK = global i32 0
@.str.2 = [18 x i8] c"/usr/local/etc/%s\00"
@.str.3 = [2 x i8] c"/\00"
define noalias i32* @xyz_conf_open(i8* %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = icmp eq i8* %0, null
br i1 %4, label %41, label %5
5: ; preds = %1
%6 = load i8, i8* %0
%7 = and i8 %6, -2
%8 = icmp eq i8 %7, 46
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %41
11: ; preds = %5
%12 = call i32 @bzero(i8* %3, i32 256)
%13 = call i32 @snprintf(i8* %3, i32 255, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%14 = load i32, i32* @R_OK
%15 = call i64 @access(i8* %3, i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %41
19: ; preds = %11
%20 = call i32 @bzero(i8* %3, i32 256)
%21 = call i32 @snprintf(i8* %3, i32 255, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%22 = load i32, i32* @R_OK
%23 = call i64 @access(i8* %3, i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %41
27: ; preds = %19
%28 = call i32 @bzero(i8* %3, i32 256)
%29 = call i32 @getcwd(i8* %3, i32 255)
%30 = call i32 @strlen(i8* %3)
%31 = sub i32 255, %30
%32 = call i32 @strncat(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %31)
%33 = call i32 @strlen(i8* %3)
%34 = sub i32 255, %33
%35 = call i32 @strncat(i8* %3, i8* %0, i32 %34)
%36 = load i32, i32* @R_OK
%37 = call i64 @access(i8* %3, i32 %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %27
%40 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %41
41: ; preds = %27, %39, %25, %17, %9, %1
%42 = phi i32* [ null, %1 ], [ %10, %9 ], [ %18, %17 ], [ %26, %25 ], [ %40, %39 ], [ null, %27 ]
ret i32* %42
}