#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 EINVAL;
scalar_t__ ENOENT;
int O_CLOEXEC;
int O_RDWR;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int);
int /*<<< orphan*/ stderr;
int write_nointr(int, char *, int);
int trigger_uevent(unsigned int times) {
  int fd, ret;
  unsigned int i;
  fd = open("/sys/devices/virtual/mem/full/uevent", O_RDWR | O_CLOEXEC);
  if (fd < 0) {
    if (errno != ENOENT)
      return -EINVAL;
    return -1;
  }
  for (i = 0; i < times; i++) {
    ret = write_nointr(fd, "add\n", sizeof("add\n") - 1);
    if (ret < 0) {
      fprintf(stderr, "Failed to trigger uevent\n");
      break;
    }
  }
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<trigger_uevent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
mov    0x2e55(%rip),%rax
or     (%rax),%esi
lea    0xe9c(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11af <trigger_uevent+0x6f>
mov    0x2e53(%rip),%rax
mov    (%rax),%rax
mov    0x2e31(%rip),%rcx
cmp    (%rcx),%rax
je     11a3 <trigger_uevent+0x63>
xor    %eax,%eax
mov    0x2e2f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    121e <trigger_uevent+0xde>
movl   $0xffffffff,-0x4(%rbp)
jmp    121e <trigger_uevent+0xde>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jae    1210 <trigger_uevent+0xd0>
mov    -0xc(%rbp),%edi
lea    0xe59(%rip),%rsi
mov    $0x4,%edx
call   1050 <write_nointr@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11fd <trigger_uevent+0xbd>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
lea    0xe37(%rip),%rsi
call   1040 <fprintf@plt>
jmp    1210 <trigger_uevent+0xd0>
jmp    1202 <trigger_uevent+0xc2>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11b6 <trigger_uevent+0x76>
mov    -0xc(%rbp),%edi
call   1030 <close@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trigger_uevent>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebp
mov    0x2e98(%rip),%rax
mov    0x2e59(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe9e(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     119d <trigger_uevent+0x5d>
mov    %eax,%r14d
test   %ebp,%ebp
je     11c5 <trigger_uevent+0x85>
lea    0xeac(%rip),%r15
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1050 <write_nointr@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11c7 <trigger_uevent+0x87>
add    $0xffffffff,%ebp
jne    1180 <trigger_uevent+0x40>
jmp    11dc <trigger_uevent+0x9c>
mov    0x2e2c(%rip),%rax
mov    (%rax),%rax
mov    0x2e0a(%rip),%rcx
mov    $0xffffffff,%ebx
cmp    (%rcx),%rax
je     11e4 <trigger_uevent+0xa4>
mov    0x2e09(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
jmp    11e4 <trigger_uevent+0xa4>
jmp    11dc <trigger_uevent+0x9c>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xe53(%rip),%rsi
call   1040 <fprintf@plt>
mov    %r14d,%edi
call   1030 <close@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [37 x i8] c"/sys/devices/virtual/mem/full/uevent\00"
@O_RDWR = global i32 0
@O_CLOEXEC = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@EINVAL = global i32 0
@.str.1 = [5 x i8] c"add\0A\00"
@stderr = global i32 0
@.str.2 = [26 x i8] c"Failed to trigger uevent\0A\00"
define i32 @trigger_uevent(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_CLOEXEC
%9 = or i32 %7, %8
%10 = call i32 @open(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %21
13: ; preds = %1
%14 = load i64, i64* @errno
%15 = load i64, i64* @ENOENT
%16 = icmp ne i64 %14, %15
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i32, i32* @EINVAL
%19 = sub nsw i32 0, %18
store i32 %19, i32* %2
br label %42
20: ; preds = %13
store i32 -1, i32* %2
br label %42
21: ; preds = %1
store i32 0, i32* %6
br label %22
22: ; preds = %35, %21
%23 = load i32, i32* %6
%24 = load i32, i32* %3
%25 = icmp ult i32 %23, %24
br i1 %25, label %26, label %38
26: ; preds = %22
%27 = load i32, i32* %4
%28 = call i32 @write_nointr(i32 %27, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
store i32 %28, i32* %5
%29 = load i32, i32* %5
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %38
34: ; preds = %26
br label %35
35: ; preds = %34
%36 = load i32, i32* %6
%37 = add i32 %36, 1
store i32 %37, i32* %6
br label %22
38: ; preds = %31, %22
%39 = load i32, i32* %4
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* %5
store i32 %41, i32* %2
br label %42
42: ; preds = %38, %20, %17
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
@.str = [37 x i8] c"/sys/devices/virtual/mem/full/uevent\00"
@O_RDWR = global i32 0
@O_CLOEXEC = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@EINVAL = global i32 0
@.str.1 = [5 x i8] c"add\0A\00"
@stderr = global i32 0
@.str.2 = [26 x i8] c"Failed to trigger uevent\0A\00"
define i32 @trigger_uevent(i32 %0) {
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_CLOEXEC
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %1
%8 = icmp eq i32 %0, 0
br i1 %8, label %26, label %18
9: ; preds = %1
%10 = load i64, i64* @errno
%11 = load i64, i64* @ENOENT
%12 = icmp eq i64 %10, %11
br i1 %12, label %29, label %13
13: ; preds = %9
%14 = load i32, i32* @EINVAL
%15 = sub nsw i32 0, %14
br label %29
16: ; preds = %18
%17 = icmp eq i32 %22, %0
br i1 %17, label %26, label %18
18: ; preds = %7, %16
%19 = phi i32 [ %22, %16 ], [ 0, %7 ]
%20 = tail call i32 @write_nointr(i32 %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%21 = icmp slt i32 %20, 0
%22 = add nuw i32 %19, 1
br i1 %21, label %23, label %16
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %16, %7, %23
%27 = phi i32 [ %20, %23 ], [ undef, %7 ], [ %20, %16 ]
%28 = tail call i32 @close(i32 %5)
br label %29
29: ; preds = %9, %26, %13
%30 = phi i32 [ %15, %13 ], [ %27, %26 ], [ -1, %9 ]
ret i32 %30
}