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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ is_device(char *, int);
int sscanf(char *, char *, char *, unsigned long *, unsigned long *);
int get_diskstats_dev_nr(int count_part, int only_used_dev) {
  FILE *fp;
  char line[256];
  char dev_name[128];
  int dev = 0, i;
  unsigned long rd_ios, wr_ios;
  if ((fp = fopen("/proc/diskstats", "r")) == NULL)
    return 0;
  while (fgets(line, sizeof(line), fp) != NULL) {
    if (!count_part) {
      i = sscanf(line, "%*d %*d %s %lu %*u %*u %*u %lu", dev_name, &rd_ios,
                 &wr_ios);
      if ((i == 2) || !is_device(dev_name, 1))
        continue;
      if (only_used_dev && !rd_ios && !wr_ios)
        continue;
    }
    dev++;
  }
  fclose(fp);
  return dev;
}


x86 assembly (clang, -O0):
<get_diskstats_dev_nr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1c0,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x1a4(%rbp)
lea    0xe8e(%rip),%rdi
lea    0xe97(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1198 <get_diskstats_dev_nr+0x48>
movl   $0x0,-0x4(%rbp)
jmp    1276 <get_diskstats_dev_nr+0x126>
jmp    119d <get_diskstats_dev_nr+0x4d>
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x100,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1264 <get_diskstats_dev_nr+0x114>
cmpl   $0x0,-0x8(%rbp)
jne    1250 <get_diskstats_dev_nr+0x100>
lea    -0x120(%rbp),%rdi
lea    -0x1a0(%rbp),%rdx
lea    0xe37(%rip),%rsi
lea    -0x1b0(%rbp),%rcx
lea    -0x1b8(%rbp),%r8
call   1060 <sscanf@plt>
mov    %eax,-0x1a8(%rbp)
cmpl   $0x2,-0x1a8(%rbp)
je     121b <get_diskstats_dev_nr+0xcb>
lea    -0x1a0(%rbp),%rdi
mov    $0x1,%esi
call   1040 <is_device@plt>
cmp    $0x0,%eax
jne    1220 <get_diskstats_dev_nr+0xd0>
jmp    119d <get_diskstats_dev_nr+0x4d>
cmpl   $0x0,-0xc(%rbp)
je     124b <get_diskstats_dev_nr+0xfb>
cmpq   $0x0,-0x1b0(%rbp)
jne    124b <get_diskstats_dev_nr+0xfb>
cmpq   $0x0,-0x1b8(%rbp)
jne    124b <get_diskstats_dev_nr+0xfb>
jmp    119d <get_diskstats_dev_nr+0x4d>
jmp    1250 <get_diskstats_dev_nr+0x100>
mov    -0x1a4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1a4(%rbp)
jmp    119d <get_diskstats_dev_nr+0x4d>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x1a4(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_diskstats_dev_nr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1a8,%rsp
mov    %esi,0xc(%rsp)
mov    %edi,0x8(%rsp)
lea    0xe90(%rip),%rdi
lea    0xe99(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1259 <get_diskstats_dev_nr+0x109>
mov    %rax,%rbx
lea    0xa0(%rsp),%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
xor    %r13d,%r13d
test   %rax,%rax
je     124f <get_diskstats_dev_nr+0xff>
lea    0xa0(%rsp),%rbp
lea    0x20(%rsp),%r14
lea    0x18(%rsp),%r15
lea    0x10(%rsp),%r12
cmpl   $0x0,0x8(%rsp)
je     1205 <get_diskstats_dev_nr+0xb5>
add    $0x1,%r13d
mov    %rbp,%rdi
mov    $0x100,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    11c0 <get_diskstats_dev_nr+0x70>
jmp    124f <get_diskstats_dev_nr+0xff>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbp,%rdi
mov    $0x100,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     124f <get_diskstats_dev_nr+0xff>
mov    %rbp,%rdi
lea    0xe03(%rip),%rsi
mov    %r14,%rdx
mov    %r15,%rcx
mov    %r12,%r8
call   1060 <sscanf@plt>
cmp    $0x2,%eax
je     11f0 <get_diskstats_dev_nr+0xa0>
mov    %r14,%rdi
mov    $0x1,%esi
call   1040 <is_device@plt>
test   %eax,%eax
je     11f0 <get_diskstats_dev_nr+0xa0>
cmpl   $0x0,0xc(%rsp)
je     11c7 <get_diskstats_dev_nr+0x77>
cmpq   $0x0,0x18(%rsp)
jne    11c7 <get_diskstats_dev_nr+0x77>
cmpq   $0x0,0x10(%rsp)
je     11f0 <get_diskstats_dev_nr+0xa0>
jmp    11c7 <get_diskstats_dev_nr+0x77>
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    125c <get_diskstats_dev_nr+0x10c>
xor    %r13d,%r13d
mov    %r13d,%eax
add    $0x1a8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"/proc/diskstats\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [31 x i8] c"%*d %*d %s %lu %*u %*u %*u %lu\00"
define i32 @get_diskstats_dev_nr(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca [256 x i8]
%8 = alloca [128 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
%12 = alloca i64
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 0, i32* %9
%13 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %6
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 0, i32* %3
br label %54
16: ; preds = %2
br label %17
17: ; preds = %47, %45, %35, %16
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%19 = load i32*, i32** %6
%20 = call i32* @fgets(i8* %18, i32 256, i32* %19)
%21 = icmp ne i32* %20, null
br i1 %21, label %22, label %50
22: ; preds = %17
%23 = load i32, i32* %4
%24 = icmp ne i32 %23, 0
br i1 %24, label %47, label %25
25: ; preds = %22
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%27 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%28 = call i32 @sscanf(i8* %26, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %27, i64* %11, i64* %12)
store i32 %28, i32* %10
%29 = load i32, i32* %10
%30 = icmp eq i32 %29, 2
br i1 %30, label %35, label %31
31: ; preds = %25
%32 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%33 = call i32 @is_device(i8* %32, i32 1)
%34 = icmp ne i32 %33, 0
br i1 %34, label %36, label %35
35: ; preds = %31, %25
br label %17
36: ; preds = %31
%37 = load i32, i32* %5
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %46
39: ; preds = %36
%40 = load i64, i64* %11
%41 = icmp ne i64 %40, 0
br i1 %41, label %46, label %42
42: ; preds = %39
%43 = load i64, i64* %12
%44 = icmp ne i64 %43, 0
br i1 %44, label %46, label %45
45: ; preds = %42
br label %17
46: ; preds = %42, %39, %36
br label %47
47: ; preds = %46, %22
%48 = load i32, i32* %9
%49 = add nsw i32 %48, 1
store i32 %49, i32* %9
br label %17
50: ; preds = %17
%51 = load i32*, i32** %6
%52 = call i32 @fclose(i32* %51)
%53 = load i32, i32* %9
store i32 %53, i32* %3
br label %54
54: ; preds = %50, %15
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/diskstats\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [31 x i8] c"%*d %*d %s %lu %*u %*u %*u %lu\00"
define i32 @get_diskstats_dev_nr(i32 %0, i32 %1) {
%3 = alloca [256 x i8]
%4 = alloca [128 x i8]
%5 = alloca i64
%6 = alloca i64
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%9 = bitcast i64* %5 to i8*
%10 = bitcast i64* %6 to i8*
%11 = tail call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %44, label %13
13: ; preds = %2
%14 = call i32* @fgets(i8* %7, i32 256, i32* %11)
%15 = icmp eq i32* %14, null
br i1 %15, label %41, label %16
16: ; preds = %13
%17 = icmp eq i32 %0, 0
%18 = icmp eq i32 %1, 0
br label %19
19: ; preds = %16, %37
%20 = phi i32 [ 0, %16 ], [ %38, %37 ]
br i1 %17, label %21, label %37
21: ; preds = %19, %27
%22 = call i32 @sscanf(i8* %7, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %8, i64* %5, i64* %6)
%23 = icmp eq i32 %22, 2
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = call i32 @is_device(i8* %8, i32 1)
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %21, %24, %30
%28 = call i32* @fgets(i8* %7, i32 256, i32* %11)
%29 = icmp eq i32* %28, null
br i1 %29, label %41, label %21
30: ; preds = %24
%31 = load i64, i64* %5
%32 = icmp ne i64 %31, 0
%33 = select i1 %18, i1 true, i1 %32
%34 = load i64, i64* %6
%35 = icmp ne i64 %34, 0
%36 = select i1 %33, i1 true, i1 %35
br i1 %36, label %37, label %27
37: ; preds = %30, %19
%38 = add nuw nsw i32 %20, 1
%39 = call i32* @fgets(i8* %7, i32 256, i32* %11)
%40 = icmp eq i32* %39, null
br i1 %40, label %41, label %19
41: ; preds = %37, %27, %13
%42 = phi i32 [ 0, %13 ], [ %20, %27 ], [ %38, %37 ]
%43 = call i32 @fclose(i32* %11)
br label %44
44: ; preds = %2, %41
%45 = phi i32 [ %42, %41 ], [ 0, %2 ]
ret i32 %45
}