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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ is_dir(char *);
int /*<<< orphan*/ *opendir(char *);
int exist_elem(char *path) {
  if (is_dir(path)) {
    DIR *dir = opendir(path);
    if (dir == NULL)
      return 0;
    else {
      closedir(dir);
      return 1;
    }
  } else {
    FILE *file = fopen(path, "r");
    if (file == NULL)
      return 0;
    else {
      fclose(file);
      return 1;
    }
  }
}


x86 assembly (clang, -O0):
<exist_elem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <is_dir@plt>
cmp    $0x0,%rax
je     11a8 <exist_elem+0x58>
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1193 <exist_elem+0x43>
movl   $0x0,-0x4(%rbp)
jmp    11e3 <exist_elem+0x93>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e3 <exist_elem+0x93>
mov    -0x10(%rbp),%rdi
lea    0xe4d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d3 <exist_elem+0x83>
movl   $0x0,-0x4(%rbp)
jmp    11e3 <exist_elem+0x93>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exist_elem>:
push   %rbx
mov    %rdi,%rbx
call   1050 <is_dir@plt>
test   %rax,%rax
je     1175 <exist_elem+0x25>
mov    %rbx,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1198 <exist_elem+0x48>
mov    %rax,%rdi
call   1060 <closedir@plt>
jmp    1191 <exist_elem+0x41>
lea    0xe84(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1198 <exist_elem+0x48>
mov    %rax,%rdi
call   1030 <fclose@plt>
mov    $0x1,%eax
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i32 @exist_elem(i8* %0) {
%2 = tail call i64 @is_dir(i8* %0)
%3 = icmp eq i64 %2, 0
br i1 %3, label %9, label %4
4: ; preds = %1
%5 = tail call i32* @opendir(i8* %0)
%6 = icmp eq i32* %5, null
br i1 %6, label %14, label %7
7: ; preds = %4
%8 = tail call i32 @closedir(i32* %5)
br label %14
9: ; preds = %1
%10 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = tail call i32 @fclose(i32* %10)
br label %14
14: ; preds = %12, %9, %7, %4
%15 = phi i32 [ 1, %7 ], [ 0, %4 ], [ 1, %12 ], [ 0, %9 ]
ret i32 %15
}