#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*/ sprintf(char *, char *, char *, char *);
scalar_t__ strlen(char *);
FILE *EnvFileOpen(char *fname, char *env) {
  FILE *fp;
  char *path;
  char full[512];
  if (env == NULL)
    return NULL;
  if ((path = getenv(env)) == NULL)
    return NULL;
  if (((int)strlen(fname) + (int)strlen(env) + 2) > 512)
    return NULL;
  sprintf(full, "%s/%s", path, fname);
  fp = fopen(full, "r");
  return fp;
}


x86 assembly (clang, -O0):
<EnvFileOpen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    116b <EnvFileOpen+0x2b>
movq   $0x0,-0x8(%rbp)
jmp    1207 <EnvFileOpen+0xc7>
mov    -0x18(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    118f <EnvFileOpen+0x4f>
movq   $0x0,-0x8(%rbp)
jmp    1207 <EnvFileOpen+0xc7>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x234(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x234(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
cmp    $0x200,%eax
jle    11cd <EnvFileOpen+0x8d>
movq   $0x0,-0x8(%rbp)
jmp    1207 <EnvFileOpen+0xc7>
lea    -0x230(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe1d(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x230(%rbp),%rdi
lea    0xe10(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<EnvFileOpen>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x208,%rsp
test   %rsi,%rsi
je     1186 <EnvFileOpen+0x46>
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1186 <EnvFileOpen+0x46>
mov    %rax,%r15
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1040 <strlen@plt>
add    %r12d,%eax
cmp    $0x1fe,%eax
jle    118a <EnvFileOpen+0x4a>
xor    %eax,%eax
jmp    11b1 <EnvFileOpen+0x71>
lea    0xe6f(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
call   1060 <sprintf@plt>
lea    0xe5d(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [2 x i8] c"r\00"
define noalias i32* @EnvFileOpen(i8* %0, i8* %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = icmp eq i8* %1, null
br i1 %5, label %19, label %6
6: ; preds = %2
%7 = tail call i8* @getenv(i8* %1)
%8 = icmp eq i8* %7, null
br i1 %8, label %19, label %9
9: ; preds = %6
%10 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%11 = trunc i64 %10 to i32
%12 = tail call i64 @strlen(i8* %1)
%13 = trunc i64 %12 to i32
%14 = add nsw i32 %13, %11
%15 = icmp sgt i32 %14, 510
br i1 %15, label %19, label %16
16: ; preds = %9
%17 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %7, i8* %0)
%18 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %9, %6, %2, %16
%20 = phi i32* [ %18, %16 ], [ null, %2 ], [ null, %6 ], [ null, %9 ]
ret i32* %20
}