#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 */
struct stat {
  int st_mode;
};
typedef int mode_t;

/* Variables and functions */
int /*<<< orphan*/ free(struct stat *);
int lstat(char *, struct stat *);
scalar_t__ malloc(int);
int check_exists(char *path, mode_t mask) {
  struct stat *statbuf;
  int flag = 0;
  statbuf = (struct stat *)malloc(sizeof(struct stat));
  flag = lstat(path, statbuf);
  if (flag == -1) {
    flag = -1;
  } else {
    if ((statbuf->st_mode & mask) == mask) {
      flag = 1;
    } else {
      flag = 0;
    }
  }
  free(statbuf);
  return flag;
}


x86 assembly (clang, -O0):
<check_exists>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    $0x4,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <lstat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    117a <check_exists+0x4a>
movl   $0xffffffff,-0x1c(%rbp)
jmp    11a4 <check_exists+0x74>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
and    -0xc(%rbp),%eax
cmp    -0xc(%rbp),%eax
jne    1198 <check_exists+0x68>
movl   $0x1,-0x1c(%rbp)
jmp    119f <check_exists+0x6f>
movl   $0x0,-0x1c(%rbp)
jmp    11a4 <check_exists+0x74>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_exists>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbp
mov    $0x4,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    %rax,%rsi
call   1040 <lstat@plt>
cmp    $0xffffffff,%eax
je     1166 <check_exists+0x36>
mov    (%rbx),%eax
not    %eax
xor    %ebp,%ebp
test   %eax,%r14d
sete   %bpl
jmp    116b <check_exists+0x3b>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
define i32 @check_exists(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.stat*
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %6
%7 = call i64 @malloc(i32 4)
%8 = inttoptr i64 %7 to %struct.stat*
store %struct.stat* %8, %struct.stat** %5
%9 = load i8*, i8** %3
%10 = load %struct.stat*, %struct.stat** %5
%11 = call i32 @lstat(i8* %9, %struct.stat* %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 -1, i32* %6
br label %26
15: ; preds = %2
%16 = load %struct.stat*, %struct.stat** %5
%17 = getelementptr inbounds %struct.stat, %struct.stat* %16, i32 0, i32 0
%18 = load i32, i32* %17
%19 = load i32, i32* %4
%20 = and i32 %18, %19
%21 = load i32, i32* %4
%22 = icmp eq i32 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %15
store i32 1, i32* %6
br label %25
24: ; preds = %15
store i32 0, i32* %6
br label %25
25: ; preds = %24, %23
br label %26
26: ; preds = %25, %14
%27 = load %struct.stat*, %struct.stat** %5
%28 = call i32 @free(%struct.stat* %27)
%29 = load i32, i32* %6
ret i32 %29
}

LLVM IR (-O2):
%struct.stat = type { i32 }
define i32 @check_exists(i8* nocapture %0, i32 %1) {
%3 = tail call i64 @malloc(i32 4)
%4 = inttoptr i64 %3 to %struct.stat*
%5 = tail call i32 @lstat(i8* %0, %struct.stat* %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %13, label %7
7: ; preds = %2
%8 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%9 = load i32, i32* %8
%10 = and i32 %9, %1
%11 = icmp eq i32 %10, %1
%12 = zext i1 %11 to i32
br label %13
13: ; preds = %7, %2
%14 = phi i32 [ -1, %2 ], [ %12, %7 ]
%15 = tail call i32 @free(%struct.stat* %4)
ret i32 %14
}