#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*/ diag(int, char *, char *);
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, char *, int);
int sscanf(char *, char *, unsigned long *);
unsigned long time(int /*<<< orphan*/);
unsigned long fetch_boot_time(void) {
  char buf[64];
  int uptime_fd, bufsiz, scanned;
  unsigned long uptime;
  uptime_fd = open("/proc/uptime", O_RDONLY, 0);
  if (uptime_fd < 0)
    do {
      diag(1, "error opening %s for read", "/proc/uptime");
      exit(2);
    } while (0);
  bufsiz = read(uptime_fd, buf, sizeof(buf) - 1);
  if (bufsiz < 0)
    do {
      diag(1, "read: %s", "/proc/uptime");
      exit(2);
    } while (0);
  buf[bufsiz] = '\0';
  scanned = sscanf(buf, "%lu.", &uptime);
  if (scanned != 1)
    do {
      diag(0, "cannot get uptime from %s", "/proc/uptime");
      exit(2);
    } while (0);
  return (time(0) - uptime);
}


x86 assembly (clang, -O0):
<fetch_boot_time>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
lea    0xe88(%rip),%rdi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    11b3 <fetch_boot_time+0x53>
jmp    1191 <fetch_boot_time+0x31>
mov    $0x1,%edi
lea    0xe70(%rip),%rsi
lea    0xe5c(%rip),%rdx
call   1030 <diag@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    -0x44(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x3f,%edx
call   1040 <read@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jge    11f8 <fetch_boot_time+0x98>
jmp    11d6 <fetch_boot_time+0x76>
mov    $0x1,%edi
lea    0xe45(%rip),%rsi
lea    0xe17(%rip),%rdx
call   1030 <diag@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
movslq -0x48(%rbp),%rax
movb   $0x0,-0x40(%rbp,%rax,1)
lea    -0x40(%rbp),%rdi
lea    0xe24(%rip),%rsi
lea    -0x58(%rbp),%rdx
call   1060 <sscanf@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x1,-0x4c(%rbp)
je     1246 <fetch_boot_time+0xe6>
jmp    1227 <fetch_boot_time+0xc7>
xor    %edi,%edi
lea    0xe05(%rip),%rsi
lea    0xdc9(%rip),%rdx
call   1030 <diag@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1050 <time@plt>
sub    -0x58(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fetch_boot_time>:
sub    $0x58,%rsp
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
lea    0xe8c(%rip),%rdi
xor    %edx,%edx
call   1070 <open@plt>
test   %eax,%eax
js     11c7 <fetch_boot_time+0x67>
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x3f,%edx
call   1040 <read@plt>
test   %eax,%eax
js     11d0 <fetch_boot_time+0x70>
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xe8e(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
jne    11e5 <fetch_boot_time+0x85>
xor    %edi,%edi
call   1050 <time@plt>
sub    0x8(%rsp),%rax
add    $0x58,%rsp
ret
lea    0xe3f(%rip),%rsi
jmp    11d7 <fetch_boot_time+0x77>
lea    0xe50(%rip),%rsi
lea    0xe22(%rip),%rdx
mov    $0x1,%edi
jmp    11f5 <fetch_boot_time+0x95>
lea    0xe49(%rip),%rsi
lea    0xe0d(%rip),%rdx
xor    %edi,%edi
call   1030 <diag@plt>
mov    $0x2,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"/proc/uptime\00"
@O_RDONLY = global i32 0
@.str.1 = [26 x i8] c"error opening %s for read\00"
@.str.2 = [9 x i8] c"read: %s\00"
@.str.3 = [5 x i8] c"%lu.\00"
@.str.4 = [26 x i8] c"cannot get uptime from %s\00"
define i64 @fetch_boot_time() {
%1 = alloca [64 x i8]
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6, i32 0)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %0
br label %11
11: ; preds = %10
%12 = call i32 @diag(i32 1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 2)
unreachable
14: ; No predecessors!
br label %15
15: ; preds = %14, %0
%16 = load i32, i32* %2
%17 = getelementptr inbounds [64 x i8], [64 x i8]* %1, i64 0, i64 0
%18 = call i32 @read(i32 %16, i8* %17, i32 63)
store i32 %18, i32* %3
%19 = load i32, i32* %3
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %15
br label %22
22: ; preds = %21
%23 = call i32 @diag(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 2)
unreachable
25: ; No predecessors!
br label %26
26: ; preds = %25, %15
%27 = load i32, i32* %3
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [64 x i8], [64 x i8]* %1, i64 0, i64 %28
store i8 0, i8* %29
%30 = getelementptr inbounds [64 x i8], [64 x i8]* %1, i64 0, i64 0
%31 = call i32 @sscanf(i8* %30, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64* %5)
store i32 %31, i32* %4
%32 = load i32, i32* %4
%33 = icmp ne i32 %32, 1
br i1 %33, label %34, label %39
34: ; preds = %26
br label %35
35: ; preds = %34
%36 = call i32 @diag(i32 0, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%37 = call i32 @exit(i32 2)
unreachable
38: ; No predecessors!
br label %39
39: ; preds = %38, %26
%40 = call i64 @time(i32 0)
%41 = load i64, i64* %5
%42 = sub i64 %40, %41
ret i64 %42
}

LLVM IR (-O2):
@.str = [13 x i8] c"/proc/uptime\00"
@O_RDONLY = global i32 0
@.str.1 = [26 x i8] c"error opening %s for read\00"
@.str.2 = [9 x i8] c"read: %s\00"
@.str.3 = [5 x i8] c"%lu.\00"
@.str.4 = [26 x i8] c"cannot get uptime from %s\00"
define i64 @fetch_boot_time() {
%1 = alloca [64 x i8]
%2 = alloca i64
%3 = getelementptr inbounds [64 x i8], [64 x i8]* %1, i64 0, i64 0
%4 = bitcast i64* %2 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = tail call i32 @diag(i32 1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 2)
unreachable
11: ; preds = %0
%12 = call i32 @read(i32 %6, i8* %3, i32 63)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = tail call i32 @diag(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @exit(i32 2)
unreachable
17: ; preds = %11
%18 = zext i32 %12 to i64
%19 = getelementptr inbounds [64 x i8], [64 x i8]* %1, i64 0, i64 %18
store i8 0, i8* %19
%20 = call i32 @sscanf(i8* %3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64* %2)
%21 = icmp eq i32 %20, 1
br i1 %21, label %25, label %22
22: ; preds = %17
%23 = call i32 @diag(i32 0, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 2)
unreachable
25: ; preds = %17
%26 = call i64 @time(i32 0)
%27 = load i64, i64* %2
%28 = sub i64 %26, %27
ret i64 %28
}