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

/* Variables and functions */
int /*<<< orphan*/ free(char *);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
scalar_t__ util_file_read_line(char *, int, char *, char *);
int /*<<< orphan*/ util_path_is_dir(char *);
char *util_path_sysfs(char *, unsigned int);
int sysfs_get_card_level(unsigned int card) {
  char *dev_path;
  char type[20];
  int rc;
  dev_path = util_path_sysfs("bus/ap/devices/card%02x", card);
  if (!util_path_is_dir(dev_path)) {
    rc = -1;
    goto out;
  }
  if (util_file_read_line(type, sizeof(type), "%s/type", dev_path) != 0) {
    rc = -1;
    goto out;
  }
  if (strncmp(type, "CEX", 3) != 0 || strlen(type) < 5) {
    rc = -1;
    goto out;
  }
  if (type[4] != 'C' && type[4] != 'P') {
    rc = -1;
    goto out;
  }
  if (type[3] < '1' || type[3] > '9') {
    rc = -1;
    goto out;
  }
  rc = type[3] - '0';
out:
  free(dev_path);
  return rc;
}


x86 assembly (clang, -O0):
<sysfs_get_card_level>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%esi
lea    0xe8b(%rip),%rdi
call   1060 <util_path_sysfs@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <util_path_is_dir@plt>
cmp    $0x0,%eax
jne    119c <sysfs_get_card_level+0x3c>
movl   $0xffffffff,-0x34(%rbp)
jmp    125e <sysfs_get_card_level+0xfe>
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x14,%esi
lea    0xe68(%rip),%rdx
call   1070 <util_file_read_line@plt>
cmp    $0x0,%rax
je     11cb <sysfs_get_card_level+0x6b>
movl   $0xffffffff,-0x34(%rbp)
jmp    125e <sysfs_get_card_level+0xfe>
lea    -0x30(%rbp),%rdi
lea    0xe4a(%rip),%rsi
mov    $0x3,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    11fc <sysfs_get_card_level+0x9c>
lea    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x5,%eax
jge    1208 <sysfs_get_card_level+0xa8>
movl   $0xffffffff,-0x34(%rbp)
jmp    125e <sysfs_get_card_level+0xfe>
movsbl -0x2c(%rbp),%eax
cmp    $0x43,%eax
je     122e <sysfs_get_card_level+0xce>
movsbl -0x2c(%rbp),%eax
cmp    $0x50,%eax
je     122e <sysfs_get_card_level+0xce>
movl   $0xffffffff,-0x34(%rbp)
jmp    125e <sysfs_get_card_level+0xfe>
movsbl -0x2d(%rbp),%eax
cmp    $0x31,%eax
jl     1248 <sysfs_get_card_level+0xe8>
movsbl -0x2d(%rbp),%eax
cmp    $0x39,%eax
jle    1254 <sysfs_get_card_level+0xf4>
movl   $0xffffffff,-0x34(%rbp)
jmp    125e <sysfs_get_card_level+0xfe>
movsbl -0x2d(%rbp),%eax
sub    $0x30,%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_get_card_level>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%esi
lea    0xe91(%rip),%rdi
call   1060 <util_path_sysfs@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <util_path_is_dir@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
je     11e8 <sysfs_get_card_level+0x88>
lea    0xe89(%rip),%rdx
mov    %rsp,%rdi
mov    $0x14,%esi
mov    %rbx,%rcx
call   1070 <util_file_read_line@plt>
test   %rax,%rax
jne    11e8 <sysfs_get_card_level+0x88>
lea    0xe75(%rip),%rsi
mov    %rsp,%rdi
mov    $0x3,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
jne    11e8 <sysfs_get_card_level+0x88>
mov    %rsp,%rdi
call   1050 <strlen@plt>
cmp    $0x5,%eax
jl     11e8 <sysfs_get_card_level+0x88>
mov    0x4(%rsp),%al
cmp    $0x50,%al
je     11d6 <sysfs_get_card_level+0x76>
cmp    $0x43,%al
jne    11e8 <sysfs_get_card_level+0x88>
movzbl 0x3(%rsp),%eax
lea    -0x3a(%rax),%ecx
cmp    $0xf7,%cl
jb     11e8 <sysfs_get_card_level+0x88>
add    $0xffffffd0,%eax
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"bus/ap/devices/card%02x\00"
@.str.1 = [8 x i8] c"%s/type\00"
@.str.2 = [4 x i8] c"CEX\00"
define i32 @sysfs_get_card_level(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [20 x i8]
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = call i8* @util_path_sysfs(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %6)
store i8* %7, i8** %3
%8 = load i8*, i8** %3
%9 = call i32 @util_path_is_dir(i8* %8)
%10 = icmp ne i32 %9, 0
br i1 %10, label %12, label %11
11: ; preds = %1
store i32 -1, i32* %5
br label %54
12: ; preds = %1
%13 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%14 = load i8*, i8** %3
%15 = call i64 @util_file_read_line(i8* %13, i32 20, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %12
store i32 -1, i32* %5
br label %54
18: ; preds = %12
%19 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%20 = call i64 @strncmp(i8* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%21 = icmp ne i64 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %18
%23 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%24 = call i32 @strlen(i8* %23)
%25 = icmp slt i32 %24, 5
br i1 %25, label %26, label %27
26: ; preds = %22, %18
store i32 -1, i32* %5
br label %54
27: ; preds = %22
%28 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 4
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp ne i32 %30, 67
br i1 %31, label %32, label %38
32: ; preds = %27
%33 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 4
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp ne i32 %35, 80
br i1 %36, label %37, label %38
37: ; preds = %32
store i32 -1, i32* %5
br label %54
38: ; preds = %32, %27
%39 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 3
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp slt i32 %41, 49
br i1 %42, label %48, label %43
43: ; preds = %38
%44 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 3
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp sgt i32 %46, 57
br i1 %47, label %48, label %49
48: ; preds = %43, %38
store i32 -1, i32* %5
br label %54
49: ; preds = %43
%50 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 3
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = sub nsw i32 %52, 48
store i32 %53, i32* %5
br label %54
54: ; preds = %49, %48, %37, %26, %17, %11
%55 = load i8*, i8** %3
%56 = call i32 @free(i8* %55)
%57 = load i32, i32* %5
ret i32 %57
}

LLVM IR (-O2):
@.str = [24 x i8] c"bus/ap/devices/card%02x\00"
@.str.1 = [8 x i8] c"%s/type\00"
@.str.2 = [4 x i8] c"CEX\00"
define i32 @sysfs_get_card_level(i32 %0) {
%2 = alloca [20 x i8]
%3 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%4 = tail call i8* @util_path_sysfs(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = tail call i32 @util_path_is_dir(i8* %4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %27, label %7
7: ; preds = %1
%8 = call i64 @util_file_read_line(i8* %3, i32 20, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %27
10: ; preds = %7
%11 = call i64 @strncmp(i8* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %27
13: ; preds = %10
%14 = call i32 @strlen(i8* %3)
%15 = icmp slt i32 %14, 5
br i1 %15, label %27, label %16
16: ; preds = %13
%17 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 4
%18 = load i8, i8* %17
switch i8 %18, label %27 [
i8 80, label %19
i8 67, label %19
]
19: ; preds = %16, %16
%20 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 3
%21 = load i8, i8* %20
%22 = add i8 %21, -58
%23 = icmp ult i8 %22, -9
br i1 %23, label %27, label %24
24: ; preds = %19
%25 = zext i8 %21 to i32
%26 = add nsw i32 %25, -48
br label %27
27: ; preds = %19, %16, %10, %13, %7, %1, %24
%28 = phi i32 [ %26, %24 ], [ -1, %1 ], [ -1, %7 ], [ -1, %13 ], [ -1, %10 ], [ -1, %16 ], [ -1, %19 ]
%29 = call i32 @free(i8* %4)
ret i32 %28
}