#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 */
typedef int /*<<< orphan*/ kbdbck_node;

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ LOG_WARNING;
scalar_t__ errno;
int /*<<< orphan*/ logdebug(char *, char *);
int /*<<< orphan*/ logmsg(int /*<<< orphan*/, char *, char *,
                          int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ strerror(scalar_t__);
int kbd_backlight_open(int flags) {
  char *kbdbck_node[] = {"/sys/class/leds/smc::kbd_backlight/brightness",
                         "/sys/class/leds/smc:kbd_backlight/brightness"};
  int fd;
  int i;
  for (i = 0; i < sizeof(kbdbck_node) / sizeof(*kbdbck_node); i++) {
    logdebug("Trying %s\n", kbdbck_node[i]);
    fd = open(kbdbck_node[i], flags);
    if (fd >= 0)
      return fd;
    if (errno == ENOENT)
      continue;
    logmsg(LOG_WARNING, "Could not open %s: %s", kbdbck_node[i],
           strerror(errno));
    return -1;
  }
  return -1;
}


x86 assembly (clang, -O0):
<kbd_backlight_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2c9e(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2c9b(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x28(%rbp)
movslq -0x28(%rbp),%rax
cmp    $0x2,%rax
jae    122a <kbd_backlight_open+0xea>
movslq -0x28(%rbp),%rax
mov    -0x20(%rbp,%rax,8),%rsi
lea    0xed5(%rip),%rdi
call   1030 <logdebug@plt>
movslq -0x28(%rbp),%rax
mov    -0x20(%rbp,%rax,8),%rdi
mov    -0x8(%rbp),%esi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     11b4 <kbd_backlight_open+0x74>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1231 <kbd_backlight_open+0xf1>
mov    0x2e15(%rip),%rax
mov    (%rax),%rax
mov    0x2dfb(%rip),%rcx
cmp    (%rcx),%rax
jne    11d3 <kbd_backlight_open+0x93>
jmp    121c <kbd_backlight_open+0xdc>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
movslq -0x28(%rbp),%rax
mov    -0x20(%rbp,%rax,8),%rax
mov    %rax,-0x30(%rbp)
mov    0x2ddd(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %eax,%ecx
lea    0xe5b(%rip),%rsi
call   1040 <logmsg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1231 <kbd_backlight_open+0xf1>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1168 <kbd_backlight_open+0x28>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kbd_backlight_open>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebp
lea    0xf0b(%rip),%rdi
lea    0xea9(%rip),%rbx
mov    %rbx,%rsi
call   1030 <logdebug@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1050 <open@plt>
test   %eax,%eax
jns    11da <kbd_backlight_open+0x9a>
mov    0x2e64(%rip),%r14
mov    (%r14),%rdi
mov    0x2e4a(%rip),%r15
cmp    (%r15),%rdi
jne    11b4 <kbd_backlight_open+0x74>
lea    0xed1(%rip),%rdi
lea    0xe9d(%rip),%rbx
mov    %rbx,%rsi
call   1030 <logdebug@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1050 <open@plt>
test   %eax,%eax
jns    11da <kbd_backlight_open+0x9a>
mov    (%r14),%rdi
mov    $0xffffffff,%eax
cmp    (%r15),%rdi
je     11da <kbd_backlight_open+0x9a>
mov    0x2e2d(%rip),%rax
mov    (%rax),%ebp
call   1060 <strerror@plt>
lea    0xe9d(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %eax,%ecx
call   1040 <logmsg@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [46 x i8] c"/sys/class/leds/smc::kbd_backlight/brightness\00"
@.str.1 = [45 x i8] c"/sys/class/leds/smc:kbd_backlight/brightness\00"
@__const.kbd_backlight_open.kbdbck_node = [2 x i8*] [i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i32 0, i32 0)]
@.str.2 = [11 x i8] c"Trying %s\0A\00"
@errno = global i64 0
@ENOENT = global i64 0
@LOG_WARNING = global i32 0
@.str.3 = [22 x i8] c"Could not open %s: %s\00"
define i32 @kbd_backlight_open(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [2 x i8*]
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
%7 = bitcast [2 x i8*]* %4 to i8*
store i32 0, i32* %6
br label %8
8: ; preds = %42, %1
%9 = load i32, i32* %6
%10 = sext i32 %9 to i64
%11 = icmp ult i64 %10, 2
br i1 %11, label %12, label %45
12: ; preds = %8
%13 = load i32, i32* %6
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [2 x i8*], [2 x i8*]* %4, i64 0, i64 %14
%16 = load i8*, i8** %15
%17 = call i32 @logdebug(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%18 = load i32, i32* %6
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [2 x i8*], [2 x i8*]* %4, i64 0, i64 %19
%21 = load i8*, i8** %20
%22 = load i32, i32* %3
%23 = call i32 @open(i8* %21, i32 %22)
store i32 %23, i32* %5
%24 = load i32, i32* %5
%25 = icmp sge i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %12
%27 = load i32, i32* %5
store i32 %27, i32* %2
br label %46
28: ; preds = %12
%29 = load i64, i64* @errno
%30 = load i64, i64* @ENOENT
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %28
br label %42
33: ; preds = %28
%34 = load i32, i32* @LOG_WARNING
%35 = load i32, i32* %6
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [2 x i8*], [2 x i8*]* %4, i64 0, i64 %36
%38 = load i8*, i8** %37
%39 = load i64, i64* @errno
%40 = call i32 @strerror(i64 %39)
%41 = call i32 @logmsg(i32 %34, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %38, i32 %40)
store i32 -1, i32* %2
br label %46
42: ; preds = %32
%43 = load i32, i32* %6
%44 = add nsw i32 %43, 1
store i32 %44, i32* %6
br label %8
45: ; preds = %8
store i32 -1, i32* %2
br label %46
46: ; preds = %45, %33, %26
%47 = load i32, i32* %2
ret i32 %47
}

LLVM IR (-O2):
@.str = [46 x i8] c"/sys/class/leds/smc::kbd_backlight/brightness\00"
@.str.1 = [45 x i8] c"/sys/class/leds/smc:kbd_backlight/brightness\00"
@.str.2 = [11 x i8] c"Trying %s\0A\00"
@errno = global i64 0
@ENOENT = global i64 0
@LOG_WARNING = global i32 0
@.str.3 = [22 x i8] c"Could not open %s: %s\00"
define i32 @kbd_backlight_open(i32 %0) {
%2 = tail call i32 @logdebug(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
%3 = tail call i32 @open(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 %0)
%4 = icmp sgt i32 %3, -1
br i1 %4, label %23, label %13
5: ; preds = %13
%6 = tail call i32 @logdebug(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
%7 = tail call i32 @open(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%8 = icmp sgt i32 %7, -1
br i1 %8, label %23, label %9
9: ; preds = %5
%10 = load i64, i64* @errno
%11 = load i64, i64* @ENOENT
%12 = icmp eq i64 %10, %11
br i1 %12, label %23, label %17
13: ; preds = %1
%14 = load i64, i64* @errno
%15 = load i64, i64* @ENOENT
%16 = icmp eq i64 %14, %15
br i1 %16, label %5, label %17
17: ; preds = %9, %13
%18 = phi i8* [ getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), %13 ], [ getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), %9 ]
%19 = phi i64 [ %14, %13 ], [ %10, %9 ]
%20 = load i32, i32* @LOG_WARNING
%21 = tail call i32 @strerror(i64 %19)
%22 = tail call i32 @logmsg(i32 %20, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %18, i32 %21)
br label %23
23: ; preds = %1, %5, %9, %17
%24 = phi i32 [ -1, %17 ], [ %3, %1 ], [ %7, %5 ], [ -1, %9 ]
ret i32 %24
}