#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;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ err(int /*<<< orphan*/, char *, int, ...);
int lseek(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int read(int, int *, int);
void read_ec_val(int fd, int byte_offset) {
  uint8_t buf;
  int error;
  error = lseek(fd, byte_offset, SEEK_SET);
  if (error != byte_offset) {
    err(EXIT_FAILURE, "Cannot set offset to 0x%.2x", byte_offset);
  }
  error = read(fd, &buf, 1);
  if (error != 1)
    err(EXIT_FAILURE, "Could not read byte 0x%.2x from %s\n", byte_offset,
        "/sys/kernel/debug/ec/ec0/io");
  printf("0x%.2x\n", buf);
  return;
}


x86 assembly (clang, -O0):
<read_ec_val>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    0x2e85(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
je     118b <read_ec_val+0x4b>
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe7c(%rip),%rsi
mov    $0x0,%al
call   1060 <err@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x1,-0x10(%rbp)
je     11ca <read_ec_val+0x8a>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe60(%rip),%rsi
lea    0xe7d(%rip),%rcx
mov    $0x0,%al
call   1060 <err@plt>
mov    -0xc(%rbp),%esi
lea    0xe88(%rip),%rdi
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_ec_val>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
mov    0x2e92(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmp    %ebx,%eax
je     1172 <read_ec_val+0x32>
mov    0x2e78(%rip),%rax
mov    (%rax),%edi
lea    0xe97(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1060 <err@plt>
lea    0x4(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
je     11a8 <read_ec_val+0x68>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    0xe84(%rip),%rsi
lea    0xea1(%rip),%rcx
mov    %ebx,%edx
xor    %eax,%eax
call   1060 <err@plt>
mov    0x4(%rsp),%esi
lea    0xea9(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <printf@plt>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [28 x i8] c"Cannot set offset to 0x%.2x\00"
@.str.1 = [36 x i8] c"Could not read byte 0x%.2x from %s\0A\00"
@.str.2 = [28 x i8] c"/sys/kernel/debug/ec/ec0/io\00"
@.str.3 = [8 x i8] c"0x%.2x\0A\00"
define void @read_ec_val(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* %3
%8 = load i32, i32* %4
%9 = load i32, i32* @SEEK_SET
%10 = call i32 @lseek(i32 %7, i32 %8, i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = load i32, i32* %4
%13 = icmp ne i32 %11, %12
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i32, i32* @EXIT_FAILURE
%16 = load i32, i32* %4
%17 = call i32 (i32, i8*, i32, ...) @err(i32 %15, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %16)
br label %18
18: ; preds = %14, %2
%19 = load i32, i32* %3
%20 = call i32 @read(i32 %19, i32* %5, i32 1)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp ne i32 %21, 1
br i1 %22, label %23, label %27
23: ; preds = %18
%24 = load i32, i32* @EXIT_FAILURE
%25 = load i32, i32* %4
%26 = call i32 (i32, i8*, i32, ...) @err(i32 %24, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %25, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %23, %18
%28 = load i32, i32* %5
%29 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %28)
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [28 x i8] c"Cannot set offset to 0x%.2x\00"
@.str.1 = [36 x i8] c"Could not read byte 0x%.2x from %s\0A\00"
@.str.2 = [28 x i8] c"/sys/kernel/debug/ec/ec0/io\00"
@.str.3 = [8 x i8] c"0x%.2x\0A\00"
define void @read_ec_val(i32 %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = load i32, i32* @SEEK_SET
%6 = tail call i32 @lseek(i32 %0, i32 %1, i32 %5)
%7 = icmp eq i32 %6, %1
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 (i32, i8*, i32, ...) @err(i32 %9, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %1)
br label %11
11: ; preds = %8, %2
%12 = call i32 @read(i32 %0, i32* %3, i32 1)
%13 = icmp eq i32 %12, 1
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 (i32, i8*, i32, ...) @err(i32 %15, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %14, %11
%18 = load i32, i32* %3
%19 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %18)
ret void
}