#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_3__ TYPE_1__;

/* Type definitions */
struct sockaddr {
  int dummy;
};
struct ifaddrs {
  int ifa_flags;
  TYPE_1__ *ifa_addr;
  int /*<<< orphan*/ ifa_name;
  struct ifaddrs *ifa_next;
};
struct TYPE_3__ {
  int sa_family;
};

/* Variables and functions */
int /*<<< orphan*/ freeifaddrs(struct ifaddrs *);
int getifaddrs(struct ifaddrs **);
int /*<<< orphan*/ memcpy(struct sockaddr *, TYPE_1__ *, int /*<<< orphan*/);
int /*<<< orphan*/ sock_addrlen(TYPE_1__ *);
scalar_t__ strcmp(char const *, int /*<<< orphan*/);
int sock_getifaddr(const char *ifname, int family, int type,
                   struct sockaddr *sa) {
  struct ifaddrs *ifap, *ifa;
  int rc;
  if (getifaddrs(&ifap) == -1)
    return 0;
  rc = 0;
  for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
    if (strcmp(ifname, ifa->ifa_name))
      continue;
    if (ifa->ifa_addr == NULL)
      continue;
    if (ifa->ifa_addr->sa_family != family)
      continue;
    if (type != 0)
      if (!(ifa->ifa_flags & type))
        continue;
    switch (type) {
    case 0:
      memcpy(sa, ifa->ifa_addr, sock_addrlen(ifa->ifa_addr));
      rc = 1;
      break;
    default:
      break;
    }
    break;
  }
  freeifaddrs(ifap);
  return rc;
}


x86 assembly (clang, -O0):
<sock_getifaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
call   1060 <getifaddrs@plt>
cmp    $0xffffffff,%eax
jne    1184 <sock_getifaddr+0x34>
movl   $0x0,-0x4(%rbp)
jmp    1285 <sock_getifaddr+0x135>
movl   $0x0,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1276 <sock_getifaddr+0x126>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    0x10(%rax),%esi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
je     11bd <sock_getifaddr+0x6d>
jmp    1265 <sock_getifaddr+0x115>
mov    -0x30(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    11d1 <sock_getifaddr+0x81>
jmp    1265 <sock_getifaddr+0x115>
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%eax
cmp    -0x14(%rbp),%eax
je     11e9 <sock_getifaddr+0x99>
jmp    1265 <sock_getifaddr+0x115>
cmpl   $0x0,-0x18(%rbp)
je     120f <sock_getifaddr+0xbf>
mov    -0x30(%rbp),%rax
mov    (%rax),%eax
and    -0x18(%rbp),%eax
cmp    $0x0,%eax
jne    120a <sock_getifaddr+0xba>
jmp    1265 <sock_getifaddr+0x115>
jmp    120f <sock_getifaddr+0xbf>
mov    -0x18(%rbp),%eax
test   %eax,%eax
jne    125b <sock_getifaddr+0x10b>
jmp    121f <sock_getifaddr+0xcf>
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <sock_addrlen@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
call   1050 <memcpy@plt>
movl   $0x1,-0x34(%rbp)
jmp    1260 <sock_getifaddr+0x110>
jmp    1260 <sock_getifaddr+0x110>
jmp    1276 <sock_getifaddr+0x126>
mov    -0x30(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    1193 <sock_getifaddr+0x43>
mov    -0x28(%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):
<sock_getifaddr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%rbx
mov    %edx,%r12d
mov    %esi,%r13d
mov    %rdi,%rbp
lea    0x8(%rsp),%rdi
call   1060 <getifaddrs@plt>
cmp    $0xffffffff,%eax
je     11c4 <sock_getifaddr+0x74>
mov    0x8(%rsp),%r14
xor    %r15d,%r15d
test   %r14,%r14
je     11e6 <sock_getifaddr+0x96>
mov    %rbx,0x10(%rsp)
jmp    1199 <sock_getifaddr+0x49>
nopl   (%rax)
mov    0x18(%r14),%r14
test   %r14,%r14
je     11e6 <sock_getifaddr+0x96>
mov    0x10(%r14),%esi
mov    %rbp,%rdi
call   1030 <strcmp@plt>
test   %rax,%rax
jne    1190 <sock_getifaddr+0x40>
mov    0x8(%r14),%rbx
test   %rbx,%rbx
je     1190 <sock_getifaddr+0x40>
cmp    %r13d,(%rbx)
jne    1190 <sock_getifaddr+0x40>
test   %r12d,%r12d
je     11c9 <sock_getifaddr+0x79>
test   %r12d,(%r14)
je     1190 <sock_getifaddr+0x40>
jmp    11e6 <sock_getifaddr+0x96>
xor    %r15d,%r15d
jmp    11f0 <sock_getifaddr+0xa0>
mov    %rbx,%rdi
call   1040 <sock_addrlen@plt>
mov    0x10(%rsp),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    $0x1,%r15d
mov    0x8(%rsp),%rdi
call   1070 <freeifaddrs@plt>
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.ifaddrs = type { i32, %struct.TYPE_3__*, i32, %struct.ifaddrs* }
%struct.TYPE_3__ = type { i32 }
define i32 @sock_getifaddr(i8* %0, i32 %1, i32 %2, %struct.sockaddr* %3) {
%5 = alloca %struct.ifaddrs*
%6 = bitcast %struct.ifaddrs** %5 to i8*
%7 = call i32 @getifaddrs(%struct.ifaddrs** %5)
%8 = icmp eq i32 %7, -1
br i1 %8, label %45, label %9
9: ; preds = %4
%10 = load %struct.ifaddrs*, %struct.ifaddrs** %5
%11 = icmp eq %struct.ifaddrs* %10, null
br i1 %11, label %41, label %12
12: ; preds = %9
%13 = icmp eq i32 %2, 0
br label %14
14: ; preds = %12, %37
%15 = phi %struct.ifaddrs* [ %10, %12 ], [ %39, %37 ]
%16 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %15, i64 0, i32 2
%17 = load i32, i32* %16
%18 = call i64 @strcmp(i8* %0, i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %37
20: ; preds = %14
%21 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %15, i64 0, i32 1
%22 = load %struct.TYPE_3__*, %struct.TYPE_3__** %21
%23 = icmp eq %struct.TYPE_3__* %22, null
br i1 %23, label %37, label %24
24: ; preds = %20
%25 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %22, i64 0, i32 0
%26 = load i32, i32* %25
%27 = icmp eq i32 %26, %1
br i1 %27, label %28, label %37
28: ; preds = %24
br i1 %13, label %34, label %29
29: ; preds = %28
%30 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %15, i64 0, i32 0
%31 = load i32, i32* %30
%32 = and i32 %31, %2
%33 = icmp eq i32 %32, 0
br i1 %33, label %37, label %41
34: ; preds = %28
%35 = call i32 @sock_addrlen(%struct.TYPE_3__* %22)
%36 = call i32 @memcpy(%struct.sockaddr* %3, %struct.TYPE_3__* %22, i32 %35)
br label %41
37: ; preds = %29, %24, %20, %14
%38 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %15, i64 0, i32 3
%39 = load %struct.ifaddrs*, %struct.ifaddrs** %38
%40 = icmp eq %struct.ifaddrs* %39, null
br i1 %40, label %41, label %14
41: ; preds = %37, %29, %9, %34
%42 = phi i32 [ 1, %34 ], [ 0, %9 ], [ 0, %29 ], [ 0, %37 ]
%43 = load %struct.ifaddrs*, %struct.ifaddrs** %5
%44 = call i32 @freeifaddrs(%struct.ifaddrs* %43)
br label %45
45: ; preds = %4, %41
%46 = phi i32 [ %42, %41 ], [ 0, %4 ]
ret i32 %46
}