#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*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sscanf(char *, char *, char *, int *);
int /*<<< orphan*/ *strstr(char *, char *);
long contextSwitchesSinceBoot() {
  FILE *stat = fopen("/proc/stat", "r");
  if (stat == NULL) {
    perror("Unable to open /proc/stat.");
    return -1l;
  }
  char ch_buf[256];
  while (fgets(ch_buf, sizeof ch_buf, stat) != NULL) {
    if (strstr(ch_buf, "ctxt") != NULL) {
      char label[32];
      int switch_count;
      sscanf(ch_buf, "%s %d", label, &switch_count);
      return switch_count;
    }
  }
  perror("Unable to parse /proc/stat. No process count was found.");
  return 0l;
}


x86 assembly (clang, -O0):
<contextSwitchesSinceBoot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
lea    0xe9e(%rip),%rdi
lea    0xea2(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1196 <contextSwitchesSinceBoot+0x46>
lea    0xe89(%rip),%rdi
call   1060 <perror@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1221 <contextSwitchesSinceBoot+0xd1>
jmp    119b <contextSwitchesSinceBoot+0x4b>
lea    -0x110(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x100,%esi
call   1030 <fgets@plt>
cmp    $0x0,%rax
je     120d <contextSwitchesSinceBoot+0xbd>
lea    -0x110(%rbp),%rdi
lea    0xe60(%rip),%rsi
call   1070 <strstr@plt>
cmp    $0x0,%rax
je     1208 <contextSwitchesSinceBoot+0xb8>
lea    -0x110(%rbp),%rdi
lea    -0x130(%rbp),%rdx
lea    0xe41(%rip),%rsi
lea    -0x134(%rbp),%rcx
call   1040 <sscanf@plt>
movslq -0x134(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1221 <contextSwitchesSinceBoot+0xd1>
jmp    119b <contextSwitchesSinceBoot+0x4b>
lea    0xe1f(%rip),%rdi
call   1060 <perror@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<contextSwitchesSinceBoot>:
push   %r15
push   %r14
push   %rbx
sub    $0x130,%rsp
lea    0xe9d(%rip),%rdi
lea    0xea1(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11e2 <contextSwitchesSinceBoot+0x92>
mov    %rax,%r14
lea    0x30(%rsp),%rbx
lea    0xea5(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %r14,%rdx
call   1030 <fgets@plt>
test   %rax,%rax
je     11f7 <contextSwitchesSinceBoot+0xa7>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     1190 <contextSwitchesSinceBoot+0x40>
lea    0xe71(%rip),%rsi
lea    0x30(%rsp),%rdi
lea    0x10(%rsp),%rdx
lea    0xc(%rsp),%rcx
call   1040 <sscanf@plt>
movslq 0xc(%rsp),%rax
add    $0x130,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe24(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffffffffffff,%rax
jmp    11d5 <contextSwitchesSinceBoot+0x85>
lea    0xe35(%rip),%rdi
call   1060 <perror@plt>
xor    %eax,%eax
jmp    11d5 <contextSwitchesSinceBoot+0x85>

LLVM IR (-O0):
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [27 x i8] c"Unable to open /proc/stat.\00"
@.str.3 = [5 x i8] c"ctxt\00"
@.str.4 = [6 x i8] c"%s %d\00"
@.str.5 = [56 x i8] c"Unable to parse /proc/stat. No process count was found.\00"
define i64 @contextSwitchesSinceBoot() {
%1 = alloca i64
%2 = alloca i32*
%3 = alloca [256 x i8]
%4 = alloca [32 x i8]
%5 = alloca i32
%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 = load i32*, i32** %2
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
store i64 -1, i64* %1
br label %30
11: ; preds = %0
br label %12
12: ; preds = %27, %11
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%14 = load i32*, i32** %2
%15 = call i32* @fgets(i8* %13, i32 256, i32* %14)
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %28
17: ; preds = %12
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%19 = call i32* @strstr(i8* %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%20 = icmp ne i32* %19, null
br i1 %20, label %21, label %27
21: ; preds = %17
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%23 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%24 = call i32 @sscanf(i8* %22, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %23, i32* %5)
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
store i64 %26, i64* %1
br label %30
27: ; preds = %17
br label %12
28: ; preds = %12
%29 = call i32 @perror(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.5, i64 0, i64 0))
store i64 0, i64* %1
br label %30
30: ; preds = %28, %21, %9
%31 = load i64, i64* %1
ret i64 %31
}

LLVM IR (-O2):
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [27 x i8] c"Unable to open /proc/stat.\00"
@.str.3 = [5 x i8] c"ctxt\00"
@.str.4 = [6 x i8] c"%s %d\00"
@.str.5 = [56 x i8] c"Unable to parse /proc/stat. No process count was found.\00"
define i64 @contextSwitchesSinceBoot() {
%1 = alloca [256 x i8]
%2 = alloca [32 x i8]
%3 = alloca i32
%4 = 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))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %26
8: ; preds = %0
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
br label %10
10: ; preds = %13, %8
%11 = call i32* @fgets(i8* %9, i32 256, i32* %4)
%12 = icmp eq i32* %11, null
br i1 %12, label %22, label %13
13: ; preds = %10
%14 = call i32* @strstr(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %10, label %16
16: ; preds = %13
%17 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%18 = bitcast i32* %3 to i8*
%19 = call i32 @sscanf(i8* %9, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %17, i32* %3)
%20 = load i32, i32* %3
%21 = sext i32 %20 to i64
br label %24
22: ; preds = %10
%23 = call i32 @perror(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.5, i64 0, i64 0))
br label %24
24: ; preds = %22, %16
%25 = phi i64 [ %21, %16 ], [ 0, %22 ]
br label %26
26: ; preds = %24, %6
%27 = phi i64 [ -1, %6 ], [ %25, %24 ]
ret i64 %27
}