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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int sscanf(char *, char *, unsigned long *, unsigned long *);
int get_diskstats_dev_nr(int count_part, int only_used_dev) {
  FILE *fp;
  char line[256];
  int dev = 0, i;
  unsigned long rd_ios, wr_ios;
  if ((fp = fopen("/proc/diskstats", "r")) == NULL)
    return 0;
  while (fgets(line, 256, fp) != NULL) {
    if (!count_part) {
      i = sscanf(line, "%*d %*d %*s %lu %*u %*u %*u %lu", &rd_ios, &wr_ios);
      if (i == 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    $0x140,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x124(%rbp)
lea    0xe9e(%rip),%rdi
lea    0xea7(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1188 <get_diskstats_dev_nr+0x48>
movl   $0x0,-0x4(%rbp)
jmp    1245 <get_diskstats_dev_nr+0x105>
jmp    118d <get_diskstats_dev_nr+0x4d>
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x100,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     1233 <get_diskstats_dev_nr+0xf3>
cmpl   $0x0,-0x8(%rbp)
jne    121f <get_diskstats_dev_nr+0xdf>
lea    -0x120(%rbp),%rdi
lea    0xe4e(%rip),%rsi
lea    -0x130(%rbp),%rdx
lea    -0x138(%rbp),%rcx
call   1050 <sscanf@plt>
mov    %eax,-0x128(%rbp)
cmpl   $0x1,-0x128(%rbp)
jne    11ef <get_diskstats_dev_nr+0xaf>
jmp    118d <get_diskstats_dev_nr+0x4d>
cmpl   $0x0,-0xc(%rbp)
je     121a <get_diskstats_dev_nr+0xda>
cmpq   $0x0,-0x130(%rbp)
jne    121a <get_diskstats_dev_nr+0xda>
cmpq   $0x0,-0x138(%rbp)
jne    121a <get_diskstats_dev_nr+0xda>
jmp    118d <get_diskstats_dev_nr+0x4d>
jmp    121f <get_diskstats_dev_nr+0xdf>
mov    -0x124(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x124(%rbp)
jmp    118d <get_diskstats_dev_nr+0x4d>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x124(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%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    $0x128,%rsp
mov    %esi,0xc(%rsp)
mov    %edi,0x8(%rsp)
lea    0xea0(%rip),%rdi
lea    0xea9(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     121e <get_diskstats_dev_nr+0xde>
mov    %rax,%rbx
lea    0x20(%rsp),%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
xor    %r13d,%r13d
test   %rax,%rax
je     1214 <get_diskstats_dev_nr+0xd4>
lea    0xe75(%rip),%r12
lea    0x20(%rsp),%rbp
lea    0x18(%rsp),%r14
lea    0x10(%rsp),%r15
cmpl   $0x0,0x8(%rsp)
je     11e5 <get_diskstats_dev_nr+0xa5>
add    $0x1,%r13d
mov    %rbp,%rdi
mov    $0x100,%esi
mov    %rbx,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
jne    11ac <get_diskstats_dev_nr+0x6c>
jmp    1214 <get_diskstats_dev_nr+0xd4>
xchg   %ax,%ax
mov    %rbp,%rdi
mov    $0x100,%esi
mov    %rbx,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1214 <get_diskstats_dev_nr+0xd4>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
mov    %r15,%rcx
call   1050 <sscanf@plt>
cmp    $0x1,%eax
je     11d0 <get_diskstats_dev_nr+0x90>
cmpl   $0x0,0xc(%rsp)
je     11b3 <get_diskstats_dev_nr+0x73>
cmpq   $0x0,0x18(%rsp)
jne    11b3 <get_diskstats_dev_nr+0x73>
cmpq   $0x0,0x10(%rsp)
je     11d0 <get_diskstats_dev_nr+0x90>
jmp    11b3 <get_diskstats_dev_nr+0x73>
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    1221 <get_diskstats_dev_nr+0xe1>
xor    %r13d,%r13d
mov    %r13d,%eax
add    $0x128,%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 = [32 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 i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca i64
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 0, i32* %8
%12 = 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* %12, i32** %6
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 0, i32* %3
br label %48
15: ; preds = %2
br label %16
16: ; preds = %41, %39, %29, %15
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%18 = load i32*, i32** %6
%19 = call i32* @fgets(i8* %17, i32 256, i32* %18)
%20 = icmp ne i32* %19, null
br i1 %20, label %21, label %44
21: ; preds = %16
%22 = load i32, i32* %4
%23 = icmp ne i32 %22, 0
br i1 %23, label %41, label %24
24: ; preds = %21
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%26 = call i32 @sscanf(i8* %25, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i64* %10, i64* %11)
store i32 %26, i32* %9
%27 = load i32, i32* %9
%28 = icmp eq i32 %27, 1
br i1 %28, label %29, label %30
29: ; preds = %24
br label %16
30: ; preds = %24
%31 = load i32, i32* %5
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %40
33: ; preds = %30
%34 = load i64, i64* %10
%35 = icmp ne i64 %34, 0
br i1 %35, label %40, label %36
36: ; preds = %33
%37 = load i64, i64* %11
%38 = icmp ne i64 %37, 0
br i1 %38, label %40, label %39
39: ; preds = %36
br label %16
40: ; preds = %36, %33, %30
br label %41
41: ; preds = %40, %21
%42 = load i32, i32* %8
%43 = add nsw i32 %42, 1
store i32 %43, i32* %8
br label %16
44: ; preds = %16
%45 = load i32*, i32** %6
%46 = call i32 @fclose(i32* %45)
%47 = load i32, i32* %8
store i32 %47, i32* %3
br label %48
48: ; preds = %44, %14
%49 = load i32, i32* %3
ret i32 %49
}

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