#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*/ sscanf(char *, char *, char *, unsigned long *);
scalar_t__ strcasecmp(char *, char *);
unsigned long get_num_switches() {
  unsigned long val;
  FILE *fp;
  char line[256], name[64];
  if ((fp = fopen("/proc/stat", "r")) == NULL)
    return (0);
  while (fgets(line, sizeof line, fp) != NULL) {
    sscanf(line, "%s %lu", name, &val);
    if (strcasecmp(name, "ctxt") != 0)
      continue;
    fclose(fp);
    return (val);
  }
  fclose(fp);
  return (0);
}


x86 assembly (clang, -O0):
<get_num_switches>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
lea    0xe9e(%rip),%rdi
lea    0xea2(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1189 <get_num_switches+0x39>
movq   $0x0,-0x8(%rbp)
jmp    1214 <get_num_switches+0xc4>
jmp    118e <get_num_switches+0x3e>
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x100,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1203 <get_num_switches+0xb3>
lea    -0x120(%rbp),%rdi
lea    -0x160(%rbp),%rdx
lea    0xe4b(%rip),%rsi
lea    -0x10(%rbp),%rcx
call   1060 <sscanf@plt>
lea    -0x160(%rbp),%rdi
lea    0xe3b(%rip),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%rax
je     11ed <get_num_switches+0x9d>
jmp    118e <get_num_switches+0x3e>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1214 <get_num_switches+0xc4>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_num_switches>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x158,%rsp
lea    0xe98(%rip),%rdi
lea    0xe9c(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11ed <get_num_switches+0x9d>
mov    %rax,%rbx
lea    0x50(%rsp),%r14
lea    0xe85(%rip),%r15
lea    0x10(%rsp),%r12
lea    0x8(%rsp),%r13
lea    0xe7b(%rip),%rbp
nopl   0x0(%rax)
mov    %r14,%rdi
mov    $0x100,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11e5 <get_num_switches+0x95>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
call   1060 <sscanf@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
call   1030 <strcasecmp@plt>
test   %rax,%rax
jne    11a0 <get_num_switches+0x50>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x8(%rsp),%rax
jmp    11ef <get_num_switches+0x9f>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x158,%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 = [7 x i8] c"%s %lu\00"
@.str.3 = [5 x i8] c"ctxt\00"
define i64 @get_num_switches() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i32*
%4 = alloca [256 x i8]
%5 = alloca [64 x i8]
%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** %3
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %9
8: ; preds = %0
store i64 0, i64* %1
br label %30
9: ; preds = %0
br label %10
10: ; preds = %22, %9
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%12 = load i32*, i32** %3
%13 = call i32* @fgets(i8* %11, i32 256, i32* %12)
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %27
15: ; preds = %10
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%17 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%18 = call i32 @sscanf(i8* %16, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %17, i64* %2)
%19 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%20 = call i64 @strcasecmp(i8* %19, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %15
br label %10
23: ; preds = %15
%24 = load i32*, i32** %3
%25 = call i32 @fclose(i32* %24)
%26 = load i64, i64* %2
store i64 %26, i64* %1
br label %30
27: ; preds = %10
%28 = load i32*, i32** %3
%29 = call i32 @fclose(i32* %28)
store i64 0, i64* %1
br label %30
30: ; preds = %27, %23, %8
%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 = [7 x i8] c"%s %lu\00"
@.str.3 = [5 x i8] c"ctxt\00"
define i64 @get_num_switches() {
%1 = alloca i64
%2 = alloca [256 x i8]
%3 = alloca [64 x i8]
%4 = bitcast i64* %1 to i8*
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%7 = 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))
%8 = icmp eq i32* %7, null
br i1 %8, label %21, label %9
9: ; preds = %0, %12
%10 = call i32* @fgets(i8* %5, i32 256, i32* %7)
%11 = icmp eq i32* %10, null
br i1 %11, label %19, label %12
12: ; preds = %9
%13 = call i32 @sscanf(i8* %5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %6, i64* %1)
%14 = call i64 @strcasecmp(i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %9
16: ; preds = %12
%17 = call i32 @fclose(i32* %7)
%18 = load i64, i64* %1
br label %21
19: ; preds = %9
%20 = call i32 @fclose(i32* %7)
br label %21
21: ; preds = %0, %19, %16
%22 = phi i64 [ %18, %16 ], [ 0, %19 ], [ 0, %0 ]
ret i64 %22
}