#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 /*<<< orphan*/ ioc_iswhole(unsigned int, unsigned int);
int sscanf(char *, char *, unsigned int *, unsigned int *, unsigned int *);
int get_ppartitions_dev_nr(int count_part) {
  FILE *fp;
  char line[256];
  int dev = 0;
  unsigned int major, minor, tmp;
  if ((fp = fopen("/proc/partitions", "r")) == NULL)
    return 0;
  while (fgets(line, 256, fp) != NULL) {
    if (sscanf(line, "%u %u %*u %*s %u", &major, &minor, &tmp) == 3) {
      if (!count_part && !ioc_iswhole(major, minor))
        continue;
      dev++;
    }
  }
  fclose(fp);
  return dev;
}


x86 assembly (clang, -O0):
<get_ppartitions_dev_nr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x114(%rbp)
lea    0xe91(%rip),%rdi
lea    0xe9b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1195 <get_ppartitions_dev_nr+0x45>
movl   $0x0,-0x4(%rbp)
jmp    1239 <get_ppartitions_dev_nr+0xe9>
jmp    119a <get_ppartitions_dev_nr+0x4a>
lea    -0x110(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x100,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     1227 <get_ppartitions_dev_nr+0xd7>
lea    -0x110(%rbp),%rdi
lea    0xe4c(%rip),%rsi
lea    -0x118(%rbp),%rdx
lea    -0x11c(%rbp),%rcx
lea    -0x120(%rbp),%r8
call   1050 <sscanf@plt>
cmp    $0x3,%eax
jne    1222 <get_ppartitions_dev_nr+0xd2>
cmpl   $0x0,-0x8(%rbp)
jne    1213 <get_ppartitions_dev_nr+0xc3>
mov    -0x118(%rbp),%edi
mov    -0x11c(%rbp),%esi
call   1060 <ioc_iswhole@plt>
cmp    $0x0,%eax
jne    1213 <get_ppartitions_dev_nr+0xc3>
jmp    119a <get_ppartitions_dev_nr+0x4a>
mov    -0x114(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x114(%rbp)
jmp    119a <get_ppartitions_dev_nr+0x4a>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x114(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ppartitions_dev_nr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %edi,%r15d
lea    0xe95(%rip),%rdi
lea    0xe9f(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11f7 <get_ppartitions_dev_nr+0xa7>
mov    %rax,%rbp
movl   $0x0,(%rsp)
lea    0x10(%rsp),%rbx
lea    0x8(%rsp),%r13
lea    0x4(%rsp),%r14
lea    0xc(%rsp),%r12
jmp    11a0 <get_ppartitions_dev_nr+0x50>
addl   $0x1,(%rsp)
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %rbp,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     11ea <get_ppartitions_dev_nr+0x9a>
mov    %rbx,%rdi
lea    0xe54(%rip),%rsi
mov    %r13,%rdx
mov    %r14,%rcx
mov    %r12,%r8
call   1050 <sscanf@plt>
cmp    $0x3,%eax
jne    11a0 <get_ppartitions_dev_nr+0x50>
test   %r15d,%r15d
jne    119c <get_ppartitions_dev_nr+0x4c>
mov    0x8(%rsp),%edi
mov    0x4(%rsp),%esi
call   1060 <ioc_iswhole@plt>
test   %eax,%eax
je     11a0 <get_ppartitions_dev_nr+0x50>
jmp    119c <get_ppartitions_dev_nr+0x4c>
mov    %rbp,%rdi
call   1030 <fclose@plt>
mov    (%rsp),%eax
jmp    11f9 <get_ppartitions_dev_nr+0xa9>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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