#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 scalar_t__ uint8_t;
struct ifaddrs {
  char *ifa_name;
  int /*<<< orphan*/ *ifa_addr;
  struct ifaddrs *ifa_next;
};
typedef int /*<<< orphan*/ mac_addr_one;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ freeifaddrs(struct ifaddrs *);
int /*<<< orphan*/ get_mac_by_ifname(char *, scalar_t__ *);
int getifaddrs(struct ifaddrs **);
scalar_t__ memcmp(scalar_t__ *, scalar_t__ *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int sscanf(char *, char *, unsigned int *, unsigned int *, unsigned int *,
           unsigned int *, unsigned int *, unsigned int *);
int main(int argc, char *argv[]) {
  struct ifaddrs *ifaddr, *ifa;
  unsigned int scanfbuf[6];
  uint8_t mac_addr_one[6];
  uint8_t mac_addr_two[6];
  int n;
  if (argc < 2) {
    printf("    usage: mac2ifname MAC_ADDRESS\n");
    return -1;
  }
  n = sscanf(argv[1], "%x:%x:%x:%x:%x:%x", &scanfbuf[0], &scanfbuf[1],
             &scanfbuf[2], &scanfbuf[3], &scanfbuf[4], &scanfbuf[5]);
  if (n != 6) {
    printf("    invalid MAC address %s\n", argv[1]);
    return -1;
  }
  for (n = 0; n < 6; n++) {
    mac_addr_one[n] = (uint8_t)scanfbuf[n];
  }
  if (getifaddrs(&ifaddr) == -1) {
    perror("getifaddrs");
    exit(EXIT_FAILURE);
  }
  for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
    if (ifa->ifa_addr == NULL)
      continue;
    get_mac_by_ifname(ifa->ifa_name, mac_addr_two);
    if (memcmp(mac_addr_one, mac_addr_two, sizeof(mac_addr_one)) == 0) {
      printf("%s\n", ifa->ifa_name);
      freeifaddrs(ifaddr);
      return 0;
    }
  }
  freeifaddrs(ifaddr);
  return -1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11bd <main+0x3d>
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136d <main+0x1ed>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x40(%rbp),%rdx
lea    -0x40(%rbp),%rcx
add    $0x4,%rcx
lea    -0x40(%rbp),%r8
add    $0x8,%r8
lea    -0x40(%rbp),%r9
add    $0xc,%r9
lea    -0x40(%rbp),%r10
add    $0x10,%r10
lea    -0x40(%rbp),%rax
add    $0x14,%rax
lea    0xe2b(%rip),%rsi
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
call   1060 <sscanf@plt>
mov    %eax,-0xa4(%rbp)
cmpl   $0x6,-0xa4(%rbp)
je     123b <main+0xbb>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136d <main+0x1ed>
movl   $0x0,-0xa4(%rbp)
cmpl   $0x6,-0xa4(%rbp)
jge    127f <main+0xff>
movslq -0xa4(%rbp),%rax
mov    -0x40(%rbp,%rax,4),%eax
mov    %eax,%ecx
movslq -0xa4(%rbp),%rax
mov    %rcx,-0x70(%rbp,%rax,8)
mov    -0xa4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa4(%rbp)
jmp    1245 <main+0xc5>
lea    -0x18(%rbp),%rdi
call   1050 <getifaddrs@plt>
cmp    $0xffffffff,%eax
jne    12ab <main+0x12b>
lea    0xdb9(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0xa4(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     135d <main+0x1dd>
mov    -0x20(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    12dc <main+0x15c>
jmp    133d <main+0x1bd>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    -0xa0(%rbp),%rsi
call   10a0 <get_mac_by_ifname@plt>
lea    -0x70(%rbp),%rdi
lea    -0xa0(%rbp),%rsi
mov    $0x30,%edx
call   1040 <memcmp@plt>
cmp    $0x0,%rax
jne    1338 <main+0x1b8>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xd31(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1070 <freeifaddrs@plt>
movl   $0x0,-0x4(%rbp)
jmp    136d <main+0x1ed>
jmp    133d <main+0x1bd>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0xa4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa4(%rbp)
jmp    12bd <main+0x13d>
mov    -0x18(%rbp),%rdi
call   1070 <freeifaddrs@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x90,%rsp
cmp    $0x1,%edi
jg     11b2 <main+0x22>
lea    0xe91(%rip),%rdi
call   1030 <puts@plt>
jmp    12b8 <main+0x128>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0x14(%rsp),%rcx
lea    0x18(%rsp),%r8
lea    0x1c(%rsp),%r9
lea    0x20(%rsp),%r10
lea    0x24(%rsp),%rax
lea    0xe27(%rip),%rsi
lea    0x10(%rsp),%rdx
push   %rax
push   %r10
call   1070 <sscanf@plt>
add    $0x10,%rsp
cmp    $0x6,%eax
jne    1296 <main+0x106>
movsd  0x10(%rsp),%xmm0
xorps  %xmm1,%xmm1
unpcklps %xmm1,%xmm0
movaps %xmm0,0x30(%rsp)
movsd  0x18(%rsp),%xmm0
unpcklps %xmm1,%xmm0
movaps %xmm0,0x40(%rsp)
movsd  0x20(%rsp),%xmm0
unpcklps %xmm1,%xmm0
movaps %xmm0,0x50(%rsp)
lea    0x8(%rsp),%rdi
call   1060 <getifaddrs@plt>
cmp    $0xffffffff,%eax
je     12ca <main+0x13a>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     12b1 <main+0x121>
lea    0x60(%rsp),%r15
lea    0x30(%rsp),%r14
jmp    1259 <main+0xc9>
nopl   0x0(%rax)
mov    0x10(%rbx),%rbx
test   %rbx,%rbx
je     12aa <main+0x11a>
cmpq   $0x0,0x8(%rbx)
je     1250 <main+0xc0>
mov    (%rbx),%rdi
mov    %r15,%rsi
call   10b0 <get_mac_by_ifname@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x30,%edx
call   1050 <memcmp@plt>
test   %rax,%rax
jne    1250 <main+0xc0>
mov    (%rbx),%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%rdi
call   1080 <freeifaddrs@plt>
xor    %eax,%eax
jmp    12bd <main+0x12d>
mov    0x8(%rbx),%rsi
lea    0xd71(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    12b8 <main+0x128>
mov    0x8(%rsp),%rdi
jmp    12b3 <main+0x123>
xor    %edi,%edi
call   1080 <freeifaddrs@plt>
mov    $0xffffffff,%eax
add    $0x90,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xd5d(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.ifaddrs = type { i8*, i32*, %struct.ifaddrs* }
@.str = [35 x i8] c" usage: mac2ifname MAC_ADDRESS\0A\00"
@.str.1 = [18 x i8] c"%x:%x:%x:%x:%x:%x\00"
@.str.2 = [28 x i8] c" invalid MAC address %s\0A\00"
@.str.3 = [11 x i8] c"getifaddrs\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.ifaddrs*
%7 = alloca %struct.ifaddrs*
%8 = alloca [6 x i32]
%9 = alloca [6 x i64]
%10 = alloca [6 x i64]
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %95
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = getelementptr inbounds [6 x i32], [6 x i32]* %8, i64 0, i64 0
%21 = getelementptr inbounds [6 x i32], [6 x i32]* %8, i64 0, i64 1
%22 = getelementptr inbounds [6 x i32], [6 x i32]* %8, i64 0, i64 2
%23 = getelementptr inbounds [6 x i32], [6 x i32]* %8, i64 0, i64 3
%24 = getelementptr inbounds [6 x i32], [6 x i32]* %8, i64 0, i64 4
%25 = getelementptr inbounds [6 x i32], [6 x i32]* %8, i64 0, i64 5
%26 = call i32 @sscanf(i8* %19, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32* %20, i32* %21, i32* %22, i32* %23, i32* %24, i32* %25)
store i32 %26, i32* %11
%27 = load i32, i32* %11
%28 = icmp ne i32 %27, 6
br i1 %28, label %29, label %34
29: ; preds = %16
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %32)
store i32 -1, i32* %3
br label %95
34: ; preds = %16
store i32 0, i32* %11
br label %35
35: ; preds = %47, %34
%36 = load i32, i32* %11
%37 = icmp slt i32 %36, 6
br i1 %37, label %38, label %50
38: ; preds = %35
%39 = load i32, i32* %11
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [6 x i32], [6 x i32]* %8, i64 0, i64 %40
%42 = load i32, i32* %41
%43 = zext i32 %42 to i64
%44 = load i32, i32* %11
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [6 x i64], [6 x i64]* %9, i64 0, i64 %45
store i64 %43, i64* %46
br label %47
47: ; preds = %38
%48 = load i32, i32* %11
%49 = add nsw i32 %48, 1
store i32 %49, i32* %11
br label %35
50: ; preds = %35
%51 = call i32 @getifaddrs(%struct.ifaddrs** %6)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %57
53: ; preds = %50
%54 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%55 = load i32, i32* @EXIT_FAILURE
%56 = call i32 @exit(i32 %55)
unreachable
57: ; preds = %50
%58 = load %struct.ifaddrs*, %struct.ifaddrs** %6
store %struct.ifaddrs* %58, %struct.ifaddrs** %7
store i32 0, i32* %11
br label %59
59: ; preds = %86, %57
%60 = load %struct.ifaddrs*, %struct.ifaddrs** %7
%61 = icmp ne %struct.ifaddrs* %60, null
br i1 %61, label %62, label %92
62: ; preds = %59
%63 = load %struct.ifaddrs*, %struct.ifaddrs** %7
%64 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %63, i32 0, i32 1
%65 = load i32*, i32** %64
%66 = icmp eq i32* %65, null
br i1 %66, label %67, label %68
67: ; preds = %62
br label %86
68: ; preds = %62
%69 = load %struct.ifaddrs*, %struct.ifaddrs** %7
%70 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %69, i32 0, i32 0
%71 = load i8*, i8** %70
%72 = getelementptr inbounds [6 x i64], [6 x i64]* %10, i64 0, i64 0
%73 = call i32 @get_mac_by_ifname(i8* %71, i64* %72)
%74 = getelementptr inbounds [6 x i64], [6 x i64]* %9, i64 0, i64 0
%75 = getelementptr inbounds [6 x i64], [6 x i64]* %10, i64 0, i64 0
%76 = call i64 @memcmp(i64* %74, i64* %75, i32 48)
%77 = icmp eq i64 %76, 0
br i1 %77, label %78, label %85
78: ; preds = %68
%79 = load %struct.ifaddrs*, %struct.ifaddrs** %7
%80 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %79, i32 0, i32 0
%81 = load i8*, i8** %80
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %81)
%83 = load %struct.ifaddrs*, %struct.ifaddrs** %6
%84 = call i32 @freeifaddrs(%struct.ifaddrs* %83)
store i32 0, i32* %3
br label %95
85: ; preds = %68
br label %86
86: ; preds = %85, %67
%87 = load %struct.ifaddrs*, %struct.ifaddrs** %7
%88 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %87, i32 0, i32 2
%89 = load %struct.ifaddrs*, %struct.ifaddrs** %88
store %struct.ifaddrs* %89, %struct.ifaddrs** %7
%90 = load i32, i32* %11
%91 = add nsw i32 %90, 1
store i32 %91, i32* %11
br label %59
92: ; preds = %59
%93 = load %struct.ifaddrs*, %struct.ifaddrs** %6
%94 = call i32 @freeifaddrs(%struct.ifaddrs* %93)
store i32 -1, i32* %3
br label %95
95: ; preds = %92, %78, %29, %14
%96 = load i32, i32* %3
ret i32 %96
}

LLVM IR (-O2):
%struct.ifaddrs = type { i8*, i32*, %struct.ifaddrs* }
@.str.1 = [18 x i8] c"%x:%x:%x:%x:%x:%x\00"
@.str.2 = [28 x i8] c" invalid MAC address %s\0A\00"
@.str.3 = [11 x i8] c"getifaddrs\00"
@EXIT_FAILURE = global i32 0
@str = [34 x i8] c" usage: mac2ifname MAC_ADDRESS\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.ifaddrs*
%4 = alloca [6 x i32]
%5 = alloca [6 x i64]
%6 = alloca [6 x i64]
%7 = bitcast %struct.ifaddrs** %3 to i8*
%8 = bitcast [6 x i32]* %4 to i8*
%9 = bitcast [6 x i64]* %5 to i8*
%10 = bitcast [6 x i64]* %6 to i8*
%11 = icmp slt i32 %0, 2
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
br label %81
14: ; preds = %2
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = getelementptr inbounds [6 x i32], [6 x i32]* %4, i64 0, i64 0
%18 = getelementptr inbounds [6 x i32], [6 x i32]* %4, i64 0, i64 1
%19 = getelementptr inbounds [6 x i32], [6 x i32]* %4, i64 0, i64 2
%20 = getelementptr inbounds [6 x i32], [6 x i32]* %4, i64 0, i64 3
%21 = getelementptr inbounds [6 x i32], [6 x i32]* %4, i64 0, i64 4
%22 = getelementptr inbounds [6 x i32], [6 x i32]* %4, i64 0, i64 5
%23 = call i32 @sscanf(i8* %16, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32* %17, i32* %18, i32* %19, i32* %20, i32* %21, i32* %22)
%24 = icmp eq i32 %23, 6
br i1 %24, label %25, label %43
25: ; preds = %14
%26 = getelementptr inbounds [6 x i64], [6 x i64]* %5, i64 0, i64 0
%27 = bitcast [6 x i32]* %4 to <2 x i32>*
%28 = load <2 x i32>, <2 x i32>* %27
%29 = zext <2 x i32> %28 to <2 x i64>
%30 = bitcast [6 x i64]* %5 to <2 x i64>*
store <2 x i64> %29, <2 x i64>* %30
%31 = getelementptr inbounds [6 x i64], [6 x i64]* %5, i64 0, i64 2
%32 = bitcast i32* %19 to <2 x i32>*
%33 = load <2 x i32>, <2 x i32>* %32
%34 = zext <2 x i32> %33 to <2 x i64>
%35 = bitcast i64* %31 to <2 x i64>*
store <2 x i64> %34, <2 x i64>* %35
%36 = getelementptr inbounds [6 x i64], [6 x i64]* %5, i64 0, i64 4
%37 = bitcast i32* %21 to <2 x i32>*
%38 = load <2 x i32>, <2 x i32>* %37
%39 = zext <2 x i32> %38 to <2 x i64>
%40 = bitcast i64* %36 to <2 x i64>*
store <2 x i64> %39, <2 x i64>* %40
%41 = call i32 @getifaddrs(%struct.ifaddrs** %3)
%42 = icmp eq i32 %41, -1
br i1 %42, label %51, label %46
43: ; preds = %14
%44 = load i8*, i8** %15
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %44)
br label %81
46: ; preds = %25
%47 = load %struct.ifaddrs*, %struct.ifaddrs** %3
%48 = icmp eq %struct.ifaddrs* %47, null
br i1 %48, label %78, label %49
49: ; preds = %46
%50 = getelementptr inbounds [6 x i64], [6 x i64]* %6, i64 0, i64 0
br label %55
51: ; preds = %25
%52 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%53 = load i32, i32* @EXIT_FAILURE
%54 = call i32 @exit(i32 %53)
unreachable
55: ; preds = %49, %72
%56 = phi %struct.ifaddrs* [ %47, %49 ], [ %74, %72 ]
%57 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %56, i64 0, i32 1
%58 = load i32*, i32** %57
%59 = icmp eq i32* %58, null
br i1 %59, label %72, label %60
60: ; preds = %55
%61 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %56, i64 0, i32 0
%62 = load i8*, i8** %61
%63 = call i32 @get_mac_by_ifname(i8* %62, i64* %50)
%64 = call i64 @memcmp(i64* %26, i64* %50, i32 48)
%65 = icmp eq i64 %64, 0
br i1 %65, label %66, label %72
66: ; preds = %60
%67 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %56, i64 0, i32 0
%68 = load i8*, i8** %67
%69 = call i32 @puts(i8* dereferenceable(1) %68)
%70 = load %struct.ifaddrs*, %struct.ifaddrs** %3
%71 = call i32 @freeifaddrs(%struct.ifaddrs* %70)
br label %81
72: ; preds = %60, %55
%73 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %56, i64 0, i32 2
%74 = load %struct.ifaddrs*, %struct.ifaddrs** %73
%75 = icmp eq %struct.ifaddrs* %74, null
br i1 %75, label %76, label %55
76: ; preds = %72
%77 = load %struct.ifaddrs*, %struct.ifaddrs** %3
br label %78
78: ; preds = %76, %46
%79 = phi %struct.ifaddrs* [ %77, %76 ], [ null, %46 ]
%80 = call i32 @freeifaddrs(%struct.ifaddrs* %79)
br label %81
81: ; preds = %78, %66, %43, %12
%82 = phi i32 [ -1, %12 ], [ -1, %43 ], [ 0, %66 ], [ -1, %78 ]
ret i32 %82
}