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

/* Variables and functions */
long atol(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *memchr(char *, char, int);
int /*<<< orphan*/ setLogging(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
long getjiffies(int pid) {
  static char stat[20];
  static char buffer[1024];
  sprintf(stat, "/proc/%d/stat", pid);
  FILE *f = fopen(stat, "r");
  if (f == NULL) {
    setLogging("Can not open /proc/%d/stat", pid);
    return -1;
  }
  fgets(buffer, sizeof(buffer), f);
  fclose(f);
  char *p = buffer;
  p = memchr(p + 1, ')', sizeof(buffer) - (p - buffer));
  int sp = 12;
  while (sp--)
    p = memchr(p + 1, ' ', sizeof(buffer) - (p - buffer));
  long utime = atol(p + 1);
  p = memchr(p + 1, ' ', sizeof(buffer) - (p - buffer));
  long ktime = atol(p + 1);
  return utime + ktime;
}


x86 assembly (clang, -O0):
<getjiffies>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edx
lea    0x2eeb(%rip),%rdi
lea    0xe83(%rip),%rsi
call   1090 <sprintf@plt>
lea    0x2ed8(%rip),%rdi
lea    0xe61(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11cf <getjiffies+0x5f>
mov    -0xc(%rbp),%esi
lea    0xe45(%rip),%rdi
call   1040 <setLogging@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12d0 <getjiffies+0x160>
mov    -0x18(%rbp),%rdx
lea    0x2eb6(%rip),%rdi
mov    $0x400,%esi
call   1060 <fgets@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
lea    0x2e9c(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
mov    -0x20(%rbp),%rcx
lea    0x2e85(%rip),%rax
sub    %rax,%rcx
mov    $0x400,%eax
sub    %rcx,%rax
mov    %eax,%edx
mov    $0x29,%esi
call   1050 <memchr@plt>
mov    %rax,-0x20(%rbp)
movl   $0xc,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x24(%rbp)
cmp    $0x0,%eax
je     1274 <getjiffies+0x104>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
mov    -0x20(%rbp),%rcx
lea    0x2e3c(%rip),%rax
sub    %rax,%rcx
mov    $0x400,%eax
sub    %rcx,%rax
mov    %eax,%edx
mov    $0x20,%esi
call   1050 <memchr@plt>
mov    %rax,-0x20(%rbp)
jmp    122d <getjiffies+0xbd>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1070 <atol@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
mov    -0x20(%rbp),%rcx
lea    0x2df8(%rip),%rax
sub    %rax,%rcx
mov    $0x400,%eax
sub    %rcx,%rax
mov    %eax,%edx
mov    $0x20,%esi
call   1050 <memchr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1070 <atol@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getjiffies>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebp
lea    0x2ef0(%rip),%rbx
lea    0xe88(%rip),%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
call   1090 <sprintf@plt>
lea    0xe68(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     12ff <getjiffies+0x18f>
mov    %rax,%rbx
lea    0x2edd(%rip),%r14
mov    %r14,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0x2ebf(%rip),%rdi
mov    $0x29,%esi
mov    $0x400,%edx
call   1050 <memchr@plt>
lea    0x1(%rax),%rdi
add    $0x400,%r14d
mov    %r14d,%edx
sub    %eax,%edx
mov    $0x20,%esi
call   1050 <memchr@plt>
lea    0x1(%rax),%rdi
mov    %r14d,%edx
sub    %eax,%edx
mov    $0x20,%esi
call   1050 <memchr@plt>
lea    0x1(%rax),%rdi
mov    %r14d,%edx
sub    %eax,%edx
mov    $0x20,%esi
call   1050 <memchr@plt>
lea    0x1(%rax),%rdi
mov    %r14d,%edx
sub    %eax,%edx
mov    $0x20,%esi
call   1050 <memchr@plt>
lea    0x1(%rax),%rdi
mov    %r14d,%edx
sub    %eax,%edx
mov    $0x20,%esi
call   1050 <memchr@plt>
lea    0x1(%rax),%rdi
mov    %r14d,%edx
sub    %eax,%edx
mov    $0x20,%esi
call   1050 <memchr@plt>
lea    0x1(%rax),%rdi
mov    %r14d,%edx
sub    %eax,%edx
mov    $0x20,%esi
call   1050 <memchr@plt>
lea    0x1(%rax),%rdi
mov    %r14d,%edx
sub    %eax,%edx
mov    $0x20,%esi
call   1050 <memchr@plt>
lea    0x1(%rax),%rdi
mov    %r14d,%edx
sub    %eax,%edx
mov    $0x20,%esi
call   1050 <memchr@plt>
lea    0x1(%rax),%rdi
mov    %r14d,%edx
sub    %eax,%edx
mov    $0x20,%esi
call   1050 <memchr@plt>
lea    0x1(%rax),%rdi
mov    %r14d,%edx
sub    %eax,%edx
mov    $0x20,%esi
call   1050 <memchr@plt>
lea    0x1(%rax),%rdi
mov    %r14d,%edx
sub    %eax,%edx
mov    $0x20,%esi
call   1050 <memchr@plt>
mov    %rax,%rbx
lea    0x1(%rax),%rbp
mov    %rbp,%rdi
call   1070 <atol@plt>
mov    %rax,%r15
sub    %ebx,%r14d
mov    %rbp,%rdi
mov    $0x20,%esi
mov    %r14d,%edx
call   1050 <memchr@plt>
lea    0x1(%rax),%rdi
call   1070 <atol@plt>
add    %r15,%rax
jmp    1314 <getjiffies+0x1a4>
lea    0xcfc(%rip),%rdi
mov    %ebp,%esi
call   1040 <setLogging@plt>
mov    $0xffffffffffffffff,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@getjiffies.stat = internal global [20 x i8] zeroinitializer
@getjiffies.buffer = internal global [1024 x i8] zeroinitializer
@.str = [14 x i8] c"/proc/%d/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [27 x i8] c"Can not open /proc/%d/stat\00"
define i64 @getjiffies(i32 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %3
%9 = load i32, i32* %3
%10 = call i32 @sprintf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @getjiffies.stat, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = call i32* @fopen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @getjiffies.stat, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %4
%12 = load i32*, i32** %4
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load i32, i32* %3
%16 = call i32 @setLogging(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %15)
store i64 -1, i64* %2
br label %61
17: ; preds = %1
%18 = load i32*, i32** %4
%19 = call i32 @fgets(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @getjiffies.buffer, i64 0, i64 0), i32 1024, i32* %18)
%20 = load i32*, i32** %4
%21 = call i32 @fclose(i32* %20)
store i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @getjiffies.buffer, i64 0, i64 0), i8** %5
%22 = load i8*, i8** %5
%23 = getelementptr inbounds i8, i8* %22, i64 1
%24 = load i8*, i8** %5
%25 = ptrtoint i8* %24 to i64
%26 = sub i64 %25, ptrtoint ([1024 x i8]* @getjiffies.buffer to i64)
%27 = sub i64 1024, %26
%28 = trunc i64 %27 to i32
%29 = call i8* @memchr(i8* %23, i8 signext 41, i32 %28)
store i8* %29, i8** %5
store i32 12, i32* %6
br label %30
30: ; preds = %34, %17
%31 = load i32, i32* %6
%32 = add nsw i32 %31, -1
store i32 %32, i32* %6
%33 = icmp ne i32 %31, 0
br i1 %33, label %34, label %43
34: ; preds = %30
%35 = load i8*, i8** %5
%36 = getelementptr inbounds i8, i8* %35, i64 1
%37 = load i8*, i8** %5
%38 = ptrtoint i8* %37 to i64
%39 = sub i64 %38, ptrtoint ([1024 x i8]* @getjiffies.buffer to i64)
%40 = sub i64 1024, %39
%41 = trunc i64 %40 to i32
%42 = call i8* @memchr(i8* %36, i8 signext 32, i32 %41)
store i8* %42, i8** %5
br label %30
43: ; preds = %30
%44 = load i8*, i8** %5
%45 = getelementptr inbounds i8, i8* %44, i64 1
%46 = call i64 @atol(i8* %45)
store i64 %46, i64* %7
%47 = load i8*, i8** %5
%48 = getelementptr inbounds i8, i8* %47, i64 1
%49 = load i8*, i8** %5
%50 = ptrtoint i8* %49 to i64
%51 = sub i64 %50, ptrtoint ([1024 x i8]* @getjiffies.buffer to i64)
%52 = sub i64 1024, %51
%53 = trunc i64 %52 to i32
%54 = call i8* @memchr(i8* %48, i8 signext 32, i32 %53)
store i8* %54, i8** %5
%55 = load i8*, i8** %5
%56 = getelementptr inbounds i8, i8* %55, i64 1
%57 = call i64 @atol(i8* %56)
store i64 %57, i64* %8
%58 = load i64, i64* %7
%59 = load i64, i64* %8
%60 = add nsw i64 %58, %59
store i64 %60, i64* %2
br label %61
61: ; preds = %43, %14
%62 = load i64, i64* %2
ret i64 %62
}

LLVM IR (-O2):
@getjiffies.stat = internal global [20 x i8] zeroinitializer
@getjiffies.buffer = internal global [1024 x i8] zeroinitializer
@.str = [14 x i8] c"/proc/%d/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [27 x i8] c"Can not open /proc/%d/stat\00"
define i64 @getjiffies(i32 %0) {
%2 = tail call i32 @sprintf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @getjiffies.stat, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @getjiffies.stat, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @setLogging(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %0)
br label %80
7: ; preds = %1
%8 = tail call i32 @fgets(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @getjiffies.buffer, i64 0, i64 0), i32 1024, i32* %3)
%9 = tail call i32 @fclose(i32* %3)
%10 = tail call i8* @memchr(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @getjiffies.buffer, i64 0, i64 1), i8 signext 41, i32 1024)
%11 = getelementptr inbounds i8, i8* %10, i64 1
%12 = ptrtoint i8* %10 to i64
%13 = trunc i64 %12 to i32
%14 = sub i32 trunc (i64 add (i64 ptrtoint ([1024 x i8]* @getjiffies.buffer to i64), i64 1024) to i32), %13
%15 = tail call i8* @memchr(i8* %11, i8 signext 32, i32 %14)
%16 = getelementptr inbounds i8, i8* %15, i64 1
%17 = ptrtoint i8* %15 to i64
%18 = trunc i64 %17 to i32
%19 = sub i32 trunc (i64 add (i64 ptrtoint ([1024 x i8]* @getjiffies.buffer to i64), i64 1024) to i32), %18
%20 = tail call i8* @memchr(i8* %16, i8 signext 32, i32 %19)
%21 = getelementptr inbounds i8, i8* %20, i64 1
%22 = ptrtoint i8* %20 to i64
%23 = trunc i64 %22 to i32
%24 = sub i32 trunc (i64 add (i64 ptrtoint ([1024 x i8]* @getjiffies.buffer to i64), i64 1024) to i32), %23
%25 = tail call i8* @memchr(i8* %21, i8 signext 32, i32 %24)
%26 = getelementptr inbounds i8, i8* %25, i64 1
%27 = ptrtoint i8* %25 to i64
%28 = trunc i64 %27 to i32
%29 = sub i32 trunc (i64 add (i64 ptrtoint ([1024 x i8]* @getjiffies.buffer to i64), i64 1024) to i32), %28
%30 = tail call i8* @memchr(i8* %26, i8 signext 32, i32 %29)
%31 = getelementptr inbounds i8, i8* %30, i64 1
%32 = ptrtoint i8* %30 to i64
%33 = trunc i64 %32 to i32
%34 = sub i32 trunc (i64 add (i64 ptrtoint ([1024 x i8]* @getjiffies.buffer to i64), i64 1024) to i32), %33
%35 = tail call i8* @memchr(i8* %31, i8 signext 32, i32 %34)
%36 = getelementptr inbounds i8, i8* %35, i64 1
%37 = ptrtoint i8* %35 to i64
%38 = trunc i64 %37 to i32
%39 = sub i32 trunc (i64 add (i64 ptrtoint ([1024 x i8]* @getjiffies.buffer to i64), i64 1024) to i32), %38
%40 = tail call i8* @memchr(i8* %36, i8 signext 32, i32 %39)
%41 = getelementptr inbounds i8, i8* %40, i64 1
%42 = ptrtoint i8* %40 to i64
%43 = trunc i64 %42 to i32
%44 = sub i32 trunc (i64 add (i64 ptrtoint ([1024 x i8]* @getjiffies.buffer to i64), i64 1024) to i32), %43
%45 = tail call i8* @memchr(i8* %41, i8 signext 32, i32 %44)
%46 = getelementptr inbounds i8, i8* %45, i64 1
%47 = ptrtoint i8* %45 to i64
%48 = trunc i64 %47 to i32
%49 = sub i32 trunc (i64 add (i64 ptrtoint ([1024 x i8]* @getjiffies.buffer to i64), i64 1024) to i32), %48
%50 = tail call i8* @memchr(i8* %46, i8 signext 32, i32 %49)
%51 = getelementptr inbounds i8, i8* %50, i64 1
%52 = ptrtoint i8* %50 to i64
%53 = trunc i64 %52 to i32
%54 = sub i32 trunc (i64 add (i64 ptrtoint ([1024 x i8]* @getjiffies.buffer to i64), i64 1024) to i32), %53
%55 = tail call i8* @memchr(i8* %51, i8 signext 32, i32 %54)
%56 = getelementptr inbounds i8, i8* %55, i64 1
%57 = ptrtoint i8* %55 to i64
%58 = trunc i64 %57 to i32
%59 = sub i32 trunc (i64 add (i64 ptrtoint ([1024 x i8]* @getjiffies.buffer to i64), i64 1024) to i32), %58
%60 = tail call i8* @memchr(i8* %56, i8 signext 32, i32 %59)
%61 = getelementptr inbounds i8, i8* %60, i64 1
%62 = ptrtoint i8* %60 to i64
%63 = trunc i64 %62 to i32
%64 = sub i32 trunc (i64 add (i64 ptrtoint ([1024 x i8]* @getjiffies.buffer to i64), i64 1024) to i32), %63
%65 = tail call i8* @memchr(i8* %61, i8 signext 32, i32 %64)
%66 = getelementptr inbounds i8, i8* %65, i64 1
%67 = ptrtoint i8* %65 to i64
%68 = trunc i64 %67 to i32
%69 = sub i32 trunc (i64 add (i64 ptrtoint ([1024 x i8]* @getjiffies.buffer to i64), i64 1024) to i32), %68
%70 = tail call i8* @memchr(i8* %66, i8 signext 32, i32 %69)
%71 = getelementptr inbounds i8, i8* %70, i64 1
%72 = tail call i64 @atol(i8* %71)
%73 = ptrtoint i8* %70 to i64
%74 = trunc i64 %73 to i32
%75 = sub i32 trunc (i64 add (i64 ptrtoint ([1024 x i8]* @getjiffies.buffer to i64), i64 1024) to i32), %74
%76 = tail call i8* @memchr(i8* %71, i8 signext 32, i32 %75)
%77 = getelementptr inbounds i8, i8* %76, i64 1
%78 = tail call i64 @atol(i8* %77)
%79 = add nsw i64 %78, %72
br label %80
80: ; preds = %7, %5
%81 = phi i64 [ -1, %5 ], [ %79, %7 ]
ret i64 %81
}