#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 /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
unsigned char inb(int);
int /*<<< orphan*/ outb(unsigned char, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ wait_for_ec_status(int, int);
int read_ec(unsigned char reg, unsigned char *val) {
  int ret;
  outb(0x80, 0x66);
  if (!wait_for_ec_status(0x02, 0)) {
    fprintf(stderr, "EC read timeout 1\n");
    return 0;
  }
  outb(reg, 0x62);
  if (!wait_for_ec_status(0x01, 1)) {
    fprintf(stderr, "EC read timeout 2\n");
    return 0;
  }
  *val = inb(0x62);
  return 1;
}


x86 assembly (clang, -O0):
<read_ec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dil,%al
mov    %al,-0x5(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x80,%edi
mov    $0x66,%esi
call   1030 <outb@plt>
mov    $0x2,%edi
xor    %esi,%esi
call   1060 <wait_for_ec_status@plt>
cmp    $0x0,%eax
jne    1197 <read_ec+0x57>
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
lea    0xe7a(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f4 <read_ec+0xb4>
mov    $0x62,%esi
movzbl -0x5(%rbp),%edi
call   1030 <outb@plt>
mov    $0x1,%esi
mov    %esi,%edi
call   1060 <wait_for_ec_status@plt>
cmp    $0x0,%eax
jne    11db <read_ec+0x9b>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
lea    0xe49(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f4 <read_ec+0xb4>
mov    $0x62,%edi
call   1050 <inb@plt>
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_ec>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebx
mov    $0x80,%edi
mov    $0x66,%esi
call   1030 <outb@plt>
xor    %ebp,%ebp
mov    $0x2,%edi
xor    %esi,%esi
call   1060 <wait_for_ec_status@plt>
test   %eax,%eax
je     119e <read_ec+0x5e>
movzbl %bl,%edi
mov    $0x62,%esi
call   1030 <outb@plt>
mov    $0x1,%ebp
mov    $0x1,%edi
mov    $0x1,%esi
call   1060 <wait_for_ec_status@plt>
test   %eax,%eax
je     11b5 <read_ec+0x75>
mov    $0x62,%edi
call   1050 <inb@plt>
mov    %al,(%r14)
jmp    11cc <read_ec+0x8c>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
lea    0xe52(%rip),%rsi
call   1040 <fprintf@plt>
jmp    11cc <read_ec+0x8c>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    0xe4e(%rip),%rsi
call   1040 <fprintf@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [19 x i8] c"EC read timeout 1\0A\00"
@.str.1 = [19 x i8] c"EC read timeout 2\0A\00"
define i32 @read_ec(i8 zeroext %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8
%5 = alloca i8*
%6 = alloca i32
store i8 %0, i8* %4
store i8* %1, i8** %5
%7 = call i32 @outb(i8 zeroext -128, i32 102)
%8 = call i32 @wait_for_ec_status(i32 2, i32 0)
%9 = icmp ne i32 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %24
13: ; preds = %2
%14 = load i8, i8* %4
%15 = call i32 @outb(i8 zeroext %14, i32 98)
%16 = call i32 @wait_for_ec_status(i32 1, i32 1)
%17 = icmp ne i32 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %24
21: ; preds = %13
%22 = call zeroext i8 @inb(i32 98)
%23 = load i8*, i8** %5
store i8 %22, i8* %23
store i32 1, i32* %3
br label %24
24: ; preds = %21, %18, %10
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [19 x i8] c"EC read timeout 1\0A\00"
@.str.1 = [19 x i8] c"EC read timeout 2\0A\00"
define i32 @read_ec(i8 zeroext %0, i8* nocapture writeonly %1) {
%3 = tail call i32 @outb(i8 zeroext -128, i32 102)
%4 = tail call i32 @wait_for_ec_status(i32 2, i32 0)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %18
9: ; preds = %2
%10 = tail call i32 @outb(i8 zeroext %0, i32 98)
%11 = tail call i32 @wait_for_ec_status(i32 1, i32 1)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %18
16: ; preds = %9
%17 = tail call zeroext i8 @inb(i32 98)
store i8 %17, i8* %1
br label %18
18: ; preds = %16, %13, %6
%19 = phi i32 [ 1, %16 ], [ 0, %13 ], [ 0, %6 ]
ret i32 %19
}