#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_drvinfo {
  int n_stats;
  int /*<<< orphan*/ cmd;
};
typedef scalar_t__ caddr_t;

/* Variables and functions */
int /*<<< orphan*/ ETHTOOL_GDRVINFO;
int /*<<< orphan*/ SIOCETHTOOL;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int num_stats(int fd, struct ifreq *ifr) {
  struct ethtool_drvinfo drvinfo;
  drvinfo.cmd = ETHTOOL_GDRVINFO;
  ifr->ifr_data = (caddr_t)&drvinfo;
  if (ioctl(fd, SIOCETHTOOL, ifr) < 0)
    return -1;
  return drvinfo.n_stats;
}


x86 assembly (clang, -O0):
<num_stats>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2eaa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
lea    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%edi
mov    0x2e98(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    1161 <num_stats+0x51>
movl   $0xffffffff,-0x4(%rbp)
jmp    1167 <num_stats+0x57>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<num_stats>:
push   %rax
mov    %rsi,%rdx
mov    0x2eb5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %rsp,%rax
mov    %rax,(%rsi)
mov    0x2eaa(%rip),%rax
mov    (%rax),%esi
call   1030 <ioctl@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
js     1145 <num_stats+0x35>
mov    (%rsp),%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.ifreq = type { i64 }
%struct.ethtool_drvinfo = type { i32, i32 }
@ETHTOOL_GDRVINFO = global i32 0
@SIOCETHTOOL = global i32 0
define i32 @num_stats(i32 %0, %struct.ifreq* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.ifreq*
%6 = alloca %struct.ethtool_drvinfo
store i32 %0, i32* %4
store %struct.ifreq* %1, %struct.ifreq** %5
%7 = load i32, i32* @ETHTOOL_GDRVINFO
%8 = getelementptr inbounds %struct.ethtool_drvinfo, %struct.ethtool_drvinfo* %6, i32 0, i32 1
store i32 %7, i32* %8
%9 = ptrtoint %struct.ethtool_drvinfo* %6 to i64
%10 = load %struct.ifreq*, %struct.ifreq** %5
%11 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %10, i32 0, i32 0
store i64 %9, i64* %11
%12 = load i32, i32* %4
%13 = load i32, i32* @SIOCETHTOOL
%14 = load %struct.ifreq*, %struct.ifreq** %5
%15 = call i64 @ioctl(i32 %12, i32 %13, %struct.ifreq* %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %2
store i32 -1, i32* %3
br label %21
18: ; preds = %2
%19 = getelementptr inbounds %struct.ethtool_drvinfo, %struct.ethtool_drvinfo* %6, i32 0, i32 0
%20 = load i32, i32* %19
store i32 %20, i32* %3
br label %21
21: ; preds = %18, %17
%22 = load i32, i32* %3
ret i32 %22
}

LLVM IR (-O2):
%struct.ifreq = type { i64 }
%struct.ethtool_drvinfo = type { i32, i32 }
@ETHTOOL_GDRVINFO = global i32 0
@SIOCETHTOOL = global i32 0
define i32 @num_stats(i32 %0, %struct.ifreq* %1) {
%3 = alloca %struct.ethtool_drvinfo
%4 = bitcast %struct.ethtool_drvinfo* %3 to i8*
%5 = load i32, i32* @ETHTOOL_GDRVINFO
%6 = getelementptr inbounds %struct.ethtool_drvinfo, %struct.ethtool_drvinfo* %3, i64 0, i32 1
store i32 %5, i32* %6
%7 = ptrtoint %struct.ethtool_drvinfo* %3 to i64
%8 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %1, i64 0, i32 0
store i64 %7, i64* %8
%9 = load i32, i32* @SIOCETHTOOL
%10 = call i64 @ioctl(i32 %0, i32 %9, %struct.ifreq* %1)
%11 = icmp slt i64 %10, 0
%12 = getelementptr inbounds %struct.ethtool_drvinfo, %struct.ethtool_drvinfo* %3, i64 0, i32 0
%13 = load i32, i32* %12
%14 = select i1 %11, i32 -1, i32 %13
ret i32 %14
}