#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*/ ksft_exit_fail();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
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 = {};
  power_state_fd = open("/sys/power/state", O_RDWR);
  if (power_state_fd < 0) {
    perror("open(\"/sys/power/state\") failed (is this test running as root?)");
    ksft_exit_fail();
  }
  timerfd = timerfd_create(CLOCK_BOOTTIME_ALARM, 0);
  if (timerfd < 0) {
    perror("timerfd_create() failed");
    ksft_exit_fail();
  }
  spec.it_value.tv_sec = 5;
  err = timerfd_settime(timerfd, 0, &spec, NULL);
  if (err < 0) {
    perror("timerfd_settime() failed");
    ksft_exit_fail();
  }
  if (write(power_state_fd, "mem", strlen("mem")) != strlen("mem")) {
    perror("entering suspend failed");
    ksft_exit_fail();
  }
  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   1060 <memset@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
lea    0xe38(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11ed <suspend+0x5d>
lea    0xe30(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x0,%al
call   1040 <ksft_exit_fail@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1080 <timerfd_create@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    121d <suspend+0x8d>
lea    0xe40(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x0,%al
call   1040 <ksft_exit_fail@plt>
movl   $0x5,-0x18(%rbp)
mov    -0xc(%rbp),%edi
xor    %esi,%esi
lea    -0x18(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   10b0 <timerfd_settime@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1256 <suspend+0xc6>
lea    0xe1f(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x0,%al
call   1040 <ksft_exit_fail@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x24(%rbp)
lea    0xe1f(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xe0d(%rip),%rsi
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
lea    0xdfd(%rip),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
je     12ad <suspend+0x11d>
lea    0xde5(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x0,%al
call   1040 <ksft_exit_fail@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    -0x4(%rbp),%edi
call   1070 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<suspend>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
lea    0xe7d(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11eb <suspend+0x7b>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1060 <timerfd_create@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1200 <suspend+0x90>
movl   $0x5,(%rsp)
mov    %rsp,%rdx
mov    %ebp,%edi
xor    %esi,%esi
xor    %ecx,%ecx
call   1090 <timerfd_settime@plt>
test   %eax,%eax
js     1215 <suspend+0xa5>
lea    0xebe(%rip),%rsi
mov    $0x3,%edx
mov    %ebx,%edi
call   1030 <write@plt>
cmp    $0x3,%rax
jne    122a <suspend+0xba>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe1f(%rip),%rdi
call   1080 <perror@plt>
xor    %eax,%eax
call   1040 <ksft_exit_fail@plt>
jmp    118e <suspend+0x1e>
lea    0xe4a(%rip),%rdi
call   1080 <perror@plt>
xor    %eax,%eax
call   1040 <ksft_exit_fail@plt>
jmp    11a4 <suspend+0x34>
lea    0xe4d(%rip),%rdi
call   1080 <perror@plt>
xor    %eax,%eax
call   1040 <ksft_exit_fail@plt>
jmp    11bd <suspend+0x4d>
lea    0xe55(%rip),%rdi
call   1080 <perror@plt>
xor    %eax,%eax
call   1040 <ksft_exit_fail@plt>
jmp    11d6 <suspend+0x66>

LLVM IR (-O0):
%struct.sigevent = type { i32 }
%struct.itimerspec = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@.str = [17 x i8] c"/sys/power/state\00"
@O_RDWR = global i32 0
@.str.1 = [64 x i8] c"open(\22/sys/power/state\22) failed (is this test running as root?)\00"
@CLOCK_BOOTTIME_ALARM = global i32 0
@.str.2 = [24 x i8] c"timerfd_create() failed\00"
@.str.3 = [25 x i8] c"timerfd_settime() failed\00"
@.str.4 = [4 x i8] c"mem\00"
@.str.5 = [24 x i8] c"entering suspend failed\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 = load i32, i32* @O_RDWR
%9 = call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %1
%10 = load i32, i32* %1
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 (...) @ksft_exit_fail()
br label %15
15: ; preds = %12, %0
%16 = load i32, i32* @CLOCK_BOOTTIME_ALARM
%17 = call i32 @timerfd_create(i32 %16, i32 0)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 (...) @ksft_exit_fail()
br label %23
23: ; preds = %20, %15
%24 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %5, i32 0, i32 0
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
store i32 5, i32* %25
%26 = load i32, i32* %3
%27 = call i32 @timerfd_settime(i32 %26, i32 0, %struct.itimerspec* %5, i32* null)
store i32 %27, i32* %4
%28 = load i32, i32* %4
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %23
%31 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 (...) @ksft_exit_fail()
br label %33
33: ; preds = %30, %23
%34 = load i32, i32* %1
%35 = call i64 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
%36 = call i64 @write(i32 %34, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i64 %35)
%37 = call i64 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
%38 = icmp ne i64 %36, %37
br i1 %38, label %39, label %42
39: ; preds = %33
%40 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
%41 = call i32 (...) @ksft_exit_fail()
br label %42
42: ; preds = %39, %33
%43 = load i32, i32* %3
%44 = call i32 @close(i32 %43)
%45 = load i32, i32* %1
%46 = call i32 @close(i32 %45)
ret void
}

LLVM IR (-O2):
%struct.itimerspec = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@.str = [17 x i8] c"/sys/power/state\00"
@O_RDWR = global i32 0
@.str.1 = [64 x i8] c"open(\22/sys/power/state\22) failed (is this test running as root?)\00"
@CLOCK_BOOTTIME_ALARM = global i32 0
@.str.2 = [24 x i8] c"timerfd_create() failed\00"
@.str.3 = [25 x i8] c"timerfd_settime() failed\00"
@.str.4 = [4 x i8] c"mem\00"
@.str.5 = [24 x i8] c"entering suspend failed\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 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 (...) @ksft_exit_fail()
br label %10
10: ; preds = %7, %0
%11 = load i32, i32* @CLOCK_BOOTTIME_ALARM
%12 = tail call i32 @timerfd_create(i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i32 (...) @ksft_exit_fail()
br label %17
17: ; preds = %14, %10
store i32 5, i32* %3
%18 = call i32 @timerfd_settime(i32 %12, i32 0, %struct.itimerspec* %1, i32* null)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%22 = call i32 (...) @ksft_exit_fail()
br label %23
23: ; preds = %20, %17
%24 = call i64 @write(i32 %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i64 3)
%25 = icmp eq i64 %24, 3
br i1 %25, label %29, label %26
26: ; preds = %23
%27 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
%28 = call i32 (...) @ksft_exit_fail()
br label %29
29: ; preds = %26, %23
%30 = call i32 @close(i32 %12)
%31 = call i32 @close(i32 %5)
ret void
}