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

/* Variables and functions */
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, unsigned long *, unsigned long *);
int /*<<< orphan*/ ksft_test_result_error(char *);
int read_proc_uptime(struct timespec *uptime) {
  unsigned long up_sec, up_nsec;
  FILE *proc;
  proc = fopen("/proc/uptime", "r");
  if (proc == NULL) {
    ({
      ksft_test_result_error("Unable to open /proc/uptime"
                             ": %m"
                             "\n");
      -1;
    });
    return -1;
  }
  if (fscanf(proc, "%lu.%02lu", &up_sec, &up_nsec) != 2) {
    if (errno) {
      ({
        ksft_test_result_error("fscanf"
                               ": %m"
                               "\n");
        -1;
      });
      return -errno;
    }
    ({
      ksft_test_result_error("failed to parse /proc/uptime"
                             "\n");
      -1;
    });
    return -1;
  }
  fclose(proc);
  uptime->tv_sec = up_sec;
  uptime->tv_nsec = up_nsec;
  return 0;
}


x86 assembly (clang, -O0):
<read_proc_uptime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xead(%rip),%rdi
lea    0xeb3(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118d <read_proc_uptime+0x4d>
lea    0xe9a(%rip),%rdi
call   1050 <ksft_test_result_error@plt>
movl   $0xffffffff,-0x2c(%rbp)
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <read_proc_uptime+0xea>
mov    -0x28(%rbp),%rdi
lea    0xe98(%rip),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   1040 <fscanf@plt>
cmp    $0x2,%eax
je     1203 <read_proc_uptime+0xc3>
mov    0x2e2b(%rip),%rax
cmpl   $0x0,(%rax)
je     11e4 <read_proc_uptime+0xa4>
lea    0xe75(%rip),%rdi
call   1050 <ksft_test_result_error@plt>
movl   $0xffffffff,-0x30(%rbp)
xor    %eax,%eax
mov    0x2e06(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    122a <read_proc_uptime+0xea>
lea    0xe5b(%rip),%rdi
call   1050 <ksft_test_result_error@plt>
movl   $0xffffffff,-0x34(%rbp)
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <read_proc_uptime+0xea>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x8(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_proc_uptime>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
lea    0xeaf(%rip),%rdi
lea    0xeb5(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11a0 <read_proc_uptime+0x60>
mov    %rax,%rbx
lea    0xec4(%rip),%rsi
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %rax,%rdi
call   1040 <fscanf@plt>
cmp    $0x2,%eax
jne    11a9 <read_proc_uptime+0x69>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x10(%rsp),%rax
mov    %rax,(%r14)
mov    0x8(%rsp),%rax
mov    %rax,0x8(%r14)
xor    %eax,%eax
jmp    11d8 <read_proc_uptime+0x98>
lea    0xe68(%rip),%rdi
jmp    11ce <read_proc_uptime+0x8e>
mov    0x2e30(%rip),%rbx
cmpl   $0x0,(%rbx)
je     11c7 <read_proc_uptime+0x87>
lea    0xe7e(%rip),%rdi
call   1050 <ksft_test_result_error@plt>
xor    %eax,%eax
sub    (%rbx),%eax
jmp    11d8 <read_proc_uptime+0x98>
lea    0xe78(%rip),%rdi
call   1050 <ksft_test_result_error@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@.str = [13 x i8] c"/proc/uptime\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [33 x i8] c"Unable to open /proc/uptime: %m\0A\00"
@.str.3 = [10 x i8] c"%lu.%02lu\00"
@errno = global i32 0
@.str.4 = [12 x i8] c"fscanf: %m\0A\00"
@.str.5 = [30 x i8] c"failed to parse /proc/uptime\0A\00"
define i32 @read_proc_uptime(%struct.timespec* %0) {
%2 = alloca i32
%3 = alloca %struct.timespec*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store %struct.timespec* %0, %struct.timespec** %3
%10 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %6
%11 = load i32*, i32** %6
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = call i32 @ksft_test_result_error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %7
%15 = load i32, i32* %7
store i32 -1, i32* %2
br label %40
16: ; preds = %1
%17 = load i32*, i32** %6
%18 = call i32 @fscanf(i32* %17, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i64* %4, i64* %5)
%19 = icmp ne i32 %18, 2
br i1 %19, label %20, label %31
20: ; preds = %16
%21 = load i32, i32* @errno
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = call i32 @ksft_test_result_error(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %8
%25 = load i32, i32* %8
%26 = load i32, i32* @errno
%27 = sub nsw i32 0, %26
store i32 %27, i32* %2
br label %40
28: ; preds = %20
%29 = call i32 @ksft_test_result_error(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %9
%30 = load i32, i32* %9
store i32 -1, i32* %2
br label %40
31: ; preds = %16
%32 = load i32*, i32** %6
%33 = call i32 @fclose(i32* %32)
%34 = load i64, i64* %4
%35 = load %struct.timespec*, %struct.timespec** %3
%36 = getelementptr inbounds %struct.timespec, %struct.timespec* %35, i32 0, i32 0
store i64 %34, i64* %36
%37 = load i64, i64* %5
%38 = load %struct.timespec*, %struct.timespec** %3
%39 = getelementptr inbounds %struct.timespec, %struct.timespec* %38, i32 0, i32 1
store i64 %37, i64* %39
store i32 0, i32* %2
br label %40
40: ; preds = %31, %28, %23, %13
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@.str = [13 x i8] c"/proc/uptime\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [33 x i8] c"Unable to open /proc/uptime: %m\0A\00"
@.str.3 = [10 x i8] c"%lu.%02lu\00"
@errno = global i32 0
@.str.4 = [12 x i8] c"fscanf: %m\0A\00"
@.str.5 = [30 x i8] c"failed to parse /proc/uptime\0A\00"
define i32 @read_proc_uptime(%struct.timespec* nocapture writeonly %0) {
%2 = alloca i64
%3 = alloca i64
%4 = bitcast i64* %2 to i8*
%5 = bitcast i64* %3 to i8*
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @ksft_test_result_error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %28
10: ; preds = %1
%11 = call i32 @fscanf(i32* %6, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i64* %2, i64* %3)
%12 = icmp eq i32 %11, 2
br i1 %12, label %22, label %13
13: ; preds = %10
%14 = load i32, i32* @errno
%15 = icmp eq i32 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %13
%17 = call i32 @ksft_test_result_error(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%18 = load i32, i32* @errno
%19 = sub nsw i32 0, %18
br label %28
20: ; preds = %13
%21 = call i32 @ksft_test_result_error(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0))
br label %28
22: ; preds = %10
%23 = call i32 @fclose(i32* %6)
%24 = load i64, i64* %2
%25 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 0
store i64 %24, i64* %25
%26 = load i64, i64* %3
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 1
store i64 %26, i64* %27
br label %28
28: ; preds = %22, %20, %16, %8
%29 = phi i32 [ -1, %8 ], [ %19, %16 ], [ -1, %20 ], [ 0, %22 ]
ret i32 %29
}