#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 */

/* Variables and functions */
int LOCK_EX;
int LOCK_NB;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
scalar_t__ flock(int, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char *);
int request_valid() {
  int fd = open("/run/suspend/immediate", O_RDWR);
  if (fd >= 0) {
    if (flock(fd, LOCK_EX | LOCK_NB) == 0) {
      unlink("/run/suspend/immediate");
      close(fd);
    } else {
      close(fd);
      return 1;
    }
  }
  fd = open("/run/suspend/request", O_RDONLY);
  if (fd < 0)
    return 0;
  close(fd);
  return 1;
}


x86 assembly (clang, -O0):
<request_valid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
lea    0xea8(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     11c0 <request_valid+0x80>
mov    -0x8(%rbp),%edi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
mov    0x2e43(%rip),%rax
or     (%rax),%esi
call   1060 <flock@plt>
cmp    $0x0,%rax
jne    11a7 <request_valid+0x67>
lea    0xe6b(%rip),%rdi
call   1030 <unlink@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
jmp    11bb <request_valid+0x7b>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    11fd <request_valid+0xbd>
jmp    11c0 <request_valid+0x80>
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
lea    0xe47(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11ee <request_valid+0xae>
movl   $0x0,-0x4(%rbp)
jmp    11fd <request_valid+0xbd>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<request_valid>:
push   %rbx
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
lea    0xeaf(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     118d <request_valid+0x4d>
mov    %eax,%ebx
mov    0x2e7d(%rip),%rax
mov    0x2e56(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %ebx,%edi
call   1060 <flock@plt>
test   %rax,%rax
jne    11a8 <request_valid+0x68>
lea    0xe7f(%rip),%rdi
call   1030 <unlink@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
lea    0xe7a(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11b6 <request_valid+0x76>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0x1,%eax
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [23 x i8] c"/run/suspend/immediate\00"
@O_RDWR = global i32 0
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str.1 = [21 x i8] c"/run/suspend/request\00"
@O_RDONLY = global i32 0
define i32 @request_valid() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @O_RDWR
%4 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %3)
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp sge i32 %5, 0
br i1 %6, label %7, label %22
7: ; preds = %0
%8 = load i32, i32* %2
%9 = load i32, i32* @LOCK_EX
%10 = load i32, i32* @LOCK_NB
%11 = or i32 %9, %10
%12 = call i64 @flock(i32 %8, i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %7
%15 = call i32 @unlink(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* %2
%17 = call i32 @close(i32 %16)
br label %21
18: ; preds = %7
%19 = load i32, i32* %2
%20 = call i32 @close(i32 %19)
store i32 1, i32* %1
br label %31
21: ; preds = %14
br label %22
22: ; preds = %21, %0
%23 = load i32, i32* @O_RDONLY
%24 = call i32 @open(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %23)
store i32 %24, i32* %2
%25 = load i32, i32* %2
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %22
store i32 0, i32* %1
br label %31
28: ; preds = %22
%29 = load i32, i32* %2
%30 = call i32 @close(i32 %29)
store i32 1, i32* %1
br label %31
31: ; preds = %28, %27, %18
%32 = load i32, i32* %1
ret i32 %32
}

LLVM IR (-O2):
@.str = [23 x i8] c"/run/suspend/immediate\00"
@O_RDWR = global i32 0
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str.1 = [21 x i8] c"/run/suspend/request\00"
@O_RDONLY = global i32 0
define i32 @request_valid() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp sgt i32 %2, -1
br i1 %3, label %4, label %13
4: ; preds = %0
%5 = load i32, i32* @LOCK_EX
%6 = load i32, i32* @LOCK_NB
%7 = or i32 %6, %5
%8 = tail call i64 @flock(i32 %2, i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %17
10: ; preds = %4
%11 = tail call i32 @unlink(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @close(i32 %2)
br label %13
13: ; preds = %10, %0
%14 = load i32, i32* @O_RDONLY
%15 = tail call i32 @open(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %13, %4
%18 = phi i32 [ %2, %4 ], [ %15, %13 ]
%19 = tail call i32 @close(i32 %18)
br label %20
20: ; preds = %17, %13
%21 = phi i32 [ 0, %13 ], [ 1, %17 ]
ret i32 %21
}