#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 */
struct ifaddrs {
  char *ifa_name;
  TYPE_1__ *ifa_addr;
  struct ifaddrs *ifa_next;
};
struct TYPE_2__ {
  scalar_t__ sa_family;
};

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ freeifaddrs(struct ifaddrs *);
int /*<<< orphan*/ getifaddrs(struct ifaddrs **);
int /*<<< orphan*/ memcpy(char *, char *, int);
size_t strlen(char *);
int guessintname(char *intname, int namelen) {
  struct ifaddrs *ifap, *ifa;
  int buflen = namelen;
  char *outbuf = "";
  getifaddrs(&ifap);
  for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    if (ifa->ifa_addr->sa_family == AF_INET) {
      outbuf = ifa->ifa_name;
    }
  }
  if (strlen(outbuf) < buflen)
    buflen = strlen(outbuf);
  memcpy(intname, outbuf, buflen);
  intname[strlen(outbuf)] = 0x00;
  freeifaddrs(ifap);
  return (0);
}


x86 assembly (clang, -O0):
<guessintname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
lea    0xea4(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    -0x18(%rbp),%rdi
call   1050 <getifaddrs@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11b8 <guessintname+0x78>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%rax
mov    0x2e5a(%rip),%rcx
cmp    (%rcx),%rax
jne    11a2 <guessintname+0x62>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    11a7 <guessintname+0x67>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    1171 <guessintname+0x31>
mov    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
movslq -0x24(%rbp),%rcx
cmp    %rcx,%rax
jae    11da <guessintname+0x9a>
mov    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1060 <freeifaddrs@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<guessintname>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
call   1050 <getifaddrs@plt>
mov    0x8(%rsp),%rax
test   %rax,%rax
je     1197 <guessintname+0x57>
mov    0x2e7f(%rip),%rcx
mov    (%rcx),%rcx
lea    0xe8d(%rip),%rbp
jmp    1189 <guessintname+0x49>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x10(%rax),%rax
test   %rax,%rax
je     119e <guessintname+0x5e>
mov    0x8(%rax),%rdx
cmp    %rcx,(%rdx)
jne    1180 <guessintname+0x40>
mov    (%rax),%rbp
jmp    1180 <guessintname+0x40>
lea    0xe62(%rip),%rbp
mov    %rbp,%rdi
call   1030 <strlen@plt>
movslq %r14d,%rdx
cmp    %rdx,%rax
cmovb  %eax,%edx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1040 <memcpy@plt>
mov    %rbp,%rdi
call   1030 <strlen@plt>
movb   $0x0,(%rbx,%rax,1)
mov    0x8(%rsp),%rdi
call   1060 <freeifaddrs@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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