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

/* Variables and functions */
int /*<<< orphan*/ PF_PACKET;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ exit(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int getPhysicalIfindexbyName(char *ifname) {
  int ifindex = 0;
  int s;
  s = socket(PF_PACKET, SOCK_DGRAM, 0);
  if (s < 0) {
    printf("getPhysicalIfindexbyName: socket error");
    exit(2);
  }
  {
    struct ifreq ifr;
    memset(&ifr, 0, sizeof(ifr));
    strncpy(ifr.ifr_name, ifname, 16 - 1);
    if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
      printf("Interface %s not found", ifname);
      exit(2);
    }
    ifindex = ifr.ifr_ifindex;
  }
  return ifindex;
}


x86 assembly (clang, -O0):
<getPhysicalIfindexbyName>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11b1 <getPhysicalIfindexbyName+0x51>
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    $0xf,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    120d <getPhysicalIfindexbyName+0xad>
mov    -0x8(%rbp),%rsi
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getPhysicalIfindexbyName>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e7e(%rip),%rax
mov    (%rax),%edi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11d4 <getPhysicalIfindexbyName+0x74>
mov    %eax,%ebp
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0xc(%rsp),%edi
mov    %r14,%rsi
mov    $0xf,%edx
call   1030 <strncpy@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1060 <ioctl@plt>
test   %rax,%rax
js     11ec <getPhysicalIfindexbyName+0x8c>
mov    0x8(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe25(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
lea    0xe34(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x2,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@PF_PACKET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [39 x i8] c"getPhysicalIfindexbyName: socket error\00"
@SIOCGIFINDEX = global i32 0
@.str.1 = [23 x i8] c"Interface %s not found\00"
define i32 @getPhysicalIfindexbyName(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.ifreq
store i8* %0, i8** %2
store i32 0, i32* %3
%6 = load i32, i32* @PF_PACKET
%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 %14
11: ; preds = %1
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 2)
unreachable
14: ; preds = %1
%15 = call i32 @memset(%struct.ifreq* %5, i32 0, i32 8)
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 1
%17 = load i32, i32* %16
%18 = load i8*, i8** %2
%19 = call i32 @strncpy(i32 %17, i8* %18, i32 15)
%20 = load i32, i32* %4
%21 = load i32, i32* @SIOCGIFINDEX
%22 = call i64 @ioctl(i32 %20, i32 %21, %struct.ifreq* %5)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %14
%25 = load i8*, i8** %2
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = call i32 @exit(i32 2)
unreachable
28: ; preds = %14
%29 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 0
%30 = load i32, i32* %29
store i32 %30, i32* %3
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@PF_PACKET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [39 x i8] c"getPhysicalIfindexbyName: socket error\00"
@SIOCGIFINDEX = global i32 0
@.str.1 = [23 x i8] c"Interface %s not found\00"
define i32 @getPhysicalIfindexbyName(i8* %0) {
%2 = alloca %struct.ifreq
%3 = load i32, i32* @PF_PACKET
%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 %7, label %10
7: ; preds = %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 2)
unreachable
10: ; preds = %1
%11 = bitcast %struct.ifreq* %2 to i8*
%12 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 8)
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%14 = load i32, i32* %13
%15 = call i32 @strncpy(i32 %14, i8* %0, i32 15)
%16 = load i32, i32* @SIOCGIFINDEX
%17 = call i64 @ioctl(i32 %5, i32 %16, %struct.ifreq* %2)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %10
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%21 = call i32 @exit(i32 2)
unreachable
22: ; preds = %10
%23 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%24 = load i32, i32* %23
ret i32 %24
}