#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 uint8_t;
struct cmd_info {
  int dummy;
};
typedef int /*<<< orphan*/ data;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ lseek(int, unsigned long, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int read(int, int *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
unsigned long strtoul(char const *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int cmos_rd(int argc, const char *argv[], const struct cmd_info *info) {
  unsigned long index;
  uint8_t data;
  int fd;
  index = strtoul(argv[1], NULL, 0);
  if (index < 14) {
    fprintf(stderr, "can't read bytes below %d\n", 14);
    return -1;
  }
  index -= 14;
  fd = open("/dev/nvram", O_RDONLY);
  if (fd < 0) {
    fprintf(stderr, "open(\"%s\"): %s\n", "/dev/nvram", strerror(errno));
    return -1;
  }
  if (lseek(fd, index, SEEK_SET) < 0) {
    fprintf(stderr, "lseek(%lu): %s\n", index, strerror(errno));
    close(fd);
    return -1;
  }
  if (read(fd, &data, sizeof(data)) != sizeof(data)) {
    fprintf(stderr, "read(): %s\n", strerror(errno));
    close(fd);
    return -1;
  }
  close(fd);
  printf("0x%02x\n", data);
  return 0;
}


x86 assembly (clang, -O0):
<cmos_rd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <strtoul@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xe,-0x20(%rbp)
jae    11dd <cmos_rd+0x5d>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
lea    0xe3b(%rip),%rsi
mov    $0xe,%edx
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1336 <cmos_rd+0x1b6>
mov    -0x20(%rbp),%rax
sub    $0xe,%rax
mov    %rax,-0x20(%rbp)
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
lea    0xe22(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    124c <cmos_rd+0xcc>
mov    0x2dc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rcx
lea    0xdf4(%rip),%rsi
lea    0xde2(%rip),%rdx
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1336 <cmos_rd+0x1b6>
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    0x2d6e(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmp    $0x0,%rax
jge    12b9 <cmos_rd+0x139>
mov    0x2d66(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xd98(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1336 <cmos_rd+0x1b6>
mov    -0x28(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
cltq
cmp    $0x4,%rax
je     1318 <cmos_rd+0x198>
mov    0x2cfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x40(%rbp),%edi
mov    %rax,%rdx
lea    0xd49(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1336 <cmos_rd+0x1b6>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%esi
lea    0xd28(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmos_rd>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x8(%rsi),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <strtoul@plt>
cmp    $0xd,%rax
ja     11bc <cmos_rd+0x3c>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
lea    0xe55(%rip),%rsi
mov    $0xe,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    12c3 <cmos_rd+0x143>
mov    %rax,%rbx
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
lea    0xe4c(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     122e <cmos_rd+0xae>
mov    %eax,%ebp
add    $0xfffffffffffffff2,%rbx
mov    0x2de3(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <lseek@plt>
test   %rax,%rax
js     1261 <cmos_rd+0xe1>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1060 <read@plt>
cmp    $0x4,%eax
jne    1292 <cmos_rd+0x112>
mov    %ebp,%edi
call   1050 <close@plt>
mov    0xc(%rsp),%esi
lea    0xe30(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
jmp    12c8 <cmos_rd+0x148>
mov    0x2da3(%rip),%rax
mov    (%rax),%ebx
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdda(%rip),%rsi
lea    0xdc8(%rip),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    12c3 <cmos_rd+0x143>
mov    0x2d70(%rip),%rax
mov    (%rax),%r14d
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdb6(%rip),%rsi
mov    %r14d,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    12bc <cmos_rd+0x13c>
mov    0x2d3f(%rip),%rax
mov    (%rax),%ebx
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd96(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.cmd_info = type { i32 }
@stderr = global i32 0
@.str = [27 x i8] c"can't read bytes below %d\0A\00"
@.str.1 = [11 x i8] c"/dev/nvram\00"
@O_RDONLY = global i32 0
@.str.2 = [16 x i8] c"open(\22%s\22): %s\0A\00"
@errno = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [16 x i8] c"lseek(%lu): %s\0A\00"
@.str.4 = [12 x i8] c"read(): %s\0A\00"
@.str.5 = [8 x i8] c"0x%02x\0A\00"
define i32 @cmos_rd(i32 %0, i8** %1, %struct.cmd_info* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca %struct.cmd_info*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8** %1, i8*** %6
store %struct.cmd_info* %2, %struct.cmd_info** %7
%11 = load i8**, i8*** %6
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = call i64 @strtoul(i8* %13, i32* null, i32 0)
store i64 %14, i64* %8
%15 = load i64, i64* %8
%16 = icmp ult i64 %15, 14
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 14)
store i32 -1, i32* %4
br label %63
20: ; preds = %3
%21 = load i64, i64* %8
%22 = sub i64 %21, 14
store i64 %22, i64* %8
%23 = load i32, i32* @O_RDONLY
%24 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %23)
store i32 %24, i32* %10
%25 = load i32, i32* %10
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %20
%28 = load i32, i32* @stderr
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %30)
store i32 -1, i32* %4
br label %63
32: ; preds = %20
%33 = load i32, i32* %10
%34 = load i64, i64* %8
%35 = load i32, i32* @SEEK_SET
%36 = call i64 @lseek(i32 %33, i64 %34, i32 %35)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %46
38: ; preds = %32
%39 = load i32, i32* @stderr
%40 = load i64, i64* %8
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i64 %40, i8* %42)
%44 = load i32, i32* %10
%45 = call i32 @close(i32 %44)
store i32 -1, i32* %4
br label %63
46: ; preds = %32
%47 = load i32, i32* %10
%48 = call i32 @read(i32 %47, i32* %9, i32 4)
%49 = sext i32 %48 to i64
%50 = icmp ne i64 %49, 4
br i1 %50, label %51, label %58
51: ; preds = %46
%52 = load i32, i32* @stderr
%53 = load i32, i32* @errno
%54 = call i8* @strerror(i32 %53)
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %54)
%56 = load i32, i32* %10
%57 = call i32 @close(i32 %56)
store i32 -1, i32* %4
br label %63
58: ; preds = %46
%59 = load i32, i32* %10
%60 = call i32 @close(i32 %59)
%61 = load i32, i32* %9
%62 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32 %61)
store i32 0, i32* %4
br label %63
63: ; preds = %58, %51, %38, %27, %17
%64 = load i32, i32* %4
ret i32 %64
}

LLVM IR (-O2):
%struct.cmd_info = type { i32 }
@stderr = global i32 0
@.str = [27 x i8] c"can't read bytes below %d\0A\00"
@.str.1 = [11 x i8] c"/dev/nvram\00"
@O_RDONLY = global i32 0
@.str.2 = [16 x i8] c"open(\22%s\22): %s\0A\00"
@errno = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [16 x i8] c"lseek(%lu): %s\0A\00"
@.str.4 = [12 x i8] c"read(): %s\0A\00"
@.str.5 = [8 x i8] c"0x%02x\0A\00"
define i32 @cmos_rd(i32 %0, i8** nocapture %1, %struct.cmd_info* nocapture readnone %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = tail call i64 @strtoul(i8* nocapture %7, i32* null, i32 0)
%9 = icmp ult i64 %8, 14
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = load i32, i32* @stderr
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 14)
br label %46
13: ; preds = %3
%14 = add i64 %8, -14
%15 = load i32, i32* @O_RDONLY
%16 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = tail call i8* @strerror(i32 %20)
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %21)
br label %46
23: ; preds = %13
%24 = load i32, i32* @SEEK_SET
%25 = tail call i64 @lseek(i32 %16, i64 %14, i32 %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = load i32, i32* @errno
%30 = tail call i8* @strerror(i32 %29)
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i64 %14, i8* %30)
%32 = tail call i32 @close(i32 %16)
br label %46
33: ; preds = %23
%34 = call i32 @read(i32 %16, i32* %4, i32 4)
%35 = icmp eq i32 %34, 4
br i1 %35, label %42, label %36
36: ; preds = %33
%37 = load i32, i32* @stderr
%38 = load i32, i32* @errno
%39 = tail call i8* @strerror(i32 %38)
%40 = tail call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %39)
%41 = tail call i32 @close(i32 %16)
br label %46
42: ; preds = %33
%43 = tail call i32 @close(i32 %16)
%44 = load i32, i32* %4
%45 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32 %44)
br label %46
46: ; preds = %42, %36, %27, %18, %10
%47 = phi i32 [ -1, %10 ], [ -1, %18 ], [ -1, %27 ], [ -1, %36 ], [ 0, %42 ]
ret i32 %47
}