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

/* Variables and functions */
int EINVAL;
int ENOBUFS;
int ENOSYS;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ assert(int);
int errno;
int /*<<< orphan*/ is_valid_control_type(char const *);
int snprintf_base_path(char *, int, char const *, int /*<<< orphan*/ const *,
                       int /*<<< orphan*/);
scalar_t__ stat(char *, struct stat *);
int powercap_sysfs_zone_exists(const char *control_type, const uint32_t *zones,
                               uint32_t depth) {
  char path[4096];
  struct stat ss;
  int w;
  if (!is_valid_control_type(control_type) || (depth && !zones)) {
    errno = EINVAL;
    return -errno;
  }
  if ((w = snprintf_base_path(path, sizeof(path), control_type, zones, depth)) <
      0) {
    assert(errno);
    return -1;
  }
  if ((size_t)w >= sizeof(path)) {
    errno = ENOBUFS;
    return -errno;
  }
  if (stat(path, &ss) || !S_ISDIR(ss.st_mode)) {
    errno = ENOSYS;
    return -errno;
  }
  return 0;
}


x86 assembly (clang, -O0):
<powercap_sysfs_zone_exists>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <is_valid_control_type@plt>
cmp    $0x0,%eax
je     118d <powercap_sysfs_zone_exists+0x3d>
cmpl   $0x0,-0x1c(%rbp)
je     11b2 <powercap_sysfs_zone_exists+0x62>
cmpq   $0x0,-0x18(%rbp)
jne    11b2 <powercap_sysfs_zone_exists+0x62>
mov    0x2e34(%rip),%rax
mov    (%rax),%ecx
mov    0x2e33(%rip),%rax
mov    %ecx,(%rax)
xor    %eax,%eax
mov    0x2e28(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    128d <powercap_sysfs_zone_exists+0x13d>
lea    -0x1020(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x1c(%rbp),%r8d
mov    $0x1000,%esi
call   1070 <snprintf_base_path@plt>
mov    %eax,-0x102c(%rbp)
cmp    $0x0,%eax
jge    11f8 <powercap_sysfs_zone_exists+0xa8>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   1040 <assert@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128d <powercap_sysfs_zone_exists+0x13d>
movslq -0x102c(%rbp),%rax
cmp    $0x1000,%rax
jb     1230 <powercap_sysfs_zone_exists+0xe0>
mov    0x2de6(%rip),%rax
mov    (%rax),%ecx
mov    0x2db5(%rip),%rax
mov    %ecx,(%rax)
xor    %eax,%eax
mov    0x2daa(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    128d <powercap_sysfs_zone_exists+0x13d>
lea    -0x1020(%rbp),%rdi
lea    -0x1028(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jne    1261 <powercap_sysfs_zone_exists+0x111>
mov    -0x1028(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1286 <powercap_sysfs_zone_exists+0x136>
mov    0x2d78(%rip),%rax
mov    (%rax),%ecx
mov    0x2d5f(%rip),%rax
mov    %ecx,(%rax)
xor    %eax,%eax
mov    0x2d54(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    128d <powercap_sysfs_zone_exists+0x13d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<powercap_sysfs_zone_exists>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1010,%rsp
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,%rbp
call   1060 <is_valid_control_type@plt>
test   %eax,%eax
je     1177 <powercap_sysfs_zone_exists+0x27>
test   %rbx,%rbx
jne    1197 <powercap_sysfs_zone_exists+0x47>
test   %r14d,%r14d
je     1197 <powercap_sysfs_zone_exists+0x47>
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    0x2e49(%rip),%rcx
mov    %eax,(%rcx)
neg    %eax
add    $0x1010,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0x10(%rsp),%rdi
mov    $0x1000,%esi
mov    %rbp,%rdx
mov    %rbx,%rcx
mov    %r14d,%r8d
call   1070 <snprintf_base_path@plt>
test   %eax,%eax
js     11c3 <powercap_sysfs_zone_exists+0x73>
cmp    $0x1000,%eax
jb     11d8 <powercap_sysfs_zone_exists+0x88>
mov    0x2e37(%rip),%rax
jmp    117e <powercap_sysfs_zone_exists+0x2e>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
call   1040 <assert@plt>
mov    $0xffffffff,%eax
jmp    118b <powercap_sysfs_zone_exists+0x3b>
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
test   %rax,%rax
jne    11fd <powercap_sysfs_zone_exists+0xad>
mov    0x8(%rsp),%edi
call   1030 <S_ISDIR@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jne    118b <powercap_sysfs_zone_exists+0x3b>
mov    0x2ddc(%rip),%rax
jmp    117e <powercap_sysfs_zone_exists+0x2e>

LLVM IR (-O0):
%struct.stat = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
@ENOBUFS = global i32 0
@ENOSYS = global i32 0
define i32 @powercap_sysfs_zone_exists(i8* %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca [4096 x i8]
%9 = alloca %struct.stat
%10 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%11 = load i8*, i8** %5
%12 = call i32 @is_valid_control_type(i8* %11)
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %3
%15 = load i32, i32* %7
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %14
%18 = load i32*, i32** %6
%19 = icmp ne i32* %18, null
br i1 %19, label %24, label %20
20: ; preds = %17, %3
%21 = load i32, i32* @EINVAL
store i32 %21, i32* @errno
%22 = load i32, i32* @errno
%23 = sub nsw i32 0, %22
store i32 %23, i32* %4
br label %56
24: ; preds = %17, %14
%25 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%26 = load i8*, i8** %5
%27 = load i32*, i32** %6
%28 = load i32, i32* %7
%29 = call i32 @snprintf_base_path(i8* %25, i32 4096, i8* %26, i32* %27, i32 %28)
store i32 %29, i32* %10
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %24
%32 = load i32, i32* @errno
%33 = call i32 @assert(i32 %32)
store i32 -1, i32* %4
br label %56
34: ; preds = %24
%35 = load i32, i32* %10
%36 = sext i32 %35 to i64
%37 = icmp uge i64 %36, 4096
br i1 %37, label %38, label %42
38: ; preds = %34
%39 = load i32, i32* @ENOBUFS
store i32 %39, i32* @errno
%40 = load i32, i32* @errno
%41 = sub nsw i32 0, %40
store i32 %41, i32* %4
br label %56
42: ; preds = %34
%43 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%44 = call i64 @stat(i8* %43, %struct.stat* %9)
%45 = icmp ne i64 %44, 0
br i1 %45, label %51, label %46
46: ; preds = %42
%47 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%48 = load i32, i32* %47
%49 = call i32 @S_ISDIR(i32 %48)
%50 = icmp ne i32 %49, 0
br i1 %50, label %55, label %51
51: ; preds = %46, %42
%52 = load i32, i32* @ENOSYS
store i32 %52, i32* @errno
%53 = load i32, i32* @errno
%54 = sub nsw i32 0, %53
store i32 %54, i32* %4
br label %56
55: ; preds = %46
store i32 0, i32* %4
br label %56
56: ; preds = %55, %51, %38, %31, %20
%57 = load i32, i32* %4
ret i32 %57
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
@ENOBUFS = global i32 0
@ENOSYS = global i32 0
define i32 @powercap_sysfs_zone_exists(i8* %0, i32* %1, i32 %2) {
%4 = alloca [4096 x i8]
%5 = alloca %struct.stat
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%7 = bitcast %struct.stat* %5 to i8*
%8 = tail call i32 @is_valid_control_type(i8* %0)
%9 = icmp eq i32 %8, 0
br i1 %9, label %14, label %10
10: ; preds = %3
%11 = icmp eq i32 %2, 0
%12 = icmp ne i32* %1, null
%13 = or i1 %12, %11
br i1 %13, label %17, label %14
14: ; preds = %10, %3
%15 = load i32, i32* @EINVAL
store i32 %15, i32* @errno
%16 = sub nsw i32 0, %15
br label %39
17: ; preds = %10
%18 = call i32 @snprintf_base_path(i8* %6, i32 4096, i8* %0, i32* %1, i32 %2)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i32, i32* @errno
%22 = call i32 @assert(i32 %21)
br label %39
23: ; preds = %17
%24 = icmp ugt i32 %18, 4095
br i1 %24, label %25, label %28
25: ; preds = %23
%26 = load i32, i32* @ENOBUFS
store i32 %26, i32* @errno
%27 = sub nsw i32 0, %26
br label %39
28: ; preds = %23
%29 = call i64 @stat(i8* %6, %struct.stat* %5)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @S_ISDIR(i32 %33)
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %31, %28
%37 = load i32, i32* @ENOSYS
store i32 %37, i32* @errno
%38 = sub nsw i32 0, %37
br label %39
39: ; preds = %31, %36, %25, %20, %14
%40 = phi i32 [ -1, %20 ], [ %27, %25 ], [ %38, %36 ], [ %16, %14 ], [ 0, %31 ]
ret i32 %40
}