#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 stat {
  int /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ count;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ asprintf(char **, char *, int /*<<< orphan*/, char *);
int calibrate(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(unsigned long *, int, int, int /*<<< orphan*/ *);
int fwrite(unsigned long *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ getenv(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stat(char *, struct stat *);
int rpc_calibrate() {
  int ret;
  struct stat info;
  char *path;
  unsigned long count = 0;
  asprintf(&path, "%s/%s", getenv("HOME"), ".wk-calibrate");
  printf("path=%s\n", path);
  stat(path, &info);
  if (S_ISREG(info.st_mode)) {
    FILE *f = fopen(path, "r");
    ret = fread(&count, sizeof(count), 1, f);
    fclose(f);
  } else {
    count = calibrate(10000) / 10;
    FILE *f = fopen(path, "w");
    ret = fwrite(&count, sizeof(count), 1, f);
    fclose(f);
  }
  return count;
}


x86 assembly (clang, -O0):
<rpc_calibrate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movq   $0x0,-0x18(%rbp)
lea    0xe4f(%rip),%rdi
call   1030 <getenv@plt>
mov    %eax,%edx
lea    -0x10(%rbp),%rdi
lea    0xe37(%rip),%rsi
lea    0xe3b(%rip),%rcx
call   10a0 <asprintf@plt>
mov    -0x10(%rbp),%rsi
lea    0xe39(%rip),%rdi
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x8(%rbp),%rsi
call   1070 <stat@plt>
mov    -0x8(%rbp),%edi
call   1090 <S_ISREG@plt>
cmp    $0x0,%rax
je     1240 <rpc_calibrate+0xa0>
mov    -0x10(%rbp),%rdi
lea    0xe13(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rcx
lea    -0x18(%rbp),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
call   1040 <fread@plt>
mov    %eax,-0x4(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
jmp    128f <rpc_calibrate+0xef>
mov    $0x2710,%edi
call   1080 <calibrate@plt>
mov    $0xa,%ecx
cltd
idiv   %ecx
cltq
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xdc1(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rcx
lea    -0x18(%rbp),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
call   10c0 <fwrite@plt>
mov    %eax,-0x4(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rpc_calibrate>:
push   %rbx
sub    $0x20,%rsp
movq   $0x0,0x8(%rsp)
lea    0xe51(%rip),%rdi
call   1030 <getenv@plt>
lea    0xe3f(%rip),%rsi
lea    0xe43(%rip),%rcx
lea    0x10(%rsp),%rdi
mov    %eax,%edx
call   10a0 <asprintf@plt>
mov    0x10(%rsp),%rsi
lea    0xe39(%rip),%rdi
call   1060 <printf@plt>
mov    0x10(%rsp),%rdi
lea    0x18(%rsp),%rsi
call   1070 <stat@plt>
mov    0x18(%rsp),%edi
call   1090 <S_ISREG@plt>
test   %rax,%rax
je     122f <rpc_calibrate+0x8f>
mov    0x10(%rsp),%rdi
lea    0xe14(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbx
lea    0x8(%rsp),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
jmp    1281 <rpc_calibrate+0xe1>
mov    $0x2710,%edi
call   1080 <calibrate@plt>
cltq
imul   $0x66666667,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x22,%rax
add    %ecx,%eax
cltq
mov    %rax,0x8(%rsp)
mov    0x10(%rsp),%rdi
lea    0xdc2(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbx
lea    0x8(%rsp),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10c0 <fwrite@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0x8(%rsp),%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [14 x i8] c".wk-calibrate\00"
@.str.3 = [9 x i8] c"path=%s\0A\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [2 x i8] c"w\00"
define i32 @rpc_calibrate() {
%1 = alloca i32
%2 = alloca %struct.stat
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i32*
store i64 0, i64* %4
%7 = call i32 @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%8 = call i32 @asprintf(i8** %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %7, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%9 = load i8*, i8** %3
%10 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %9)
%11 = load i8*, i8** %3
%12 = call i32 @stat(i8* %11, %struct.stat* %2)
%13 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%14 = load i32, i32* %13
%15 = call i64 @S_ISREG(i32 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %0
%18 = load i8*, i8** %3
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %19, i32** %5
%20 = load i32*, i32** %5
%21 = call i32 @fread(i64* %4, i32 8, i32 1, i32* %20)
store i32 %21, i32* %1
%22 = load i32*, i32** %5
%23 = call i32 @fclose(i32* %22)
br label %34
24: ; preds = %0
%25 = call i32 @calibrate(i32 10000)
%26 = sdiv i32 %25, 10
%27 = sext i32 %26 to i64
store i64 %27, i64* %4
%28 = load i8*, i8** %3
%29 = call i32* @fopen(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %29, i32** %6
%30 = load i32*, i32** %6
%31 = call i32 @fwrite(i64* %4, i32 8, i32 1, i32* %30)
store i32 %31, i32* %1
%32 = load i32*, i32** %6
%33 = call i32 @fclose(i32* %32)
br label %34
34: ; preds = %24, %17
%35 = load i64, i64* %4
%36 = trunc i64 %35 to i32
ret i32 %36
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [14 x i8] c".wk-calibrate\00"
@.str.3 = [9 x i8] c"path=%s\0A\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [2 x i8] c"w\00"
define i32 @rpc_calibrate() {
%1 = alloca %struct.stat
%2 = alloca i8*
%3 = alloca i64
%4 = bitcast %struct.stat* %1 to i8*
%5 = bitcast i8** %2 to i8*
%6 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%7 = tail call i32 @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%8 = call i32 @asprintf(i8** %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %7, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%9 = load i8*, i8** %2
%10 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %9)
%11 = load i8*, i8** %2
%12 = call i32 @stat(i8* %11, %struct.stat* %1)
%13 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%14 = load i32, i32* %13
%15 = call i64 @S_ISREG(i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %0
%18 = load i8*, i8** %2
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%20 = call i32 @fread(i64* %3, i32 8, i32 1, i32* %19)
br label %28
21: ; preds = %0
%22 = call i32 @calibrate(i32 10000)
%23 = sdiv i32 %22, 10
%24 = sext i32 %23 to i64
store i64 %24, i64* %3
%25 = load i8*, i8** %2
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%27 = call i32 @fwrite(i64* %3, i32 8, i32 1, i32* %26)
br label %28
28: ; preds = %21, %17
%29 = phi i32* [ %26, %21 ], [ %19, %17 ]
%30 = call i32 @fclose(i32* %29)
%31 = load i64, i64* %3
%32 = trunc i64 %31 to i32
ret i32 %32
}