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

/* Variables and functions */
int EBUSY;
int /*<<< orphan*/ ETHTOOL_GLINK;
int /*<<< orphan*/ SIOCETHTOOL;
int errno;
int /*<<< orphan*/ exit(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *, int);
char *strerror(int);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int detect_link(int skfd, char *ifname) {
  struct ifreq ifr;
  struct ethtool_value edata;
  memset(&ifr, 0, sizeof(ifr));
  edata.cmd = ETHTOOL_GLINK;
  strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
  ifr.ifr_data = (char *)&edata;
  if (ioctl(skfd, SIOCETHTOOL, &ifr) == -1) {
    printf("Error: ETHTOOL_GLINK ioctl failed < %s (%d) >\n", strerror(errno),
           errno);
    if (errno == EBUSY)
      return 2;
    else
      exit(-1);
  }
  return (edata.data ? 1 : 0);
}


x86 assembly (clang, -O0):
<detect_link>:
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   1050 <memset@plt>
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%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    0x2e12(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1216 <detect_link+0xb6>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
mov    0x2dfe(%rip),%rax
mov    (%rax),%edx
lea    0xe1d(%rip),%rdi
call   1040 <printf@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    0x2dd8(%rip),%rcx
cmp    (%rcx),%eax
jne    120c <detect_link+0xac>
movl   $0x2,-0x4(%rbp)
jmp    122b <detect_link+0xcb>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x30(%rbp),%rdx
xor    %eax,%eax
mov    $0x1,%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_link>:
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   1050 <memset@plt>
mov    0x2e60(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x18(%rsp),%edi
mov    %rbx,%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    %rsp,%rax
mov    %rax,0x10(%rsp)
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11cb <detect_link+0x6b>
xor    %eax,%eax
cmpq   $0x0,(%rsp)
setne  %al
jmp    11fc <detect_link+0x9c>
mov    0x2e06(%rip),%rbx
mov    (%rbx),%edi
call   1080 <strerror@plt>
mov    (%rbx),%edx
lea    0xe1e(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    (%rbx),%ecx
mov    0x2ddd(%rip),%rdx
mov    $0x2,%eax
cmp    (%rdx),%ecx
jne    1205 <detect_link+0xa5>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.ifreq = type { i8*, i32 }
%struct.ethtool_value = type { i64, i32 }
@ETHTOOL_GLINK = global i32 0
@SIOCETHTOOL = global i32 0
@.str = [47 x i8] c"Error: ETHTOOL_GLINK ioctl failed < %s (%d) >\0A\00"
@errno = global i32 0
@EBUSY = global i32 0
define i32 @detect_link(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 = load i32, i32* @ETHTOOL_GLINK
%10 = getelementptr inbounds %struct.ethtool_value, %struct.ethtool_value* %7, i32 0, i32 1
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%12 = load i32, i32* %11
%13 = load i8*, i8** %5
%14 = call i32 @strncpy(i32 %12, i8* %13, i32 3)
%15 = bitcast %struct.ethtool_value* %7 to i8*
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
store i8* %15, i8** %16
%17 = load i32, i32* %4
%18 = load i32, i32* @SIOCETHTOOL
%19 = call i32 @ioctl(i32 %17, i32 %18, %struct.ifreq* %6)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %32
21: ; preds = %2
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = load i32, i32* @errno
%25 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8* %23, i32 %24)
%26 = load i32, i32* @errno
%27 = load i32, i32* @EBUSY
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %21
store i32 2, i32* %3
br label %38
30: ; preds = %21
%31 = call i32 @exit(i32 -1)
unreachable
32: ; preds = %2
%33 = getelementptr inbounds %struct.ethtool_value, %struct.ethtool_value* %7, i32 0, i32 0
%34 = load i64, i64* %33
%35 = icmp ne i64 %34, 0
%36 = zext i1 %35 to i64
%37 = select i1 %35, i32 1, i32 0
store i32 %37, i32* %3
br label %38
38: ; preds = %32, %29
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
%struct.ifreq = type { i8*, i32 }
%struct.ethtool_value = type { i64, i32 }
@ETHTOOL_GLINK = global i32 0
@SIOCETHTOOL = global i32 0
@.str = [47 x i8] c"Error: ETHTOOL_GLINK ioctl failed < %s (%d) >\0A\00"
@errno = global i32 0
@EBUSY = global i32 0
define i32 @detect_link(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 = load i32, i32* @ETHTOOL_GLINK
%9 = getelementptr inbounds %struct.ethtool_value, %struct.ethtool_value* %4, i64 0, i32 1
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%11 = load i32, i32* %10
%12 = call i32 @strncpy(i32 %11, i8* %1, i32 3)
%13 = bitcast %struct.ifreq* %3 to %struct.ethtool_value**
store %struct.ethtool_value* %4, %struct.ethtool_value** %13
%14 = load i32, i32* @SIOCETHTOOL
%15 = call i32 @ioctl(i32 %0, i32 %14, %struct.ifreq* %3)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %27
17: ; preds = %2
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = load i32, i32* @errno
%21 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8* %19, i32 %20)
%22 = load i32, i32* @errno
%23 = load i32, i32* @EBUSY
%24 = icmp eq i32 %22, %23
br i1 %24, label %32, label %25
25: ; preds = %17
%26 = call i32 @exit(i32 -1)
unreachable
27: ; preds = %2
%28 = getelementptr inbounds %struct.ethtool_value, %struct.ethtool_value* %4, i64 0, i32 0
%29 = load i64, i64* %28
%30 = icmp ne i64 %29, 0
%31 = zext i1 %30 to i32
br label %32
32: ; preds = %17, %27
%33 = phi i32 [ %31, %27 ], [ 2, %17 ]
ret i32 %33
}