#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 */
struct vt {
  int dummy;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int atoi(char *);
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
scalar_t__ read(int, char *, int);
scalar_t__ set_console_blank_timer(struct vt *, int);
int ensure_console_blank_timer_enabled(struct vt *vt) {
  int fd = open("/sys/module/kernel/parameters/consoleblank", O_RDONLY);
  if (fd < 0) {
    return -1;
  }
  char buf[20];
  if (read(fd, buf, sizeof(buf)) < 0) {
    return -1;
  }
  int ret = atoi(buf);
  close(fd);
  if (ret == 0) {
    if (set_console_blank_timer(vt, 1) < 0) {
      ret = -1;
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<ensure_console_blank_timer_enabled>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
lea    0xe94(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    118a <ensure_console_blank_timer_enabled+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f9 <ensure_console_blank_timer_enabled+0xa9>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x14,%edx
call   1050 <read@plt>
cmp    $0x0,%rax
jge    11b1 <ensure_console_blank_timer_enabled+0x61>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f9 <ensure_console_blank_timer_enabled+0xa9>
lea    -0x30(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x34(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x0,-0x34(%rbp)
jne    11f3 <ensure_console_blank_timer_enabled+0xa3>
mov    -0x10(%rbp),%rdi
mov    $0x1,%esi
call   1030 <set_console_blank_timer@plt>
cmp    $0x0,%rax
jge    11ee <ensure_console_blank_timer_enabled+0x9e>
movl   $0xffffffff,-0x34(%rbp)
jmp    11f3 <ensure_console_blank_timer_enabled+0xa3>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ensure_console_blank_timer_enabled>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%r14
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
lea    0xe95(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11b5 <ensure_console_blank_timer_enabled+0x65>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x14,%edx
call   1050 <read@plt>
test   %rax,%rax
js     11b5 <ensure_console_blank_timer_enabled+0x65>
mov    %rsp,%rdi
call   1070 <atoi@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
test   %ebx,%ebx
jne    11ba <ensure_console_blank_timer_enabled+0x6a>
mov    %r14,%rdi
mov    $0x1,%esi
call   1030 <set_console_blank_timer@plt>
mov    %rax,%rbx
sar    $0x3f,%rbx
jmp    11ba <ensure_console_blank_timer_enabled+0x6a>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.vt = type { i32 }
@.str = [43 x i8] c"/sys/module/kernel/parameters/consoleblank\00"
@O_RDONLY = global i32 0
define i32 @ensure_console_blank_timer_enabled(%struct.vt* %0) {
%2 = alloca i32
%3 = alloca %struct.vt*
%4 = alloca i32
%5 = alloca [20 x i8]
%6 = alloca i32
store %struct.vt* %0, %struct.vt** %3
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %33
12: ; preds = %1
%13 = load i32, i32* %4
%14 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%15 = call i64 @read(i32 %13, i8* %14, i32 20)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %12
store i32 -1, i32* %2
br label %33
18: ; preds = %12
%19 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%20 = call i32 @atoi(i8* %19)
store i32 %20, i32* %6
%21 = load i32, i32* %4
%22 = call i32 @close(i32 %21)
%23 = load i32, i32* %6
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %18
%26 = load %struct.vt*, %struct.vt** %3
%27 = call i64 @set_console_blank_timer(%struct.vt* %26, i32 1)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %25
store i32 -1, i32* %6
br label %30
30: ; preds = %29, %25
br label %31
31: ; preds = %30, %18
%32 = load i32, i32* %6
store i32 %32, i32* %2
br label %33
33: ; preds = %31, %17, %11
%34 = load i32, i32* %2
ret i32 %34
}

LLVM IR (-O2):
%struct.vt = type { i32 }
@.str = [43 x i8] c"/sys/module/kernel/parameters/consoleblank\00"
@O_RDONLY = global i32 0
define i32 @ensure_console_blank_timer_enabled(%struct.vt* %0) {
%2 = alloca [20 x i8]
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %20, label %6
6: ; preds = %1
%7 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%8 = call i64 @read(i32 %4, i8* %7, i32 20)
%9 = icmp slt i64 %8, 0
br i1 %9, label %18, label %10
10: ; preds = %6
%11 = call i32 @atoi(i8* %7)
%12 = tail call i32 @close(i32 %4)
%13 = icmp eq i32 %11, 0
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = tail call i64 @set_console_blank_timer(%struct.vt* %0, i32 1)
%16 = ashr i64 %15, 63
%17 = trunc i64 %16 to i32
br label %18
18: ; preds = %14, %10, %6
%19 = phi i32 [ -1, %6 ], [ %11, %10 ], [ %17, %14 ]
br label %20
20: ; preds = %1, %18
%21 = phi i32 [ %19, %18 ], [ -1, %1 ]
ret i32 %21
}