#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 *);
scalar_t__ strcspn(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncmp(char *, char *, int);
int get_irq_nr(void) {
  FILE *fp;
  char line[8192];
  int in = 0;
  int pos = 4;
  if ((fp = fopen("/proc/stat", "r")) == NULL)
    return 0;
  while (fgets(line, 8192, fp) != NULL) {
    if (!strncmp(line, "intr ", 5)) {
      while (pos < strlen(line)) {
        in++;
        pos += strcspn(line + pos + 1, " ") + 1;
      }
    }
  }
  fclose(fp);
  return in;
}


x86 assembly (clang, -O0):
<get_irq_nr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2020,%rsp
movl   $0x0,-0x2014(%rbp)
movl   $0x4,-0x2018(%rbp)
lea    0xe7a(%rip),%rdi
lea    0xe7e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11ac <get_irq_nr+0x4c>
movl   $0x0,-0x4(%rbp)
jmp    1286 <get_irq_nr+0x126>
jmp    11b1 <get_irq_nr+0x51>
lea    -0x2010(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x2000,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     1274 <get_irq_nr+0x114>
lea    -0x2010(%rbp),%rdi
lea    0xe2f(%rip),%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    126f <get_irq_nr+0x10f>
jmp    11f6 <get_irq_nr+0x96>
mov    -0x2018(%rbp),%eax
mov    %eax,-0x201c(%rbp)
lea    -0x2010(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x201c(%rbp),%eax
cmp    %ecx,%eax
jge    126a <get_irq_nr+0x10a>
mov    -0x2014(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2014(%rbp)
lea    -0x2010(%rbp),%rdi
movslq -0x2018(%rbp),%rax
add    %rax,%rdi
add    $0x1,%rdi
lea    0xdc8(%rip),%rsi
call   1060 <strcspn@plt>
mov    %rax,%rcx
add    $0x1,%rcx
movslq -0x2018(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x2018(%rbp)
jmp    11f6 <get_irq_nr+0x96>
jmp    126f <get_irq_nr+0x10f>
jmp    11b1 <get_irq_nr+0x51>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x2014(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_irq_nr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2008,%rsp
lea    0xe88(%rip),%rdi
lea    0xe8c(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1223 <get_irq_nr+0xc3>
mov    %rax,%r15
mov    %rsp,%rdi
mov    $0x2000,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     1219 <get_irq_nr+0xb9>
mov    $0x4,%ebx
lea    0xe5a(%rip),%r14
mov    %rsp,%r13
lea    0xe54(%rip),%r12
jmp    11d5 <get_irq_nr+0x75>
nop
mov    %r13,%rdi
mov    $0x2000,%esi
mov    %r15,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1219 <get_irq_nr+0xb9>
mov    %r13,%rdi
mov    %r14,%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    11c0 <get_irq_nr+0x60>
jmp    120b <get_irq_nr+0xab>
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebp
movslq %ebx,%rbx
lea    (%rsp,%rbx,1),%rdi
add    $0x1,%rdi
mov    %r12,%rsi
call   1060 <strcspn@plt>
add    %eax,%ebx
add    $0x1,%ebx
mov    %r13,%rdi
call   1050 <strlen@plt>
cmp    %eax,%ebx
jl     11f0 <get_irq_nr+0x90>
jmp    11c0 <get_irq_nr+0x60>
mov    %r15,%rdi
call   1040 <fclose@plt>
jmp    1225 <get_irq_nr+0xc5>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x2008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"intr \00"
@.str.3 = [2 x i8] c" \00"
define i32 @get_irq_nr() {
%1 = alloca [8192 x i8]
%2 = getelementptr inbounds [8192 x i8], [8192 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %37, label %5
5: ; preds = %0
%6 = call i32* @fgets(i8* %2, i32 8192, i32* %3)
%7 = icmp eq i32* %6, null
br i1 %7, label %34, label %8
8: ; preds = %5, %29
%9 = phi i32 [ %31, %29 ], [ 4, %5 ]
%10 = phi i32 [ %30, %29 ], [ 0, %5 ]
%11 = call i32 @strncmp(i8* %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %29
13: ; preds = %8
%14 = call i32 @strlen(i8* %2)
%15 = icmp slt i32 %9, %14
br i1 %15, label %16, label %29
16: ; preds = %13, %16
%17 = phi i32 [ %26, %16 ], [ %9, %13 ]
%18 = phi i32 [ %19, %16 ], [ %10, %13 ]
%19 = add nsw i32 %18, 1
%20 = sext i32 %17 to i64
%21 = getelementptr inbounds [8192 x i8], [8192 x i8]* %1, i64 0, i64 %20
%22 = getelementptr inbounds i8, i8* %21, i64 1
%23 = call i64 @strcspn(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%24 = trunc i64 %23 to i32
%25 = add nsw i32 %17, 1
%26 = add i32 %25, %24
%27 = call i32 @strlen(i8* %2)
%28 = icmp slt i32 %26, %27
br i1 %28, label %16, label %29
29: ; preds = %16, %13, %8
%30 = phi i32 [ %10, %8 ], [ %10, %13 ], [ %19, %16 ]
%31 = phi i32 [ %9, %8 ], [ %9, %13 ], [ %26, %16 ]
%32 = call i32* @fgets(i8* %2, i32 8192, i32* %3)
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %8
34: ; preds = %29, %5
%35 = phi i32 [ 0, %5 ], [ %30, %29 ]
%36 = call i32 @fclose(i32* %3)
br label %37
37: ; preds = %0, %34
%38 = phi i32 [ %35, %34 ], [ 0, %0 ]
ret i32 %38
}