#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 /*<<< orphan*/ st_mode;
};

/* Variables and functions */
scalar_t__ ENOENT;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int stat(char *, struct stat *);
int check_dir_exist(char *dir_path) {
  struct stat st;
  int err = stat(dir_path, &st);
  if (-1 == err) {
    if (ENOENT == errno) {
      return 0;
    } else {
      perror("stat");
      exit(1);
    }
  } else {
    if (S_ISDIR(st.st_mode)) {
      return 1;
    } else {
      return 2;
    }
  }
}


x86 assembly (clang, -O0):
<check_dir_exist>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
mov    %eax,-0x1c(%rbp)
mov    $0xffffffff,%eax
cmp    -0x1c(%rbp),%eax
jne    11a6 <check_dir_exist+0x66>
mov    0x2e5f(%rip),%rax
mov    (%rax),%rax
mov    0x2e65(%rip),%rcx
cmp    (%rcx),%rax
jne    1190 <check_dir_exist+0x50>
movl   $0x0,-0x4(%rbp)
jmp    11cb <check_dir_exist+0x8b>
lea    0xe69(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%rax
je     11c4 <check_dir_exist+0x84>
movl   $0x1,-0x4(%rbp)
jmp    11cb <check_dir_exist+0x8b>
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_dir_exist>:
push   %rax
mov    %rsp,%rsi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
je     1164 <check_dir_exist+0x24>
mov    (%rsp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x1,%rax
mov    $0x1,%eax
adc    $0x0,%eax
jmp    117c <check_dir_exist+0x3c>
mov    0x2e65(%rip),%rax
mov    (%rax),%rcx
mov    0x2e6b(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
jne    117e <check_dir_exist+0x3e>
pop    %rcx
ret
lea    0xe7b(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@ENOENT = global i64 0
@errno = global i64 0
@.str = [5 x i8] c"stat\00"
define i32 @check_dir_exist(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @stat(i8* %6, %struct.stat* %4)
store i32 %7, i32* %5
%8 = load i32, i32* %5
%9 = icmp eq i32 -1, %8
br i1 %9, label %10, label %18
10: ; preds = %1
%11 = load i64, i64* @ENOENT
%12 = load i64, i64* @errno
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %15
14: ; preds = %10
store i32 0, i32* %2
br label %25
15: ; preds = %10
%16 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %1
%19 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i64 @S_ISDIR(i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %18
store i32 1, i32* %2
br label %25
24: ; preds = %18
store i32 2, i32* %2
br label %25
25: ; preds = %24, %23, %14
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@ENOENT = global i64 0
@errno = global i64 0
@.str = [5 x i8] c"stat\00"
define i32 @check_dir_exist(i8* nocapture %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i32 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %13
6: ; preds = %1
%7 = load i64, i64* @ENOENT
%8 = load i64, i64* @errno
%9 = icmp eq i64 %7, %8
br i1 %9, label %19, label %10
10: ; preds = %6
%11 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %1
%14 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%15 = load i32, i32* %14
%16 = tail call i64 @S_ISDIR(i32 %15)
%17 = icmp eq i64 %16, 0
%18 = select i1 %17, i32 2, i32 1
br label %19
19: ; preds = %13, %6
%20 = phi i32 [ 0, %6 ], [ %18, %13 ]
ret i32 %20
}