#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 ether_addr {
  int dummy;
};
typedef int /*<<< orphan*/ in_mac;
typedef int /*<<< orphan*/ full_path;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ debugfs_make_path(char *, char const *, char *, int);
char *debugfs_mount(int /*<<< orphan*/ *);
struct ether_addr *ether_aton(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcmp(struct ether_addr *, struct ether_addr *, int);
int /*<<< orphan*/ memcpy(struct ether_addr *, struct ether_addr *, int);
int /*<<< orphan*/ strcmp(char *, char *);
char *strtok_r(char *, char *, char **);
struct ether_addr *translate_mac(const char *mesh_iface,
                                 struct ether_addr *mac) {
  enum {
    tg_start,
    tg_mac,
    tg_via,
    tg_originator,
  } pos;
  char full_path[256 + 1];
  char *debugfs_mnt;
  static struct ether_addr in_mac;
  struct ether_addr *mac_result, *mac_tmp;
  FILE *f = NULL;
  size_t len = 0;
  char *line = NULL;
  char *input, *saveptr, *token;
  int line_invalid;
  memcpy(&in_mac, mac, sizeof(in_mac));
  mac_result = &in_mac;
  debugfs_mnt = debugfs_mount(NULL);
  if (!debugfs_mnt)
    goto out;
  debugfs_make_path("%s/batman_adv/%s"
                    "/"
                    "transtable_global",
                    mesh_iface, full_path, sizeof(full_path));
  f = fopen(full_path, "r");
  if (!f)
    goto out;
  while (getline(&line, &len, f) != -1) {
    line_invalid = 0;
    pos = tg_start;
    input = line;
    while ((token = strtok_r(input, " \t", &saveptr))) {
      input = NULL;
      switch (pos) {
      case tg_start:
        if (strcmp(token, "*") != 0)
          line_invalid = 1;
        else
          pos = tg_mac;
        break;
      case tg_mac:
        mac_tmp = ether_aton(token);
        if (!mac_tmp || memcmp(mac_tmp, &in_mac, sizeof(in_mac)) != 0)
          line_invalid = 1;
        else
          pos = tg_via;
        break;
      case tg_via:
        if (strcmp(token, "via") == 0)
          pos = tg_originator;
        break;
      case tg_originator:
        mac_tmp = ether_aton(token);
        if (!mac_tmp) {
          line_invalid = 1;
        } else {
          mac_result = mac_tmp;
          goto out;
        }
        break;
      }
      if (line_invalid)
        break;
    }
  }
out:
  if (f)
    fclose(f);
  free(line);
  return mac_result;
}


x86 assembly (clang, -O0):
<translate_mac>:
push   %rbp
mov    %rsp,%rbp
sub    $0x180,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x140(%rbp)
movq   $0x0,-0x148(%rbp)
movq   $0x0,-0x150(%rbp)
mov    -0x10(%rbp),%rsi
lea    0x2e8d(%rip),%rdi
mov    $0x4,%edx
call   1090 <memcpy@plt>
lea    0x2e7c(%rip),%rax
mov    %rax,-0x130(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   10d0 <debugfs_mount@plt>
mov    %rax,-0x128(%rbp)
cmpq   $0x0,-0x128(%rbp)
jne    122a <translate_mac+0x7a>
jmp    1440 <translate_mac+0x290>
mov    -0x8(%rbp),%rsi
lea    -0x120(%rbp),%rdx
lea    0xdd4(%rip),%rdi
mov    $0x101,%ecx
call   1050 <debugfs_make_path@plt>
lea    -0x120(%rbp),%rdi
lea    0xddf(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x140(%rbp)
cmpq   $0x0,-0x140(%rbp)
jne    1273 <translate_mac+0xc3>
jmp    1440 <translate_mac+0x290>
jmp    1278 <translate_mac+0xc8>
mov    -0x140(%rbp),%rdx
lea    -0x150(%rbp),%rdi
lea    -0x148(%rbp),%rsi
call   10c0 <getline@plt>
cmp    $0xffffffff,%eax
je     143b <translate_mac+0x28b>
movl   $0x0,-0x16c(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x150(%rbp),%rax
mov    %rax,-0x158(%rbp)
mov    -0x158(%rbp),%rdi
lea    0xd6d(%rip),%rsi
lea    -0x160(%rbp),%rdx
call   1060 <strtok_r@plt>
mov    %rax,-0x168(%rbp)
cmp    $0x0,%rax
je     1436 <translate_mac+0x286>
movq   $0x0,-0x158(%rbp)
mov    -0x14(%rbp),%eax
mov    %rax,-0x178(%rbp)
sub    $0x3,%rax
ja     141f <translate_mac+0x26f>
mov    -0x178(%rbp),%rax
lea    0xcee(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x168(%rbp),%rdi
lea    0xd0f(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
je     1346 <translate_mac+0x196>
movl   $0x1,-0x16c(%rbp)
jmp    134d <translate_mac+0x19d>
movl   $0x1,-0x14(%rbp)
jmp    141f <translate_mac+0x26f>
mov    -0x168(%rbp),%rdi
call   10a0 <ether_aton@plt>
mov    %rax,-0x138(%rbp)
cmpq   $0x0,-0x138(%rbp)
je     1394 <translate_mac+0x1e4>
mov    -0x138(%rbp),%rdi
lea    0x2cfb(%rip),%rsi
mov    $0x4,%edx
call   1070 <memcmp@plt>
cmp    $0x0,%eax
je     13a3 <translate_mac+0x1f3>
movl   $0x1,-0x16c(%rbp)
jmp    13aa <translate_mac+0x1fa>
movl   $0x2,-0x14(%rbp)
jmp    141f <translate_mac+0x26f>
mov    -0x168(%rbp),%rdi
lea    0xc7d(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
jne    13d2 <translate_mac+0x222>
movl   $0x3,-0x14(%rbp)
jmp    141f <translate_mac+0x26f>
mov    -0x168(%rbp),%rdi
call   10a0 <ether_aton@plt>
mov    %rax,-0x138(%rbp)
cmpq   $0x0,-0x138(%rbp)
jne    1407 <translate_mac+0x257>
movl   $0x1,-0x16c(%rbp)
jmp    141a <translate_mac+0x26a>
mov    -0x138(%rbp),%rax
mov    %rax,-0x130(%rbp)
jmp    1440 <translate_mac+0x290>
jmp    141f <translate_mac+0x26f>
cmpl   $0x0,-0x16c(%rbp)
je     1431 <translate_mac+0x281>
jmp    1436 <translate_mac+0x286>
jmp    12ba <translate_mac+0x10a>
jmp    1278 <translate_mac+0xc8>
jmp    1440 <translate_mac+0x290>
cmpq   $0x0,-0x140(%rbp)
je     145a <translate_mac+0x2aa>
mov    -0x140(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x150(%rbp),%rdi
call   1030 <free@plt>
mov    -0x130(%rbp),%rax
add    $0x180,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<translate_mac>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x128,%rsp
mov    %rdi,%rbx
movq   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0x2e9f(%rip),%r15
mov    %r15,%rdi
mov    $0x4,%edx
call   1090 <memcpy@plt>
xor    %ebp,%ebp
xor    %edi,%edi
call   10d0 <debugfs_mount@plt>
test   %rax,%rax
je     1368 <translate_mac+0x1b8>
lea    0xe0d(%rip),%rdi
lea    0x20(%rsp),%rbp
mov    %rbx,%rsi
mov    %rbp,%rdx
mov    $0x101,%ecx
call   1050 <debugfs_make_path@plt>
lea    0xe14(%rip),%rsi
mov    %rbp,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1350 <translate_mac+0x1a0>
mov    %rax,%rbp
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    %rax,%rdx
call   10c0 <getline@plt>
cmp    $0xffffffff,%eax
je     1354 <translate_mac+0x1a4>
lea    0xde0(%rip),%r12
lea    0x18(%rsp),%rbx
lea    0x2e1b(%rip),%r15
lea    0xd98(%rip),%r14
jmp    129b <translate_mac+0xeb>
mov    %rax,%rdi
call   10a0 <ether_aton@plt>
test   %rax,%rax
jne    1385 <translate_mac+0x1d5>
nopl   0x0(%rax,%rax,1)
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    %rbp,%rdx
call   10c0 <getline@plt>
cmp    $0xffffffff,%eax
je     135b <translate_mac+0x1ab>
mov    0x8(%rsp),%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1060 <strtok_r@plt>
test   %rax,%rax
je     1280 <translate_mac+0xd0>
xor    %r13d,%r13d
jmp    12ee <translate_mac+0x13e>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rax,%rdi
lea    0xd70(%rip),%rsi
call   1080 <strcmp@plt>
xor    %r13d,%r13d
test   %eax,%eax
sete   %r13b
or     $0x2,%r13d
xor    %edi,%edi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1060 <strtok_r@plt>
test   %rax,%rax
je     1280 <translate_mac+0xd0>
mov    %r13d,%ecx
movslq (%r14,%rcx,4),%rcx
add    %r14,%rcx
jmp    *%rcx
mov    %rax,%rdi
lea    0xd34(%rip),%rsi
call   1080 <strcmp@plt>
mov    $0x1,%r13d
test   %eax,%eax
je     12dc <translate_mac+0x12c>
jmp    1280 <translate_mac+0xd0>
nopl   0x0(%rax,%rax,1)
mov    %rax,%rdi
call   10a0 <ether_aton@plt>
test   %rax,%rax
je     1280 <translate_mac+0xd0>
mov    %rax,%rdi
mov    %r15,%rsi
mov    $0x4,%edx
call   1070 <memcmp@plt>
mov    $0x2,%r13d
test   %eax,%eax
je     12dc <translate_mac+0x12c>
jmp    1280 <translate_mac+0xd0>
xor    %ebp,%ebp
jmp    1368 <translate_mac+0x1b8>
lea    0x2d21(%rip),%r15
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r15,%rax
add    $0x128,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rax,%r15
jmp    135b <translate_mac+0x1ab>

LLVM IR (-O0):
%struct.ether_addr = type { i32 }
@translate_mac.in_mac = internal global %struct.ether_addr zeroinitializer
@.str = [35 x i8] c"%s/batman_adv/%s/transtable_global\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c" \09\00"
@.str.3 = [2 x i8] c"*\00"
@.str.4 = [4 x i8] c"via\00"
define %struct.ether_addr* @translate_mac(i8* %0, %struct.ether_addr* %1) {
%3 = alloca i8*
%4 = alloca %struct.ether_addr*
%5 = alloca i32
%6 = alloca [257 x i8]
%7 = alloca i8*
%8 = alloca %struct.ether_addr*
%9 = alloca %struct.ether_addr*
%10 = alloca i32*
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i8*
%16 = alloca i32
store i8* %0, i8** %3
store %struct.ether_addr* %1, %struct.ether_addr** %4
store i32* null, i32** %10
store i64 0, i64* %11
store i8* null, i8** %12
%17 = load %struct.ether_addr*, %struct.ether_addr** %4
%18 = call i32 @memcpy(%struct.ether_addr* @translate_mac.in_mac, %struct.ether_addr* %17, i32 4)
store %struct.ether_addr* @translate_mac.in_mac, %struct.ether_addr** %8
%19 = call i8* @debugfs_mount(i32* null)
store i8* %19, i8** %7
%20 = load i8*, i8** %7
%21 = icmp ne i8* %20, null
br i1 %21, label %23, label %22
22: ; preds = %2
br label %86
23: ; preds = %2
%24 = load i8*, i8** %3
%25 = getelementptr inbounds [257 x i8], [257 x i8]* %6, i64 0, i64 0
%26 = call i32 @debugfs_make_path(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %24, i8* %25, i32 257)
%27 = getelementptr inbounds [257 x i8], [257 x i8]* %6, i64 0, i64 0
%28 = call i32* @fopen(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %28, i32** %10
%29 = load i32*, i32** %10
%30 = icmp ne i32* %29, null
br i1 %30, label %32, label %31
31: ; preds = %23
br label %86
32: ; preds = %23
br label %33
33: ; preds = %84, %32
%34 = load i32*, i32** %10
%35 = call i32 @getline(i8** %12, i64* %11, i32* %34)
%36 = icmp ne i32 %35, -1
br i1 %36, label %37, label %85
37: ; preds = %33
store i32 0, i32* %16
store i32 0, i32* %5
%38 = load i8*, i8** %12
store i8* %38, i8** %13
br label %39
39: ; preds = %83, %37
%40 = load i8*, i8** %13
%41 = call i8* @strtok_r(i8* %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8** %14)
store i8* %41, i8** %15
%42 = icmp ne i8* %41, null
br i1 %42, label %43, label %84
43: ; preds = %39
store i8* null, i8** %13
%44 = load i32, i32* %5
switch i32 %44, label %79 [
i32 0, label %45
i32 1, label %52
i32 2, label %64
i32 3, label %70
]
45: ; preds = %43
%46 = load i8*, i8** %15
%47 = call i32 @strcmp(i8* %46, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %45
store i32 1, i32* %16
br label %51
50: ; preds = %45
store i32 1, i32* %5
br label %51
51: ; preds = %50, %49
br label %79
52: ; preds = %43
%53 = load i8*, i8** %15
%54 = call %struct.ether_addr* @ether_aton(i8* %53)
store %struct.ether_addr* %54, %struct.ether_addr** %9
%55 = load %struct.ether_addr*, %struct.ether_addr** %9
%56 = icmp ne %struct.ether_addr* %55, null
br i1 %56, label %57, label %61
57: ; preds = %52
%58 = load %struct.ether_addr*, %struct.ether_addr** %9
%59 = call i32 @memcmp(%struct.ether_addr* %58, %struct.ether_addr* @translate_mac.in_mac, i32 4)
%60 = icmp ne i32 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %57, %52
store i32 1, i32* %16
br label %63
62: ; preds = %57
store i32 2, i32* %5
br label %63
63: ; preds = %62, %61
br label %79
64: ; preds = %43
%65 = load i8*, i8** %15
%66 = call i32 @strcmp(i8* %65, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
%67 = icmp eq i32 %66, 0
br i1 %67, label %68, label %69
68: ; preds = %64
store i32 3, i32* %5
br label %69
69: ; preds = %68, %64
br label %79
70: ; preds = %43
%71 = load i8*, i8** %15
%72 = call %struct.ether_addr* @ether_aton(i8* %71)
store %struct.ether_addr* %72, %struct.ether_addr** %9
%73 = load %struct.ether_addr*, %struct.ether_addr** %9
%74 = icmp ne %struct.ether_addr* %73, null
br i1 %74, label %76, label %75
75: ; preds = %70
store i32 1, i32* %16
br label %78
76: ; preds = %70
%77 = load %struct.ether_addr*, %struct.ether_addr** %9
store %struct.ether_addr* %77, %struct.ether_addr** %8
br label %86
78: ; preds = %75
br label %79
79: ; preds = %43, %78, %69, %63, %51
%80 = load i32, i32* %16
%81 = icmp ne i32 %80, 0
br i1 %81, label %82, label %83
82: ; preds = %79
br label %84
83: ; preds = %79
br label %39
84: ; preds = %82, %39
br label %33
85: ; preds = %33
br label %86
86: ; preds = %85, %76, %31, %22
%87 = load i32*, i32** %10
%88 = icmp ne i32* %87, null
br i1 %88, label %89, label %92
89: ; preds = %86
%90 = load i32*, i32** %10
%91 = call i32 @fclose(i32* %90)
br label %92
92: ; preds = %89, %86
%93 = load i8*, i8** %12
%94 = call i32 @free(i8* %93)
%95 = load %struct.ether_addr*, %struct.ether_addr** %8
ret %struct.ether_addr* %95
}

LLVM IR (-O2):
%struct.ether_addr = type { i32 }
@translate_mac.in_mac = internal global %struct.ether_addr zeroinitializer
@.str = [35 x i8] c"%s/batman_adv/%s/transtable_global\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c" \09\00"
@.str.3 = [2 x i8] c"*\00"
@.str.4 = [4 x i8] c"via\00"
define %struct.ether_addr* @translate_mac(i8* %0, %struct.ether_addr* %1) {
%3 = alloca [257 x i8]
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = getelementptr inbounds [257 x i8], [257 x i8]* %3, i64 0, i64 0
%8 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%9 = bitcast i8** %5 to i8*
store i8* null, i8** %5
%10 = bitcast i8** %6 to i8*
%11 = tail call i32 @memcpy(%struct.ether_addr* @translate_mac.in_mac, %struct.ether_addr* %1, i32 4)
%12 = tail call i8* @debugfs_mount(i32* null)
%13 = icmp eq i8* %12, null
br i1 %13, label %56, label %14
14: ; preds = %2
%15 = call i32 @debugfs_make_path(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %7, i32 257)
%16 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %56, label %18
18: ; preds = %14
%19 = call i32 @getline(i8** %5, i64* %4, i32* %16)
%20 = icmp eq i32 %19, -1
br i1 %20, label %52, label %21
21: ; preds = %18, %49
%22 = load i8*, i8** %5
%23 = call i8* @strtok_r(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8** %6)
%24 = icmp eq i8* %23, null
br i1 %24, label %49, label %25
25: ; preds = %21, %31
%26 = phi i8* [ %33, %31 ], [ %23, %21 ]
%27 = phi i32 [ %32, %31 ], [ 0, %21 ]
switch i32 %27, label %48 [
i32 0, label %28
i32 1, label %35
i32 2, label %41
i32 3, label %45
]
28: ; preds = %25
%29 = call i32 @strcmp(i8* %26, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %49
31: ; preds = %28, %38, %41
%32 = phi i32 [ 1, %28 ], [ %44, %41 ], [ 2, %38 ]
%33 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8** %6)
%34 = icmp eq i8* %33, null
br i1 %34, label %49, label %25
35: ; preds = %25
%36 = call %struct.ether_addr* @ether_aton(i8* %26)
%37 = icmp eq %struct.ether_addr* %36, null
br i1 %37, label %49, label %38
38: ; preds = %35
%39 = call i32 @memcmp(%struct.ether_addr* %36, %struct.ether_addr* @translate_mac.in_mac, i32 4)
%40 = icmp eq i32 %39, 0
br i1 %40, label %31, label %49
41: ; preds = %25
%42 = call i32 @strcmp(i8* %26, i8* dereferenceable(4) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
%43 = icmp eq i32 %42, 0
%44 = select i1 %43, i32 3, i32 2
br label %31
45: ; preds = %25
%46 = call %struct.ether_addr* @ether_aton(i8* %26)
%47 = icmp eq %struct.ether_addr* %46, null
br i1 %47, label %49, label %52
48: ; preds = %25
unreachable
49: ; preds = %31, %28, %38, %35, %21, %45
%50 = call i32 @getline(i8** %5, i64* %4, i32* %16)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %21
52: ; preds = %45, %49, %18
%53 = phi %struct.ether_addr* [ @translate_mac.in_mac, %18 ], [ @translate_mac.in_mac, %49 ], [ %46, %45 ]
%54 = call i32 @fclose(i32* %16)
%55 = load i8*, i8** %5
br label %56
56: ; preds = %2, %14, %52
%57 = phi i8* [ %55, %52 ], [ null, %14 ], [ null, %2 ]
%58 = phi %struct.ether_addr* [ %53, %52 ], [ @translate_mac.in_mac, %14 ], [ @translate_mac.in_mac, %2 ]
%59 = call i32 @free(i8* %57)
ret %struct.ether_addr* %58
}