#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 dummy;
};
typedef int /*<<< orphan*/ newpath;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int MAXPATHLEN;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *, int *);
int /*<<< orphan*/ lstat(char *, struct stat *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int sysfs_is_dev(char *path, int *maj, int *min) {
  char newpath[MAXPATHLEN];
  struct stat sb;
  FILE *f;
  snprintf(newpath, sizeof(newpath), "%s/dev", path);
  if (!lstat(newpath, &sb)) {
    f = fopen(newpath, "r");
    if (f) {
      fscanf(f, "%d:%d", maj, min);
      fclose(f);
      return 1;
    } else
      return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<sysfs_is_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x50(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xe63(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x50(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1060 <lstat@plt>
cmp    $0x0,%eax
jne    121e <sysfs_is_dev+0xce>
mov    -0x50(%rbp),%rdi
lea    0xe44(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     120b <sysfs_is_dev+0xbb>
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xe1f(%rip),%rsi
call   1050 <fscanf@plt>
mov    -0x40(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    122c <sysfs_is_dev+0xdc>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    122c <sysfs_is_dev+0xdc>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_is_dev>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rcx
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe79(%rip),%rdx
mov    %rbx,%rdi
call   1040 <snprintf@plt>
lea    -0x20(%rbp),%rsi
mov    %rbx,%rdi
call   1060 <lstat@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jne    11e3 <sysfs_is_dev+0x93>
lea    0xe5d(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11de <sysfs_is_dev+0x8e>
mov    %rax,%rbx
lea    0xe48(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
call   1050 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    $0x1,%eax
jmp    11e3 <sysfs_is_dev+0x93>
mov    $0xffffffff,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@MAXPATHLEN = global i32 0
@.str = [7 x i8] c"%s/dev\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"%d:%d\00"
define i32 @sysfs_is_dev(i8* %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca %struct.stat
%11 = alloca i32*
%12 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
%13 = load i32, i32* @MAXPATHLEN
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %8
%16 = alloca i8, i64 %14
store i64 %14, i64* %9
%17 = trunc i64 %14 to i32
%18 = load i8*, i8** %5
%19 = call i32 @snprintf(i8* %16, i32 %17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = call i32 @lstat(i8* %16, %struct.stat* %10)
%21 = icmp ne i32 %20, 0
br i1 %21, label %34, label %22
22: ; preds = %3
%23 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %23, i32** %11
%24 = load i32*, i32** %11
%25 = icmp ne i32* %24, null
br i1 %25, label %26, label %33
26: ; preds = %22
%27 = load i32*, i32** %11
%28 = load i32*, i32** %6
%29 = load i32*, i32** %7
%30 = call i32 @fscanf(i32* %27, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32* %28, i32* %29)
%31 = load i32*, i32** %11
%32 = call i32 @fclose(i32* %31)
store i32 1, i32* %4
store i32 1, i32* %12
br label %35
33: ; preds = %22
store i32 -1, i32* %4
store i32 1, i32* %12
br label %35
34: ; preds = %3
store i32 0, i32* %4
store i32 1, i32* %12
br label %35
35: ; preds = %34, %33, %26
%36 = load i8*, i8** %8
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@MAXPATHLEN = global i32 0
@.str = [7 x i8] c"%s/dev\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"%d:%d\00"
define i32 @sysfs_is_dev(i8* %0, i32* %1, i32* %2) {
%4 = alloca %struct.stat
%5 = load i32, i32* @MAXPATHLEN
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = bitcast %struct.stat* %4 to i8*
%9 = call i32 @snprintf(i8* %7, i32 %5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %0)
%10 = call i32 @lstat(i8* %7, %struct.stat* %4)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %3
%13 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = call i32 @fscanf(i32* %13, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32* %1, i32* %2)
%17 = call i32 @fclose(i32* %13)
br label %18
18: ; preds = %3, %12, %15
%19 = phi i32 [ 1, %15 ], [ -1, %12 ], [ 0, %3 ]
ret i32 %19
}