#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*/ EXIT_FAILURE;
int /*<<< orphan*/ SIOCGIFMTU;
int /*<<< orphan*/ SOCK_DGRAM;
int close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int get_mtu(const char *if_name) {
  struct ifreq ifr;
  int s, ret;
  s = socket(AF_INET, SOCK_DGRAM, 0);
  if (s < 0) {
    perror("socket");
    exit(EXIT_FAILURE);
  }
  memset(&ifr, 0, sizeof(struct ifreq));
  ifr.ifr_addr.sa_family = AF_INET;
  strcpy(ifr.ifr_name, if_name);
  ret = ioctl(s, SIOCGIFMTU, &ifr);
  if (ret < 0) {
    perror("ioctl");
    exit(EXIT_FAILURE);
  }
  ret = close(s);
  if (ret < 0) {
    perror("close");
    exit(EXIT_FAILURE);
  }
  return ifr.ifr_mtu;
}


x86 assembly (clang, -O0):
<get_mtu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11bc <get_mtu+0x4c>
lea    0xe57(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1220 <get_mtu+0xb0>
lea    0xdfa(%rip),%rdi
call   1070 <perror@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    124f <get_mtu+0xdf>
lea    0xdd1(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_mtu>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e63(%rip),%r12
mov    (%r12),%edi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     11f9 <get_mtu+0x89>
mov    %eax,%ebx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x10(%rsp)
mov    0xc(%rsp),%edi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r15,%rdx
call   1050 <ioctl@plt>
test   %eax,%eax
js     1202 <get_mtu+0x92>
mov    %ebx,%edi
call   1060 <close@plt>
test   %eax,%eax
js     120b <get_mtu+0x9b>
mov    0x8(%rsp),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe00(%rip),%rdi
jmp    1212 <get_mtu+0xa2>
lea    0xdfe(%rip),%rdi
jmp    1212 <get_mtu+0xa2>
lea    0xdfb(%rip),%rdi
call   1070 <perror@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

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
@.str = [7 x i8] c"socket\00"
@EXIT_FAILURE = global i32 0
@SIOCGIFMTU = global i32 0
@.str.1 = [6 x i8] c"ioctl\00"
@.str.2 = [6 x i8] c"close\00"
define i32 @get_mtu(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.ifreq
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
%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 %15
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %1
%16 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i32 0, i32 2
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
store i32 %17, i32* %19
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i32 0, i32 1
%21 = load i32, i32* %20
%22 = load i8*, i8** %2
%23 = call i32 @strcpy(i32 %21, i8* %22)
%24 = load i32, i32* %4
%25 = load i32, i32* @SIOCGIFMTU
%26 = call i32 @ioctl(i32 %24, i32 %25, %struct.ifreq* %3)
store i32 %26, i32* %5
%27 = load i32, i32* %5
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %15
%30 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %15
%34 = load i32, i32* %4
%35 = call i32 @close(i32 %34)
store i32 %35, i32* %5
%36 = load i32, i32* %5
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %33
%39 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %33
%43 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i32 0, i32 0
%44 = load i32, i32* %43
ret i32 %44
}

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
@.str = [7 x i8] c"socket\00"
@EXIT_FAILURE = global i32 0
@SIOCGIFMTU = global i32 0
@.str.1 = [6 x i8] c"ioctl\00"
@.str.2 = [6 x i8] c"close\00"
define i32 @get_mtu(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_DGRAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %1
%13 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 2, i32 0
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%17 = load i32, i32* %16
%18 = call i32 @strcpy(i32 %17, i8* %0)
%19 = load i32, i32* @SIOCGIFMTU
%20 = call i32 @ioctl(i32 %6, i32 %19, %struct.ifreq* %2)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %12
%23 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %12
%27 = call i32 @close(i32 %6)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %26
%34 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%35 = load i32, i32* %34
ret i32 %35
}