#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ sa_family;
};
struct ifreq {
  int ifr_mtu;
  int /*<<< orphan*/ ifr_name;
  TYPE_1__ ifr_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIFMTU;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int sysapi_get_mtu(char *ifname) {
  int ret;
  int fd;
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd < 0)
    return -1;
  struct ifreq ifr;
  ifr.ifr_addr.sa_family = AF_INET;
  strcpy(ifr.ifr_name, ifname);
  ret = ioctl(fd, SIOCGIFMTU, &ifr);
  if (ret < 0) {
    close(fd);
    return -1;
  }
  close(fd);
  ret = ifr.ifr_mtu;
  return ret;
}


x86 assembly (clang, -O0):
<sysapi_get_mtu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    117e <sysapi_get_mtu+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e0 <sysapi_get_mtu+0xa0>
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%edi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11cc <sysapi_get_mtu+0x8c>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e0 <sysapi_get_mtu+0xa0>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysapi_get_mtu>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e96(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
test   %eax,%eax
js     11a4 <sysapi_get_mtu+0x64>
mov    %eax,%ebx
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%eax
test   %ebp,%ebp
js     11a9 <sysapi_get_mtu+0x69>
mov    (%rsp),%eax
jmp    11a9 <sysapi_get_mtu+0x69>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFMTU = global i32 0
define i32 @sysapi_get_mtu(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.ifreq
store i8* %0, i8** %3
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 -1, i32* %2
br label %35
13: ; preds = %1
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 2
%16 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %15, i32 0, i32 0
store i32 %14, i32* %16
%17 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%18 = load i32, i32* %17
%19 = load i8*, i8** %3
%20 = call i32 @strcpy(i32 %18, i8* %19)
%21 = load i32, i32* %5
%22 = load i32, i32* @SIOCGIFMTU
%23 = call i32 @ioctl(i32 %21, i32 %22, %struct.ifreq* %6)
store i32 %23, i32* %4
%24 = load i32, i32* %4
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %13
%27 = load i32, i32* %5
%28 = call i32 @close(i32 %27)
store i32 -1, i32* %2
br label %35
29: ; preds = %13
%30 = load i32, i32* %5
%31 = call i32 @close(i32 %30)
%32 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%33 = load i32, i32* %32
store i32 %33, i32* %4
%34 = load i32, i32* %4
store i32 %34, i32* %2
br label %35
35: ; preds = %29, %26, %12
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFMTU = global i32 0
define i32 @sysapi_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 = icmp slt i32 %5, 0
br i1 %6, label %19, label %7
7: ; preds = %1
%8 = bitcast %struct.ifreq* %2 to i8*
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 2, i32 0
store i32 %9, i32* %10
%11 = tail call i32 @strcpy(i32 undef, i8* %0)
%12 = load i32, i32* @SIOCGIFMTU
%13 = call i32 @ioctl(i32 %5, i32 %12, %struct.ifreq* %2)
%14 = call i32 @close(i32 %5)
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%16 = load i32, i32* %15
%17 = icmp sgt i32 %13, -1
%18 = select i1 %17, i32 %16, i32 -1
br label %19
19: ; preds = %1, %7
%20 = phi i32 [ %18, %7 ], [ -1, %1 ]
ret i32 %20
}