#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 sockaddr_in {
  scalar_t__ sin_family;
};
struct ifaddrs {
  int /*<<< orphan*/ *ifa_addr;
  int /*<<< orphan*/ ifa_name;
  struct ifaddrs *ifa_next;
};

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ freeifaddrs(struct ifaddrs *);
int /*<<< orphan*/ getifaddrs(struct ifaddrs **);
int /*<<< orphan*/ memcpy(struct sockaddr_in *, struct sockaddr_in *, size_t);
scalar_t__ strcmp(int /*<<< orphan*/, char const *);
int interface_to_sa(const char *ifname, struct sockaddr_in *addr,
                    size_t addrlen) {
  int rc = -1;
  struct ifaddrs *ifr;
  struct ifaddrs *ifc;
  struct sockaddr_in *sin;
  getifaddrs(&ifr);
  for (ifc = ifr; ifc != NULL; ifc = ifc->ifa_next) {
    if (strcmp(ifc->ifa_name, ifname))
      continue;
    if (ifc->ifa_addr == NULL)
      continue;
    sin = (struct sockaddr_in *)ifc->ifa_addr;
    if (sin->sin_family != AF_INET)
      continue;
    memcpy(addr, sin, addrlen);
    rc = 0;
  }
  freeifaddrs(ifr);
  return rc;
}


x86 assembly (clang, -O0):
<interface_to_sa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
lea    -0x28(%rbp),%rdi
call   1050 <getifaddrs@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11f9 <interface_to_sa+0xb9>
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x8(%rbp),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
je     1196 <interface_to_sa+0x56>
jmp    11e8 <interface_to_sa+0xa8>
mov    -0x30(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11a9 <interface_to_sa+0x69>
jmp    11e8 <interface_to_sa+0xa8>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
mov    0x2e26(%rip),%rcx
cmp    (%rcx),%rax
je     11d0 <interface_to_sa+0x90>
jmp    11e8 <interface_to_sa+0xa8>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x30(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    116c <interface_to_sa+0x2c>
mov    -0x28(%rbp),%rdi
call   1060 <freeifaddrs@plt>
mov    -0x1c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<interface_to_sa>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    %rsp,%rdi
call   1050 <getifaddrs@plt>
mov    (%rsp),%rbp
mov    $0xffffffff,%r12d
test   %rbp,%rbp
je     11c1 <interface_to_sa+0x81>
mov    0x2e76(%rip),%r13
jmp    1189 <interface_to_sa+0x49>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x10(%rbp),%rbp
test   %rbp,%rbp
je     11bb <interface_to_sa+0x7b>
mov    0x8(%rbp),%edi
mov    %rbx,%rsi
call   1030 <strcmp@plt>
test   %rax,%rax
jne    1180 <interface_to_sa+0x40>
mov    0x0(%rbp),%rsi
test   %rsi,%rsi
je     1180 <interface_to_sa+0x40>
mov    (%rsi),%rax
cmp    0x0(%r13),%rax
jne    1180 <interface_to_sa+0x40>
mov    %r15,%rdi
mov    %r14,%rdx
call   1040 <memcpy@plt>
xor    %r12d,%r12d
jmp    1180 <interface_to_sa+0x40>
mov    (%rsp),%rdi
jmp    11c3 <interface_to_sa+0x83>
xor    %edi,%edi
call   1060 <freeifaddrs@plt>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i64 }
%struct.ifaddrs = type { i32*, i32, %struct.ifaddrs* }
@AF_INET = global i64 0
define i32 @interface_to_sa(i8* %0, %struct.sockaddr_in* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca %struct.sockaddr_in*
%6 = alloca i64
%7 = alloca i32
%8 = alloca %struct.ifaddrs*
%9 = alloca %struct.ifaddrs*
%10 = alloca %struct.sockaddr_in*
store i8* %0, i8** %4
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %5
store i64 %2, i64* %6
store i32 -1, i32* %7
%11 = call i32 @getifaddrs(%struct.ifaddrs** %8)
%12 = load %struct.ifaddrs*, %struct.ifaddrs** %8
store %struct.ifaddrs* %12, %struct.ifaddrs** %9
br label %13
13: ; preds = %46, %3
%14 = load %struct.ifaddrs*, %struct.ifaddrs** %9
%15 = icmp ne %struct.ifaddrs* %14, null
br i1 %15, label %16, label %50
16: ; preds = %13
%17 = load %struct.ifaddrs*, %struct.ifaddrs** %9
%18 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %17, i32 0, i32 1
%19 = load i32, i32* %18
%20 = load i8*, i8** %4
%21 = call i64 @strcmp(i32 %19, i8* %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %16
br label %46
24: ; preds = %16
%25 = load %struct.ifaddrs*, %struct.ifaddrs** %9
%26 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %25, i32 0, i32 0
%27 = load i32*, i32** %26
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %30
29: ; preds = %24
br label %46
30: ; preds = %24
%31 = load %struct.ifaddrs*, %struct.ifaddrs** %9
%32 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %31, i32 0, i32 0
%33 = load i32*, i32** %32
%34 = bitcast i32* %33 to %struct.sockaddr_in*
store %struct.sockaddr_in* %34, %struct.sockaddr_in** %10
%35 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %35, i32 0, i32 0
%37 = load i64, i64* %36
%38 = load i64, i64* @AF_INET
%39 = icmp ne i64 %37, %38
br i1 %39, label %40, label %41
40: ; preds = %30
br label %46
41: ; preds = %30
%42 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%43 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%44 = load i64, i64* %6
%45 = call i32 @memcpy(%struct.sockaddr_in* %42, %struct.sockaddr_in* %43, i64 %44)
store i32 0, i32* %7
br label %46
46: ; preds = %41, %40, %29, %23
%47 = load %struct.ifaddrs*, %struct.ifaddrs** %9
%48 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %47, i32 0, i32 2
%49 = load %struct.ifaddrs*, %struct.ifaddrs** %48
store %struct.ifaddrs* %49, %struct.ifaddrs** %9
br label %13
50: ; preds = %13
%51 = load %struct.ifaddrs*, %struct.ifaddrs** %8
%52 = call i32 @freeifaddrs(%struct.ifaddrs* %51)
%53 = load i32, i32* %7
ret i32 %53
}

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