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


x86 assembly (clang, -O0):
<write_ec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %sil,%al
mov    %dil,%cl
mov    %cl,-0x5(%rbp)
mov    %al,-0x6(%rbp)
mov    $0x81,%edi
mov    $0x66,%esi
call   1030 <outb@plt>
mov    $0x2,%edi
xor    %esi,%esi
call   1050 <wait_for_ec_status@plt>
cmp    $0x0,%eax
jne    1189 <write_ec+0x59>
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
lea    0xe88(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1218 <write_ec+0xe8>
mov    $0x62,%esi
movzbl -0x5(%rbp),%edi
call   1030 <outb@plt>
mov    $0x2,%edi
xor    %esi,%esi
call   1050 <wait_for_ec_status@plt>
cmp    $0x0,%eax
jne    11cd <write_ec+0x9d>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
lea    0xe58(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1218 <write_ec+0xe8>
mov    $0x62,%esi
movzbl -0x6(%rbp),%edi
call   1030 <outb@plt>
mov    $0x2,%edi
xor    %esi,%esi
call   1050 <wait_for_ec_status@plt>
cmp    $0x0,%eax
jne    1211 <write_ec+0xe1>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
lea    0xe28(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1218 <write_ec+0xe8>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_ec>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
mov    $0x81,%edi
mov    $0x66,%esi
call   1030 <outb@plt>
mov    $0x2,%edi
xor    %esi,%esi
call   1050 <wait_for_ec_status@plt>
test   %eax,%eax
je     11a1 <write_ec+0x71>
movzbl %bpl,%edi
mov    $0x62,%esi
call   1030 <outb@plt>
mov    $0x2,%edi
xor    %esi,%esi
call   1050 <wait_for_ec_status@plt>
test   %eax,%eax
je     11aa <write_ec+0x7a>
movzbl %bl,%edi
mov    $0x62,%esi
call   1030 <outb@plt>
mov    $0x2,%edi
xor    %esi,%esi
call   1050 <wait_for_ec_status@plt>
mov    %eax,%ecx
mov    $0x1,%eax
test   %ecx,%ecx
jne    11c1 <write_ec+0x91>
lea    0xe89(%rip),%rsi
jmp    11b1 <write_ec+0x81>
lea    0xe58(%rip),%rsi
jmp    11b1 <write_ec+0x81>
lea    0xe63(%rip),%rsi
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
call   1040 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [20 x i8] c"EC write timeout 1\0A\00"
@.str.1 = [20 x i8] c"EC write timeout 2\0A\00"
@.str.2 = [20 x i8] c"EC write timeout 3\0A\00"
define i32 @write_ec(i8 zeroext %0, i8 zeroext %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 -127, 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 ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %30
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 2, i32 0)
%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 ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %30
21: ; preds = %13
%22 = load i8, i8* %5
%23 = call i32 @outb(i8 zeroext %22, i32 98)
%24 = call i32 @wait_for_ec_status(i32 2, i32 0)
%25 = icmp ne i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %21
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %30
29: ; preds = %21
store i32 1, i32* %3
br label %30
30: ; preds = %29, %26, %18, %10
%31 = load i32, i32* %3
ret i32 %31
}

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