#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
scalar_t__ strlen(char *);
int file_check(char *path, char *fname, char *mode) {
  char *full_path;
  FILE *file = 0;
  int ok = 0;
  full_path = calloc(strlen(path) + strlen(fname) + 2, sizeof(char));
  sprintf(full_path, "%s/%s", path, fname);
  if ((file = fopen(full_path, mode)) != 0) {
    fclose(file);
    ok = 1;
  }
  free(full_path);
  return ok;
}


x86 assembly (clang, -O0):
<file_check>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%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,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe3b(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11f5 <file_check+0x95>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_check>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%rbp
call   1050 <strlen@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x2,%rdi
mov    $0x1,%r15d
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%rbx
lea    0xe5d(%rip),%rsi
mov    %rax,%rdi
mov    %rbp,%rdx
mov    %r12,%rcx
call   1080 <sprintf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11cb <file_check+0x6b>
mov    %rax,%rdi
call   1040 <fclose@plt>
jmp    11ce <file_check+0x6e>
xor    %r15d,%r15d
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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