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

/* Variables and functions */
scalar_t__ EOPNOTSUPP;
int /*<<< orphan*/ ETHTOOL_GLINK;
int /*<<< orphan*/ SIOCETHTOOL;
scalar_t__ errno;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int linkStatus(int sd, struct ifreq *ifr) {
  int resultCode = 0;
  struct ethtool_value edata;
  caddr_t oldIFRData = ifr->ifr_data;
  edata.cmd = ETHTOOL_GLINK;
  ifr->ifr_data = (caddr_t)&edata;
  if (ioctl(sd, SIOCETHTOOL, ifr) == 0) {
    resultCode = (edata.data ? 1 : 2);
  } else if (errno != EOPNOTSUPP) {
    perror("Cannot get link status");
    resultCode = 3;
  }
  ifr->ifr_data = oldIFRData;
  return resultCode;
}


x86 assembly (clang, -O0):
<linkStatus>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2ea0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
lea    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x4(%rbp),%edi
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jne    1194 <linkStatus+0x74>
mov    -0x28(%rbp),%rdx
mov    $0x2,%eax
mov    $0x1,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    11c6 <linkStatus+0xa6>
mov    0x2e35(%rip),%rax
mov    (%rax),%rax
mov    0x2e33(%rip),%rcx
cmp    (%rcx),%rax
je     11c1 <linkStatus+0xa1>
lea    0xe4b(%rip),%rdi
call   1040 <perror@plt>
movl   $0x3,-0x14(%rbp)
jmp    11c6 <linkStatus+0xa6>
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<linkStatus>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    (%rsi),%r14
mov    0x2eb4(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
lea    0x8(%rsp),%rax
mov    %rax,(%rsi)
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdx
call   1030 <ioctl@plt>
test   %rax,%rax
je     1183 <linkStatus+0x63>
mov    0x2e71(%rip),%rax
mov    (%rax),%rcx
mov    0x2e6f(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
je     1191 <linkStatus+0x71>
lea    0xe89(%rip),%rdi
call   1040 <perror@plt>
mov    $0x3,%eax
jmp    1191 <linkStatus+0x71>
cmpq   $0x1,0x8(%rsp)
mov    $0x1,%eax
adc    $0x0,%eax
mov    %r14,(%rbx)
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.ifreq = type { i8* }
%struct.ethtool_value = type { i64, i32 }
@ETHTOOL_GLINK = global i32 0
@SIOCETHTOOL = global i32 0
@errno = global i64 0
@EOPNOTSUPP = global i64 0
@.str = [23 x i8] c"Cannot get link status\00"
define i32 @linkStatus(i32 %0, %struct.ifreq* %1) {
%3 = alloca i32
%4 = alloca %struct.ifreq*
%5 = alloca i32
%6 = alloca %struct.ethtool_value
%7 = alloca i8*
store i32 %0, i32* %3
store %struct.ifreq* %1, %struct.ifreq** %4
store i32 0, i32* %5
%8 = load %struct.ifreq*, %struct.ifreq** %4
%9 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 0
%10 = load i8*, i8** %9
store i8* %10, i8** %7
%11 = load i32, i32* @ETHTOOL_GLINK
%12 = getelementptr inbounds %struct.ethtool_value, %struct.ethtool_value* %6, i32 0, i32 1
store i32 %11, i32* %12
%13 = bitcast %struct.ethtool_value* %6 to i8*
%14 = load %struct.ifreq*, %struct.ifreq** %4
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %14, i32 0, i32 0
store i8* %13, i8** %15
%16 = load i32, i32* %3
%17 = load i32, i32* @SIOCETHTOOL
%18 = load %struct.ifreq*, %struct.ifreq** %4
%19 = call i64 @ioctl(i32 %16, i32 %17, %struct.ifreq* %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %2
%22 = getelementptr inbounds %struct.ethtool_value, %struct.ethtool_value* %6, i32 0, i32 0
%23 = load i64, i64* %22
%24 = icmp ne i64 %23, 0
%25 = zext i1 %24 to i64
%26 = select i1 %24, i32 1, i32 2
store i32 %26, i32* %5
br label %34
27: ; preds = %2
%28 = load i64, i64* @errno
%29 = load i64, i64* @EOPNOTSUPP
%30 = icmp ne i64 %28, %29
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 3, i32* %5
br label %33
33: ; preds = %31, %27
br label %34
34: ; preds = %33, %21
%35 = load i8*, i8** %7
%36 = load %struct.ifreq*, %struct.ifreq** %4
%37 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %36, i32 0, i32 0
store i8* %35, i8** %37
%38 = load i32, i32* %5
ret i32 %38
}

LLVM IR (-O2):
%struct.ifreq = type { i8* }
%struct.ethtool_value = type { i64, i32 }
@ETHTOOL_GLINK = global i32 0
@SIOCETHTOOL = global i32 0
@errno = global i64 0
@EOPNOTSUPP = global i64 0
@.str = [23 x i8] c"Cannot get link status\00"
define i32 @linkStatus(i32 %0, %struct.ifreq* %1) {
%3 = alloca %struct.ethtool_value
%4 = bitcast %struct.ethtool_value* %3 to i8*
%5 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %1, i64 0, i32 0
%6 = load i8*, i8** %5
%7 = load i32, i32* @ETHTOOL_GLINK
%8 = getelementptr inbounds %struct.ethtool_value, %struct.ethtool_value* %3, i64 0, i32 1
store i32 %7, i32* %8
%9 = bitcast %struct.ifreq* %1 to %struct.ethtool_value**
store %struct.ethtool_value* %3, %struct.ethtool_value** %9
%10 = load i32, i32* @SIOCETHTOOL
%11 = call i64 @ioctl(i32 %0, i32 %10, %struct.ifreq* %1)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = getelementptr inbounds %struct.ethtool_value, %struct.ethtool_value* %3, i64 0, i32 0
%15 = load i64, i64* %14
%16 = icmp eq i64 %15, 0
%17 = select i1 %16, i32 2, i32 1
br label %24
18: ; preds = %2
%19 = load i64, i64* @errno
%20 = load i64, i64* @EOPNOTSUPP
%21 = icmp eq i64 %19, %20
br i1 %21, label %24, label %22
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %18, %22, %13
%25 = phi i32 [ %17, %13 ], [ 3, %22 ], [ 0, %18 ]
store i8* %6, i8** %5
ret i32 %25
}