#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 /*<<< orphan*/ ifr_name;
};
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int num_stats(int, struct ifreq *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int stat_data(int, struct ifreq *, int, int);
int stat_index(int, struct ifreq *, int, char *);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int peer_ifindex(char *ifname) {
  int ifindex = -1;
  int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
  if (fd < 0)
    return -1;
  struct ifreq ifr;
  memset(&ifr, 0, sizeof(ifr));
  strcpy(ifr.ifr_name, "eth0");
  int n = num_stats(fd, &ifr);
  if (n < 0)
    goto error;
  int i = stat_index(fd, &ifr, n, "peer_ifindex");
  if (i < 0)
    goto error;
  ifindex = stat_data(fd, &ifr, n, i);
error:
  close(fd);
  return ifindex;
}


x86 assembly (clang, -O0):
<peer_ifindex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
mov    0x2e4c(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11bc <peer_ifindex+0x4c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1244 <peer_ifindex+0xd4>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    -0x20(%rbp),%edi
lea    0xe2a(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1050 <num_stats@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11f9 <peer_ifindex+0x89>
jmp    1236 <peer_ifindex+0xc6>
mov    -0x18(%rbp),%edi
mov    -0x24(%rbp),%edx
lea    -0x20(%rbp),%rsi
lea    0xdfb(%rip),%rcx
call   1030 <stat_index@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1221 <peer_ifindex+0xb1>
jmp    1236 <peer_ifindex+0xc6>
mov    -0x18(%rbp),%edi
mov    -0x24(%rbp),%edx
mov    -0x28(%rbp),%ecx
lea    -0x20(%rbp),%rsi
call   1080 <stat_data@plt>
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<peer_ifindex>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    0x2e57(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
test   %eax,%eax
js     120d <peer_ifindex+0x9d>
mov    %eax,%ebx
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    0x8(%rsp),%edi
lea    0xe43(%rip),%rsi
call   1040 <strcpy@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1050 <num_stats@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     1204 <peer_ifindex+0x94>
mov    %eax,%ebp
lea    0xe26(%rip),%rcx
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1030 <stat_index@plt>
test   %eax,%eax
js     1204 <peer_ifindex+0x94>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %eax,%ecx
call   1080 <stat_data@plt>
mov    %eax,%r14d
mov    %ebx,%edi
call   1070 <close@plt>
jmp    1213 <peer_ifindex+0xa3>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_IP = global i32 0
@.str = [5 x i8] c"eth0\00"
@.str.1 = [13 x i8] c"peer_ifindex\00"
define i32 @peer_ifindex(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.ifreq
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i32 -1, i32* %4
%9 = load i32, i32* @PF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = load i32, i32* @IPPROTO_IP
%12 = call i32 @socket(i32 %9, i32 %10, i32 %11)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %1
store i32 -1, i32* %2
br label %42
16: ; preds = %1
%17 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 4)
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @strcpy(i32 %19, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* %5
%22 = call i32 @num_stats(i32 %21, %struct.ifreq* %6)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %16
br label %38
26: ; preds = %16
%27 = load i32, i32* %5
%28 = load i32, i32* %7
%29 = call i32 @stat_index(i32 %27, %struct.ifreq* %6, i32 %28, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %26
br label %38
33: ; preds = %26
%34 = load i32, i32* %5
%35 = load i32, i32* %7
%36 = load i32, i32* %8
%37 = call i32 @stat_data(i32 %34, %struct.ifreq* %6, i32 %35, i32 %36)
store i32 %37, i32* %4
br label %38
38: ; preds = %33, %32, %25
%39 = load i32, i32* %5
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* %4
store i32 %41, i32* %2
br label %42
42: ; preds = %38, %15
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
%struct.ifreq = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_IP = global i32 0
@.str = [5 x i8] c"eth0\00"
@.str.1 = [13 x i8] c"peer_ifindex\00"
define i32 @peer_ifindex(i8* nocapture readnone %0) {
%2 = alloca %struct.ifreq
%3 = load i32, i32* @PF_INET
%4 = load i32, i32* @SOCK_DGRAM
%5 = load i32, i32* @IPPROTO_IP
%6 = tail call i32 @socket(i32 %3, i32 %4, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %24, label %8
8: ; preds = %1
%9 = bitcast %struct.ifreq* %2 to i8*
%10 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 4)
%11 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%12 = load i32, i32* %11
%13 = call i32 @strcpy(i32 %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @num_stats(i32 %6, %struct.ifreq* %2)
%15 = icmp slt i32 %14, 0
br i1 %15, label %21, label %16
16: ; preds = %8
%17 = call i32 @stat_index(i32 %6, %struct.ifreq* %2, i32 %14, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp slt i32 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = call i32 @stat_data(i32 %6, %struct.ifreq* %2, i32 %14, i32 %17)
br label %21
21: ; preds = %16, %8, %19
%22 = phi i32 [ -1, %8 ], [ -1, %16 ], [ %20, %19 ]
%23 = call i32 @close(i32 %6)
br label %24
24: ; preds = %1, %21
%25 = phi i32 [ %22, %21 ], [ -1, %1 ]
ret i32 %25
}