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

/* Variables and functions */
scalar_t__ ENOENT;
scalar_t__ errno;
int /*<<< orphan*/ *fopen(char const *, char *);
char *getenv(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
FILE *open_config_file(const char *filepath) {
  FILE *f = NULL;
  if (filepath == NULL) {
    char cp[4096];
    char *env;
    if ((env = getenv("XDG_CONFIG_HOME")) != NULL && env[0] != '\0') {
      snprintf(cp, sizeof(cp), "%s/redshift.conf", env);
      filepath = cp;
    } else if ((env = getenv("HOME")) != NULL && env[0] != '\0') {
      snprintf(cp, sizeof(cp), "%s/.config/redshift.conf", env);
      filepath = cp;
    }
    if (filepath != NULL) {
      f = fopen(filepath, "r");
      if (f != NULL)
        return f;
      else if (f == NULL && errno != ENOENT)
        return NULL;
    }
  } else {
    f = fopen(filepath, "r");
    if (f == NULL) {
      perror("fopen");
      return NULL;
    }
  }
  return f;
}


x86 assembly (clang, -O0):
<open_config_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1298 <open_config_file+0x158>
lea    0xe97(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x1028(%rbp)
cmp    $0x0,%rax
je     11c1 <open_config_file+0x81>
mov    -0x1028(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11c1 <open_config_file+0x81>
lea    -0x1020(%rbp),%rdi
mov    -0x1028(%rbp),%rcx
mov    $0x1000,%esi
lea    0xe64(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x1020(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    1220 <open_config_file+0xe0>
lea    0xe43(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x1028(%rbp)
cmp    $0x0,%rax
je     121b <open_config_file+0xdb>
mov    -0x1028(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     121b <open_config_file+0xdb>
lea    -0x1020(%rbp),%rdi
mov    -0x1028(%rbp),%rcx
mov    $0x1000,%esi
lea    0xe16(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x1020(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    1220 <open_config_file+0xe0>
cmpq   $0x0,-0x10(%rbp)
je     1293 <open_config_file+0x153>
mov    -0x10(%rbp),%rdi
lea    0xe04(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1257 <open_config_file+0x117>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12dd <open_config_file+0x19d>
cmpq   $0x0,-0x18(%rbp)
jne    1289 <open_config_file+0x149>
mov    0x2d77(%rip),%rax
mov    (%rax),%rax
mov    0x2d5d(%rip),%rcx
cmp    (%rcx),%rax
je     1289 <open_config_file+0x149>
movq   $0x0,-0x8(%rbp)
jmp    12dd <open_config_file+0x19d>
jmp    128e <open_config_file+0x14e>
jmp    1293 <open_config_file+0x153>
jmp    12d5 <open_config_file+0x195>
mov    -0x10(%rbp),%rdi
lea    0xd97(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    12d0 <open_config_file+0x190>
lea    0xd7e(%rip),%rdi
call   1060 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    12dd <open_config_file+0x19d>
jmp    12d5 <open_config_file+0x195>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_config_file>:
sub    $0x1008,%rsp
test   %rdi,%rdi
je     1169 <open_config_file+0x29>
lea    0xee7(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11e1 <open_config_file+0xa1>
add    $0x1008,%rsp
ret
lea    0xe90(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1188 <open_config_file+0x48>
cmpb   $0x0,(%rax)
je     1188 <open_config_file+0x48>
lea    0xe8a(%rip),%rdx
jmp    11a5 <open_config_file+0x65>
lea    0xe7c(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11ed <open_config_file+0xad>
cmpb   $0x0,(%rax)
je     11ed <open_config_file+0xad>
lea    0xe7c(%rip),%rdx
mov    %rsp,%rdi
mov    $0x1000,%esi
mov    %rax,%rcx
call   1040 <snprintf@plt>
lea    0xe7e(%rip),%rsi
mov    %rsp,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
jne    1161 <open_config_file+0x21>
mov    0x2e10(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2df6(%rip),%rdx
cmp    (%rdx),%rcx
jne    1161 <open_config_file+0x21>
jmp    11ed <open_config_file+0xad>
lea    0xe54(%rip),%rdi
call   1060 <perror@plt>
xor    %eax,%eax
add    $0x1008,%rsp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"XDG_CONFIG_HOME\00"
@.str.1 = [17 x i8] c"%s/redshift.conf\00"
@.str.2 = [5 x i8] c"HOME\00"
@.str.3 = [25 x i8] c"%s/.config/redshift.conf\00"
@.str.4 = [2 x i8] c"r\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.5 = [6 x i8] c"fopen\00"
define i32* @open_config_file(i8* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [4096 x i8]
%6 = alloca i8*
store i8* %0, i8** %3
store i32* null, i32** %4
%7 = load i8*, i8** %3
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %59
9: ; preds = %1
%10 = call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i8* %10, i8** %6
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %23
12: ; preds = %9
%13 = load i8*, i8** %6
%14 = getelementptr inbounds i8, i8* %13, i64 0
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %12
%19 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%20 = load i8*, i8** %6
%21 = call i32 @snprintf(i8* %19, i32 4096, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
store i8* %22, i8** %3
br label %38
23: ; preds = %12, %9
%24 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i8* %24, i8** %6
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %37
26: ; preds = %23
%27 = load i8*, i8** %6
%28 = getelementptr inbounds i8, i8* %27, i64 0
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %26
%33 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%34 = load i8*, i8** %6
%35 = call i32 @snprintf(i8* %33, i32 4096, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %34)
%36 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
store i8* %36, i8** %3
br label %37
37: ; preds = %32, %26, %23
br label %38
38: ; preds = %37, %18
%39 = load i8*, i8** %3
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %58
41: ; preds = %38
%42 = load i8*, i8** %3
%43 = call i32* @fopen(i8* %42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, 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 %48
46: ; preds = %41
%47 = load i32*, i32** %4
store i32* %47, i32** %2
br label %69
48: ; preds = %41
%49 = load i32*, i32** %4
%50 = icmp eq i32* %49, null
br i1 %50, label %51, label %56
51: ; preds = %48
%52 = load i64, i64* @errno
%53 = load i64, i64* @ENOENT
%54 = icmp ne i64 %52, %53
br i1 %54, label %55, label %56
55: ; preds = %51
store i32* null, i32** %2
br label %69
56: ; preds = %51, %48
br label %57
57: ; preds = %56
br label %58
58: ; preds = %57, %38
br label %67
59: ; preds = %1
%60 = load i8*, i8** %3
%61 = call i32* @fopen(i8* %60, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %61, i32** %4
%62 = load i32*, i32** %4
%63 = icmp eq i32* %62, null
br i1 %63, label %64, label %66
64: ; preds = %59
%65 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
store i32* null, i32** %2
br label %69
66: ; preds = %59
br label %67
67: ; preds = %66, %58
%68 = load i32*, i32** %4
store i32* %68, i32** %2
br label %69
69: ; preds = %67, %64, %55, %46
%70 = load i32*, i32** %2
ret i32* %70
}

LLVM IR (-O2):
@.str = [16 x i8] c"XDG_CONFIG_HOME\00"
@.str.1 = [17 x i8] c"%s/redshift.conf\00"
@.str.2 = [5 x i8] c"HOME\00"
@.str.3 = [25 x i8] c"%s/.config/redshift.conf\00"
@.str.4 = [2 x i8] c"r\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.5 = [6 x i8] c"fopen\00"
define noalias i32* @open_config_file(i8* %0) {
%2 = alloca [4096 x i8]
%3 = icmp eq i8* %0, null
br i1 %3, label %4, label %30
4: ; preds = %1
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%6 = tail call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %13, label %8
8: ; preds = %4
%9 = load i8, i8* %6
%10 = icmp eq i8 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = call i32 @snprintf(i8* %5, i32 4096, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %6)
br label %21
13: ; preds = %8, %4
%14 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp eq i8* %14, null
br i1 %15, label %29, label %16
16: ; preds = %13
%17 = load i8, i8* %14
%18 = icmp eq i8 %17, 0
br i1 %18, label %29, label %19
19: ; preds = %16
%20 = call i32 @snprintf(i8* %5, i32 4096, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %14)
br label %21
21: ; preds = %11, %19
%22 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %28
24: ; preds = %21
%25 = load i64, i64* @errno
%26 = load i64, i64* @ENOENT
%27 = icmp eq i64 %25, %26
br i1 %27, label %29, label %28
28: ; preds = %21, %24
br label %35
29: ; preds = %13, %16, %24
br label %35
30: ; preds = %1
%31 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %35
35: ; preds = %30, %29, %28, %33
%36 = phi i32* [ null, %33 ], [ %22, %28 ], [ null, %29 ], [ %31, %30 ]
ret i32* %36
}