#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*/ IFNAMSIZ;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCSIFMTU;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ safe_strncpy(int /*<<< orphan*/, char const *,
                                int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tap_set_mtu(int fd, const char *dev, int mtu) {
  struct ifreq ifr;
  fd = socket(PF_INET, SOCK_DGRAM, 0);
  safe_strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  ifr.ifr_mtu = mtu;
  if ((ioctl(fd, SIOCSIFMTU, &ifr)) < 0) {
    perror("SIOCSIFMTU");
    return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<tap_set_mtu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x8(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e4c(%rip),%rax
mov    (%rax),%edx
call   1050 <safe_strncpy@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    11d0 <tap_set_mtu+0x80>
lea    0xe41(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11df <tap_set_mtu+0x8f>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tap_set_mtu>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%rbx
mov    0x2e6c(%rip),%rax
mov    (%rax),%edi
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
xor    %r14d,%r14d
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ebp
mov    0x2e56(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rsi
call   1050 <safe_strncpy@plt>
mov    %r15d,(%rsp)
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
js     11bd <tap_set_mtu+0x6d>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe3c(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%r14d
jmp    11af <tap_set_mtu+0x5f>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IFNAMSIZ = global i32 0
@SIOCSIFMTU = global i32 0
@.str = [11 x i8] c"SIOCSIFMTU\00"
define i32 @tap_set_mtu(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.ifreq
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i32, i32* @PF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %5
%12 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 1
%13 = load i32, i32* %12
%14 = load i8*, i8** %6
%15 = load i32, i32* @IFNAMSIZ
%16 = call i32 @safe_strncpy(i32 %13, i8* %14, i32 %15)
%17 = load i32, i32* %7
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* %5
%20 = load i32, i32* @SIOCSIFMTU
%21 = call i64 @ioctl(i32 %19, i32 %20, %struct.ifreq* %8)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %3
%24 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %28
25: ; preds = %3
%26 = load i32, i32* %5
%27 = call i32 @close(i32 %26)
store i32 0, i32* %4
br label %28
28: ; preds = %25, %23
%29 = load i32, i32* %4
ret i32 %29
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IFNAMSIZ = global i32 0
@SIOCSIFMTU = global i32 0
@.str = [11 x i8] c"SIOCSIFMTU\00"
define i32 @tap_set_mtu(i32 %0, i8* %1, i32 %2) {
%4 = alloca %struct.ifreq
%5 = bitcast %struct.ifreq* %4 to i8*
%6 = load i32, i32* @PF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = load i32, i32* @IFNAMSIZ
%10 = tail call i32 @safe_strncpy(i32 undef, i8* %1, i32 %9)
%11 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 0
store i32 %2, i32* %11
%12 = load i32, i32* @SIOCSIFMTU
%13 = call i64 @ioctl(i32 %8, i32 %12, %struct.ifreq* %4)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %19
17: ; preds = %3
%18 = call i32 @close(i32 %8)
br label %19
19: ; preds = %17, %15
%20 = phi i32 [ -1, %15 ], [ 0, %17 ]
ret i32 %20
}