#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 EOF;
int MAXPATHLEN;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int 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) {
      int err;
      err = fscanf(f, "%d:%d", maj, min);
      fclose(f);
      if ((err == EOF) || (err != 2))
        return -1;
      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    0x2e75(%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    1250 <sysfs_is_dev+0x100>
mov    -0x50(%rbp),%rdi
lea    0xe44(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     123d <sysfs_is_dev+0xed>
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xe1f(%rip),%rsi
call   1050 <fscanf@plt>
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x44(%rbp),%eax
mov    0x2de3(%rip),%rcx
cmp    (%rcx),%eax
je     1217 <sysfs_is_dev+0xc7>
cmpl   $0x2,-0x44(%rbp)
je     122a <sysfs_is_dev+0xda>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    125e <sysfs_is_dev+0x10e>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    125e <sysfs_is_dev+0x10e>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    125e <sysfs_is_dev+0x10e>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x48(%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    0x2e76(%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    1200 <sysfs_is_dev+0xb0>
lea    0xe5d(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11fb <sysfs_is_dev+0xab>
mov    %rax,%rbx
lea    0xe48(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
call   1050 <fscanf@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x2e07(%rip),%rax
xor    %ecx,%ecx
cmpl   $0x2,(%rax)
setne  %cl
cmp    $0x2,%r14d
lea    -0x1(%rcx,%rcx,1),%ecx
mov    $0xffffffff,%eax
cmove  %ecx,%eax
jmp    1200 <sysfs_is_dev+0xb0>
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"
@EOF = global i32 0
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
%13 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
%14 = load i32, i32* @MAXPATHLEN
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %8
%17 = alloca i8, i64 %15
store i64 %15, i64* %9
%18 = trunc i64 %15 to i32
%19 = load i8*, i8** %5
%20 = call i32 @snprintf(i8* %17, i32 %18, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = call i32 @lstat(i8* %17, %struct.stat* %10)
%22 = icmp ne i32 %21, 0
br i1 %22, label %43, label %23
23: ; preds = %3
%24 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %24, i32** %11
%25 = load i32*, i32** %11
%26 = icmp ne i32* %25, null
br i1 %26, label %27, label %42
27: ; preds = %23
%28 = load i32*, i32** %11
%29 = load i32*, i32** %6
%30 = load i32*, i32** %7
%31 = call i32 @fscanf(i32* %28, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32* %29, i32* %30)
store i32 %31, i32* %12
%32 = load i32*, i32** %11
%33 = call i32 @fclose(i32* %32)
%34 = load i32, i32* %12
%35 = load i32, i32* @EOF
%36 = icmp eq i32 %34, %35
br i1 %36, label %40, label %37
37: ; preds = %27
%38 = load i32, i32* %12
%39 = icmp ne i32 %38, 2
br i1 %39, label %40, label %41
40: ; preds = %37, %27
store i32 -1, i32* %4
store i32 1, i32* %13
br label %44
41: ; preds = %37
store i32 1, i32* %4
store i32 1, i32* %13
br label %44
42: ; preds = %23
store i32 -1, i32* %4
store i32 1, i32* %13
br label %44
43: ; preds = %3
store i32 0, i32* %4
store i32 1, i32* %13
br label %44
44: ; preds = %43, %42, %41, %40
%45 = load i8*, i8** %8
%46 = load i32, i32* %4
ret i32 %46
}

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"
@EOF = global i32 0
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 %23
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 %23, 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)
%18 = load i32, i32* @EOF
%19 = icmp ne i32 %16, 2
%20 = icmp eq i32 %18, 2
%21 = or i1 %19, %20
%22 = select i1 %21, i32 -1, i32 1
br label %23
23: ; preds = %3, %12, %15
%24 = phi i32 [ %22, %15 ], [ -1, %12 ], [ 0, %3 ]
ret i32 %24
}