#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 {
  char *ifr_name;
  int ifr_mtu;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFMTU;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int getifmtu(const char *ifname) {
  int fd;
  struct ifreq if_mtu;
  fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd < 0) {
    return -1;
  }
  strncpy(if_mtu.ifr_name, ifname, IFNAMSIZ);
  if_mtu.ifr_name[IFNAMSIZ - 1] = '\0';
  if (ioctl(fd, SIOCGIFMTU, &if_mtu) < 0) {
    return -1;
  }
  return if_mtu.ifr_mtu;
}


x86 assembly (clang, -O0):
<getifmtu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2ea5(%rip),%rax
mov    (%rax),%edi
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    116e <getifmtu+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <getifmtu+0x9c>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e53(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
mov    0x2e41(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%edi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11c6 <getifmtu+0x96>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <getifmtu+0x9c>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getifmtu>:
push   %rbx
mov    %rdi,%rbx
mov    0x2ebd(%rip),%rax
mov    (%rax),%edi
mov    0x2e9c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <socket@plt>
test   %eax,%eax
jns    1148 <getifmtu+0x28>
mov    $0xffffffff,%eax
pop    %rbx
ret
mov    0x2e89(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rsi
call   1030 <strncpy@plt>

LLVM IR (-O0):
%struct.ifreq = type { i8*, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFMTU = global i32 0
define i32 @getifmtu(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_STREAM
%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
store i32 -1, i32* %2
br label %32
12: ; preds = %1
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 0
%14 = load i8*, i8** %13
%15 = load i8*, i8** %3
%16 = load i32, i32* @IFNAMSIZ
%17 = call i32 @strncpy(i8* %14, i8* %15, i32 %16)
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 0
%19 = load i8*, i8** %18
%20 = load i32, i32* @IFNAMSIZ
%21 = sub nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %19, i64 %22
store i8 0, i8* %23
%24 = load i32, i32* %4
%25 = load i32, i32* @SIOCGIFMTU
%26 = call i64 @ioctl(i32 %24, i32 %25, %struct.ifreq* %5)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %12
store i32 -1, i32* %2
br label %32
29: ; preds = %12
%30 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 1
%31 = load i32, i32* %30
store i32 %31, i32* %2
br label %32
32: ; preds = %29, %28, %11
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFMTU = global i32 0
define i32 @getifmtu(i8* %0) {
%2 = load i32, i32* @AF_INET
%3 = load i32, i32* @SOCK_STREAM
%4 = tail call i32 @socket(i32 %2, i32 %3, i32 0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = load i32, i32* @IFNAMSIZ
%8 = tail call i32 @strncpy(i8* undef, i8* %0, i32 %7)
unreachable
9: ; preds = %1
ret i32 -1
}