#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*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, unsigned long *);
__attribute__((no)) int get_cpu_ticks(void) {
  static unsigned long last_utime;
  static int fd;
  unsigned long utime, ret;
  char buf[128];
  if (fd == 0)
    fd = open("/proc/self/stat", O_RDONLY);
  lseek(fd, 0, SEEK_SET);
  buf[0] = 0;
  read(fd, buf, sizeof(buf));
  buf[sizeof(buf) - 1] = 0;
  sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu",
         &utime);
  ret = utime - last_utime;
  last_utime = utime;
  return ret;
}


x86 assembly (clang, -O0):
<get_cpu_ticks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
cmpl   $0x0,0x2efe(%rip)
jne    1173 <get_cpu_ticks+0x33>
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
lea    0xe98(%rip),%rdi
call   1060 <open@plt>
mov    %eax,0x2edd(%rip)
mov    0x2ed7(%rip),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
movb   $0x0,-0x90(%rbp)
mov    0x2eba(%rip),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1040 <read@plt>
movb   $0x0,-0x11(%rbp)
lea    -0x90(%rbp),%rdi
lea    0xe57(%rip),%rsi
lea    -0x8(%rbp),%rdx
call   1050 <sscanf@plt>
mov    -0x8(%rbp),%rax
sub    0x2e7b(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,0x2e6c(%rip)
mov    -0x10(%rbp),%rax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_cpu_ticks>:
push   %rbx
sub    $0x90,%rsp
mov    0x2f02(%rip),%edi
test   %edi,%edi
jne    116f <get_cpu_ticks+0x2f>
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
lea    0xe9e(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%edi
mov    %eax,0x2ee1(%rip)
mov    0x2e62(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
movb   $0x0,0x10(%rsp)
mov    0x2ec6(%rip),%edi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x80,%edx
call   1040 <read@plt>
movb   $0x0,0x8f(%rsp)
lea    0xe65(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rbx,%rdi
call   1050 <sscanf@plt>
mov    0x8(%rsp),%rax
mov    0x2e85(%rip),%ecx
mov    %rax,0x2e7e(%rip)
sub    %ecx,%eax
add    $0x90,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@get_cpu_ticks.last_utime = internal global i64 0
@get_cpu_ticks.fd = internal global i32 0
@.str = [16 x i8] c"/proc/self/stat\00"
@O_RDONLY = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [56 x i8] c"%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu\00"
define i32 @get_cpu_ticks() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca [128 x i8]
%4 = load i32, i32* @get_cpu_ticks.fd
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* @get_cpu_ticks.fd
br label %9
9: ; preds = %6, %0
%10 = load i32, i32* @get_cpu_ticks.fd
%11 = load i32, i32* @SEEK_SET
%12 = call i32 @lseek(i32 %10, i32 0, i32 %11)
%13 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
store i8 0, i8* %13
%14 = load i32, i32* @get_cpu_ticks.fd
%15 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%16 = call i32 @read(i32 %14, i8* %15, i32 128)
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 127
store i8 0, i8* %17
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%19 = call i32 @sscanf(i8* %18, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i64* %1)
%20 = load i64, i64* %1
%21 = load i64, i64* @get_cpu_ticks.last_utime
%22 = sub i64 %20, %21
store i64 %22, i64* %2
%23 = load i64, i64* %1
store i64 %23, i64* @get_cpu_ticks.last_utime
%24 = load i64, i64* %2
%25 = trunc i64 %24 to i32
ret i32 %25
}

LLVM IR (-O2):
@get_cpu_ticks.last_utime = internal unnamed_addr global i64 0
@get_cpu_ticks.fd = internal unnamed_addr global i32 0
@.str = [16 x i8] c"/proc/self/stat\00"
@O_RDONLY = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [56 x i8] c"%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu\00"
define i32 @get_cpu_ticks() {
%1 = alloca i64
%2 = alloca [128 x i8]
%3 = bitcast i64* %1 to i8*
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @get_cpu_ticks.fd
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* @get_cpu_ticks.fd
br label %10
10: ; preds = %7, %0
%11 = phi i32 [ %9, %7 ], [ %5, %0 ]
%12 = load i32, i32* @SEEK_SET
%13 = tail call i32 @lseek(i32 %11, i32 0, i32 %12)
store i8 0, i8* %4
%14 = load i32, i32* @get_cpu_ticks.fd
%15 = call i32 @read(i32 %14, i8* %4, i32 128)
%16 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 127
store i8 0, i8* %16
%17 = call i32 @sscanf(i8* %4, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i64* %1)
%18 = load i64, i64* %1
%19 = load i64, i64* @get_cpu_ticks.last_utime
%20 = sub i64 %18, %19
store i64 %18, i64* @get_cpu_ticks.last_utime
%21 = trunc i64 %20 to i32
ret i32 %21
}