#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 */
struct device_rf {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ printf(char *, unsigned short, unsigned char, unsigned char);
unsigned char rf_read_short_reg(struct device_rf *, unsigned short);
int /*<<< orphan*/ spi_readwrite(struct device_rf *, unsigned char *,
                                 unsigned char *, int);
int rf_write_long_reg(struct device_rf *rf, unsigned short reg,
                      unsigned char val, int check) {
  unsigned char tx[3], rx[3], fb;
  tx[0] = (((reg >> 3)) & 0x7F) | 0x80;
  tx[1] = (((reg << 5)) & 0xE0) | 0x10;
  tx[2] = val;
  spi_readwrite(rf, tx, rx, 2);
  if (check) {
    fb = rf_read_short_reg(rf, reg);
    if (fb != val)
      printf("PHY set reg %04X=%02X failed, read back %02X\r\n", reg, val, fb);
  }
  return 0;
}


x86 assembly (clang, -O0):
<rf_write_long_reg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dl,%al
mov    %si,%dx
mov    %rdi,-0x8(%rbp)
mov    %dx,-0xa(%rbp)
mov    %al,-0xb(%rbp)
mov    %ecx,-0x10(%rbp)
movzwl -0xa(%rbp),%eax
sar    $0x3,%eax
and    $0x7f,%eax
or     $0x80,%eax
mov    %al,-0x13(%rbp)
movzwl -0xa(%rbp),%eax
shl    $0x5,%eax
and    $0xe0,%eax
or     $0x10,%eax
mov    %al,-0x12(%rbp)
mov    -0xb(%rbp),%al
mov    %al,-0x11(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x13(%rbp),%rsi
lea    -0x16(%rbp),%rdx
mov    $0x2,%ecx
call   1050 <spi_readwrite@plt>
cmpl   $0x0,-0x10(%rbp)
je     11d7 <rf_write_long_reg+0xa7>
mov    -0x8(%rbp),%rdi
movzwl -0xa(%rbp),%esi
call   1040 <rf_read_short_reg@plt>
mov    %al,-0x17(%rbp)
movzbl -0x17(%rbp),%eax
movzbl -0xb(%rbp),%ecx
cmp    %ecx,%eax
je     11d2 <rf_write_long_reg+0xa2>
mov    -0xa(%rbp),%cx
mov    -0xb(%rbp),%al
lea    0xe3d(%rip),%rdi
movzwl %cx,%esi
movzbl %al,%edx
movzbl -0x17(%rbp),%ecx
call   1030 <printf@plt>
jmp    11d7 <rf_write_long_reg+0xa7>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rf_write_long_reg>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r15d
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %esi,%eax
shr    $0x3,%eax
or     $0x80,%al
mov    %al,0x2(%rsp)
mov    %ebp,%eax
shl    $0x5,%al
or     $0x10,%al
mov    %al,0x3(%rsp)
mov    %r14b,0x4(%rsp)
lea    0x2(%rsp),%rsi
lea    0x5(%rsp),%rdx
mov    $0x2,%ecx
call   1050 <spi_readwrite@plt>
test   %r15d,%r15d
je     119d <rf_write_long_reg+0x6d>
movzwl %bp,%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
call   1040 <rf_read_short_reg@plt>
cmp    %r14b,%al
je     119d <rf_write_long_reg+0x6d>
movzbl %r14b,%edx
movzbl %al,%ecx
lea    0xe6a(%rip),%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.device_rf = type { i32 }
@.str = [47 x i8] c"PHY set reg %04X=%02X failed, read back %02X\0D\0A\00"
define i32 @rf_write_long_reg(%struct.device_rf* %0, i16 zeroext %1, i8 zeroext %2, i32 %3) {
%5 = alloca %struct.device_rf*
%6 = alloca i16
%7 = alloca i8
%8 = alloca i32
%9 = alloca [3 x i8]
%10 = alloca [3 x i8]
%11 = alloca i8
store %struct.device_rf* %0, %struct.device_rf** %5
store i16 %1, i16* %6
store i8 %2, i8* %7
store i32 %3, i32* %8
%12 = load i16, i16* %6
%13 = zext i16 %12 to i32
%14 = ashr i32 %13, 3
%15 = and i32 %14, 127
%16 = or i32 %15, 128
%17 = trunc i32 %16 to i8
%18 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 0
store i8 %17, i8* %18
%19 = load i16, i16* %6
%20 = zext i16 %19 to i32
%21 = shl i32 %20, 5
%22 = and i32 %21, 224
%23 = or i32 %22, 16
%24 = trunc i32 %23 to i8
%25 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 1
store i8 %24, i8* %25
%26 = load i8, i8* %7
%27 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 2
store i8 %26, i8* %27
%28 = load %struct.device_rf*, %struct.device_rf** %5
%29 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 0
%30 = getelementptr inbounds [3 x i8], [3 x i8]* %10, i64 0, i64 0
%31 = call i32 @spi_readwrite(%struct.device_rf* %28, i8* %29, i8* %30, i32 2)
%32 = load i32, i32* %8
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %49
34: ; preds = %4
%35 = load %struct.device_rf*, %struct.device_rf** %5
%36 = load i16, i16* %6
%37 = call zeroext i8 @rf_read_short_reg(%struct.device_rf* %35, i16 zeroext %36)
store i8 %37, i8* %11
%38 = load i8, i8* %11
%39 = zext i8 %38 to i32
%40 = load i8, i8* %7
%41 = zext i8 %40 to i32
%42 = icmp ne i32 %39, %41
br i1 %42, label %43, label %48
43: ; preds = %34
%44 = load i16, i16* %6
%45 = load i8, i8* %7
%46 = load i8, i8* %11
%47 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i16 zeroext %44, i8 zeroext %45, i8 zeroext %46)
br label %48
48: ; preds = %43, %34
br label %49
49: ; preds = %48, %4
ret i32 0
}

LLVM IR (-O2):
%struct.device_rf = type { i32 }
@.str = [47 x i8] c"PHY set reg %04X=%02X failed, read back %02X\0D\0A\00"
define i32 @rf_write_long_reg(%struct.device_rf* %0, i16 zeroext %1, i8 zeroext %2, i32 %3) {
%5 = alloca [3 x i8]
%6 = alloca [3 x i8]
%7 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 0
%8 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 0
%9 = trunc i16 %1 to i8
%10 = lshr i16 %1, 3
%11 = trunc i16 %10 to i8
%12 = or i8 %11, -128
store i8 %12, i8* %7
%13 = shl i8 %9, 5
%14 = or i8 %13, 16
%15 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 1
store i8 %14, i8* %15
%16 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 2
store i8 %2, i8* %16
%17 = call i32 @spi_readwrite(%struct.device_rf* %0, i8* %7, i8* %8, i32 2)
%18 = icmp eq i32 %3, 0
br i1 %18, label %24, label %19
19: ; preds = %4
%20 = call zeroext i8 @rf_read_short_reg(%struct.device_rf* %0, i16 zeroext %1)
%21 = icmp eq i8 %20, %2
br i1 %21, label %24, label %22
22: ; preds = %19
%23 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i16 zeroext %1, i8 zeroext %2, i8 zeroext %20)
br label %24
24: ; preds = %19, %22, %4
ret i32 0
}