#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 */
typedef size_t ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int atoi(char *);
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
size_t read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char const *, char const *);
int relayd_get_interface_mtu(const char *ifname) {
  char buf[64];
  const char *sysctl_pattern = "/proc/sys/net/ipv6/conf/%s/mtu";
  snprintf(buf, sizeof(buf), sysctl_pattern, ifname);
  int fd = open(buf, O_RDONLY);
  ssize_t len = read(fd, buf, sizeof(buf) - 1);
  close(fd);
  if (len < 0)
    return -1;
  buf[len] = 0;
  return atoi(buf);
}


x86 assembly (clang, -O0):
<relayd_get_interface_mtu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe9d(%rip),%rax
mov    %rax,-0x58(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x40,%esi
call   1030 <snprintf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x5c(%rbp)
mov    -0x5c(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x3f,%edx
call   1050 <read@plt>
mov    %rax,-0x68(%rbp)
mov    -0x5c(%rbp),%edi
call   1040 <close@plt>
cmpq   $0x0,-0x68(%rbp)
jae    11c6 <relayd_get_interface_mtu+0x76>
movl   $0xffffffff,-0x4(%rbp)
jmp    11db <relayd_get_interface_mtu+0x8b>
mov    -0x68(%rbp),%rax
movb   $0x0,-0x50(%rbp,%rax,1)
lea    -0x50(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<relayd_get_interface_mtu>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %rdi,%rcx
lea    0xe9e(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x40,%esi
call   1030 <snprintf@plt>
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1060 <open@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x3f,%edx
call   1050 <read@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1040 <close@plt>
movb   $0x0,(%rsp,%rbx,1)
mov    %r14,%rdi
call   1070 <atoi@plt>
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [31 x i8] c"/proc/sys/net/ipv6/conf/%s/mtu\00"
@O_RDONLY = global i32 0
define i32 @relayd_get_interface_mtu(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [64 x i8]
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
store i8* %0, i8** %3
store i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8** %5
%8 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%9 = load i8*, i8** %5
%10 = load i8*, i8** %3
%11 = call i32 @snprintf(i8* %8, i32 64, i8* %9, i8* %10)
%12 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%17 = call i64 @read(i32 %15, i8* %16, i32 63)
store i64 %17, i64* %7
%18 = load i32, i32* %6
%19 = call i32 @close(i32 %18)
%20 = load i64, i64* %7
%21 = icmp ult i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %1
store i32 -1, i32* %2
br label %28
23: ; preds = %1
%24 = load i64, i64* %7
%25 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 %24
store i8 0, i8* %25
%26 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%27 = call i32 @atoi(i8* %26)
store i32 %27, i32* %2
br label %28
28: ; preds = %23, %22
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
@.str = [31 x i8] c"/proc/sys/net/ipv6/conf/%s/mtu\00"
@O_RDONLY = global i32 0
define i32 @relayd_get_interface_mtu(i8* %0) {
%2 = alloca [64 x i8]
%3 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 64, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* %3, i32 %5)
%7 = call i64 @read(i32 %6, i8* %3, i32 63)
%8 = call i32 @close(i32 %6)
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 %7
store i8 0, i8* %9
%10 = call i32 @atoi(i8* %3)
ret i32 %10
}