#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 sockaddr_in {
  scalar_t__ sin_family;
};
struct ifaddrs {
  struct ifaddrs *ifa_next;
  int /*<<< orphan*/ ifa_name;
  scalar_t__ ifa_addr;
};

/* Variables and functions */
scalar_t__ AF_PACKET;
int /*<<< orphan*/ freeifaddrs(struct ifaddrs *);
scalar_t__ get_device_mac(int, char *, unsigned char *);
scalar_t__ getifaddrs(struct ifaddrs **);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, int);
int get_macs(int sockfd, char *name, int name_len, unsigned char *mac) {
  static int first = 1;
  static struct ifaddrs *int_addrs;
  static const struct ifaddrs *int_cursor;
  const struct sockaddr_in *dl_addr;
  if (first == 1) {
    first = 0;
    if (getifaddrs(&int_addrs) == 0) {
      int_cursor = int_addrs;
    } else {
      first = 1;
      return 0;
    }
  }
  if (int_cursor != NULL) {
    while (int_cursor != NULL) {
      dl_addr = (const struct sockaddr_in *)int_cursor->ifa_addr;
      if (dl_addr != NULL && dl_addr->sin_family == AF_PACKET) {
        strncpy(name, int_cursor->ifa_name, name_len - 1);
        name[name_len - 1] = '\0';
        int_cursor = int_cursor->ifa_next;
        if (get_device_mac(sockfd, name, mac)) {
          return 1;
        }
      }
      int_cursor = int_cursor->ifa_next;
    }
  }
  if (int_addrs != NULL) {
    freeifaddrs(int_addrs);
    int_addrs = NULL;
  }
  return 0;
}


x86 assembly (clang, -O0):
<get_macs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
cmpl   $0x1,0x2ee3(%rip)
jne    11b1 <get_macs+0x71>
movl   $0x0,0x2ed3(%rip)
lea    0x2edc(%rip),%rdi
call   1040 <getifaddrs@plt>
cmp    $0x0,%rax
jne    1196 <get_macs+0x56>
mov    0x2ec6(%rip),%rax
mov    %rax,0x2ec7(%rip)
jmp    11ac <get_macs+0x6c>
movl   $0x1,0x2ea0(%rip)
movl   $0x0,-0x4(%rbp)
jmp    12b0 <get_macs+0x170>
jmp    11b1 <get_macs+0x71>
cmpq   $0x0,0x2e9f(%rip)
je     1284 <get_macs+0x144>
jmp    11c4 <get_macs+0x84>
cmpq   $0x0,0x2e8c(%rip)
je     127f <get_macs+0x13f>
mov    0x2e7f(%rip),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1269 <get_macs+0x129>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    0x2de6(%rip),%rcx
cmp    (%rcx),%rax
jne    1269 <get_macs+0x129>
mov    -0x10(%rbp),%rdi
mov    0x2e4a(%rip),%rax
mov    0x8(%rax),%esi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2e24(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x2e1a(%rip)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1060 <get_device_mac@plt>
cmp    $0x0,%rax
je     1264 <get_macs+0x124>
movl   $0x1,-0x4(%rbp)
jmp    12b0 <get_macs+0x170>
jmp    1269 <get_macs+0x129>
mov    0x2de8(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x2dde(%rip)
jmp    11c4 <get_macs+0x84>
jmp    1284 <get_macs+0x144>
cmpq   $0x0,0x2dc4(%rip)
je     12a9 <get_macs+0x169>
mov    0x2db7(%rip),%rdi
call   1050 <freeifaddrs@plt>
movq   $0x0,0x2da7(%rip)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_macs>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r15d
cmpb   $0x0,0x2eee(%rip)
je     116d <get_macs+0x2d>
mov    0x2ef5(%rip),%rax
test   %rax,%rax
jne    11a6 <get_macs+0x66>
jmp    121f <get_macs+0xdf>
movb   $0x1,0x2ed4(%rip)
lea    0x2ed5(%rip),%rdi
call   1040 <getifaddrs@plt>
test   %rax,%rax
je     1193 <get_macs+0x53>
movb   $0x0,0x2ebc(%rip)
xor    %ebx,%ebx
jmp    1244 <get_macs+0x104>
mov    0x2eb6(%rip),%rax
mov    %rax,0x2eb7(%rip)
test   %rax,%rax
je     121f <get_macs+0xdf>
movslq %ebp,%rbp
add    $0xffffffffffffffff,%rbp
mov    0x2e2c(%rip),%r12
jmp    11cf <get_macs+0x8f>
cs nopw 0x0(%rax,%rax,1)
mov    (%rax),%rax
mov    %rax,0x2e8e(%rip)
test   %rax,%rax
je     121f <get_macs+0xdf>
mov    0x10(%rax),%rcx
test   %rcx,%rcx
je     11c0 <get_macs+0x80>
mov    (%rcx),%rcx
cmp    (%r12),%rcx
jne    11c0 <get_macs+0x80>
mov    0x8(%rax),%esi
mov    %rbx,%rdi
mov    %ebp,%edx
call   1030 <strncpy@plt>
movb   $0x0,(%rbx,%rbp,1)
mov    0x2e5f(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x2e55(%rip)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1060 <get_device_mac@plt>
test   %rax,%rax
jne    123f <get_macs+0xff>
mov    0x2e3b(%rip),%rax
jmp    11c0 <get_macs+0x80>
mov    0x2e2a(%rip),%rdi
xor    %ebx,%ebx
test   %rdi,%rdi
je     1244 <get_macs+0x104>
call   1050 <freeifaddrs@plt>
movq   $0x0,0x2e13(%rip)
jmp    1244 <get_macs+0x104>
mov    $0x1,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifaddrs = type { %struct.ifaddrs*, i32, i64 }
%struct.sockaddr_in = type { i64 }
@get_macs.first = internal global i32 1
@get_macs.int_addrs = internal global %struct.ifaddrs* null
@get_macs.int_cursor = internal global %struct.ifaddrs* null
@AF_PACKET = global i64 0
define i32 @get_macs(i32 %0, i8* %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca %struct.sockaddr_in*
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%11 = load i32, i32* @get_macs.first
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %20
13: ; preds = %4
store i32 0, i32* @get_macs.first
%14 = call i64 @getifaddrs(%struct.ifaddrs** @get_macs.int_addrs)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = load %struct.ifaddrs*, %struct.ifaddrs** @get_macs.int_addrs
store %struct.ifaddrs* %17, %struct.ifaddrs** @get_macs.int_cursor
br label %19
18: ; preds = %13
store i32 1, i32* @get_macs.first
store i32 0, i32* %5
br label %75
19: ; preds = %16
br label %20
20: ; preds = %19, %4
%21 = load %struct.ifaddrs*, %struct.ifaddrs** @get_macs.int_cursor
%22 = icmp ne %struct.ifaddrs* %21, null
br i1 %22, label %23, label %68
23: ; preds = %20
br label %24
24: ; preds = %63, %23
%25 = load %struct.ifaddrs*, %struct.ifaddrs** @get_macs.int_cursor
%26 = icmp ne %struct.ifaddrs* %25, null
br i1 %26, label %27, label %67
27: ; preds = %24
%28 = load %struct.ifaddrs*, %struct.ifaddrs** @get_macs.int_cursor
%29 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %28, i32 0, i32 2
%30 = load i64, i64* %29
%31 = inttoptr i64 %30 to %struct.sockaddr_in*
store %struct.sockaddr_in* %31, %struct.sockaddr_in** %10
%32 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%33 = icmp ne %struct.sockaddr_in* %32, null
br i1 %33, label %34, label %63
34: ; preds = %27
%35 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %35, i32 0, i32 0
%37 = load i64, i64* %36
%38 = load i64, i64* @AF_PACKET
%39 = icmp eq i64 %37, %38
br i1 %39, label %40, label %63
40: ; preds = %34
%41 = load i8*, i8** %7
%42 = load %struct.ifaddrs*, %struct.ifaddrs** @get_macs.int_cursor
%43 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %42, i32 0, i32 1
%44 = load i32, i32* %43
%45 = load i32, i32* %8
%46 = sub nsw i32 %45, 1
%47 = call i32 @strncpy(i8* %41, i32 %44, i32 %46)
%48 = load i8*, i8** %7
%49 = load i32, i32* %8
%50 = sub nsw i32 %49, 1
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %48, i64 %51
store i8 0, i8* %52
%53 = load %struct.ifaddrs*, %struct.ifaddrs** @get_macs.int_cursor
%54 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %53, i32 0, i32 0
%55 = load %struct.ifaddrs*, %struct.ifaddrs** %54
store %struct.ifaddrs* %55, %struct.ifaddrs** @get_macs.int_cursor
%56 = load i32, i32* %6
%57 = load i8*, i8** %7
%58 = load i8*, i8** %9
%59 = call i64 @get_device_mac(i32 %56, i8* %57, i8* %58)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %40
store i32 1, i32* %5
br label %75
62: ; preds = %40
br label %63
63: ; preds = %62, %34, %27
%64 = load %struct.ifaddrs*, %struct.ifaddrs** @get_macs.int_cursor
%65 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %64, i32 0, i32 0
%66 = load %struct.ifaddrs*, %struct.ifaddrs** %65
store %struct.ifaddrs* %66, %struct.ifaddrs** @get_macs.int_cursor
br label %24
67: ; preds = %24
br label %68
68: ; preds = %67, %20
%69 = load %struct.ifaddrs*, %struct.ifaddrs** @get_macs.int_addrs
%70 = icmp ne %struct.ifaddrs* %69, null
br i1 %70, label %71, label %74
71: ; preds = %68
%72 = load %struct.ifaddrs*, %struct.ifaddrs** @get_macs.int_addrs
%73 = call i32 @freeifaddrs(%struct.ifaddrs* %72)
store %struct.ifaddrs* null, %struct.ifaddrs** @get_macs.int_addrs
br label %74
74: ; preds = %71, %68
store i32 0, i32* %5
br label %75
75: ; preds = %74, %61, %18
%76 = load i32, i32* %5
ret i32 %76
}

LLVM IR (-O2):
%struct.ifaddrs = type { %struct.ifaddrs*, i32, i64 }
%struct.sockaddr_in = type { i64 }
@get_macs.first = internal unnamed_addr global i1 false
@get_macs.int_addrs = internal global %struct.ifaddrs* null
@get_macs.int_cursor = internal unnamed_addr global %struct.ifaddrs* null
@AF_PACKET = global i64 0
define i32 @get_macs(i32 %0, i8* %1, i32 %2, i8* %3) {
%5 = load i1, i1* @get_macs.first
br i1 %5, label %12, label %6
6: ; preds = %4
store i1 true, i1* @get_macs.first
%7 = tail call i64 @getifaddrs(%struct.ifaddrs** @get_macs.int_addrs)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = load %struct.ifaddrs*, %struct.ifaddrs** @get_macs.int_addrs
store %struct.ifaddrs* %10, %struct.ifaddrs** @get_macs.int_cursor
br label %14
11: ; preds = %6
store i1 false, i1* @get_macs.first
br label %53
12: ; preds = %4
%13 = load %struct.ifaddrs*, %struct.ifaddrs** @get_macs.int_cursor
br label %14
14: ; preds = %12, %9
%15 = phi %struct.ifaddrs* [ %13, %12 ], [ %10, %9 ]
%16 = icmp eq %struct.ifaddrs* %15, null
br i1 %16, label %48, label %17
17: ; preds = %14
%18 = add nsw i32 %2, -1
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %1, i64 %19
br label %21
21: ; preds = %17, %43
%22 = phi %struct.ifaddrs* [ %15, %17 ], [ %46, %43 ]
%23 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %22, i64 0, i32 2
%24 = load i64, i64* %23
%25 = icmp eq i64 %24, 0
br i1 %25, label %43, label %26
26: ; preds = %21
%27 = inttoptr i64 %24 to %struct.sockaddr_in*
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %27, i64 0, i32 0
%29 = load i64, i64* %28
%30 = load i64, i64* @AF_PACKET
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %43
32: ; preds = %26
%33 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %22, i64 0, i32 1
%34 = load i32, i32* %33
%35 = tail call i32 @strncpy(i8* %1, i32 %34, i32 %18)
store i8 0, i8* %20
%36 = load %struct.ifaddrs*, %struct.ifaddrs** @get_macs.int_cursor
%37 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %36, i64 0, i32 0
%38 = load %struct.ifaddrs*, %struct.ifaddrs** %37
store %struct.ifaddrs* %38, %struct.ifaddrs** @get_macs.int_cursor
%39 = tail call i64 @get_device_mac(i32 %0, i8* %1, i8* %3)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %53
41: ; preds = %32
%42 = load %struct.ifaddrs*, %struct.ifaddrs** @get_macs.int_cursor
br label %43
43: ; preds = %41, %26, %21
%44 = phi %struct.ifaddrs* [ %42, %41 ], [ %22, %26 ], [ %22, %21 ]
%45 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %44, i64 0, i32 0
%46 = load %struct.ifaddrs*, %struct.ifaddrs** %45
store %struct.ifaddrs* %46, %struct.ifaddrs** @get_macs.int_cursor
%47 = icmp eq %struct.ifaddrs* %46, null
br i1 %47, label %48, label %21
48: ; preds = %43, %14
%49 = load %struct.ifaddrs*, %struct.ifaddrs** @get_macs.int_addrs
%50 = icmp eq %struct.ifaddrs* %49, null
br i1 %50, label %53, label %51
51: ; preds = %48
%52 = tail call i32 @freeifaddrs(%struct.ifaddrs* %49)
store %struct.ifaddrs* null, %struct.ifaddrs** @get_macs.int_addrs
br label %53
53: ; preds = %32, %48, %51, %11
%54 = phi i32 [ 0, %11 ], [ 0, %51 ], [ 0, %48 ], [ 1, %32 ]
ret i32 %54
}