#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct sigevent {
  int dummy;
};
struct TYPE_2__ {
  int tv_sec;
};
struct itimerspec {
  TYPE_1__ it_value;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_BOOTTIME_ALARM;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
scalar_t__ getuid();
int /*<<< orphan*/ ksft_exit_fail_msg(char *, ...);
int /*<<< orphan*/ ksft_exit_skip(char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
scalar_t__ strlen(char *);
int timerfd_create(int /*<<< orphan*/, int /*<<< orphan*/);
int timerfd_settime(int, int /*<<< orphan*/, struct itimerspec *,
                    int /*<<< orphan*/ *);
scalar_t__ write(int, char *, scalar_t__);
void suspend(void) {
  int power_state_fd;
  struct sigevent event = {};
  int timerfd;
  int err;
  struct itimerspec spec = {};
  if (getuid() != 0)
    ksft_exit_skip("Please run the test as root - Exiting.\n");
  power_state_fd = open("/sys/power/state", O_RDWR);
  if (power_state_fd < 0)
    ksft_exit_fail_msg("open(\"/sys/power/state\") failed %s)\n",
                       strerror(errno));
  timerfd = timerfd_create(CLOCK_BOOTTIME_ALARM, 0);
  if (timerfd < 0)
    ksft_exit_fail_msg("timerfd_create() failed\n");
  spec.it_value.tv_sec = 5;
  err = timerfd_settime(timerfd, 0, &spec, NULL);
  if (err < 0)
    ksft_exit_fail_msg("timerfd_settime() failed\n");
  if (write(power_state_fd, "mem", strlen("mem")) != strlen("mem"))
    ksft_exit_fail_msg("Failed to enter Suspend state\n");
  close(timerfd);
  close(power_state_fd);
}


x86 assembly (clang, -O0):
<suspend>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1080 <memset@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1080 <memset@plt>
mov    $0x0,%al
call   1060 <getuid@plt>
cmp    $0x0,%rax
je     11f5 <suspend+0x45>
lea    0xe10(%rip),%rdi
call   1070 <ksft_exit_skip@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
lea    0xe23(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    1235 <suspend+0x85>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %eax,%esi
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1040 <ksft_exit_fail_msg@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   10a0 <timerfd_create@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1260 <suspend+0xb0>
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1040 <ksft_exit_fail_msg@plt>
movl   $0x5,-0x18(%rbp)
mov    -0xc(%rbp),%edi
xor    %esi,%esi
lea    -0x18(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   10c0 <timerfd_settime@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1294 <suspend+0xe4>
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1040 <ksft_exit_fail_msg@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x24(%rbp)
lea    0xdf0(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xdde(%rip),%rsi
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
lea    0xdce(%rip),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
je     12e6 <suspend+0x136>
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1040 <ksft_exit_fail_msg@plt>
mov    -0xc(%rbp),%edi
call   1090 <close@plt>
mov    -0x4(%rbp),%edi
call   1090 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<suspend>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1050 <getuid@plt>
test   %rax,%rax
je     11ab <suspend+0x1b>
lea    0xe5a(%rip),%rdi
call   1060 <ksft_exit_skip@plt>
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
lea    0xe6d(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11e4 <suspend+0x54>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xe5e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <ksft_exit_fail_msg@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1080 <timerfd_create@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1208 <suspend+0x78>
lea    0xe5d(%rip),%rdi
xor    %eax,%eax
call   1040 <ksft_exit_fail_msg@plt>
movl   $0x5,(%rsp)
mov    %rsp,%rdx
mov    %ebp,%edi
xor    %esi,%esi
xor    %ecx,%ecx
call   10a0 <timerfd_settime@plt>
test   %eax,%eax
jns    122f <suspend+0x9f>
lea    0xe4f(%rip),%rdi
xor    %eax,%eax
call   1040 <ksft_exit_fail_msg@plt>
lea    0xe5b(%rip),%rsi
mov    $0x3,%edx
mov    %ebx,%edi
call   1030 <write@plt>
cmp    $0x3,%rax
je     1256 <suspend+0xc6>
lea    0xe46(%rip),%rdi
xor    %eax,%eax
call   1040 <ksft_exit_fail_msg@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %ebx,%edi
call   1070 <close@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sigevent = type { i32 }
%struct.itimerspec = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@.str = [40 x i8] c"Please run the test as root - Exiting.\0A\00"
@.str.1 = [17 x i8] c"/sys/power/state\00"
@O_RDWR = global i32 0
@.str.2 = [37 x i8] c"open(\22/sys/power/state\22) failed %s)\0A\00"
@errno = global i32 0
@CLOCK_BOOTTIME_ALARM = global i32 0
@.str.3 = [25 x i8] c"timerfd_create() failed\0A\00"
@.str.4 = [26 x i8] c"timerfd_settime() failed\0A\00"
@.str.5 = [4 x i8] c"mem\00"
@.str.6 = [31 x i8] c"Failed to enter Suspend state\0A\00"
define void @suspend() {
%1 = alloca i32
%2 = alloca %struct.sigevent
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.itimerspec
%6 = bitcast %struct.sigevent* %2 to i8*
%7 = bitcast %struct.itimerspec* %5 to i8*
%8 = call i64 (...) @getuid()
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @ksft_exit_skip(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = load i32, i32* @O_RDWR
%14 = call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %13)
store i32 %14, i32* %1
%15 = load i32, i32* %1
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = load i32, i32* @errno
%19 = call i32 @strerror(i32 %18)
%20 = call i32 (i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %17, %12
%22 = load i32, i32* @CLOCK_BOOTTIME_ALARM
%23 = call i32 @timerfd_create(i32 %22, i32 0)
store i32 %23, i32* %3
%24 = load i32, i32* %3
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = call i32 (i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %26, %21
%29 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %5, i32 0, i32 0
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
store i32 5, i32* %30
%31 = load i32, i32* %3
%32 = call i32 @timerfd_settime(i32 %31, i32 0, %struct.itimerspec* %5, i32* null)
store i32 %32, i32* %4
%33 = load i32, i32* %4
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %28
%36 = call i32 (i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %37
37: ; preds = %35, %28
%38 = load i32, i32* %1
%39 = call i64 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0))
%40 = call i64 @write(i32 %38, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i64 %39)
%41 = call i64 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0))
%42 = icmp ne i64 %40, %41
br i1 %42, label %43, label %45
43: ; preds = %37
%44 = call i32 (i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0))
br label %45
45: ; preds = %43, %37
%46 = load i32, i32* %3
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* %1
%49 = call i32 @close(i32 %48)
ret void
}

LLVM IR (-O2):
%struct.itimerspec = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@.str = [40 x i8] c"Please run the test as root - Exiting.\0A\00"
@.str.1 = [17 x i8] c"/sys/power/state\00"
@O_RDWR = global i32 0
@.str.2 = [37 x i8] c"open(\22/sys/power/state\22) failed %s)\0A\00"
@errno = global i32 0
@CLOCK_BOOTTIME_ALARM = global i32 0
@.str.3 = [25 x i8] c"timerfd_create() failed\0A\00"
@.str.4 = [26 x i8] c"timerfd_settime() failed\0A\00"
@.str.5 = [4 x i8] c"mem\00"
@.str.6 = [31 x i8] c"Failed to enter Suspend state\0A\00"
define void @suspend() {
%1 = alloca %struct.itimerspec
%2 = bitcast %struct.itimerspec* %1 to i8*
%3 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %1, i64 0, i32 0, i32 0
%4 = tail call i64 (...) @getuid()
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %0
%7 = tail call i32 @ksft_exit_skip(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
%9 = load i32, i32* @O_RDWR
%10 = tail call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %8
%13 = load i32, i32* @errno
%14 = tail call i32 @strerror(i32 %13)
%15 = tail call i32 (i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %14)
br label %16
16: ; preds = %12, %8
%17 = load i32, i32* @CLOCK_BOOTTIME_ALARM
%18 = tail call i32 @timerfd_create(i32 %17, i32 0)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = tail call i32 (i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
store i32 5, i32* %3
%23 = call i32 @timerfd_settime(i32 %18, i32 0, %struct.itimerspec* %1, i32* null)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 (i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %27
27: ; preds = %25, %22
%28 = call i64 @write(i32 %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i64 3)
%29 = icmp eq i64 %28, 3
br i1 %29, label %32, label %30
30: ; preds = %27
%31 = call i32 (i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0))
br label %32
32: ; preds = %30, %27
%33 = call i32 @close(i32 %18)
%34 = call i32 @close(i32 %10)
ret void
}