#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*/ proc_path;
typedef int pid_t;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strlen(char *);
char *strstr(char *, char *);
int get_voluntary_ctxt_switches(pid_t tid) {
  FILE *fp;
  char proc_path[256];
  char buf[0x1000];
  char *ptr = buf;
  int count = -1;
  snprintf(proc_path, sizeof(proc_path), "/proc/self/task/%d/status", tid);
  fp = fopen(proc_path, "rb");
  if (fp != NULL) {
    fread(buf, sizeof(unsigned char), sizeof(buf), fp);
    ptr = strstr(buf, "voluntary_ctxt_switches:");
    ptr += strlen("voluntary_ctxt_switches:");
    count = atoi(ptr);
    fclose(fp);
  }
  return count;
}


x86 assembly (clang, -O0):
<get_voluntary_ctxt_switches>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1120,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x1110(%rbp),%rax
mov    %rax,-0x1118(%rbp)
movl   $0xffffffff,-0x111c(%rbp)
ff ff ff
lea    -0x110(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x100,%esi
lea    0xe54(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x110(%rbp),%rdi
lea    0xe5b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1244 <get_voluntary_ctxt_switches+0xd4>
lea    -0x1110(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1030 <fread@plt>
lea    -0x1110(%rbp),%rdi
lea    0xe22(%rip),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x1118(%rbp)
lea    0xe0f(%rip),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x1118(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x1118(%rbp)
mov    -0x1118(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x111c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x111c(%rbp),%eax
add    $0x1120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_voluntary_ctxt_switches>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1108,%rsp
mov    %edi,%ecx
lea    0xe7a(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1060 <snprintf@plt>
lea    0xe7d(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11fe <get_voluntary_ctxt_switches+0x8e>
mov    %rax,%rbx
lea    0x100(%rsp),%r14
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
lea    0xe4c(%rip),%r15
mov    %r14,%rdi
mov    %r15,%rsi
call   1090 <strstr@plt>
mov    %rax,%rbp
mov    %r15,%rdi
call   1050 <strlen@plt>
movslq %eax,%rdi
add    %rbp,%rdi
call   1080 <atoi@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    1203 <get_voluntary_ctxt_switches+0x93>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x1108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"/proc/self/task/%d/status\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [25 x i8] c"voluntary_ctxt_switches:\00"
define i32 @get_voluntary_ctxt_switches(i32 %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca [256 x i8]
%5 = alloca [4096 x i8]
%6 = alloca i8*
%7 = alloca i32
store i32 %0, i32* %2
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
store i8* %8, i8** %6
store i32 -1, i32* %7
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%10 = load i32, i32* %2
%11 = call i32 @snprintf(i8* %9, i32 256, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %3
%14 = load i32*, i32** %3
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %30
16: ; preds = %1
%17 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%18 = load i32*, i32** %3
%19 = call i32 @fread(i8* %17, i32 1, i32 4096, i32* %18)
%20 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%21 = call i8* @strstr(i8* %20, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i8* %21, i8** %6
%22 = call i32 @strlen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%23 = load i8*, i8** %6
%24 = sext i32 %22 to i64
%25 = getelementptr inbounds i8, i8* %23, i64 %24
store i8* %25, i8** %6
%26 = load i8*, i8** %6
%27 = call i32 @atoi(i8* %26)
store i32 %27, i32* %7
%28 = load i32*, i32** %3
%29 = call i32 @fclose(i32* %28)
br label %30
30: ; preds = %16, %1
%31 = load i32, i32* %7
ret i32 %31
}

LLVM IR (-O2):
@.str = [26 x i8] c"/proc/self/task/%d/status\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [25 x i8] c"voluntary_ctxt_switches:\00"
define i32 @get_voluntary_ctxt_switches(i32 %0) {
%2 = alloca [256 x i8]
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%6 = call i32 @snprintf(i8* %4, i32 256, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %17, label %9
9: ; preds = %1
%10 = call i32 @fread(i8* %5, i32 1, i32 4096, i32* %7)
%11 = call i8* @strstr(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%12 = call i32 @strlen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %11, i64 %13
%15 = call i32 @atoi(i8* %14)
%16 = call i32 @fclose(i32* %7)
br label %17
17: ; preds = %9, %1
%18 = phi i32 [ %15, %9 ], [ -1, %1 ]
ret i32 %18
}