#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;
  TYPE_1__ *ifa_broadaddr;
  int /*<<< orphan*/ ifa_name;
  struct ifaddrs *ifa_next;
};
struct TYPE_3__ {
  int sa_family;
};

/* Variables and functions */
#define IFF_BROADCAST 128
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;
  if (getifaddrs(&ifap) == -1)
    return 0;
  int 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;
    case IFF_BROADCAST:
      memcpy(sa, ifa->ifa_broadaddr, 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    $0x60,%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    12d7 <sock_getifaddr+0x187>
movl   $0x0,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12c8 <sock_getifaddr+0x178>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    0x18(%rax),%esi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
je     11bd <sock_getifaddr+0x6d>
jmp    12b7 <sock_getifaddr+0x167>
mov    -0x30(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    11d1 <sock_getifaddr+0x81>
jmp    12b7 <sock_getifaddr+0x167>
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%eax
cmp    -0x14(%rbp),%eax
je     11e9 <sock_getifaddr+0x99>
jmp    12b7 <sock_getifaddr+0x167>
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    12b7 <sock_getifaddr+0x167>
jmp    120f <sock_getifaddr+0xbf>
mov    -0x18(%rbp),%eax
mov    %eax,-0x38(%rbp)
test   %eax,%eax
je     1235 <sock_getifaddr+0xe5>
jmp    1222 <sock_getifaddr+0xd2>
mov    -0x38(%rbp),%eax
sub    $0x80,%eax
je     1271 <sock_getifaddr+0x121>
jmp    12ad <sock_getifaddr+0x15d>
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    12b2 <sock_getifaddr+0x162>
mov    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x30(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x50(%rbp)
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <sock_addrlen@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
call   1050 <memcpy@plt>
movl   $0x1,-0x34(%rbp)
jmp    12b2 <sock_getifaddr+0x162>
jmp    12b2 <sock_getifaddr+0x162>
jmp    12c8 <sock_getifaddr+0x178>
mov    -0x30(%rbp),%rax
mov    0x20(%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    $0x60,%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,%rbp
mov    %edx,%r12d
mov    %esi,%r13d
mov    %rdi,%rbx
lea    0x10(%rsp),%rdi
call   1060 <getifaddrs@plt>
cmp    $0xffffffff,%eax
je     11e2 <sock_getifaddr+0x92>
mov    0x10(%rsp),%r14
xor    %r15d,%r15d
test   %r14,%r14
je     1204 <sock_getifaddr+0xb4>
mov    %rbp,0x8(%rsp)
jmp    1199 <sock_getifaddr+0x49>
nopl   (%rax)
mov    0x20(%r14),%r14
test   %r14,%r14
je     1204 <sock_getifaddr+0xb4>
mov    0x18(%r14),%esi
mov    %rbx,%rdi
call   1030 <strcmp@plt>
test   %rax,%rax
jne    1190 <sock_getifaddr+0x40>
mov    0x8(%r14),%rbp
test   %rbp,%rbp
je     1190 <sock_getifaddr+0x40>
cmp    %r13d,0x0(%rbp)
jne    1190 <sock_getifaddr+0x40>
test   %r12d,%r12d
je     11e7 <sock_getifaddr+0x97>
test   %r12d,(%r14)
je     1190 <sock_getifaddr+0x40>
cmp    $0x80,%r12d
jne    1204 <sock_getifaddr+0xb4>
mov    0x10(%r14),%rbx
mov    %rbp,%rdi
call   1040 <sock_addrlen@plt>
mov    0x8(%rsp),%rdi
mov    %rbx,%rsi
jmp    11f7 <sock_getifaddr+0xa7>
xor    %r15d,%r15d
jmp    120e <sock_getifaddr+0xbe>
mov    %rbp,%rdi
call   1040 <sock_addrlen@plt>
mov    0x8(%rsp),%rdi
mov    %rbp,%rsi
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    $0x1,%r15d
mov    0x10(%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__*, %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 %88
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 = %80, %16
%19 = load %struct.ifaddrs*, %struct.ifaddrs** %11
%20 = icmp ne %struct.ifaddrs* %19, null
br i1 %20, label %21, label %84
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 3
%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 %80
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 %80
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 %80
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 %80
55: ; preds = %47
br label %56
56: ; preds = %55, %44
%57 = load i32, i32* %8
switch i32 %57, label %78 [
i32 0, label %58
i32 128, label %68
]
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 %79
68: ; preds = %56
%69 = load %struct.sockaddr*, %struct.sockaddr** %9
%70 = load %struct.ifaddrs*, %struct.ifaddrs** %11
%71 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %70, i32 0, i32 2
%72 = load %struct.TYPE_3__*, %struct.TYPE_3__** %71
%73 = load %struct.ifaddrs*, %struct.ifaddrs** %11
%74 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %73, i32 0, i32 1
%75 = load %struct.TYPE_3__*, %struct.TYPE_3__** %74
%76 = call i32 @sock_addrlen(%struct.TYPE_3__* %75)
%77 = call i32 @memcpy(%struct.sockaddr* %69, %struct.TYPE_3__* %72, i32 %76)
store i32 1, i32* %12
br label %79
78: ; preds = %56
br label %79
79: ; preds = %78, %68, %58
br label %84
80: ; preds = %54, %43, %34, %28
%81 = load %struct.ifaddrs*, %struct.ifaddrs** %11
%82 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %81, i32 0, i32 4
%83 = load %struct.ifaddrs*, %struct.ifaddrs** %82
store %struct.ifaddrs* %83, %struct.ifaddrs** %11
br label %18
84: ; preds = %79, %18
%85 = load %struct.ifaddrs*, %struct.ifaddrs** %10
%86 = call i32 @freeifaddrs(%struct.ifaddrs* %85)
%87 = load i32, i32* %12
store i32 %87, i32* %5
br label %88
88: ; preds = %84, %15
%89 = load i32, i32* %5
ret i32 %89
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.ifaddrs = type { i32, %struct.TYPE_3__*, %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 %52, label %9
9: ; preds = %4
%10 = load %struct.ifaddrs*, %struct.ifaddrs** %5
%11 = icmp eq %struct.ifaddrs* %10, null
br i1 %11, label %48, label %12
12: ; preds = %9
%13 = icmp eq i32 %2, 0
br label %14
14: ; preds = %12, %44
%15 = phi %struct.ifaddrs* [ %10, %12 ], [ %46, %44 ]
%16 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %15, i64 0, i32 3
%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 %44
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 %44, 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 %44
28: ; preds = %24
br i1 %13, label %36, 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 %44, label %34
34: ; preds = %29
%35 = icmp eq i32 %2, 128
br i1 %35, label %39, label %48
36: ; preds = %28
%37 = call i32 @sock_addrlen(%struct.TYPE_3__* %22)
%38 = call i32 @memcpy(%struct.sockaddr* %3, %struct.TYPE_3__* %22, i32 %37)
br label %48
39: ; preds = %34
%40 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %15, i64 0, i32 2
%41 = load %struct.TYPE_3__*, %struct.TYPE_3__** %40
%42 = call i32 @sock_addrlen(%struct.TYPE_3__* %22)
%43 = call i32 @memcpy(%struct.sockaddr* %3, %struct.TYPE_3__* %41, i32 %42)
br label %48
44: ; preds = %29, %24, %20, %14
%45 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %15, i64 0, i32 4
%46 = load %struct.ifaddrs*, %struct.ifaddrs** %45
%47 = icmp eq %struct.ifaddrs* %46, null
br i1 %47, label %48, label %14
48: ; preds = %44, %9, %34, %36, %39
%49 = phi i32 [ 0, %34 ], [ 1, %39 ], [ 1, %36 ], [ 0, %9 ], [ 0, %44 ]
%50 = load %struct.ifaddrs*, %struct.ifaddrs** %5
%51 = call i32 @freeifaddrs(%struct.ifaddrs* %50)
br label %52
52: ; preds = %4, %48
%53 = phi i32 [ %49, %48 ], [ 0, %4 ]
ret i32 %53
}