#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_4__ TYPE_2__;
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_4__ {
  scalar_t__ s_addr;
};
struct sockaddr_in {
  TYPE_2__ sin_addr;
};
struct ifaddrs {
  int ifa_flags;
  int /*<<< orphan*/ ifa_name;
  TYPE_1__ *ifa_addr;
  struct ifaddrs *ifa_next;
};
struct TYPE_3__ {
  scalar_t__ sa_family;
};

/* Variables and functions */
scalar_t__ AF_INET;
int IFF_UP;
int /*<<< orphan*/ freeifaddrs(struct ifaddrs *);
int getifaddrs(struct ifaddrs **);
int inet_pton(scalar_t__, char const *, TYPE_2__ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int intf_name(const char *addr, char *if_name) {
  struct ifaddrs *ifaddr, *ifa;
  struct sockaddr_in iaddr;
  struct sockaddr_in *saddr;
  int retval = -1;
  if (getifaddrs(&ifaddr) == -1) {
    perror("getifaddrs");
    goto cleanup;
  }
  if (inet_pton(AF_INET, addr, &iaddr.sin_addr) == -1) {
    perror("inet_pton");
    goto cleanup1;
  }
  for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
    if (!(ifa->ifa_addr && (ifa->ifa_flags & IFF_UP) &&
          ifa->ifa_addr->sa_family == AF_INET))
      continue;
    saddr = (struct sockaddr_in *)ifa->ifa_addr;
    if (saddr->sin_addr.s_addr == iaddr.sin_addr.s_addr) {
      strcpy(if_name, ifa->ifa_name);
      retval = 0;
      break;
    }
  }
cleanup1:
  freeifaddrs(ifaddr);
cleanup:
  return retval;
}


x86 assembly (clang, -O0):
<intf_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x34(%rbp)
lea    -0x18(%rbp),%rdi
call   1050 <getifaddrs@plt>
cmp    $0xffffffff,%eax
jne    118a <intf_name+0x3a>
lea    0xe80(%rip),%rdi
call   1070 <perror@plt>
jmp    1272 <intf_name+0x122>
mov    0x2e57(%rip),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1040 <inet_pton@plt>
cmp    $0xffffffff,%eax
jne    11bb <intf_name+0x6b>
lea    0xe5a(%rip),%rdi
call   1070 <perror@plt>
jmp    1269 <intf_name+0x119>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1264 <intf_name+0x114>
mov    -0x20(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     1210 <intf_name+0xc0>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    0x2df6(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1210 <intf_name+0xc0>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%rax
mov    0x2de1(%rip),%rcx
cmp    (%rcx),%rax
je     1215 <intf_name+0xc5>
jmp    1253 <intf_name+0x103>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
cmp    -0x28(%rbp),%rax
jne    124e <intf_name+0xfe>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
call   1030 <strcpy@plt>
movl   $0x0,-0x34(%rbp)
jmp    1264 <intf_name+0x114>
jmp    1253 <intf_name+0x103>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    11c3 <intf_name+0x73>
jmp    1269 <intf_name+0x119>
mov    -0x18(%rbp),%rdi
call   1060 <freeifaddrs@plt>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<intf_name>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbp
mov    %rsp,%rdi
call   1050 <getifaddrs@plt>
cmp    $0xffffffff,%eax
je     11e2 <intf_name+0x92>
mov    0x2e76(%rip),%rbx
mov    (%rbx),%rdi
lea    0x8(%rsp),%rdx
mov    %rbp,%rsi
call   1040 <inet_pton@plt>
cmp    $0xffffffff,%eax
je     11f5 <intf_name+0xa5>
mov    (%rsp),%rax
mov    $0xffffffff,%ebp
test   %rax,%rax
je     1206 <intf_name+0xb6>
mov    0x2e44(%rip),%rcx
mov    (%rcx),%ecx
mov    (%rbx),%rdx
mov    0x8(%rsp),%rsi
jmp    11b9 <intf_name+0x69>
nopl   0x0(%rax,%rax,1)
mov    0x10(%rax),%rax
test   %rax,%rax
je     1206 <intf_name+0xb6>
mov    0x8(%rax),%rdi
test   %rdi,%rdi
je     11b0 <intf_name+0x60>
test   %ecx,(%rax)
je     11b0 <intf_name+0x60>
mov    (%rdi),%rdi
cmp    %rdx,%rdi
jne    11b0 <intf_name+0x60>
cmp    %rsi,%rdi
jne    11b0 <intf_name+0x60>
mov    0x4(%rax),%esi
mov    %r14,%rdi
call   1030 <strcpy@plt>
xor    %ebp,%ebp
jmp    1206 <intf_name+0xb6>
lea    0xe17(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%ebp
jmp    120f <intf_name+0xbf>
lea    0xe0f(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%ebp
mov    (%rsp),%rdi
call   1060 <freeifaddrs@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifaddrs = type { i32, i32, %struct.TYPE_3__*, %struct.ifaddrs* }
%struct.TYPE_3__ = type { i64 }
%struct.sockaddr_in = type { %struct.TYPE_4__ }
%struct.TYPE_4__ = type { i64 }
@.str = [11 x i8] c"getifaddrs\00"
@AF_INET = global i64 0
@.str.1 = [10 x i8] c"inet_pton\00"
@IFF_UP = global i32 0
define i32 @intf_name(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.ifaddrs*
%6 = alloca %struct.ifaddrs*
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.sockaddr_in*
%9 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 -1, i32* %9
%10 = call i32 @getifaddrs(%struct.ifaddrs** %5)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %76
14: ; preds = %2
%15 = load i64, i64* @AF_INET
%16 = load i8*, i8** %3
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%18 = call i32 @inet_pton(i64 %15, i8* %16, %struct.TYPE_4__* %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %73
22: ; preds = %14
%23 = load %struct.ifaddrs*, %struct.ifaddrs** %5
store %struct.ifaddrs* %23, %struct.ifaddrs** %6
br label %24
24: ; preds = %68, %22
%25 = load %struct.ifaddrs*, %struct.ifaddrs** %6
%26 = icmp ne %struct.ifaddrs* %25, null
br i1 %26, label %27, label %72
27: ; preds = %24
%28 = load %struct.ifaddrs*, %struct.ifaddrs** %6
%29 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %28, i32 0, i32 2
%30 = load %struct.TYPE_3__*, %struct.TYPE_3__** %29
%31 = icmp ne %struct.TYPE_3__* %30, null
br i1 %31, label %32, label %47
32: ; preds = %27
%33 = load %struct.ifaddrs*, %struct.ifaddrs** %6
%34 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = load i32, i32* @IFF_UP
%37 = and i32 %35, %36
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %47
39: ; preds = %32
%40 = load %struct.ifaddrs*, %struct.ifaddrs** %6
%41 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %40, i32 0, i32 2
%42 = load %struct.TYPE_3__*, %struct.TYPE_3__** %41
%43 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %42, i32 0, i32 0
%44 = load i64, i64* %43
%45 = load i64, i64* @AF_INET
%46 = icmp eq i64 %44, %45
br i1 %46, label %48, label %47
47: ; preds = %39, %32, %27
br label %68
48: ; preds = %39
%49 = load %struct.ifaddrs*, %struct.ifaddrs** %6
%50 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %49, i32 0, i32 2
%51 = load %struct.TYPE_3__*, %struct.TYPE_3__** %50
%52 = bitcast %struct.TYPE_3__* %51 to %struct.sockaddr_in*
store %struct.sockaddr_in* %52, %struct.sockaddr_in** %8
%53 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %53, i32 0, i32 0
%55 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %54, i32 0, i32 0
%56 = load i64, i64* %55
%57 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%58 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %57, i32 0, i32 0
%59 = load i64, i64* %58
%60 = icmp eq i64 %56, %59
br i1 %60, label %61, label %67
61: ; preds = %48
%62 = load i8*, i8** %4
%63 = load %struct.ifaddrs*, %struct.ifaddrs** %6
%64 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %63, i32 0, i32 1
%65 = load i32, i32* %64
%66 = call i32 @strcpy(i8* %62, i32 %65)
store i32 0, i32* %9
br label %72
67: ; preds = %48
br label %68
68: ; preds = %67, %47
%69 = load %struct.ifaddrs*, %struct.ifaddrs** %6
%70 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %69, i32 0, i32 3
%71 = load %struct.ifaddrs*, %struct.ifaddrs** %70
store %struct.ifaddrs* %71, %struct.ifaddrs** %6
br label %24
72: ; preds = %61, %24
br label %73
73: ; preds = %72, %20
%74 = load %struct.ifaddrs*, %struct.ifaddrs** %5
%75 = call i32 @freeifaddrs(%struct.ifaddrs* %74)
br label %76
76: ; preds = %73, %12
%77 = load i32, i32* %9
ret i32 %77
}

LLVM IR (-O2):
%struct.ifaddrs = type { i32, i32, %struct.TYPE_3__*, %struct.ifaddrs* }
%struct.TYPE_3__ = type { i64 }
%struct.sockaddr_in = type { %struct.TYPE_4__ }
%struct.TYPE_4__ = type { i64 }
@.str = [11 x i8] c"getifaddrs\00"
@AF_INET = global i64 0
@.str.1 = [10 x i8] c"inet_pton\00"
@IFF_UP = global i32 0
define i32 @intf_name(i8* %0, i8* %1) {
%3 = alloca %struct.ifaddrs*
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.ifaddrs** %3 to i8*
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = call i32 @getifaddrs(%struct.ifaddrs** %3)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %54
11: ; preds = %2
%12 = load i64, i64* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%14 = call i32 @inet_pton(i64 %12, i8* %0, %struct.TYPE_4__* %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %24, label %16
16: ; preds = %11
%17 = load %struct.ifaddrs*, %struct.ifaddrs** %3
%18 = icmp eq %struct.ifaddrs* %17, null
br i1 %18, label %50, label %19
19: ; preds = %16
%20 = load i32, i32* @IFF_UP
%21 = load i64, i64* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
%23 = load i64, i64* %22
br label %26
24: ; preds = %11
%25 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %50
26: ; preds = %19, %46
%27 = phi %struct.ifaddrs* [ %17, %19 ], [ %48, %46 ]
%28 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %27, i64 0, i32 2
%29 = load %struct.TYPE_3__*, %struct.TYPE_3__** %28
%30 = icmp eq %struct.TYPE_3__* %29, null
br i1 %30, label %46, label %31
31: ; preds = %26
%32 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %27, i64 0, i32 0
%33 = load i32, i32* %32
%34 = and i32 %20, %33
%35 = icmp eq i32 %34, 0
br i1 %35, label %46, label %36
36: ; preds = %31
%37 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %29, i64 0, i32 0
%38 = load i64, i64* %37
%39 = icmp eq i64 %38, %21
%40 = icmp eq i64 %38, %23
%41 = select i1 %39, i1 %40, i1 false
br i1 %41, label %42, label %46
42: ; preds = %36
%43 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %27, i64 0, i32 1
%44 = load i32, i32* %43
%45 = call i32 @strcpy(i8* %1, i32 %44)
br label %50
46: ; preds = %26, %31, %36
%47 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %27, i64 0, i32 3
%48 = load %struct.ifaddrs*, %struct.ifaddrs** %47
%49 = icmp eq %struct.ifaddrs* %48, null
br i1 %49, label %50, label %26
50: ; preds = %46, %16, %42, %24
%51 = phi i32 [ -1, %24 ], [ 0, %42 ], [ -1, %16 ], [ -1, %46 ]
%52 = load %struct.ifaddrs*, %struct.ifaddrs** %3
%53 = call i32 @freeifaddrs(%struct.ifaddrs* %52)
br label %54
54: ; preds = %50, %9
%55 = phi i32 [ -1, %9 ], [ %51, %50 ]
ret i32 %55
}