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

/* Variables and functions */
int /*<<< orphan*/ SIOCGIFMTU;
int /*<<< orphan*/ errno;
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ pcap_snprintf(char *, int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ pcap_strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int iface_get_mtu(int fd, const char *device, char *ebuf) {
  struct ifreq ifr;
  if (!device)
    return (64 * 1024);
  memset(&ifr, 0, sizeof(ifr));
  strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
  if (ioctl(fd, SIOCGIFMTU, &ifr) == -1) {
    pcap_snprintf(ebuf, 256, "ioctl: %s", pcap_strerror(errno));
    return -1;
  }
  return ifr.ifr_mtu;
}


x86 assembly (clang, -O0):
<iface_get_mtu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    117a <iface_get_mtu+0x2a>
movl   $0x10000,-0x4(%rbp)
jmp    11f8 <iface_get_mtu+0xa8>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%edi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    11f2 <iface_get_mtu+0xa2>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   1060 <pcap_strerror@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%ecx
mov    $0x100,%esi
lea    0xe1f(%rip),%rdx
call   1070 <pcap_snprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f8 <iface_get_mtu+0xa8>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iface_get_mtu>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rsi,%rsi
je     11a4 <iface_get_mtu+0x54>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x4(%rsp),%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r15,%rdx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11ab <iface_get_mtu+0x5b>
mov    (%rsp),%eax
jmp    11d4 <iface_get_mtu+0x84>
mov    $0x10000,%eax
jmp    11d4 <iface_get_mtu+0x84>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
call   1060 <pcap_strerror@plt>
lea    0xe40(%rip),%rdx
mov    %r14,%rdi
mov    $0x100,%esi
mov    %eax,%ecx
call   1070 <pcap_snprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@SIOCGIFMTU = global i32 0
@.str = [10 x i8] c"ioctl: %s\00"
@errno = global i32 0
define i32 @iface_get_mtu(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.ifreq
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%9 = load i8*, i8** %6
%10 = icmp ne i8* %9, null
br i1 %10, label %12, label %11
11: ; preds = %3
store i32 65536, i32* %4
br label %30
12: ; preds = %3
%13 = call i32 @memset(%struct.ifreq* %8, i32 0, i32 8)
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 1
%15 = load i32, i32* %14
%16 = load i8*, i8** %6
%17 = call i32 @strncpy(i32 %15, i8* %16, i32 4)
%18 = load i32, i32* %5
%19 = load i32, i32* @SIOCGIFMTU
%20 = call i32 @ioctl(i32 %18, i32 %19, %struct.ifreq* %8)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %27
22: ; preds = %12
%23 = load i8*, i8** %7
%24 = load i32, i32* @errno
%25 = call i32 @pcap_strerror(i32 %24)
%26 = call i32 @pcap_snprintf(i8* %23, i32 256, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %25)
store i32 -1, i32* %4
br label %30
27: ; preds = %12
%28 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 0
%29 = load i32, i32* %28
store i32 %29, i32* %4
br label %30
30: ; preds = %27, %22, %11
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@SIOCGIFMTU = global i32 0
@.str = [10 x i8] c"ioctl: %s\00"
@errno = global i32 0
define i32 @iface_get_mtu(i32 %0, i8* %1, i8* %2) {
%4 = alloca %struct.ifreq
%5 = bitcast %struct.ifreq* %4 to i8*
%6 = icmp eq i8* %1, null
br i1 %6, label %22, label %7
7: ; preds = %3
%8 = call i32 @memset(%struct.ifreq* %4, i32 0, i32 8)
%9 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 1
%10 = load i32, i32* %9
%11 = call i32 @strncpy(i32 %10, i8* %1, i32 4)
%12 = load i32, i32* @SIOCGIFMTU
%13 = call i32 @ioctl(i32 %0, i32 %12, %struct.ifreq* %4)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %7
%16 = load i32, i32* @errno
%17 = call i32 @pcap_strerror(i32 %16)
%18 = call i32 @pcap_snprintf(i8* %2, i32 256, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %17)
br label %22
19: ; preds = %7
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 0
%21 = load i32, i32* %20
br label %22
22: ; preds = %3, %19, %15
%23 = phi i32 [ -1, %15 ], [ %21, %19 ], [ 65536, %3 ]
ret i32 %23
}