#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 scalar_t__ time_t;
struct utmp {
  scalar_t__ ut_type;
  scalar_t__ ut_time;
};

/* Variables and functions */
scalar_t__ BOOT_TIME;
int /*<<< orphan*/ endutent();
struct utmp *getutent();
int /*<<< orphan*/ setutent();
scalar_t__ time(int /*<<< orphan*/ *);
time_t get_uptime(void) {
  struct utmp *ut;
  time_t boot_time;
  setutent();
  while (ut = getutent()) {
    if (ut->ut_type == BOOT_TIME) {
      boot_time = ut->ut_time;
    }
  }
  endutent();
  if (boot_time)
    return (time(NULL) - boot_time);
  else
    return 0;
}


x86 assembly (clang, -O0):
<get_uptime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1060 <setutent@plt>
mov    $0x0,%al
call   1050 <getutent@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     118c <get_uptime+0x4c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e6e(%rip),%rcx
cmp    (%rcx),%rax
jne    1187 <get_uptime+0x47>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    114f <get_uptime+0xf>
mov    $0x0,%al
call   1030 <endutent@plt>
cmpq   $0x0,-0x18(%rbp)
je     11b4 <get_uptime+0x74>
xor    %eax,%eax
mov    %eax,%edi
call   1040 <time@plt>
sub    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11bc <get_uptime+0x7c>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_uptime>:
push   %r15
push   %r14
push   %rbx
xor    %eax,%eax
call   1060 <setutent@plt>
xor    %eax,%eax
call   1050 <getutent@plt>
test   %rax,%rax
je     119b <get_uptime+0x5b>
mov    0x2e81(%rip),%rbx
jmp    117c <get_uptime+0x3c>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1050 <getutent@plt>
test   %rax,%rax
je     118a <get_uptime+0x4a>
mov    (%rax),%rcx
cmp    (%rbx),%rcx
jne    1170 <get_uptime+0x30>
mov    0x8(%rax),%r15
jmp    1170 <get_uptime+0x30>
xor    %r14d,%r14d
xor    %eax,%eax
call   1030 <endutent@plt>
test   %r15,%r15
jne    11a2 <get_uptime+0x62>
jmp    11af <get_uptime+0x6f>
xor    %eax,%eax
call   1030 <endutent@plt>
xor    %edi,%edi
call   1040 <time@plt>
mov    %rax,%r14
sub    %r15,%r14
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.utmp = type { i64, i64 }
@BOOT_TIME = global i64 0
define i64 @get_uptime() {
%1 = alloca i64
%2 = alloca %struct.utmp*
%3 = alloca i64
%4 = call i32 (...) @setutent()
br label %5
5: ; preds = %18, %0
%6 = call %struct.utmp* (...) @getutent()
store %struct.utmp* %6, %struct.utmp** %2
%7 = icmp ne %struct.utmp* %6, null
br i1 %7, label %8, label %19
8: ; preds = %5
%9 = load %struct.utmp*, %struct.utmp** %2
%10 = getelementptr inbounds %struct.utmp, %struct.utmp* %9, i32 0, i32 0
%11 = load i64, i64* %10
%12 = load i64, i64* @BOOT_TIME
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %18
14: ; preds = %8
%15 = load %struct.utmp*, %struct.utmp** %2
%16 = getelementptr inbounds %struct.utmp, %struct.utmp* %15, i32 0, i32 1
%17 = load i64, i64* %16
store i64 %17, i64* %3
br label %18
18: ; preds = %14, %8
br label %5
19: ; preds = %5
%20 = call i32 (...) @endutent()
%21 = load i64, i64* %3
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = call i64 @time(i32* null)
%25 = load i64, i64* %3
%26 = sub nsw i64 %24, %25
store i64 %26, i64* %1
br label %28
27: ; preds = %19
store i64 0, i64* %1
br label %28
28: ; preds = %27, %23
%29 = load i64, i64* %1
ret i64 %29
}

LLVM IR (-O2):
%struct.utmp = type { i64, i64 }
@BOOT_TIME = global i64 0
define i64 @get_uptime() {
%1 = tail call i32 (...) @setutent()
%2 = tail call %struct.utmp* (...) @getutent()
%3 = icmp eq %struct.utmp* %2, null
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 (...) @endutent()
br label %23
6: ; preds = %0, %16
%7 = phi %struct.utmp* [ %18, %16 ], [ %2, %0 ]
%8 = phi i64 [ %17, %16 ], [ undef, %0 ]
%9 = getelementptr inbounds %struct.utmp, %struct.utmp* %7, i64 0, i32 0
%10 = load i64, i64* %9
%11 = load i64, i64* @BOOT_TIME
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %16
13: ; preds = %6
%14 = getelementptr inbounds %struct.utmp, %struct.utmp* %7, i64 0, i32 1
%15 = load i64, i64* %14
br label %16
16: ; preds = %13, %6
%17 = phi i64 [ %15, %13 ], [ %8, %6 ]
%18 = tail call %struct.utmp* (...) @getutent()
%19 = icmp eq %struct.utmp* %18, null
br i1 %19, label %20, label %6
20: ; preds = %16
%21 = tail call i32 (...) @endutent()
%22 = icmp eq i64 %17, 0
br i1 %22, label %27, label %23
23: ; preds = %4, %20
%24 = phi i64 [ undef, %4 ], [ %17, %20 ]
%25 = tail call i64 @time(i32* null)
%26 = sub nsw i64 %25, %24
br label %27
27: ; preds = %20, %23
%28 = phi i64 [ %26, %23 ], [ 0, %20 ]
ret i64 %28
}