#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;
};

/* Variables and functions */
int S_IFBLK;
int S_IFCHR;
int S_IFDIR;
int S_IFIFO;
int S_IFREG;
int S_IFSOCK;
scalar_t__ errno;
int /*<<< orphan*/ free(struct stat *);
scalar_t__ malloc(int);
int stat(char *, struct stat *);
int is_valid_file(char *path) {
  struct stat *buf;
  int chk;
  buf = (struct stat *)malloc(sizeof(struct stat));
  if (buf == NULL) {
    free(buf);
    return -1;
  }
  errno = 0;
  chk = stat(path, buf);
  if (errno != 0) {
    if (chk == 0) {
      free(buf);
      return -1;
    } else {
      free(buf);
      return 0;
    }
  } else {
    free(buf);
    return -1;
  }
  if (buf->st_mode & S_IFDIR) {
    free(buf);
    return 0;
  } else {
    free(buf);
    return -1;
  }
  if (buf->st_mode & S_IFREG) {
    free(buf);
    return 0;
  } else if (buf->st_mode & S_IFCHR) {
    free(buf);
    return -1;
  } else if (buf->st_mode & S_IFBLK) {
    free(buf);
    return -1;
  } else if (buf->st_mode & S_IFIFO) {
    free(buf);
    return -1;
  } else if (buf->st_mode & S_IFSOCK) {
    free(buf);
    return -1;
  } else if (buf->st_mode & S_IFDIR) {
    free(buf);
    return -1;
  } else {
    free(buf);
    return -1;
  }
}


x86 assembly (clang, -O0):
<is_valid_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x4,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    116a <is_valid_file+0x3a>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dd <is_valid_file+0xad>
mov    0x2e6f(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <stat@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e51(%rip),%rax
cmpq   $0x0,(%rax)
je     11cd <is_valid_file+0x9d>
cmpl   $0x0,-0x1c(%rbp)
jne    11b8 <is_valid_file+0x88>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dd <is_valid_file+0xad>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    11dd <is_valid_file+0xad>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_valid_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x4,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     117c <is_valid_file+0x4c>
mov    %rax,%rbx
mov    0x2e8d(%rip),%r15
movq   $0x0,(%r15)
mov    %r14,%rdi
mov    %rax,%rsi
call   1040 <stat@plt>
cmpq   $0x0,(%r15)
je     1180 <is_valid_file+0x50>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
cmp    $0x1,%ebp
sbb    %eax,%eax
jmp    118d <is_valid_file+0x5d>
xor    %edi,%edi
jmp    1183 <is_valid_file+0x53>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@S_IFBLK = global i32 0
@S_IFCHR = global i32 0
@S_IFDIR = global i32 0
@S_IFIFO = global i32 0
@S_IFREG = global i32 0
@S_IFSOCK = global i32 0
define i32 @is_valid_file(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat*
%5 = alloca i32
store i8* %0, i8** %3
%6 = call i64 @malloc(i32 4)
%7 = inttoptr i64 %6 to %struct.stat*
store %struct.stat* %7, %struct.stat** %4
%8 = load %struct.stat*, %struct.stat** %4
%9 = icmp eq %struct.stat* %8, null
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load %struct.stat*, %struct.stat** %4
%12 = call i32 @free(%struct.stat* %11)
store i32 -1, i32* %2
br label %31
13: ; preds = %1
store i64 0, i64* @errno
%14 = load i8*, i8** %3
%15 = load %struct.stat*, %struct.stat** %4
%16 = call i32 @stat(i8* %14, %struct.stat* %15)
store i32 %16, i32* %5
%17 = load i64, i64* @errno
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %13
%20 = load i32, i32* %5
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load %struct.stat*, %struct.stat** %4
%24 = call i32 @free(%struct.stat* %23)
store i32 -1, i32* %2
br label %31
25: ; preds = %19
%26 = load %struct.stat*, %struct.stat** %4
%27 = call i32 @free(%struct.stat* %26)
store i32 0, i32* %2
br label %31
28: ; preds = %13
%29 = load %struct.stat*, %struct.stat** %4
%30 = call i32 @free(%struct.stat* %29)
store i32 -1, i32* %2
br label %31
31: ; preds = %28, %25, %22, %10
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@S_IFBLK = global i32 0
@S_IFCHR = global i32 0
@S_IFDIR = global i32 0
@S_IFIFO = global i32 0
@S_IFREG = global i32 0
@S_IFSOCK = global i32 0
define i32 @is_valid_file(i8* nocapture %0) {
%2 = tail call i64 @malloc(i32 4)
%3 = inttoptr i64 %2 to %struct.stat*
%4 = icmp eq i64 %2, 0
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @free(%struct.stat* null)
br label %17
7: ; preds = %1
store i64 0, i64* @errno
%8 = tail call i32 @stat(i8* %0, %struct.stat* %3)
%9 = load i64, i64* @errno
%10 = icmp eq i64 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %7
%12 = icmp eq i32 %8, 0
%13 = tail call i32 @free(%struct.stat* %3)
%14 = sext i1 %12 to i32
br label %17
15: ; preds = %7
%16 = tail call i32 @free(%struct.stat* %3)
br label %17
17: ; preds = %11, %15, %5
%18 = phi i32 [ -1, %5 ], [ -1, %15 ], [ %14, %11 ]
ret i32 %18
}