#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 */
char *calloc(size_t, int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char *, char *);
int strlen(char *);
FILE *open_config_file(void) {
  char *homedir, *configfile;
  size_t len;
  FILE *fp;
  if (!(homedir = getenv("HOME")))
    homedir = "/home/user";
  len = strlen(homedir) +
        strlen("/.config/"
               "browser-switchboard") +
        1;
  if (!(configfile = calloc(len, sizeof(char))))
    return NULL;
  snprintf(configfile, len, "%s%s", homedir,
           "/.config/"
           "browser-switchboard");
  if (!(fp = fopen(configfile, "r"))) {
    snprintf(configfile, len, "%s%s", homedir,
             "/.config/"
             "browser-proxy");
    fp = fopen(configfile, "r");
  }
  free(configfile);
  return fp;
}


x86 assembly (clang, -O0):
<open_config_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xe91(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    118d <open_config_file+0x2d>
lea    0xe7c(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x2c(%rbp)
lea    0xe70(%rip),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11de <open_config_file+0x7e>
movq   $0x0,-0x8(%rbp)
jmp    125f <open_config_file+0xff>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rcx
lea    0xe3c(%rip),%rdx
lea    0xe18(%rip),%r8
call   1060 <snprintf@plt>
mov    -0x18(%rbp),%rdi
lea    0xe06(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    124e <open_config_file+0xee>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rcx
lea    0xdff(%rip),%rdx
lea    0xdfd(%rip),%r8
call   1060 <snprintf@plt>
mov    -0x18(%rbp),%rdi
lea    0xdc9(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_config_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xe91(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe87(%rip),%r12
cmovne %rax,%r12
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
lea    0xe7d(%rip),%rdi
call   1050 <strlen@plt>
add    %ebx,%eax
add    $0x1,%eax
movslq %eax,%r15
mov    %r15,%rdi
mov    $0x1,%esi
call   1070 <calloc@plt>
test   %rax,%rax
je     1220 <open_config_file+0xc0>
mov    %rax,%r14
lea    0xe71(%rip),%rdx
lea    0xe4d(%rip),%r8
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r12,%rcx
call   1060 <snprintf@plt>
lea    0xe36(%rip),%rsi
mov    %r14,%rdi
call   1080 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1216 <open_config_file+0xb6>
lea    0xe3e(%rip),%rdx
lea    0xe3c(%rip),%r8
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rcx
call   1060 <snprintf@plt>
lea    0xe03(%rip),%rsi
mov    %r14,%rdi
call   1080 <fopen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <free@plt>
jmp    1222 <open_config_file+0xc2>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [11 x i8] c"/home/user\00"
@.str.2 = [29 x i8] c"/.config/browser-switchboard\00"
@.str.3 = [5 x i8] c"%s%s\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [23 x i8] c"/.config/browser-proxy\00"
define i32* @open_config_file() {
%1 = alloca i32*
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32*
%6 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %6, i8** %2
%7 = icmp ne i8* %6, null
br i1 %7, label %9, label %8
8: ; preds = %0
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8** %2
br label %9
9: ; preds = %8, %0
%10 = load i8*, i8** %2
%11 = call i32 @strlen(i8* %10)
%12 = call i32 @strlen(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%13 = add nsw i32 %11, %12
%14 = add nsw i32 %13, 1
%15 = sext i32 %14 to i64
store i64 %15, i64* %4
%16 = load i64, i64* %4
%17 = call i8* @calloc(i64 %16, i32 1)
store i8* %17, i8** %3
%18 = icmp ne i8* %17, null
br i1 %18, label %20, label %19
19: ; preds = %9
store i32* null, i32** %1
br label %39
20: ; preds = %9
%21 = load i8*, i8** %3
%22 = load i64, i64* %4
%23 = load i8*, i8** %2
%24 = call i32 @snprintf(i8* %21, i64 %22, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %23, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%25 = load i8*, i8** %3
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %26, i32** %5
%27 = icmp ne i32* %26, null
br i1 %27, label %35, label %28
28: ; preds = %20
%29 = load i8*, i8** %3
%30 = load i64, i64* %4
%31 = load i8*, i8** %2
%32 = call i32 @snprintf(i8* %29, i64 %30, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %31, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
%33 = load i8*, i8** %3
%34 = call i32* @fopen(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %34, i32** %5
br label %35
35: ; preds = %28, %20
%36 = load i8*, i8** %3
%37 = call i32 @free(i8* %36)
%38 = load i32*, i32** %5
store i32* %38, i32** %1
br label %39
39: ; preds = %35, %19
%40 = load i32*, i32** %1
ret i32* %40
}

LLVM IR (-O2):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [11 x i8] c"/home/user\00"
@.str.2 = [29 x i8] c"/.config/browser-switchboard\00"
@.str.3 = [5 x i8] c"%s%s\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [23 x i8] c"/.config/browser-proxy\00"
define noalias i32* @open_config_file() {
%1 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i8* %1, null
%3 = select i1 %2, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %1
%4 = tail call i32 @strlen(i8* %3)
%5 = tail call i32 @strlen(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%6 = add i32 %4, 1
%7 = add i32 %6, %5
%8 = sext i32 %7 to i64
%9 = tail call i8* @calloc(i64 %8, i32 1)
%10 = icmp eq i8* %9, null
br i1 %10, label %21, label %11
11: ; preds = %0
%12 = tail call i32 @snprintf(i8* %9, i64 %8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %3, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%13 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = tail call i32 @snprintf(i8* %9, i64 %8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %3, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
%17 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %18
18: ; preds = %15, %11
%19 = phi i32* [ %13, %11 ], [ %17, %15 ]
%20 = tail call i32 @free(i8* %9)
br label %21
21: ; preds = %0, %18
%22 = phi i32* [ %19, %18 ], [ null, %0 ]
ret i32* %22
}