#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 {
  int /*<<< orphan*/ ifr_name;
  int /*<<< orphan*/ ifr_data;
};
struct ethtool_cmd {
  scalar_t__ speed;
  int /*<<< orphan*/ cmd;
};
typedef int /*<<< orphan*/ caddr_t;

/* Variables and functions */
int /*<<< orphan*/ ETHTOOL_GSET;
int /*<<< orphan*/ SIOCETHTOOL;
scalar_t__ USHRT_MAX;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, char const *, size_t);
size_t strlen(char const *);
int get_ethernet_speed(int sockfd, const char *iface) {
  struct ethtool_cmd ecmd = {.cmd = ETHTOOL_GSET};
  struct ifreq ifr = {.ifr_data = (caddr_t)&ecmd};
  size_t niface = strlen(iface);
  if (niface + 1 > sizeof(ifr.ifr_name))
    goto fail;
  memcpy(ifr.ifr_name, iface, niface + 1);
  if (ioctl(sockfd, SIOCETHTOOL, &ifr) < 0)
    goto fail;
  if (ecmd.speed == USHRT_MAX)
    goto fail;
  return ecmd.speed;
fail:
  return 0;
}


x86 assembly (clang, -O0):
<get_ethernet_speed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e9a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x28(%rbp)
lea    -0x20(%rbp),%rax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
cmp    $0x4,%rax
jbe    1185 <get_ethernet_speed+0x55>
jmp    11e2 <get_ethernet_speed+0xb2>
mov    -0x28(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
add    $0x1,%rdx
call   1050 <memcpy@plt>
mov    -0x8(%rbp),%edi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11bd <get_ethernet_speed+0x8d>
jmp    11e2 <get_ethernet_speed+0xb2>
mov    -0x20(%rbp),%rax
mov    0x2e18(%rip),%rcx
cmp    (%rcx),%rax
jne    11d6 <get_ethernet_speed+0xa6>
jmp    11e2 <get_ethernet_speed+0xb2>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    11e9 <get_ethernet_speed+0xb9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ethernet_speed>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbp
mov    %edi,%r14d
movq   $0x0,0x10(%rsp)
mov    0x2e9a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
movl   $0x0,0x8(%rsp)
lea    0x10(%rsp),%rax
mov    %eax,0xc(%rsp)
mov    %rsi,%rdi
call   1030 <strlen@plt>
add    $0x1,%rax
xor    %ebx,%ebx
cmp    $0x4,%rax
ja     11b7 <get_ethernet_speed+0x87>
xor    %ebx,%ebx
xor    %edi,%edi
mov    %rbp,%rsi
mov    %rax,%rdx
call   1050 <memcpy@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %r14d,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     11b7 <get_ethernet_speed+0x87>
mov    0x10(%rsp),%rbx
mov    0x2e31(%rip),%rax
xor    %ecx,%ecx
cmp    (%rax),%rbx
cmove  %ecx,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ethtool_cmd = type { i64, i32 }
%struct.ifreq = type { i32, i32 }
@ETHTOOL_GSET = global i32 0
@SIOCETHTOOL = global i32 0
@USHRT_MAX = global i64 0
define i32 @get_ethernet_speed(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.ethtool_cmd
%7 = alloca %struct.ifreq
%8 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
%9 = getelementptr inbounds %struct.ethtool_cmd, %struct.ethtool_cmd* %6, i32 0, i32 0
store i64 0, i64* %9
%10 = getelementptr inbounds %struct.ethtool_cmd, %struct.ethtool_cmd* %6, i32 0, i32 1
%11 = load i32, i32* @ETHTOOL_GSET
store i32 %11, i32* %10
%12 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
store i32 0, i32* %12
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 1
%14 = ptrtoint %struct.ethtool_cmd* %6 to i32
store i32 %14, i32* %13
%15 = load i8*, i8** %5
%16 = call i64 @strlen(i8* %15)
store i64 %16, i64* %8
%17 = load i64, i64* %8
%18 = add i64 %17, 1
%19 = icmp ugt i64 %18, 4
br i1 %19, label %20, label %21
20: ; preds = %2
br label %43
21: ; preds = %2
%22 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%23 = load i32, i32* %22
%24 = load i8*, i8** %5
%25 = load i64, i64* %8
%26 = add i64 %25, 1
%27 = call i32 @memcpy(i32 %23, i8* %24, i64 %26)
%28 = load i32, i32* %4
%29 = load i32, i32* @SIOCETHTOOL
%30 = call i64 @ioctl(i32 %28, i32 %29, %struct.ifreq* %7)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %21
br label %43
33: ; preds = %21
%34 = getelementptr inbounds %struct.ethtool_cmd, %struct.ethtool_cmd* %6, i32 0, i32 0
%35 = load i64, i64* %34
%36 = load i64, i64* @USHRT_MAX
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %39
38: ; preds = %33
br label %43
39: ; preds = %33
%40 = getelementptr inbounds %struct.ethtool_cmd, %struct.ethtool_cmd* %6, i32 0, i32 0
%41 = load i64, i64* %40
%42 = trunc i64 %41 to i32
store i32 %42, i32* %3
br label %44
43: ; preds = %38, %32, %20
store i32 0, i32* %3
br label %44
44: ; preds = %43, %39
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
%struct.ethtool_cmd = type { i64, i32 }
%struct.ifreq = type { i32, i32 }
@ETHTOOL_GSET = global i32 0
@SIOCETHTOOL = global i32 0
@USHRT_MAX = global i64 0
define i32 @get_ethernet_speed(i32 %0, i8* %1) {
%3 = alloca %struct.ethtool_cmd
%4 = alloca %struct.ifreq
%5 = bitcast %struct.ethtool_cmd* %3 to i8*
%6 = getelementptr inbounds %struct.ethtool_cmd, %struct.ethtool_cmd* %3, i64 0, i32 0
store i64 0, i64* %6
%7 = getelementptr inbounds %struct.ethtool_cmd, %struct.ethtool_cmd* %3, i64 0, i32 1
%8 = load i32, i32* @ETHTOOL_GSET
store i32 %8, i32* %7
%9 = bitcast %struct.ifreq* %4 to i8*
%10 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 0
store i32 0, i32* %10
%11 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 1
%12 = ptrtoint %struct.ethtool_cmd* %3 to i64
%13 = trunc i64 %12 to i32
store i32 %13, i32* %11
%14 = call i64 @strlen(i8* dereferenceable(1) %1)
%15 = add i64 %14, 1
%16 = icmp ugt i64 %15, 4
br i1 %16, label %28, label %17
17: ; preds = %2
%18 = call i32 @memcpy(i32 0, i8* %1, i64 %15)
%19 = load i32, i32* @SIOCETHTOOL
%20 = call i64 @ioctl(i32 %0, i32 %19, %struct.ifreq* %4)
%21 = icmp slt i64 %20, 0
br i1 %21, label %28, label %22
22: ; preds = %17
%23 = load i64, i64* %6
%24 = load i64, i64* @USHRT_MAX
%25 = icmp eq i64 %23, %24
%26 = trunc i64 %23 to i32
%27 = select i1 %25, i32 0, i32 %26
br label %28
28: ; preds = %22, %2, %17
%29 = phi i32 [ 0, %17 ], [ 0, %2 ], [ %27, %22 ]
ret i32 %29
}