#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 */
struct timespec {
  long long tv_sec;
  long long tv_nsec;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ SIGKILL;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fork();
int fscanf(int /*<<< orphan*/ *, char *, unsigned long *);
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ pause();
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ wait(int *);
unsigned long long getbootlinux(long hertz) {
  int cpid;
  char tmpbuf[1280];
  FILE *fp;
  unsigned long startticks;
  unsigned long long bootjiffies = 0;
  struct timespec ts;
  if ((cpid = fork()) == 0) {
    pause();
  } else {
    (void)clock_gettime(CLOCK_REALTIME, &ts);
    bootjiffies =
        1LL * ts.tv_sec * hertz + 1LL * ts.tv_nsec * hertz / 1000000000LL;
    snprintf(tmpbuf, sizeof tmpbuf, "/proc/%d/stat", cpid);
    if ((fp = fopen(tmpbuf, "r")) != NULL) {
      if (fscanf(fp,
                 "%*d (%*[^)]) %*c %*d %*d %*d %*d "
                 "%*d %*d %*d %*d %*d %*d %*d %*d "
                 "%*d %*d %*d %*d %*d %*d %lu",
                 &startticks) == 1) {
        bootjiffies -= startticks;
      }
      fclose(fp);
    }
    kill(cpid, SIGKILL);
    (void)wait((int *)0);
  }
  return bootjiffies;
}


x86 assembly (clang, -O0):
<getbootlinux>:
push   %rbp
mov    %rsp,%rbp
sub    $0x540,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x528(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jne    11c9 <getbootlinux+0x39>
mov    $0x0,%al
call   1080 <pause@plt>
jmp    12c3 <getbootlinux+0x133>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
lea    -0x538(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x538(%rbp),%rax
shl    $0x0,%rax
imul   -0x8(%rbp),%rax
mov    %rax,-0x540(%rbp)
mov    -0x530(%rbp),%rax
shl    $0x0,%rax
imul   -0x8(%rbp),%rax
mov    $0x3b9aca00,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x540(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x528(%rbp)
lea    -0x510(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x500,%esi
lea    0xdc7(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x510(%rbp),%rdi
lea    0xdc2(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x518(%rbp)
cmp    $0x0,%rax
je     12a9 <getbootlinux+0x119>
mov    -0x518(%rbp),%rdi
lea    0xda0(%rip),%rsi
lea    -0x520(%rbp),%rdx
call   1060 <fscanf@plt>
cmp    $0x1,%eax
jne    129d <getbootlinux+0x10d>
mov    -0x520(%rbp),%rcx
mov    -0x528(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x528(%rbp)
mov    -0x518(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0xc(%rbp),%edi
mov    0x2d2d(%rip),%rax
mov    (%rax),%esi
call   1070 <kill@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
mov    -0x528(%rbp),%rax
add    $0x540,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getbootlinux>:
push   %rbp
push   %r14
push   %rbx
sub    $0x520,%rsp
mov    %rdi,%rbx
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     1249 <getbootlinux+0xb9>
mov    %eax,%r14d
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    0x10(%rsp),%rcx
imul   %rbx,%rcx
imul   0x18(%rsp),%rbx
movabs $0x112e0be826d694b3,%rdx
0b 2e 11
mov    %rbx,%rax
imul   %rdx
mov    %rdx,%rbx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x1a,%rbx
add    %rax,%rbx
add    %rcx,%rbx
lea    0xe03(%rip),%rdx
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x500,%esi
mov    %r14d,%ecx
call   1050 <snprintf@plt>
lea    0xdf5(%rip),%rsi
mov    %rbp,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1261 <getbootlinux+0xd1>
mov    %rax,%rbp
lea    0xde0(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
call   1060 <fscanf@plt>
cmp    $0x1,%eax
jne    1254 <getbootlinux+0xc4>
mov    0x8(%rsp),%rax
jmp    1256 <getbootlinux+0xc6>
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <pause@plt>
jmp    1279 <getbootlinux+0xe9>
xor    %eax,%eax
sub    %rax,%rbx
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   1070 <kill@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
mov    %rbx,%rax
add    $0x520,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@CLOCK_REALTIME = global i32 0
@.str = [14 x i8] c"/proc/%d/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [93 x i8] c"%*d (%*[^)]) %*c %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %lu\00"
@SIGKILL = global i32 0
define i64 @getbootlinux(i64 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca [1280 x i8]
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i64
%8 = alloca %struct.timespec
store i64 %0, i64* %2
store i64 0, i64* %7
%9 = call i32 (...) @fork()
store i32 %9, i32* %3
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 (...) @pause()
br label %50
13: ; preds = %1
%14 = load i32, i32* @CLOCK_REALTIME
%15 = call i32 @clock_gettime(i32 %14, %struct.timespec* %8)
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
%17 = load i64, i64* %16
%18 = mul nsw i64 1, %17
%19 = load i64, i64* %2
%20 = mul nsw i64 %18, %19
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 1
%22 = load i64, i64* %21
%23 = mul nsw i64 1, %22
%24 = load i64, i64* %2
%25 = mul nsw i64 %23, %24
%26 = sdiv i64 %25, 1000000000
%27 = add nsw i64 %20, %26
store i64 %27, i64* %7
%28 = getelementptr inbounds [1280 x i8], [1280 x i8]* %4, i64 0, i64 0
%29 = load i32, i32* %3
%30 = call i32 @snprintf(i8* %28, i32 1280, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %29)
%31 = getelementptr inbounds [1280 x i8], [1280 x i8]* %4, i64 0, i64 0
%32 = call i32* @fopen(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %32, i32** %5
%33 = icmp ne i32* %32, null
br i1 %33, label %34, label %45
34: ; preds = %13
%35 = load i32*, i32** %5
%36 = call i32 @fscanf(i32* %35, i8* getelementptr inbounds ([93 x i8], [93 x i8]* @.str.2, i64 0, i64 0), i64* %6)
%37 = icmp eq i32 %36, 1
br i1 %37, label %38, label %42
38: ; preds = %34
%39 = load i64, i64* %6
%40 = load i64, i64* %7
%41 = sub i64 %40, %39
store i64 %41, i64* %7
br label %42
42: ; preds = %38, %34
%43 = load i32*, i32** %5
%44 = call i32 @fclose(i32* %43)
br label %45
45: ; preds = %42, %13
%46 = load i32, i32* %3
%47 = load i32, i32* @SIGKILL
%48 = call i32 @kill(i32 %46, i32 %47)
%49 = call i32 @wait(i32* null)
br label %50
50: ; preds = %45, %11
%51 = load i64, i64* %7
ret i64 %51
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@CLOCK_REALTIME = global i32 0
@.str = [14 x i8] c"/proc/%d/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [93 x i8] c"%*d (%*[^)]) %*c %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %lu\00"
@SIGKILL = global i32 0
define i64 @getbootlinux(i64 %0) {
%2 = alloca [1280 x i8]
%3 = alloca i64
%4 = alloca %struct.timespec
%5 = getelementptr inbounds [1280 x i8], [1280 x i8]* %2, i64 0, i64 0
%6 = bitcast i64* %3 to i8*
%7 = bitcast %struct.timespec* %4 to i8*
%8 = tail call i32 (...) @fork()
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = tail call i32 (...) @pause()
br label %38
12: ; preds = %1
%13 = load i32, i32* @CLOCK_REALTIME
%14 = call i32 @clock_gettime(i32 %13, %struct.timespec* %4)
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
%16 = load i64, i64* %15
%17 = mul nsw i64 %16, %0
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 1
%19 = load i64, i64* %18
%20 = mul nsw i64 %19, %0
%21 = sdiv i64 %20, 1000000000
%22 = add nsw i64 %21, %17
%23 = call i32 @snprintf(i8* %5, i32 1280, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %8)
%24 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %33, label %26
26: ; preds = %12
%27 = call i32 @fscanf(i32* %24, i8* getelementptr inbounds ([93 x i8], [93 x i8]* @.str.2, i64 0, i64 0), i64* %3)
%28 = icmp eq i32 %27, 1
%29 = load i64, i64* %3
%30 = select i1 %28, i64 %29, i64 0
%31 = sub i64 %22, %30
%32 = call i32 @fclose(i32* %24)
br label %33
33: ; preds = %26, %12
%34 = phi i64 [ %31, %26 ], [ %22, %12 ]
%35 = load i32, i32* @SIGKILL
%36 = call i32 @kill(i32 %8, i32 %35)
%37 = call i32 @wait(i32* null)
br label %38
38: ; preds = %33, %10
%39 = phi i64 [ 0, %10 ], [ %34, %33 ]
ret i64 %39
}