#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*/ AF_INET;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFMTU;
int af_socket(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strlcpy(int /*<<< orphan*/, char const *,
                           int /*<<< orphan*/);
int device_mtu(const char *ifname) {
  int ret, sock, mtu;
  struct ifreq ifr;
  sock = af_socket(AF_INET);
  memset(&ifr, 0, sizeof(ifr));
  strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
  ret = ioctl(sock, SIOCGIFMTU, &ifr);
  if (!ret)
    mtu = ifr.ifr_mtu;
  else
    mtu = 0;
  close(sock);
  return mtu;
}


x86 assembly (clang, -O0):
<device_mtu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
call   1030 <af_socket@plt>
mov    %eax,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x1c(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    0x2e45(%rip),%rax
mov    (%rax),%edx
call   1070 <strlcpy@plt>
mov    -0x10(%rbp),%edi
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    11bf <device_mtu+0x6f>
mov    -0x20(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    11c6 <device_mtu+0x76>
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<device_mtu>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbp
mov    0x2e87(%rip),%rax
mov    (%rax),%edi
call   1030 <af_socket@plt>
mov    %eax,%ebx
mov    %rsp,%r15
xor    %r14d,%r14d
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x4(%rsp),%edi
mov    0x2e46(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rsi
call   1070 <strlcpy@plt>
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r15,%rdx
call   1050 <ioctl@plt>
test   %eax,%eax
jne    11af <device_mtu+0x5f>
mov    (%rsp),%r14d
mov    %ebx,%edi
call   1060 <close@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFMTU = global i32 0
define i32 @device_mtu(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.ifreq
store i8* %0, i8** %2
%7 = load i32, i32* @AF_INET
%8 = call i32 @af_socket(i32 %7)
store i32 %8, i32* %4
%9 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 8)
%10 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%11 = load i32, i32* %10
%12 = load i8*, i8** %2
%13 = load i32, i32* @IFNAMSIZ
%14 = call i32 @strlcpy(i32 %11, i8* %12, i32 %13)
%15 = load i32, i32* %4
%16 = load i32, i32* @SIOCGIFMTU
%17 = call i32 @ioctl(i32 %15, i32 %16, %struct.ifreq* %6)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = icmp ne i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %1
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%22 = load i32, i32* %21
store i32 %22, i32* %5
br label %24
23: ; preds = %1
store i32 0, i32* %5
br label %24
24: ; preds = %23, %20
%25 = load i32, i32* %4
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* %5
ret i32 %27
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFMTU = global i32 0
define i32 @device_mtu(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = load i32, i32* @AF_INET
%5 = tail call i32 @af_socket(i32 %4)
%6 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 8)
%7 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%8 = load i32, i32* %7
%9 = load i32, i32* @IFNAMSIZ
%10 = call i32 @strlcpy(i32 %8, i8* %0, i32 %9)
%11 = load i32, i32* @SIOCGIFMTU
%12 = call i32 @ioctl(i32 %5, i32 %11, %struct.ifreq* %2)
%13 = icmp eq i32 %12, 0
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%15 = load i32, i32* %14
%16 = select i1 %13, i32 %15, i32 0
%17 = call i32 @close(i32 %5)
ret i32 %16
}