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

/* Variables and functions */
int read_attr(char *, int, int);
char *strchr(char *, char);
scalar_t__ sysfs_attr_match(char *, char *);
int read_dev_state(int fd) {
  char buf[60];
  int n = read_attr(buf, 60, fd);
  char *cp;
  int rv = 0;
  if (n <= 0)
    return 0;
  cp = buf;
  while (cp) {
    if (sysfs_attr_match(cp, "faulty"))
      rv |= 1;
    if (sysfs_attr_match(cp, "in_sync"))
      rv |= 2;
    if (sysfs_attr_match(cp, "write_mostly"))
      rv |= 4;
    if (sysfs_attr_match(cp, "spare"))
      rv |= 8;
    if (sysfs_attr_match(cp, "blocked"))
      rv |= 16;
    cp = strchr(cp, ',');
    if (cp)
      cp++;
  }
  return rv;
}


x86 assembly (clang, -O0):
<read_dev_state>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%edx
mov    $0x3c,%esi
call   1050 <read_attr@plt>
mov    %eax,-0x54(%rbp)
movl   $0x0,-0x64(%rbp)
cmpl   $0x0,-0x54(%rbp)
jg     116c <read_dev_state+0x3c>
movl   $0x0,-0x4(%rbp)
jmp    1262 <read_dev_state+0x132>
lea    -0x50(%rbp),%rax
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
je     125c <read_dev_state+0x12c>
mov    -0x60(%rbp),%rdi
lea    0xe76(%rip),%rsi
call   1040 <sysfs_attr_match@plt>
cmp    $0x0,%rax
je     11a2 <read_dev_state+0x72>
mov    -0x64(%rbp),%eax
or     $0x1,%eax
mov    %eax,-0x64(%rbp)
mov    -0x60(%rbp),%rdi
lea    0xe5a(%rip),%rsi
call   1040 <sysfs_attr_match@plt>
cmp    $0x0,%rax
je     11c5 <read_dev_state+0x95>
mov    -0x64(%rbp),%eax
or     $0x2,%eax
mov    %eax,-0x64(%rbp)
mov    -0x60(%rbp),%rdi
lea    0xe3f(%rip),%rsi
call   1040 <sysfs_attr_match@plt>
cmp    $0x0,%rax
je     11e8 <read_dev_state+0xb8>
mov    -0x64(%rbp),%eax
or     $0x4,%eax
mov    %eax,-0x64(%rbp)
mov    -0x60(%rbp),%rdi
lea    0xe29(%rip),%rsi
call   1040 <sysfs_attr_match@plt>
cmp    $0x0,%rax
je     120b <read_dev_state+0xdb>
mov    -0x64(%rbp),%eax
or     $0x8,%eax
mov    %eax,-0x64(%rbp)
mov    -0x60(%rbp),%rdi
lea    0xe0c(%rip),%rsi
call   1040 <sysfs_attr_match@plt>
cmp    $0x0,%rax
je     122e <read_dev_state+0xfe>
mov    -0x64(%rbp),%eax
or     $0x10,%eax
mov    %eax,-0x64(%rbp)
mov    -0x60(%rbp),%rdi
mov    $0x2c,%esi
call   1030 <strchr@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
je     1257 <read_dev_state+0x127>
mov    -0x60(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x60(%rbp)
jmp    1174 <read_dev_state+0x44>
mov    -0x64(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_dev_state>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %edi,%edx
mov    %rsp,%r13
mov    %r13,%rdi
mov    $0x3c,%esi
call   1050 <read_attr@plt>
xor    %r14d,%r14d
test   %eax,%eax
jle    120f <read_dev_state+0xdf>
lea    0xec0(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r13,%rdi
lea    0xe86(%rip),%rsi
call   1040 <sysfs_attr_match@plt>
xor    %r15d,%r15d
test   %rax,%rax
setne  %r15b
or     %r14d,%r15d
mov    %r13,%rdi
lea    0xe71(%rip),%rsi
call   1040 <sysfs_attr_match@plt>
mov    %r15d,%ebp
or     $0x2,%ebp
test   %rax,%rax
cmove  %r15d,%ebp
mov    %r13,%rdi
lea    0xe5d(%rip),%rsi
call   1040 <sysfs_attr_match@plt>
mov    %ebp,%ebx
or     $0x4,%ebx
test   %rax,%rax
cmove  %ebp,%ebx
mov    %r13,%rdi
lea    0xe50(%rip),%rsi
call   1040 <sysfs_attr_match@plt>
mov    %ebx,%ebp
or     $0x8,%ebp
test   %rax,%rax
cmove  %ebx,%ebp
mov    %r13,%rdi
mov    %r12,%rsi
call   1040 <sysfs_attr_match@plt>
mov    %ebp,%r14d
or     $0x10,%r14d
test   %rax,%rax
cmove  %ebp,%r14d
mov    %r13,%rdi
mov    $0x2c,%esi
call   1030 <strchr@plt>
lea    0x1(%rax),%r13
test   %rax,%rax
jne    1170 <read_dev_state+0x40>
mov    %r14d,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"faulty\00"
@.str.1 = [8 x i8] c"in_sync\00"
@.str.2 = [13 x i8] c"write_mostly\00"
@.str.3 = [6 x i8] c"spare\00"
@.str.4 = [8 x i8] c"blocked\00"
define i32 @read_dev_state(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [60 x i8]
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i32 %0, i32* %3
%8 = getelementptr inbounds [60 x i8], [60 x i8]* %4, i64 0, i64 0
%9 = load i32, i32* %3
%10 = call i32 @read_attr(i8* %8, i32 60, i32 %9)
store i32 %10, i32* %5
store i32 0, i32* %7
%11 = load i32, i32* %5
%12 = icmp sle i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 0, i32* %2
br label %65
14: ; preds = %1
%15 = getelementptr inbounds [60 x i8], [60 x i8]* %4, i64 0, i64 0
store i8* %15, i8** %6
br label %16
16: ; preds = %62, %14
%17 = load i8*, i8** %6
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %63
19: ; preds = %16
%20 = load i8*, i8** %6
%21 = call i64 @sysfs_attr_match(i8* %20, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i32, i32* %7
%25 = or i32 %24, 1
store i32 %25, i32* %7
br label %26
26: ; preds = %23, %19
%27 = load i8*, i8** %6
%28 = call i64 @sysfs_attr_match(i8* %27, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = load i32, i32* %7
%32 = or i32 %31, 2
store i32 %32, i32* %7
br label %33
33: ; preds = %30, %26
%34 = load i8*, i8** %6
%35 = call i64 @sysfs_attr_match(i8* %34, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = load i32, i32* %7
%39 = or i32 %38, 4
store i32 %39, i32* %7
br label %40
40: ; preds = %37, %33
%41 = load i8*, i8** %6
%42 = call i64 @sysfs_attr_match(i8* %41, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = load i32, i32* %7
%46 = or i32 %45, 8
store i32 %46, i32* %7
br label %47
47: ; preds = %44, %40
%48 = load i8*, i8** %6
%49 = call i64 @sysfs_attr_match(i8* %48, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %47
%52 = load i32, i32* %7
%53 = or i32 %52, 16
store i32 %53, i32* %7
br label %54
54: ; preds = %51, %47
%55 = load i8*, i8** %6
%56 = call i8* @strchr(i8* %55, i8 signext 44)
store i8* %56, i8** %6
%57 = load i8*, i8** %6
%58 = icmp ne i8* %57, null
br i1 %58, label %59, label %62
59: ; preds = %54
%60 = load i8*, i8** %6
%61 = getelementptr inbounds i8, i8* %60, i32 1
store i8* %61, i8** %6
br label %62
62: ; preds = %59, %54
br label %16
63: ; preds = %16
%64 = load i32, i32* %7
store i32 %64, i32* %2
br label %65
65: ; preds = %63, %13
%66 = load i32, i32* %2
ret i32 %66
}

LLVM IR (-O2):
@.str = [7 x i8] c"faulty\00"
@.str.1 = [8 x i8] c"in_sync\00"
@.str.2 = [13 x i8] c"write_mostly\00"
@.str.3 = [6 x i8] c"spare\00"
@.str.4 = [8 x i8] c"blocked\00"
define i32 @read_dev_state(i32 %0) {
%2 = alloca [60 x i8]
%3 = getelementptr inbounds [60 x i8], [60 x i8]* %2, i64 0, i64 0
%4 = call i32 @read_attr(i8* %3, i32 60, i32 %0)
%5 = icmp slt i32 %4, 1
br i1 %5, label %32, label %6
6: ; preds = %1, %6
%7 = phi i32 [ %28, %6 ], [ 0, %1 ]
%8 = phi i8* [ %31, %6 ], [ %3, %1 ]
%9 = call i64 @sysfs_attr_match(i8* %8, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%10 = icmp ne i64 %9, 0
%11 = zext i1 %10 to i32
%12 = or i32 %7, %11
%13 = call i64 @sysfs_attr_match(i8* %8, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
%15 = or i32 %12, 2
%16 = select i1 %14, i32 %12, i32 %15
%17 = call i64 @sysfs_attr_match(i8* %8, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
%19 = or i32 %16, 4
%20 = select i1 %18, i32 %16, i32 %19
%21 = call i64 @sysfs_attr_match(i8* %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
%23 = or i32 %20, 8
%24 = select i1 %22, i32 %20, i32 %23
%25 = call i64 @sysfs_attr_match(i8* %8, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
%27 = or i32 %24, 16
%28 = select i1 %26, i32 %24, i32 %27
%29 = call i8* @strchr(i8* %8, i8 signext 44)
%30 = icmp eq i8* %29, null
%31 = getelementptr inbounds i8, i8* %29, i64 1
br i1 %30, label %32, label %6
32: ; preds = %6, %1
%33 = phi i32 [ 0, %1 ], [ %28, %6 ]
ret i32 %33
}