#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 */
typedef int uint32_t;
struct ethtool_regs {
  int cmd;
  int len;
};

/* Variables and functions */
int /*<<< orphan*/ err(int, char *);
scalar_t__ ethtool_call(char const *, struct ethtool_regs *);
uint32_t read_reg_ethtool(const char *iff_name, uint32_t addr) {
  const int REGDUMP_SIZE = 4 * 1024;
  char buf[sizeof(struct ethtool_regs) + REGDUMP_SIZE];
  struct ethtool_regs *regs = (struct ethtool_regs *)buf;
  regs->cmd = 0x00000004;
  regs->len = REGDUMP_SIZE;
  if (ethtool_call(iff_name, regs))
    err(1, "can't read registers");
  return *((uint32_t *)(buf + sizeof(struct ethtool_regs) + addr));
}


x86 assembly (clang, -O0):
<read_reg_ethtool>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x1000,-0x10(%rbp)
mov    %rsp,%rax
mov    %rax,-0x18(%rbp)
lea    -0x1030(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movl   $0x4,(%rax)
mov    -0x20(%rbp),%rax
movl   $0x1000,0x4(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <ethtool_call@plt>
cmp    $0x0,%rax
je     1188 <read_reg_ethtool+0x68>
mov    $0x1,%edi
lea    0xe7d(%rip),%rsi
call   1040 <err@plt>
movslq -0xc(%rbp),%rax
mov    -0x1028(%rbp,%rax,1),%eax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsp
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_reg_ethtool>:
push   %rbx
sub    $0x1010,%rsp
mov    %esi,%ebx
movabs $0x100000000004,%rax
mov    %rax,(%rsp)
mov    %rsp,%rsi
call   1030 <ethtool_call@plt>
test   %rax,%rax
je     1156 <read_reg_ethtool+0x36>
lea    0xeb4(%rip),%rsi
mov    $0x1,%edi
call   1040 <err@plt>
movslq %ebx,%rax
mov    0x8(%rsp,%rax,1),%eax
add    $0x1010,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.ethtool_regs = type { i32, i32 }
@.str = [21 x i8] c"can't read registers\00"
define i32 @read_reg_ethtool(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca %struct.ethtool_regs*
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 4096, i32* %5
%8 = call i8* @llvm.stacksave()
store i8* %8, i8** %6
%9 = alloca i8, i64 4104
%10 = bitcast i8* %9 to %struct.ethtool_regs*
store %struct.ethtool_regs* %10, %struct.ethtool_regs** %7
%11 = load %struct.ethtool_regs*, %struct.ethtool_regs** %7
%12 = getelementptr inbounds %struct.ethtool_regs, %struct.ethtool_regs* %11, i32 0, i32 0
store i32 4, i32* %12
%13 = load %struct.ethtool_regs*, %struct.ethtool_regs** %7
%14 = getelementptr inbounds %struct.ethtool_regs, %struct.ethtool_regs* %13, i32 0, i32 1
store i32 4096, i32* %14
%15 = load i8*, i8** %3
%16 = load %struct.ethtool_regs*, %struct.ethtool_regs** %7
%17 = call i64 @ethtool_call(i8* %15, %struct.ethtool_regs* %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @err(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %2
%22 = getelementptr inbounds i8, i8* %9, i64 8
%23 = load i32, i32* %4
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
%26 = bitcast i8* %25 to i32*
%27 = load i32, i32* %26
%28 = load i8*, i8** %6
ret i32 %27
}

LLVM IR (-O2):
%struct.ethtool_regs = type { i32, i32 }
@.str = [21 x i8] c"can't read registers\00"
define i32 @read_reg_ethtool(i8* %0, i32 %1) {
%3 = alloca [4104 x i8]
%4 = bitcast [4104 x i8]* %3 to %struct.ethtool_regs*
%5 = bitcast [4104 x i8]* %3 to i32*
store i32 4, i32* %5
%6 = getelementptr inbounds [4104 x i8], [4104 x i8]* %3, i64 0, i64 4
%7 = bitcast i8* %6 to i32*
store i32 4096, i32* %7
%8 = call i64 @ethtool_call(i8* %0, %struct.ethtool_regs* %4)
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = call i32 @err(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %2
%13 = getelementptr inbounds [4104 x i8], [4104 x i8]* %3, i64 0, i64 8
%14 = sext i32 %1 to i64
%15 = getelementptr inbounds i8, i8* %13, i64 %14
%16 = bitcast i8* %15 to i32*
%17 = load i32, i32* %16
ret i32 %17
}