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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ isdigit(char);
scalar_t__ isspace(char);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
char *strstr(char *, char *);
scalar_t__ strtoull(char *, char **, int /*<<< orphan*/);
long long procfs_read(void) {
  char buf[8192], *b;
  int fd, len;
  unsigned long long val;
  fd = open("/proc/interrupts", 0);
  if (fd < 0)
    return -1;
  len = read(fd, buf, sizeof(buf) - 1);
  close(fd);
  if (len < 0)
    return -1;
  buf[len] = '\0';
  b = strstr(buf, "i915");
  if (b == NULL)
    return -1;
  while (*--b != ':')
    ;
  val = 0;
  do {
    while (isspace(*++b))
      ;
    if (!isdigit(*b))
      break;
    val += strtoull(b, &b, 0);
  } while (1);
  return val;
}


x86 assembly (clang, -O0):
<procfs_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2030,%rsp
lea    0xe7e(%rip),%rdi
xor    %esi,%esi
call   1080 <open@plt>
mov    %eax,-0x201c(%rbp)
cmpl   $0x0,-0x201c(%rbp)
jge    11a9 <procfs_read+0x39>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12f2 <procfs_read+0x182>
mov    -0x201c(%rbp),%edi
lea    -0x2010(%rbp),%rsi
mov    $0x1fff,%edx
call   1060 <read@plt>
mov    %eax,-0x2020(%rbp)
mov    -0x201c(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x0,-0x2020(%rbp)
jge    11eb <procfs_read+0x7b>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12f2 <procfs_read+0x182>
movslq -0x2020(%rbp),%rax
movb   $0x0,-0x2010(%rbp,%rax,1)
lea    -0x2010(%rbp),%rdi
lea    0xe09(%rip),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x2018(%rbp)
cmpq   $0x0,-0x2018(%rbp)
jne    122f <procfs_read+0xbf>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12f2 <procfs_read+0x182>
jmp    1234 <procfs_read+0xc4>
mov    -0x2018(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x2018(%rbp)
movsbl -0x1(%rax),%eax
cmp    $0x3a,%eax
je     125b <procfs_read+0xeb>
jmp    1234 <procfs_read+0xc4>
movq   $0x0,-0x2028(%rbp)
jmp    126b <procfs_read+0xfb>
mov    -0x2018(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x2018(%rbp)
movsbl 0x1(%rax),%edi
call   1050 <isspace@plt>
cmp    $0x0,%rax
je     1298 <procfs_read+0x128>
jmp    126b <procfs_read+0xfb>
mov    -0x2018(%rbp),%rax
movsbl (%rax),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%eax
jne    12b5 <procfs_read+0x145>
jmp    12e7 <procfs_read+0x177>
mov    -0x2018(%rbp),%rdi
lea    -0x2018(%rbp),%rsi
xor    %edx,%edx
call   1070 <strtoull@plt>
add    -0x2028(%rbp),%rax
mov    %rax,-0x2028(%rbp)
mov    $0x1,%al
test   $0x1,%al
jne    1266 <procfs_read+0xf6>
jmp    12e7 <procfs_read+0x177>
mov    -0x2028(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x2030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<procfs_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x2018,%rsp
lea    0xe7c(%rip),%rdi
xor    %esi,%esi
call   1080 <open@plt>
mov    $0xffffffffffffffff,%r14
test   %eax,%eax
js     123a <procfs_read+0xca>
mov    %eax,%ebx
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x1fff,%edx
call   1060 <read@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
test   %ebp,%ebp
js     123a <procfs_read+0xca>
mov    %ebp,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xe45(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1090 <strstr@plt>
test   %rax,%rax
je     123a <procfs_read+0xca>
mov    %rax,%rbx
xchg   %ax,%ax
cmpb   $0x3a,-0x1(%rbx)
lea    -0x1(%rbx),%rbx
jne    11e0 <procfs_read+0x70>
mov    %rbx,0x8(%rsp)
xor    %r14d,%r14d
lea    0x8(%rsp),%r15
nopw   0x0(%rax,%rax,1)
movsbl 0x1(%rbx),%edi
add    $0x1,%rbx
call   1050 <isspace@plt>
test   %rax,%rax
jne    1200 <procfs_read+0x90>
mov    %rbx,0x8(%rsp)
movsbl (%rbx),%edi
call   1030 <isdigit@plt>
test   %eax,%eax
je     123a <procfs_read+0xca>
mov    %rbx,%rdi
mov    %r15,%rsi
xor    %edx,%edx
call   1070 <strtoull@plt>
add    %rax,%r14
mov    0x8(%rsp),%rbx
jmp    1200 <procfs_read+0x90>
mov    %r14,%rax
add    $0x2018,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/interrupts\00"
@.str.1 = [5 x i8] c"i915\00"
define i64 @procfs_read() {
%1 = alloca i64
%2 = alloca [8192 x i8]
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 0)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %0
store i64 -1, i64* %1
br label %60
11: ; preds = %0
%12 = load i32, i32* %4
%13 = getelementptr inbounds [8192 x i8], [8192 x i8]* %2, i64 0, i64 0
%14 = call i32 @read(i32 %12, i8* %13, i32 8191)
store i32 %14, i32* %5
%15 = load i32, i32* %4
%16 = call i32 @close(i32 %15)
%17 = load i32, i32* %5
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %11
store i64 -1, i64* %1
br label %60
20: ; preds = %11
%21 = load i32, i32* %5
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [8192 x i8], [8192 x i8]* %2, i64 0, i64 %22
store i8 0, i8* %23
%24 = getelementptr inbounds [8192 x i8], [8192 x i8]* %2, i64 0, i64 0
%25 = call i8* @strstr(i8* %24, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %25, i8** %3
%26 = load i8*, i8** %3
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %29
28: ; preds = %20
store i64 -1, i64* %1
br label %60
29: ; preds = %20
br label %30
30: ; preds = %36, %29
%31 = load i8*, i8** %3
%32 = getelementptr inbounds i8, i8* %31, i32 -1
store i8* %32, i8** %3
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp ne i32 %34, 58
br i1 %35, label %36, label %37
36: ; preds = %30
br label %30
37: ; preds = %30
store i64 0, i64* %6
br label %38
38: ; preds = %57, %37
br label %39
39: ; preds = %45, %38
%40 = load i8*, i8** %3
%41 = getelementptr inbounds i8, i8* %40, i32 1
store i8* %41, i8** %3
%42 = load i8, i8* %41
%43 = call i64 @isspace(i8 signext %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %39
br label %39
46: ; preds = %39
%47 = load i8*, i8** %3
%48 = load i8, i8* %47
%49 = call i32 @isdigit(i8 signext %48)
%50 = icmp ne i32 %49, 0
br i1 %50, label %52, label %51
51: ; preds = %46
br label %58
52: ; preds = %46
%53 = load i8*, i8** %3
%54 = call i64 @strtoull(i8* %53, i8** %3, i32 0)
%55 = load i64, i64* %6
%56 = add i64 %55, %54
store i64 %56, i64* %6
br label %57
57: ; preds = %52
br i1 true, label %38, label %58
58: ; preds = %57, %51
%59 = load i64, i64* %6
store i64 %59, i64* %1
br label %60
60: ; preds = %58, %28, %19, %10
%61 = load i64, i64* %1
ret i64 %61
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/interrupts\00"
@.str.1 = [5 x i8] c"i915\00"
define i64 @procfs_read() {
%1 = alloca [8192 x i8]
%2 = alloca i8*
%3 = getelementptr inbounds [8192 x i8], [8192 x i8]* %1, i64 0, i64 0
%4 = bitcast i8** %2 to i8*
%5 = tail call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %39, label %7
7: ; preds = %0
%8 = call i32 @read(i32 %5, i8* %3, i32 8191)
%9 = tail call i32 @close(i32 %5)
%10 = icmp slt i32 %8, 0
br i1 %10, label %39, label %11
11: ; preds = %7
%12 = zext i32 %8 to i64
%13 = getelementptr inbounds [8192 x i8], [8192 x i8]* %1, i64 0, i64 %12
store i8 0, i8* %13
%14 = call i8* @strstr(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i8* %14, null
br i1 %15, label %39, label %16
16: ; preds = %11, %16
%17 = phi i8* [ %18, %16 ], [ %14, %11 ]
%18 = getelementptr inbounds i8, i8* %17, i64 -1
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 58
br i1 %20, label %21, label %16
21: ; preds = %16
store i8* %18, i8** %2
br label %22
22: ; preds = %21, %35
%23 = phi i8* [ %38, %35 ], [ %18, %21 ]
%24 = phi i64 [ %37, %35 ], [ 0, %21 ]
br label %25
25: ; preds = %25, %22
%26 = phi i8* [ %27, %25 ], [ %23, %22 ]
%27 = getelementptr inbounds i8, i8* %26, i64 1
%28 = load i8, i8* %27
%29 = call i64 @isspace(i8 signext %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %25
31: ; preds = %25
store i8* %27, i8** %2
%32 = load i8, i8* %27
%33 = call i32 @isdigit(i8 signext %32)
%34 = icmp eq i32 %33, 0
br i1 %34, label %39, label %35
35: ; preds = %31
%36 = call i64 @strtoull(i8* %27, i8** %2, i32 0)
%37 = add i64 %36, %24
%38 = load i8*, i8** %2
br label %22
39: ; preds = %31, %11, %7, %0
%40 = phi i64 [ -1, %0 ], [ -1, %7 ], [ -1, %11 ], [ %24, %31 ]
ret i64 %40
}