#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 {
  char *ifr_data;
  int /*<<< orphan*/ ifr_name;
};
struct ethtool_value {
  int cmd;
  scalar_t__ data;
};
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int /*<<< orphan*/ SIOCETHTOOL;
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int detect_ethtool(int skfd, char *ifname) {
  struct ifreq ifr;
  struct ethtool_value edata;
  memset(&ifr, 0, sizeof(ifr));
  edata.cmd = 0x0000000a;
  strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
  ifr.ifr_data = (char *)&edata;
  if (ioctl(skfd, SIOCETHTOOL, &ifr) == -1) {
    return 2;
  }
  return (edata.data ? 0 : 1);
}


x86 assembly (clang, -O0):
<detect_ethtool>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
movl   $0xa,-0x30(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
lea    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1199 <detect_ethtool+0x69>
movl   $0x2,-0x4(%rbp)
jmp    11ae <detect_ethtool+0x7e>
mov    -0x28(%rbp),%rdx
mov    $0x1,%eax
xor    %ecx,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<detect_ethtool>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
movl   $0xa,(%rsp)
mov    0x18(%rsp),%edi
mov    %rbx,%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    %rsp,%rax
mov    %rax,0x10(%rsp)
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   1050 <ioctl@plt>
xor    %ecx,%ecx
cmpq   $0x0,0x8(%rsp)
sete   %cl
cmp    $0xffffffff,%eax
mov    $0x2,%eax
cmovne %ecx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i8*, i32 }
%struct.ethtool_value = type { i32, i64 }
@SIOCETHTOOL = global i32 0
define i32 @detect_ethtool(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.ifreq
%7 = alloca %struct.ethtool_value
store i32 %0, i32* %4
store i8* %1, i8** %5
%8 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 16)
%9 = getelementptr inbounds %struct.ethtool_value, %struct.ethtool_value* %7, i32 0, i32 0
store i32 10, i32* %9
%10 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%11 = load i32, i32* %10
%12 = load i8*, i8** %5
%13 = call i32 @strncpy(i32 %11, i8* %12, i32 3)
%14 = bitcast %struct.ethtool_value* %7 to i8*
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
store i8* %14, i8** %15
%16 = load i32, i32* %4
%17 = load i32, i32* @SIOCETHTOOL
%18 = call i32 @ioctl(i32 %16, i32 %17, %struct.ifreq* %6)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %2
store i32 2, i32* %3
br label %27
21: ; preds = %2
%22 = getelementptr inbounds %struct.ethtool_value, %struct.ethtool_value* %7, i32 0, i32 1
%23 = load i64, i64* %22
%24 = icmp ne i64 %23, 0
%25 = zext i1 %24 to i64
%26 = select i1 %24, i32 0, i32 1
store i32 %26, i32* %3
br label %27
27: ; preds = %21, %20
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
%struct.ifreq = type { i8*, i32 }
%struct.ethtool_value = type { i32, i64 }
@SIOCETHTOOL = global i32 0
define i32 @detect_ethtool(i32 %0, i8* %1) {
%3 = alloca %struct.ifreq
%4 = alloca %struct.ethtool_value
%5 = bitcast %struct.ifreq* %3 to i8*
%6 = bitcast %struct.ethtool_value* %4 to i8*
%7 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 16)
%8 = getelementptr inbounds %struct.ethtool_value, %struct.ethtool_value* %4, i64 0, i32 0
store i32 10, i32* %8
%9 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%10 = load i32, i32* %9
%11 = call i32 @strncpy(i32 %10, i8* %1, i32 3)
%12 = bitcast %struct.ifreq* %3 to %struct.ethtool_value**
store %struct.ethtool_value* %4, %struct.ethtool_value** %12
%13 = load i32, i32* @SIOCETHTOOL
%14 = call i32 @ioctl(i32 %0, i32 %13, %struct.ifreq* %3)
%15 = icmp eq i32 %14, -1
%16 = getelementptr inbounds %struct.ethtool_value, %struct.ethtool_value* %4, i64 0, i32 1
%17 = load i64, i64* %16
%18 = icmp eq i64 %17, 0
%19 = zext i1 %18 to i32
%20 = select i1 %15, i32 2, i32 %19
ret i32 %20
}