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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ ETHTOOL_GLINK;
int /*<<< orphan*/ SIOCETHTOOL;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int get_netlink_status(const char *ifname) {
  int fd;
  struct ifreq ifr;
  struct ethtool_value data;
  data.cmd = ETHTOOL_GLINK;
  data.data = 0;
  memset(&ifr, 0, sizeof(struct ifreq));
  strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
  ifr.ifr_data = (char *)&data;
  if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == 0) {
    perror("open socket failed");
    return -1;
  }
  if (ioctl(fd, SIOCETHTOOL, &ifr) == -1) {
    perror("ioctl failed");
    close(fd);
    return -1;
  }
  close(fd);
  return data.data;
}


x86 assembly (clang, -O0):
<get_netlink_status>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
lea    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jne    11e5 <get_netlink_status+0x85>
lea    0xe2c(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1231 <get_netlink_status+0xd1>
mov    -0x14(%rbp),%edi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1223 <get_netlink_status+0xc3>
lea    0xe09(%rip),%rdi
call   1070 <perror@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1231 <get_netlink_status+0xd1>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_netlink_status>:
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
movl   $0x0,0x8(%rsp)
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x18(%rsp),%edi
mov    %rbx,%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
lea    0x8(%rsp),%rax
mov    %rax,0x10(%rsp)
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
je     11f3 <get_netlink_status+0x93>
mov    %eax,%ebx
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1206 <get_netlink_status+0xa6>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x8(%rsp),%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe06(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
jmp    11ed <get_netlink_status+0x8d>
lea    0xe06(%rip),%rdi
call   1070 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%eax
jmp    11ed <get_netlink_status+0x8d>

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

LLVM IR (-O2):
%struct.ifreq = type { i8*, i32 }
%struct.ethtool_value = type { i32, i32 }
@ETHTOOL_GLINK = global i32 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [19 x i8] c"open socket failed\00"
@SIOCETHTOOL = global i32 0
@.str.1 = [13 x i8] c"ioctl failed\00"
define i32 @get_netlink_status(i8* %0) {
%2 = alloca %struct.ifreq
%3 = alloca %struct.ethtool_value
%4 = bitcast %struct.ifreq* %2 to i8*
%5 = bitcast %struct.ethtool_value* %3 to i8*
%6 = load i32, i32* @ETHTOOL_GLINK
%7 = getelementptr inbounds %struct.ethtool_value, %struct.ethtool_value* %3, i64 0, i32 1
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.ethtool_value, %struct.ethtool_value* %3, i64 0, i32 0
store i32 0, i32* %8
%9 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 16)
%10 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%11 = load i32, i32* %10
%12 = call i32 @strncpy(i32 %11, i8* %0, i32 3)
%13 = bitcast %struct.ifreq* %2 to %struct.ethtool_value**
store %struct.ethtool_value* %3, %struct.ethtool_value** %13
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_DGRAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %1
%19 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %30
20: ; preds = %1
%21 = load i32, i32* @SIOCETHTOOL
%22 = call i32 @ioctl(i32 %16, i32 %21, %struct.ifreq* %2)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%26 = call i32 @close(i32 %16)
br label %30
27: ; preds = %20
%28 = call i32 @close(i32 %16)
%29 = load i32, i32* %8
br label %30
30: ; preds = %27, %24, %18
%31 = phi i32 [ -1, %18 ], [ -1, %24 ], [ %29, %27 ]
ret i32 %31
}