#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIFMTU;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int if_get_mtu(const char *name) {
  int s = socket(AF_INET, SOCK_DGRAM, 0);
  struct ifreq ifr;
  if (s < 0)
    goto err;
  strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
  if (ioctl(s, SIOCGIFMTU, &ifr))
    goto err2;
  close(s);
  return ifr.ifr_mtu;
err2:
  close(s);
err:
  return -1;
}


x86 assembly (clang, -O0):
<if_get_mtu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e95(%rip),%rax
mov    (%rax),%edi
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1177 <if_get_mtu+0x37>
jmp    11c7 <if_get_mtu+0x87>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     11ac <if_get_mtu+0x6c>
jmp    11bf <if_get_mtu+0x7f>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ce <if_get_mtu+0x8e>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<if_get_mtu>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e97(%rip),%rax
mov    (%rax),%edi
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
js     11a2 <if_get_mtu+0x62>
mov    %eax,%ebp
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1050 <close@plt>
test   %rbx,%rbx
jne    11a2 <if_get_mtu+0x62>
mov    (%rsp),%r15d
mov    %r15d,%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
@SOCK_DGRAM = global i32 0
@SIOCGIFMTU = global i32 0
define i32 @if_get_mtu(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.ifreq
store i8* %0, i8** %3
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
br label %30
12: ; preds = %1
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 1
%14 = load i32, i32* %13
%15 = load i8*, i8** %3
%16 = call i32 @strncpy(i32 %14, i8* %15, i32 4)
%17 = load i32, i32* %4
%18 = load i32, i32* @SIOCGIFMTU
%19 = call i64 @ioctl(i32 %17, i32 %18, %struct.ifreq* %5)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %12
br label %27
22: ; preds = %12
%23 = load i32, i32* %4
%24 = call i32 @close(i32 %23)
%25 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 0
%26 = load i32, i32* %25
store i32 %26, i32* %2
br label %31
27: ; preds = %21
%28 = load i32, i32* %4
%29 = call i32 @close(i32 %28)
br label %30
30: ; preds = %27, %11
store i32 -1, i32* %2
br label %31
31: ; preds = %30, %22
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFMTU = global i32 0
define i32 @if_get_mtu(i8* %0) {
%2 = alloca %struct.ifreq
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_DGRAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = bitcast %struct.ifreq* %2 to i8*
%7 = icmp slt i32 %5, 0
br i1 %7, label %17, label %8
8: ; preds = %1
%9 = tail call i32 @strncpy(i32 undef, i8* %0, i32 4)
%10 = load i32, i32* @SIOCGIFMTU
%11 = call i64 @ioctl(i32 %5, i32 %10, %struct.ifreq* %2)
%12 = icmp eq i64 %11, 0
%13 = call i32 @close(i32 %5)
br i1 %12, label %14, label %17
14: ; preds = %8
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%16 = load i32, i32* %15
br label %17
17: ; preds = %8, %1, %14
%18 = phi i32 [ %16, %14 ], [ -1, %1 ], [ -1, %8 ]
ret i32 %18
}