#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 ifreq {
  scalar_t__ ifr_data;
};
struct ethtool_stats {
  int n_stats;
  scalar_t__ *data;
  int /*<<< orphan*/ cmd;
};
typedef scalar_t__ caddr_t;
typedef int /*<<< orphan*/ __u64;

/* Variables and functions */
int /*<<< orphan*/ ETHTOOL_GSTATS;
int /*<<< orphan*/ SIOCETHTOOL;
struct ethtool_stats *calloc(int, size_t);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int stat_data(int fd, struct ifreq *ifr, int n, int i) {
  size_t len = sizeof(struct ethtool_stats) + n * sizeof(__u64);
  struct ethtool_stats *stats = calloc(1, len);
  if (!stats)
    return -1;
  stats->cmd = ETHTOOL_GSTATS;
  stats->n_stats = n;
  ifr->ifr_data = (caddr_t)stats;
  if (ioctl(fd, SIOCETHTOOL, ifr) < 0)
    return -1;
  return (int)stats->data[i];
}


x86 assembly (clang, -O0):
<stat_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    $0x18,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
mov    $0x1,%edi
call   1040 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    116e <stat_data+0x4e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <stat_data+0xb0>
mov    0x2e73(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x10(%rax)
mov    -0x14(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%edi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    11bd <stat_data+0x9d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <stat_data+0xb0>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stat_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%ebx
mov    %rsi,%r13
mov    %edi,%r12d
movslq %edx,%rax
lea    0x18(,%rax,4),%rsi
mov    $0x1,%edi
call   1040 <calloc@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     1190 <stat_data+0x70>
mov    %rax,%rbp
mov    0x2e88(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rbp)
mov    %ebx,0x0(%rbp)
mov    %rbp,0x0(%r13)
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
mov    %r13,%rdx
call   1030 <ioctl@plt>
test   %rax,%rax
js     1190 <stat_data+0x70>
mov    0x8(%rbp),%rax
movslq %r14d,%rcx
mov    (%rax,%rcx,8),%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i64 }
%struct.ethtool_stats = type { i32, i64*, i32 }
@ETHTOOL_GSTATS = global i32 0
@SIOCETHTOOL = global i32 0
define i32 @stat_data(i32 %0, %struct.ifreq* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.ifreq*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca %struct.ethtool_stats*
store i32 %0, i32* %6
store %struct.ifreq* %1, %struct.ifreq** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%12 = load i32, i32* %8
%13 = sext i32 %12 to i64
%14 = mul i64 %13, 4
%15 = add i64 24, %14
store i64 %15, i64* %10
%16 = load i64, i64* %10
%17 = call %struct.ethtool_stats* @calloc(i32 1, i64 %16)
store %struct.ethtool_stats* %17, %struct.ethtool_stats** %11
%18 = load %struct.ethtool_stats*, %struct.ethtool_stats** %11
%19 = icmp ne %struct.ethtool_stats* %18, null
br i1 %19, label %21, label %20
20: ; preds = %4
store i32 -1, i32* %5
br label %47
21: ; preds = %4
%22 = load i32, i32* @ETHTOOL_GSTATS
%23 = load %struct.ethtool_stats*, %struct.ethtool_stats** %11
%24 = getelementptr inbounds %struct.ethtool_stats, %struct.ethtool_stats* %23, i32 0, i32 2
store i32 %22, i32* %24
%25 = load i32, i32* %8
%26 = load %struct.ethtool_stats*, %struct.ethtool_stats** %11
%27 = getelementptr inbounds %struct.ethtool_stats, %struct.ethtool_stats* %26, i32 0, i32 0
store i32 %25, i32* %27
%28 = load %struct.ethtool_stats*, %struct.ethtool_stats** %11
%29 = ptrtoint %struct.ethtool_stats* %28 to i64
%30 = load %struct.ifreq*, %struct.ifreq** %7
%31 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %30, i32 0, i32 0
store i64 %29, i64* %31
%32 = load i32, i32* %6
%33 = load i32, i32* @SIOCETHTOOL
%34 = load %struct.ifreq*, %struct.ifreq** %7
%35 = call i64 @ioctl(i32 %32, i32 %33, %struct.ifreq* %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %21
store i32 -1, i32* %5
br label %47
38: ; preds = %21
%39 = load %struct.ethtool_stats*, %struct.ethtool_stats** %11
%40 = getelementptr inbounds %struct.ethtool_stats, %struct.ethtool_stats* %39, i32 0, i32 1
%41 = load i64*, i64** %40
%42 = load i32, i32* %9
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i64, i64* %41, i64 %43
%45 = load i64, i64* %44
%46 = trunc i64 %45 to i32
store i32 %46, i32* %5
br label %47
47: ; preds = %38, %37, %20
%48 = load i32, i32* %5
ret i32 %48
}

LLVM IR (-O2):
%struct.ifreq = type { i64 }
%struct.ethtool_stats = type { i32, i64*, i32 }
@ETHTOOL_GSTATS = global i32 0
@SIOCETHTOOL = global i32 0
define i32 @stat_data(i32 %0, %struct.ifreq* %1, i32 %2, i32 %3) {
%5 = sext i32 %2 to i64
%6 = shl nsw i64 %5, 2
%7 = add nsw i64 %6, 24
%8 = tail call %struct.ethtool_stats* @calloc(i32 1, i64 %7)
%9 = icmp eq %struct.ethtool_stats* %8, null
br i1 %9, label %26, label %10
10: ; preds = %4
%11 = load i32, i32* @ETHTOOL_GSTATS
%12 = getelementptr inbounds %struct.ethtool_stats, %struct.ethtool_stats* %8, i64 0, i32 2
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.ethtool_stats, %struct.ethtool_stats* %8, i64 0, i32 0
store i32 %2, i32* %13
%14 = ptrtoint %struct.ethtool_stats* %8 to i64
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %1, i64 0, i32 0
store i64 %14, i64* %15
%16 = load i32, i32* @SIOCETHTOOL
%17 = tail call i64 @ioctl(i32 %0, i32 %16, %struct.ifreq* %1)
%18 = icmp slt i64 %17, 0
br i1 %18, label %26, label %19
19: ; preds = %10
%20 = getelementptr inbounds %struct.ethtool_stats, %struct.ethtool_stats* %8, i64 0, i32 1
%21 = load i64*, i64** %20
%22 = sext i32 %3 to i64
%23 = getelementptr inbounds i64, i64* %21, i64 %22
%24 = load i64, i64* %23
%25 = trunc i64 %24 to i32
br label %26
26: ; preds = %10, %4, %19
%27 = phi i32 [ %25, %19 ], [ -1, %4 ], [ -1, %10 ]
ret i32 %27
}