#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;
  int /*<<< orphan*/ ifr_name;
};
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCETHTOOL;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int errno;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int ethtool_ioctl(const char *ifname, void *data) {
  struct ifreq ifr;
  int fd, err = 0;
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd < 0)
    return errno;
  memset(&ifr, 0, sizeof(ifr));
  strcpy(ifr.ifr_name, ifname);
  ifr.ifr_data = data;
  if (ioctl(fd, SIOCETHTOOL, &ifr) < 0)
    err = errno;
  close(fd);
  return err;
}


x86 assembly (clang, -O0):
<ethtool_ioctl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x30(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    119e <ethtool_ioctl+0x4e>
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11fb <ethtool_ioctl+0xab>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    11ed <ethtool_ioctl+0x9d>
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%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):
<ethtool_ioctl>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11d0 <ethtool_ioctl+0x80>
mov    %eax,%ebp
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x10(%rsp),%edi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    %r14,0x8(%rsp)
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1050 <ioctl@plt>
mov    %rax,%rbx
mov    0x2e0f(%rip),%rax
sar    $0x3f,%rbx
and    (%rax),%ebx
mov    %ebp,%edi
call   1060 <close@plt>
jmp    11d9 <ethtool_ioctl+0x89>
mov    0x2df9(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i8*, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@errno = global i32 0
@SIOCETHTOOL = global i32 0
define i32 @ethtool_ioctl(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.ifreq
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %8
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = load i32, i32* @errno
store i32 %15, i32* %3
br label %34
16: ; preds = %2
%17 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 16)
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%19 = load i32, i32* %18
%20 = load i8*, i8** %4
%21 = call i32 @strcpy(i32 %19, i8* %20)
%22 = load i8*, i8** %5
%23 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
store i8* %22, i8** %23
%24 = load i32, i32* %7
%25 = load i32, i32* @SIOCETHTOOL
%26 = call i64 @ioctl(i32 %24, i32 %25, %struct.ifreq* %6)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %16
%29 = load i32, i32* @errno
store i32 %29, i32* %8
br label %30
30: ; preds = %28, %16
%31 = load i32, i32* %7
%32 = call i32 @close(i32 %31)
%33 = load i32, i32* %8
store i32 %33, i32* %3
br label %34
34: ; preds = %30, %14
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
%struct.ifreq = type { i8*, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@errno = global i32 0
@SIOCETHTOOL = global i32 0
define i32 @ethtool_ioctl(i8* %0, i8* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = load i32, i32* @errno
br label %23
11: ; preds = %2
%12 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 16)
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%14 = load i32, i32* %13
%15 = call i32 @strcpy(i32 %14, i8* %0)
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
store i8* %1, i8** %16
%17 = load i32, i32* @SIOCETHTOOL
%18 = call i64 @ioctl(i32 %7, i32 %17, %struct.ifreq* %3)
%19 = icmp slt i64 %18, 0
%20 = load i32, i32* @errno
%21 = select i1 %19, i32 %20, i32 0
%22 = call i32 @close(i32 %7)
br label %23
23: ; preds = %11, %9
%24 = phi i32 [ %10, %9 ], [ %21, %11 ]
ret i32 %24
}