#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*/ path;

/* Variables and functions */
int /*<<< orphan*/ F_OK;
int PATH_MAX;
int S_IFBLK;
int S_IFCHR;
int /*<<< orphan*/ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int devtype(const char *majmin) {
  char path[PATH_MAX];
  snprintf(path, sizeof(path), "/sys/dev/block/%s", majmin);
  if (!access(path, F_OK))
    return S_IFBLK;
  snprintf(path, sizeof(path), "/sys/dev/char/%s", majmin);
  if (!access(path, F_OK))
    return S_IFCHR;
  return -1;
}


x86 assembly (clang, -O0):
<devtype>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2ebd(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x38(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x30(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xe97(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x30(%rbp),%rdi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
call   1040 <access@plt>
cmp    $0x0,%eax
jne    11a1 <devtype+0x81>
mov    0x2e48(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    11fc <devtype+0xdc>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xe5c(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x30(%rbp),%rdi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
call   1040 <access@plt>
cmp    $0x0,%eax
jne    11ee <devtype+0xce>
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    11fc <devtype+0xdc>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<devtype>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
mov    0x2ebb(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%rbx
mov    %r15,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xead(%rip),%rdx
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r14,%rcx
call   1030 <snprintf@plt>
mov    0x2e58(%rip),%r12
mov    (%r12),%esi
mov    %rbx,%rdi
call   1040 <access@plt>
test   %eax,%eax
je     11a8 <devtype+0x88>
lea    0xe93(%rip),%rdx
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r14,%rcx
call   1030 <snprintf@plt>
mov    (%r12),%esi
mov    %rbx,%rdi
call   1040 <access@plt>
mov    0x2e30(%rip),%rcx
neg    %eax
sbb    %eax,%eax
or     (%rcx),%eax
jmp    11b1 <devtype+0x91>
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [18 x i8] c"/sys/dev/block/%s\00"
@F_OK = global i32 0
@S_IFBLK = global i32 0
@.str.1 = [17 x i8] c"/sys/dev/char/%s\00"
@S_IFCHR = global i32 0
define i32 @devtype(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i32, i32* @PATH_MAX
%8 = zext i32 %7 to i64
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %4
%10 = alloca i8, i64 %8
store i64 %8, i64* %5
%11 = trunc i64 %8 to i32
%12 = load i8*, i8** %3
%13 = call i32 @snprintf(i8* %10, i32 %11, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = load i32, i32* @F_OK
%15 = call i32 @access(i8* %10, i32 %14)
%16 = icmp ne i32 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %1
%18 = load i32, i32* @S_IFBLK
store i32 %18, i32* %2
store i32 1, i32* %6
br label %29
19: ; preds = %1
%20 = trunc i64 %8 to i32
%21 = load i8*, i8** %3
%22 = call i32 @snprintf(i8* %10, i32 %20, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = load i32, i32* @F_OK
%24 = call i32 @access(i8* %10, i32 %23)
%25 = icmp ne i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %19
%27 = load i32, i32* @S_IFCHR
store i32 %27, i32* %2
store i32 1, i32* %6
br label %29
28: ; preds = %19
store i32 -1, i32* %2
store i32 1, i32* %6
br label %29
29: ; preds = %28, %26, %17
%30 = load i8*, i8** %4
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [18 x i8] c"/sys/dev/block/%s\00"
@F_OK = global i32 0
@S_IFBLK = global i32 0
@.str.1 = [17 x i8] c"/sys/dev/char/%s\00"
@S_IFCHR = global i32 0
define i32 @devtype(i8* %0) {
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = call i32 @snprintf(i8* %4, i32 %2, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %0)
%6 = load i32, i32* @F_OK
%7 = call i32 @access(i8* %4, i32 %6)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = load i32, i32* @S_IFBLK
br label %18
11: ; preds = %1
%12 = call i32 @snprintf(i8* %4, i32 %2, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%13 = load i32, i32* @F_OK
%14 = call i32 @access(i8* %4, i32 %13)
%15 = icmp eq i32 %14, 0
%16 = load i32, i32* @S_IFCHR
%17 = select i1 %15, i32 %16, i32 -1
br label %18
18: ; preds = %11, %9
%19 = phi i32 [ %10, %9 ], [ %17, %11 ]
ret i32 %19
}