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


x86 assembly (clang, -O0):
<smi_phy_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %si,%ax
mov    %di,%cx
mov    %cx,-0x2(%rbp)
mov    %ax,-0x4(%rbp)
movw   $0xffff,-0x6(%rbp)
mov    $0x1c,%edi
mov    $0x18,%esi
call   1030 <smi_read@plt>
mov    %ax,-0x6(%rbp)
movzwl -0x6(%rbp),%eax
and    $0x8000,%eax
cmp    $0x0,%eax
je     1190 <smi_phy_read+0x50>
mov    $0xa,%edi
call   1060 <usleep@plt>
jmp    1195 <smi_phy_read+0x55>
jmp    119a <smi_phy_read+0x5a>
jmp    115c <smi_phy_read+0x1c>
movw   $0x0,-0x6(%rbp)
movzwl -0x2(%rbp),%eax
shl    $0x5,%eax
or     $0x9800,%eax
movzwl -0x4(%rbp),%ecx
or     %ecx,%eax
mov    %ax,-0x6(%rbp)
movzwl -0x6(%rbp),%ecx
lea    0xe3f(%rip),%rdi
mov    $0x1c,%esi
mov    $0x18,%edx
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1c,%edi
mov    $0x18,%esi
movzwl -0x6(%rbp),%edx
call   1050 <smi_write@plt>
mov    $0x1c,%edi
mov    $0x18,%esi
call   1030 <smi_read@plt>
mov    %ax,-0x6(%rbp)
movzwl -0x6(%rbp),%eax
and    $0x8000,%eax
cmp    $0x0,%eax
je     1219 <smi_phy_read+0xd9>
mov    $0xa,%edi
call   1060 <usleep@plt>
jmp    121e <smi_phy_read+0xde>
jmp    1223 <smi_phy_read+0xe3>
jmp    11e5 <smi_phy_read+0xa5>
lea    0xe00(%rip),%rdi
mov    $0x1c,%esi
mov    $0x19,%edx
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1c,%edi
mov    $0x19,%esi
call   1030 <smi_read@plt>
movzwl %ax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smi_phy_read>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
nopw   0x0(%rax,%rax,1)
mov    $0x1c,%edi
mov    $0x18,%esi
call   1030 <smi_read@plt>
test   %ax,%ax
jns    1170 <smi_phy_read+0x30>
mov    $0xa,%edi
call   1060 <usleep@plt>
jmp    1150 <smi_phy_read+0x10>
shl    $0x5,%ebp
or     %ebp,%ebx
or     $0x9800,%ebx
movzwl %bx,%ebx
lea    0xe7b(%rip),%rdi
mov    $0x1c,%esi
mov    $0x18,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1c,%edi
mov    $0x18,%esi
mov    %ebx,%edx
call   1050 <smi_write@plt>
nopl   0x0(%rax)
mov    $0x1c,%edi
mov    $0x18,%esi
call   1030 <smi_read@plt>
test   %ax,%ax
jns    11d0 <smi_phy_read+0x90>
mov    $0xa,%edi
call   1060 <usleep@plt>
jmp    11b0 <smi_phy_read+0x70>
lea    0xe53(%rip),%rdi
mov    $0x1c,%esi
mov    $0x19,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1c,%edi
mov    $0x19,%esi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <smi_read@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 = [30 x i8] c" 2: read phy 0x%x reg 0x%x\0A\0A\00"
define zeroext i16 @smi_phy_read(i16 zeroext %0, i16 zeroext %1) {
%3 = alloca i16
%4 = alloca i16
%5 = alloca i16
store i16 %0, i16* %3
store i16 %1, i16* %4
store i16 -1, i16* %5
br label %6
6: ; preds = %2, %15
%7 = call zeroext i16 @smi_read(i32 28, i32 24)
store i16 %7, i16* %5
%8 = load i16, i16* %5
%9 = zext i16 %8 to i32
%10 = and i32 %9, 32768
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %6
%13 = call i32 @usleep(i32 10)
br label %15
14: ; preds = %6
br label %16
15: ; preds = %12
br label %6
16: ; preds = %14
store i16 0, i16* %5
%17 = load i16, i16* %3
%18 = zext i16 %17 to i32
%19 = shl i32 %18, 5
%20 = or i32 38912, %19
%21 = load i16, i16* %4
%22 = zext i16 %21 to i32
%23 = or i32 %20, %22
%24 = trunc i32 %23 to i16
store i16 %24, i16* %5
%25 = load i16, i16* %5
%26 = zext i16 %25 to i32
%27 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 28, i32 24, i32 %26)
%28 = load i16, i16* %5
%29 = call i32 @smi_write(i32 28, i32 24, i16 zeroext %28)
br label %30
30: ; preds = %16, %39
%31 = call zeroext i16 @smi_read(i32 28, i32 24)
store i16 %31, i16* %5
%32 = load i16, i16* %5
%33 = zext i16 %32 to i32
%34 = and i32 %33, 32768
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %30
%37 = call i32 @usleep(i32 10)
br label %39
38: ; preds = %30
br label %40
39: ; preds = %36
br label %30
40: ; preds = %38
%41 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 28, i32 25)
%42 = call zeroext i16 @smi_read(i32 28, i32 25)
ret i16 %42
}

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