#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(scalar_t__, int);
int /*<<< orphan*/ *fopen(char *, char const *);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
scalar_t__ strlen(char *);
FILE *get_conf_stream(char *cname, const char *mode) {
  FILE *stream;
  char *str, *a;
  if (!(a = getenv("conf_dir")))
    return NULL;
  str = calloc(strlen(cname) + strlen(a) + 2, sizeof(char));
  sprintf(str, "%s/%s", a, cname);
  stream = fopen(str, mode);
  (free(str), (str) = NULL);
  return stream;
}


x86 assembly (clang, -O0):
<get_conf_stream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xe89(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1197 <get_conf_stream+0x37>
movq   $0x0,-0x8(%rbp)
jmp    1208 <get_conf_stream+0xa8>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x38(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe30(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_conf_stream>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe8b(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11da <get_conf_stream+0x7a>
mov    %rax,%rbx
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    (%r12,%rax,1),%rdi
add    $0x2,%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%r12
lea    0xe58(%rip),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
mov    %r15,%rcx
call   1080 <sprintf@plt>
mov    %r12,%rdi
mov    %r14,%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1040 <free@plt>
mov    %rbx,%rax
jmp    11dc <get_conf_stream+0x7c>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [9 x i8] c"conf_dir\00"
@.str.1 = [6 x i8] c"%s/%s\00"
define i32* @get_conf_stream(i8* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i8* %9, i8** %8
%10 = icmp ne i8* %9, null
br i1 %10, label %12, label %11
11: ; preds = %2
store i32* null, i32** %3
br label %30
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i64 @strlen(i8* %13)
%15 = load i8*, i8** %8
%16 = call i64 @strlen(i8* %15)
%17 = add nsw i64 %14, %16
%18 = add nsw i64 %17, 2
%19 = call i8* @calloc(i64 %18, i32 1)
store i8* %19, i8** %7
%20 = load i8*, i8** %7
%21 = load i8*, i8** %8
%22 = load i8*, i8** %4
%23 = call i32 @sprintf(i8* %20, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %21, i8* %22)
%24 = load i8*, i8** %7
%25 = load i8*, i8** %5
%26 = call i32* @fopen(i8* %24, i8* %25)
store i32* %26, i32** %6
%27 = load i8*, i8** %7
%28 = call i32 @free(i8* %27)
store i8* null, i8** %7
%29 = load i32*, i32** %6
store i32* %29, i32** %3
br label %30
30: ; preds = %12, %11
%31 = load i32*, i32** %3
ret i32* %31
}

LLVM IR (-O2):
@.str = [9 x i8] c"conf_dir\00"
@.str.1 = [6 x i8] c"%s/%s\00"
define noalias i32* @get_conf_stream(i8* %0, i8* nocapture %1) {
%3 = tail call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i8* %3, null
br i1 %4, label %14, label %5
5: ; preds = %2
%6 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%7 = tail call i64 @strlen(i8* %3)
%8 = add i64 %6, 2
%9 = add i64 %8, %7
%10 = tail call i8* @calloc(i64 %9, i32 1)
%11 = tail call i32 @sprintf(i8* dereferenceable(1) %10, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %3, i8* %0)
%12 = tail call i32* @fopen(i8* %10, i8* %1)
%13 = tail call i32 @free(i8* %10)
br label %14
14: ; preds = %2, %5
%15 = phi i32* [ %12, %5 ], [ null, %2 ]
ret i32* %15
}