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

/* Variables and functions */
char *getLineFromFile(char *, char *, char *, int, int *);
int /*<<< orphan*/ printf(char *, ...);
int printNumContextSwitches() {
  printf("\n\n-- Number of context switches:\n\t\t");
  int lineLength = 0;
  char *lineFromFile =
      getLineFromFile("/proc/stat", "rb", "ctxt", 4, &lineLength);
  int k = 0;
  int wordNumber = 1;
  while (k < lineLength) {
    if (lineFromFile[k] == ' ') {
      wordNumber++;
      k++;
    }
    if (wordNumber == 2)
      printf("%c", lineFromFile[k]);
    k++;
  }
  return 0;
}


x86 assembly (clang, -O0):
<printNumContextSwitches>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xed1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
lea    0xee0(%rip),%rdi
lea    0xee4(%rip),%rsi
lea    0xee0(%rip),%rdx
mov    $0x4,%ecx
lea    -0x4(%rbp),%r8
call   1040 <getLineFromFile@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11d7 <printNumContextSwitches+0xb7>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x20,%eax
jne    11a5 <printNumContextSwitches+0x85>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x2,-0x18(%rbp)
jne    11c9 <printNumContextSwitches+0xa9>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%esi
lea    0xe75(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1172 <printNumContextSwitches+0x52>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<printNumContextSwitches>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xec1(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x0,0xc(%rsp)
lea    0xecd(%rip),%rdi
lea    0xed1(%rip),%rsi
lea    0xecd(%rip),%rdx
lea    0xc(%rsp),%r8
mov    $0x4,%ecx
call   1050 <getLineFromFile@plt>
mov    %rax,%r14
mov    0xc(%rsp),%eax
test   %eax,%eax
jle    11c0 <printNumContextSwitches+0x90>
mov    $0x1,%ebp
jmp    1197 <printNumContextSwitches+0x67>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%ebx
cmp    %eax,%ebx
jge    11c0 <printNumContextSwitches+0x90>
movslq %ebx,%rbx
xor    %ecx,%ecx
cmpb   $0x20,(%r14,%rbx,1)
sete   %cl
add    %ecx,%ebx
add    %ecx,%ebp
cmp    $0x2,%ebp
jne    1190 <printNumContextSwitches+0x60>
movslq %ebx,%rax
movsbl (%r14,%rax,1),%edi
call   1030 <putchar@plt>
mov    0xc(%rsp),%eax
jmp    1190 <printNumContextSwitches+0x60>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [36 x i8] c"\0A\0A-- Number of context switches:\0A\09\09\00"
@.str.1 = [11 x i8] c"/proc/stat\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [5 x i8] c"ctxt\00"
@.str.4 = [3 x i8] c"%c\00"
define i32 @printNumContextSwitches() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
%6 = call i8* @getLineFromFile(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 4, i32* %1)
store i8* %6, i8** %2
store i32 0, i32* %3
store i32 1, i32* %4
br label %7
7: ; preds = %35, %0
%8 = load i32, i32* %3
%9 = load i32, i32* %1
%10 = icmp slt i32 %8, %9
br i1 %10, label %11, label %38
11: ; preds = %7
%12 = load i8*, i8** %2
%13 = load i32, i32* %3
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 32
br i1 %18, label %19, label %24
19: ; preds = %11
%20 = load i32, i32* %4
%21 = add nsw i32 %20, 1
store i32 %21, i32* %4
%22 = load i32, i32* %3
%23 = add nsw i32 %22, 1
store i32 %23, i32* %3
br label %24
24: ; preds = %19, %11
%25 = load i32, i32* %4
%26 = icmp eq i32 %25, 2
br i1 %26, label %27, label %35
27: ; preds = %24
%28 = load i8*, i8** %2
%29 = load i32, i32* %3
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %33)
br label %35
35: ; preds = %27, %24
%36 = load i32, i32* %3
%37 = add nsw i32 %36, 1
store i32 %37, i32* %3
br label %7
38: ; preds = %7
ret i32 0
}

LLVM IR (-O2):
@.str = [36 x i8] c"\0A\0A-- Number of context switches:\0A\09\09\00"
@.str.1 = [11 x i8] c"/proc/stat\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [5 x i8] c"ctxt\00"
define i32 @printNumContextSwitches() {
%1 = alloca i32
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%3 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%4 = call i8* @getLineFromFile(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 4, i32* %1)
%5 = load i32, i32* %1
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %30
7: ; preds = %0, %26
%8 = phi i32 [ %27, %26 ], [ %5, %0 ]
%9 = phi i32 [ %17, %26 ], [ 1, %0 ]
%10 = phi i32 [ %28, %26 ], [ 0, %0 ]
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %4, i64 %11
%13 = load i8, i8* %12
%14 = icmp eq i8 %13, 32
%15 = zext i1 %14 to i32
%16 = add nsw i32 %10, %15
%17 = add nuw nsw i32 %9, %15
%18 = icmp eq i32 %17, 2
br i1 %18, label %19, label %26
19: ; preds = %7
%20 = sext i32 %16 to i64
%21 = getelementptr inbounds i8, i8* %4, i64 %20
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = call i32 @putchar(i32 %23)
%25 = load i32, i32* %1
br label %26
26: ; preds = %19, %7
%27 = phi i32 [ %25, %19 ], [ %8, %7 ]
%28 = add nsw i32 %16, 1
%29 = icmp slt i32 %28, %27
br i1 %29, label %7, label %30
30: ; preds = %26, %0
ret i32 0
}