#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ tmp_if_buf;
struct ifaddrs {
  char *ifa_name;
  int /*<<< orphan*/ *ifa_data;
  TYPE_1__ *ifa_addr;
  struct ifaddrs *ifa_next;
};
struct TYPE_2__ {
  int sa_family;
};

/* Variables and functions */
int AF_INET;
int AF_PACKET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ freeifaddrs(struct ifaddrs *);
int getifaddrs(struct ifaddrs **);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int get_net_ifname(char *if_buf, int blen) {
  if (NULL == if_buf || blen < 8)
    return -1;
  struct ifaddrs *ifaddr, *ifa;
  int family, n;
  int ret = 1;
  if (getifaddrs(&ifaddr) == -1) {
    perror("getifaddrs");
    exit(EXIT_FAILURE);
  }
  int found = 0;
  char tmp_if_buf[16];
  memset(tmp_if_buf, 0, sizeof(tmp_if_buf));
  for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
    if (ifa->ifa_addr == NULL)
      continue;
    family = ifa->ifa_addr->sa_family;
    if (family == AF_INET) {
      if (strcmp(ifa->ifa_name, "br-lan") == 0) {
        found = 1;
        break;
      }
    } else if (family == AF_PACKET && ifa->ifa_data != NULL &&
               strcmp(ifa->ifa_name, "lo") != 0) {
      strncpy(tmp_if_buf, ifa->ifa_name, 16);
    }
  }
  if (found) {
    strncpy(if_buf, ifa->ifa_name, blen);
    ret = 0;
  } else if (tmp_if_buf[0] != 0) {
    strncpy(if_buf, tmp_if_buf, blen);
    ret = 0;
  }
  freeifaddrs(ifaddr);
  return ret;
}


x86 assembly (clang, -O0):
<get_net_ifname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
je     1195 <get_net_ifname+0x25>
cmpl   $0x8,-0x14(%rbp)
jge    11a1 <get_net_ifname+0x31>
movl   $0xffffffff,-0x4(%rbp)
jmp    133e <get_net_ifname+0x1ce>
movl   $0x1,-0x34(%rbp)
lea    -0x20(%rbp),%rdi
call   1060 <getifaddrs@plt>
cmp    $0xffffffff,%eax
jne    11d4 <get_net_ifname+0x64>
lea    0xe3f(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
movl   $0x0,-0x38(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12dd <get_net_ifname+0x16d>
mov    -0x28(%rbp),%rax
cmpq   $0x0,0x10(%rax)
jne    1219 <get_net_ifname+0xa9>
jmp    12c3 <get_net_ifname+0x153>
mov    -0x28(%rbp),%rax
mov    0x10(%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    0x2db8(%rip),%rcx
cmp    (%rcx),%eax
jne    1266 <get_net_ifname+0xf6>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xdc5(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    1261 <get_net_ifname+0xf1>
movl   $0x1,-0x38(%rbp)
jmp    12dd <get_net_ifname+0x16d>
jmp    12be <get_net_ifname+0x14e>
mov    -0x2c(%rbp),%eax
mov    0x2d68(%rip),%rcx
cmp    (%rcx),%eax
jne    12b9 <get_net_ifname+0x149>
mov    -0x28(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     12b9 <get_net_ifname+0x149>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xd7d(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     12b9 <get_net_ifname+0x149>
lea    -0x50(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
mov    $0x10,%edx
call   1030 <strncpy@plt>
jmp    12be <get_net_ifname+0x14e>
jmp    12c3 <get_net_ifname+0x153>
mov    -0x28(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11fa <get_net_ifname+0x8a>
cmpl   $0x0,-0x38(%rbp)
je     1306 <get_net_ifname+0x196>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
movl   $0x0,-0x34(%rbp)
jmp    132f <get_net_ifname+0x1bf>
movsbl -0x50(%rbp),%eax
cmp    $0x0,%eax
je     132a <get_net_ifname+0x1ba>
mov    -0x10(%rbp),%rdi
lea    -0x50(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
movl   $0x0,-0x34(%rbp)
jmp    132f <get_net_ifname+0x1bf>
mov    -0x20(%rbp),%rdi
call   1070 <freeifaddrs@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_net_ifname>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    $0xffffffff,%ebp
test   %rdi,%rdi
je     1281 <get_net_ifname+0x111>
mov    %esi,%r14d
cmp    $0x8,%esi
jl     1281 <get_net_ifname+0x111>
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
call   1060 <getifaddrs@plt>
cmp    $0xffffffff,%eax
je     1292 <get_net_ifname+0x122>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x8(%rsp),%rbp
test   %rbp,%rbp
je     1251 <get_net_ifname+0xe1>
mov    0x2e14(%rip),%r12
lea    0xe30(%rip),%r13
mov    0x2df6(%rip),%r15
jmp    120a <get_net_ifname+0x9a>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x0(%rbp),%rdi
mov    %r13,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     1266 <get_net_ifname+0xf6>
mov    0x18(%rbp),%rbp
test   %rbp,%rbp
je     1251 <get_net_ifname+0xe1>
mov    0x10(%rbp),%rax
test   %rax,%rax
je     1201 <get_net_ifname+0x91>
mov    (%rax),%eax
cmp    (%r12),%eax
je     11f0 <get_net_ifname+0x80>
cmp    (%r15),%eax
jne    1201 <get_net_ifname+0x91>
cmpq   $0x0,0x8(%rbp)
je     1201 <get_net_ifname+0x91>
mov    0x0(%rbp),%rdi
lea    0xde0(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     1201 <get_net_ifname+0x91>
mov    0x0(%rbp),%rsi
lea    0x10(%rsp),%rdi
mov    $0x10,%edx
call   1030 <strncpy@plt>
jmp    1201 <get_net_ifname+0x91>
cmpb   $0x0,0x10(%rsp)
je     125f <get_net_ifname+0xef>
lea    0x10(%rsp),%rsi
jmp    126a <get_net_ifname+0xfa>
mov    $0x1,%ebp
jmp    1277 <get_net_ifname+0x107>
mov    0x0(%rbp),%rsi
mov    %rbx,%rdi
mov    %r14d,%edx
call   1030 <strncpy@plt>
xor    %ebp,%ebp
mov    0x8(%rsp),%rdi
call   1070 <freeifaddrs@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd67(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

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

LLVM IR (-O2):
%struct.ifaddrs = type { i8*, i32*, %struct.TYPE_2__*, %struct.ifaddrs* }
%struct.TYPE_2__ = type { i32 }
@.str = [11 x i8] c"getifaddrs\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@.str.1 = [7 x i8] c"br-lan\00"
@AF_PACKET = global i32 0
@.str.2 = [3 x i8] c"lo\00"
define i32 @get_net_ifname(i8* %0, i32 %1) {
%3 = alloca %struct.ifaddrs*
%4 = alloca [16 x i8]
%5 = icmp eq i8* %0, null
%6 = icmp slt i32 %1, 8
%7 = or i1 %5, %6
br i1 %7, label %68, label %8
8: ; preds = %2
%9 = bitcast %struct.ifaddrs** %3 to i8*
%10 = call i32 @getifaddrs(%struct.ifaddrs** %3)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %8
%13 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %8
%17 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%18 = call i32 @memset(i8* %17, i32 0, i32 16)
%19 = load %struct.ifaddrs*, %struct.ifaddrs** %3
%20 = icmp eq %struct.ifaddrs* %19, null
br i1 %20, label %59, label %21
21: ; preds = %16, %51
%22 = phi %struct.ifaddrs* [ %53, %51 ], [ %19, %16 ]
%23 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %22, i64 0, i32 2
%24 = load %struct.TYPE_2__*, %struct.TYPE_2__** %23
%25 = icmp eq %struct.TYPE_2__* %24, null
br i1 %25, label %51, label %26
26: ; preds = %21
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i64 0, i32 0
%28 = load i32, i32* %27
%29 = load i32, i32* @AF_INET
%30 = icmp eq i32 %28, %29
br i1 %30, label %31, label %36
31: ; preds = %26
%32 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %22, i64 0, i32 0
%33 = load i8*, i8** %32
%34 = call i64 @strcmp(i8* %33, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%35 = icmp eq i64 %34, 0
br i1 %35, label %55, label %51
36: ; preds = %26
%37 = load i32, i32* @AF_PACKET
%38 = icmp eq i32 %28, %37
br i1 %38, label %39, label %51
39: ; preds = %36
%40 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %22, i64 0, i32 1
%41 = load i32*, i32** %40
%42 = icmp eq i32* %41, null
br i1 %42, label %51, label %43
43: ; preds = %39
%44 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %22, i64 0, i32 0
%45 = load i8*, i8** %44
%46 = call i64 @strcmp(i8* %45, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%47 = icmp eq i64 %46, 0
br i1 %47, label %51, label %48
48: ; preds = %43
%49 = load i8*, i8** %44
%50 = call i32 @strncpy(i8* %17, i8* %49, i32 16)
br label %51
51: ; preds = %31, %48, %43, %39, %36, %21
%52 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %22, i64 0, i32 3
%53 = load %struct.ifaddrs*, %struct.ifaddrs** %52
%54 = icmp eq %struct.ifaddrs* %53, null
br i1 %54, label %59, label %21
55: ; preds = %31
%56 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %22, i64 0, i32 0
%57 = load i8*, i8** %56
%58 = call i32 @strncpy(i8* %0, i8* %57, i32 %1)
br label %64
59: ; preds = %51, %16
%60 = load i8, i8* %17
%61 = icmp eq i8 %60, 0
br i1 %61, label %64, label %62
62: ; preds = %59
%63 = call i32 @strncpy(i8* %0, i8* %17, i32 %1)
br label %64
64: ; preds = %59, %62, %55
%65 = phi i32 [ 0, %55 ], [ 0, %62 ], [ 1, %59 ]
%66 = load %struct.ifaddrs*, %struct.ifaddrs** %3
%67 = call i32 @freeifaddrs(%struct.ifaddrs* %66)
br label %68
68: ; preds = %2, %64
%69 = phi i32 [ %65, %64 ], [ -1, %2 ]
ret i32 %69
}