#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 */
int ENOSYS;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
scalar_t__ stat(char *, struct stat *);
int sysfs_cpu_exists(unsigned int cpu) {
  char file[255];
  struct stat statbuf;
  snprintf(file, 255,
           "/sys/devices/system/cpu/"
           "cpu%u/",
           cpu);
  if (stat(file, &statbuf) != 0)
    return -ENOSYS;
  return S_ISDIR(statbuf.st_mode) ? 0 : -ENOSYS;
}


x86 assembly (clang, -O0):
<sysfs_cpu_exists>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0xff,%esi
lea    0xeac(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x110(%rbp),%rdi
lea    -0x118(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
je     1189 <sysfs_cpu_exists+0x59>
xor    %eax,%eax
mov    0x2e69(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11c5 <sysfs_cpu_exists+0x95>
mov    -0x118(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%rax
je     11ab <sysfs_cpu_exists+0x7b>
xor    %eax,%eax
mov    %eax,-0x11c(%rbp)
jmp    11bc <sysfs_cpu_exists+0x8c>
xor    %eax,%eax
mov    0x2e34(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x11c(%rbp)
mov    -0x11c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_cpu_exists>:
push   %rbx
sub    $0x110,%rsp
mov    %edi,%ecx
lea    0xebf(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xff,%esi
call   1040 <snprintf@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <stat@plt>
test   %rax,%rax
je     1172 <sysfs_cpu_exists+0x42>
mov    0x2e7c(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
jmp    1191 <sysfs_cpu_exists+0x61>
mov    0x8(%rsp),%edi
call   1030 <S_ISDIR@plt>
mov    %rax,%rcx
mov    0x2e63(%rip),%rdx
xor    %eax,%eax
sub    (%rdx),%eax
xor    %edx,%edx
test   %rcx,%rcx
cmovne %edx,%eax
add    $0x110,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [31 x i8] c"/sys/devices/system/cpu/cpu%u/\00"
@ENOSYS = global i32 0
define i32 @sysfs_cpu_exists(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [255 x i8]
%5 = alloca %struct.stat
store i32 %0, i32* %3
%6 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* %3
%8 = call i32 @snprintf(i8* %6, i32 255, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%10 = call i64 @stat(i8* %9, %struct.stat* %5)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i32, i32* @ENOSYS
%14 = sub nsw i32 0, %13
store i32 %14, i32* %2
br label %26
15: ; preds = %1
%16 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i64 @S_ISDIR(i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %15
br label %24
21: ; preds = %15
%22 = load i32, i32* @ENOSYS
%23 = sub nsw i32 0, %22
br label %24
24: ; preds = %21, %20
%25 = phi i32 [ 0, %20 ], [ %23, %21 ]
store i32 %25, i32* %2
br label %26
26: ; preds = %24, %12
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [31 x i8] c"/sys/devices/system/cpu/cpu%u/\00"
@ENOSYS = global i32 0
define i32 @sysfs_cpu_exists(i32 %0) {
%2 = alloca [255 x i8]
%3 = alloca %struct.stat
%4 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 0
%5 = bitcast %struct.stat* %3 to i8*
%6 = call i32 @snprintf(i8* %4, i32 255, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = call i64 @stat(i8* %4, %struct.stat* %3)
%8 = icmp eq i64 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %1
%10 = load i32, i32* @ENOSYS
%11 = sub nsw i32 0, %10
br label %20
12: ; preds = %1
%13 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%14 = load i32, i32* %13
%15 = call i64 @S_ISDIR(i32 %14)
%16 = icmp eq i64 %15, 0
%17 = load i32, i32* @ENOSYS
%18 = sub nsw i32 0, %17
%19 = select i1 %16, i32 %18, i32 0
br label %20
20: ; preds = %12, %9
%21 = phi i32 [ %11, %9 ], [ %19, %12 ]
ret i32 %21
}