#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*/ printf(char *, int, int, unsigned short);
unsigned short smi_read(int, int);
int smi_write(int, int, unsigned short);
int /*<<< orphan*/ usleep(int);
int smi_phy_write(unsigned short phyaddr, unsigned short regnum,
                  unsigned short regvalue) {
  unsigned short value = 0xffff;
  while (1) {
    value = smi_read(0x1c, 0x18);
    if (value & (1 << 15)) {
      usleep(10);
    } else {
      break;
    }
  };
  printf("\n  1: write phy 0x%x reg 0x%x value 0x%x\n", 0x1c, 0x19, regvalue);
  smi_write(0x1c, 0x19, regvalue);
  value = 0;
  value = (1 << 15) | (1 << 12) | (1 << 10) | (phyaddr << 5) | regnum;
  printf("  2: write phy 0x%x reg 0x%x value 0x%x\n\n", 0x1c, 0x18, value);
  return smi_write(0x1c, 0x18, value);
}


x86 assembly (clang, -O0):
<smi_phy_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %dx,%ax
mov    %si,%cx
mov    %di,%dx
mov    %dx,-0x2(%rbp)
mov    %cx,-0x4(%rbp)
mov    %ax,-0x6(%rbp)
movw   $0xffff,-0x8(%rbp)
mov    $0x1c,%edi
mov    $0x18,%esi
call   1030 <smi_read@plt>
mov    %ax,-0x8(%rbp)
movzwl -0x8(%rbp),%eax
and    $0x8000,%eax
cmp    $0x0,%eax
je     1197 <smi_phy_write+0x57>
mov    $0xa,%edi
call   1060 <usleep@plt>
jmp    119c <smi_phy_write+0x5c>
jmp    11a1 <smi_phy_write+0x61>
jmp    1163 <smi_phy_write+0x23>
lea    0xe58(%rip),%rdi
mov    $0x1c,%esi
mov    $0x19,%edx
movzwl -0x6(%rbp),%ecx
call   1040 <printf@plt>
mov    $0x1c,%edi
mov    $0x19,%esi
movzwl -0x6(%rbp),%edx
call   1050 <smi_write@plt>
movw   $0x0,-0x8(%rbp)
movzwl -0x2(%rbp),%eax
shl    $0x5,%eax
or     $0x9400,%eax
movzwl -0x4(%rbp),%ecx
or     %ecx,%eax
mov    %ax,-0x8(%rbp)
lea    0xe39(%rip),%rdi
mov    $0x1c,%esi
mov    $0x18,%edx
movzwl -0x8(%rbp),%ecx
call   1040 <printf@plt>
mov    $0x1c,%edi
mov    $0x18,%esi
movzwl -0x8(%rbp),%edx
call   1050 <smi_write@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smi_phy_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%ebx
mov    %edi,%r15d
nop
mov    $0x1c,%edi
mov    $0x18,%esi
call   1030 <smi_read@plt>
test   %ax,%ax
jns    1170 <smi_phy_write+0x30>
mov    $0xa,%edi
call   1060 <usleep@plt>
jmp    1150 <smi_phy_write+0x10>
movzwl %r14w,%ebp
lea    0xe85(%rip),%rdi
mov    $0x1c,%esi
mov    $0x19,%edx
mov    %ebp,%ecx
call   1040 <printf@plt>
mov    $0x1c,%edi
mov    $0x19,%esi
mov    %ebp,%edx
call   1050 <smi_write@plt>
shl    $0x5,%r15d
or     %r15d,%ebx
or     $0x9400,%ebx
movzwl %bx,%ebx
lea    0xe76(%rip),%rdi
mov    $0x1c,%esi
mov    $0x18,%edx
mov    %ebx,%ecx
call   1040 <printf@plt>
mov    $0x1c,%edi
mov    $0x18,%esi
mov    %ebx,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <smi_write@plt>

LLVM IR (-O0):
@.str = [42 x i8] c"\0A 1: write phy 0x%x reg 0x%x value 0x%x\0A\00"
@.str.1 = [42 x i8] c" 2: write phy 0x%x reg 0x%x value 0x%x\0A\0A\00"
define i32 @smi_phy_write(i16 zeroext %0, i16 zeroext %1, i16 zeroext %2) {
%4 = alloca i16
%5 = alloca i16
%6 = alloca i16
%7 = alloca i16
store i16 %0, i16* %4
store i16 %1, i16* %5
store i16 %2, i16* %6
store i16 -1, i16* %7
br label %8
8: ; preds = %3, %17
%9 = call zeroext i16 @smi_read(i32 28, i32 24)
store i16 %9, i16* %7
%10 = load i16, i16* %7
%11 = zext i16 %10 to i32
%12 = and i32 %11, 32768
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = call i32 @usleep(i32 10)
br label %17
16: ; preds = %8
br label %18
17: ; preds = %14
br label %8
18: ; preds = %16
%19 = load i16, i16* %6
%20 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 28, i32 25, i16 zeroext %19)
%21 = load i16, i16* %6
%22 = call i32 @smi_write(i32 28, i32 25, i16 zeroext %21)
store i16 0, i16* %7
%23 = load i16, i16* %4
%24 = zext i16 %23 to i32
%25 = shl i32 %24, 5
%26 = or i32 37888, %25
%27 = load i16, i16* %5
%28 = zext i16 %27 to i32
%29 = or i32 %26, %28
%30 = trunc i32 %29 to i16
store i16 %30, i16* %7
%31 = load i16, i16* %7
%32 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 28, i32 24, i16 zeroext %31)
%33 = load i16, i16* %7
%34 = call i32 @smi_write(i32 28, i32 24, i16 zeroext %33)
ret i32 %34
}

LLVM IR (-O2):
@.str = [42 x i8] c"\0A 1: write phy 0x%x reg 0x%x value 0x%x\0A\00"
@.str.1 = [42 x i8] c" 2: write phy 0x%x reg 0x%x value 0x%x\0A\0A\00"
define i32 @smi_phy_write(i16 zeroext %0, i16 zeroext %1, i16 zeroext %2) {
%4 = tail call zeroext i16 @smi_read(i32 28, i32 24)
%5 = icmp sgt i16 %4, -1
br i1 %5, label %10, label %6
6: ; preds = %3, %6
%7 = tail call i32 @usleep(i32 10)
%8 = tail call zeroext i16 @smi_read(i32 28, i32 24)
%9 = icmp sgt i16 %8, -1
br i1 %9, label %10, label %6
10: ; preds = %6, %3
%11 = tail call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 28, i32 25, i16 zeroext %2)
%12 = tail call i32 @smi_write(i32 28, i32 25, i16 zeroext %2)
%13 = shl i16 %0, 5
%14 = or i16 %13, %1
%15 = or i16 %14, -27648
%16 = tail call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 28, i32 24, i16 zeroext %15)
%17 = tail call i32 @smi_write(i32 28, i32 24, i16 zeroext %15)
ret i32 %17
}