#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 {
  int dummy;
};
struct ifreq {
  int /*<<< orphan*/ ifr_addr;
  int /*<<< orphan*/ ifr_name;
};
struct ifconf {
  int ifc_len;
  struct ifreq *ifc_req;
};
typedef int /*<<< orphan*/ ifc;

/* Variables and functions */
int /*<<< orphan*/ SIOCGIFCONF;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(struct ifreq *);
scalar_t__ ioctl(int const, int /*<<< orphan*/, struct ifconf *);
struct ifreq *malloc(int);
int /*<<< orphan*/ memcpy(struct sockaddr_in *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ memset(struct ifconf *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ strcmp(int /*<<< orphan*/, char const *);
int get_device_ip(const int sockfd, const char *device_name,
                  struct sockaddr_in *ip) {
  struct ifconf ifc;
  struct ifreq *ifr;
  int i, device_count;
  memset(&ifc, 0, sizeof(ifc));
  if (ioctl(sockfd, SIOCGIFCONF, &ifc) != 0) {
    return -1;
  }
  if ((ifr = malloc(ifc.ifc_len * 2)) == NULL) {
    perror("malloc");
    exit(1);
  }
  ifc.ifc_req = ifr;
  if (ioctl(sockfd, SIOCGIFCONF, &ifc) != 0) {
    free(ifr);
    return -1;
  }
  device_count = ifc.ifc_len / sizeof(struct ifreq);
  for (i = 0; i < device_count; ++i) {
    if (strcmp(ifr[i].ifr_name, device_name) == 0) {
      memcpy(ip, &(ifr[i].ifr_addr), sizeof(struct sockaddr_in));
      free(ifr);
      return 1;
    }
  }
  free(ifr);
  return -1;
}


x86 assembly (clang, -O0):
<get_device_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%edi
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
je     11ce <get_device_ip+0x4e>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cb <get_device_ip+0x14b>
mov    -0x28(%rbp),%edi
shl    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11fd <get_device_ip+0x7d>
lea    0xe12(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
je     1239 <get_device_ip+0xb9>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cb <get_device_ip+0x14b>
movslq -0x28(%rbp),%rax
shr    $0x3,%rax
mov    %eax,-0x38(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x38(%rbp),%eax
jge    12bb <get_device_ip+0x13b>
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    0x4(%rax,%rcx,8),%edi
mov    -0x10(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    12a8 <get_device_ip+0x128>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
movslq -0x34(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    12cb <get_device_ip+0x14b>
jmp    12ad <get_device_ip+0x12d>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    124b <get_device_ip+0xcb>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_device_ip>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rsi,%r12
mov    %edi,%ebx
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e31(%rip),%r14
mov    (%r14),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1050 <ioctl@plt>
mov    $0xffffffff,%r13d
test   %rax,%rax
jne    125c <get_device_ip+0xdc>
mov    %r15,0x10(%rsp)
mov    (%rsp),%edi
add    %edi,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     126e <get_device_ip+0xee>
mov    %rax,%r15
mov    %rax,0x8(%rsp)
mov    (%r14),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1050 <ioctl@plt>
mov    $0xffffffff,%r13d
test   %rax,%rax
jne    1254 <get_device_ip+0xd4>
mov    (%rsp),%ebp
cmp    $0x8,%ebp
jl     1254 <get_device_ip+0xd4>
shr    $0x3,%rbp
mov    %r15,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x4(%rbx),%edi
mov    %r12,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     123c <get_device_ip+0xbc>
add    $0x8,%rbx
add    $0xffffffffffffffff,%rbp
jne    1220 <get_device_ip+0xa0>
jmp    1254 <get_device_ip+0xd4>
mov    0x10(%rsp),%rdi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    $0x1,%r13d
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd8b(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.ifconf = type { i32, %struct.ifreq* }
%struct.ifreq = type { i32, i32 }
@SIOCGIFCONF = global i32 0
@.str = [7 x i8] c"malloc\00"
define i32 @get_device_ip(i32 %0, i8* %1, %struct.sockaddr_in* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca %struct.sockaddr_in*
%8 = alloca %struct.ifconf
%9 = alloca %struct.ifreq*
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store %struct.sockaddr_in* %2, %struct.sockaddr_in** %7
%12 = call i32 @memset(%struct.ifconf* %8, i32 0, i32 16)
%13 = load i32, i32* %5
%14 = load i32, i32* @SIOCGIFCONF
%15 = call i64 @ioctl(i32 %13, i32 %14, %struct.ifconf* %8)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %3
store i32 -1, i32* %4
br label %74
18: ; preds = %3
%19 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %8, i32 0, i32 0
%20 = load i32, i32* %19
%21 = mul nsw i32 %20, 2
%22 = call %struct.ifreq* @malloc(i32 %21)
store %struct.ifreq* %22, %struct.ifreq** %9
%23 = icmp eq %struct.ifreq* %22, null
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %18
%28 = load %struct.ifreq*, %struct.ifreq** %9
%29 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %8, i32 0, i32 1
store %struct.ifreq* %28, %struct.ifreq** %29
%30 = load i32, i32* %5
%31 = load i32, i32* @SIOCGIFCONF
%32 = call i64 @ioctl(i32 %30, i32 %31, %struct.ifconf* %8)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %27
%35 = load %struct.ifreq*, %struct.ifreq** %9
%36 = call i32 @free(%struct.ifreq* %35)
store i32 -1, i32* %4
br label %74
37: ; preds = %27
%38 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %8, i32 0, i32 0
%39 = load i32, i32* %38
%40 = sext i32 %39 to i64
%41 = udiv i64 %40, 8
%42 = trunc i64 %41 to i32
store i32 %42, i32* %11
store i32 0, i32* %10
br label %43
43: ; preds = %68, %37
%44 = load i32, i32* %10
%45 = load i32, i32* %11
%46 = icmp slt i32 %44, %45
br i1 %46, label %47, label %71
47: ; preds = %43
%48 = load %struct.ifreq*, %struct.ifreq** %9
%49 = load i32, i32* %10
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %48, i64 %50
%52 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %51, i32 0, i32 1
%53 = load i32, i32* %52
%54 = load i8*, i8** %6
%55 = call i64 @strcmp(i32 %53, i8* %54)
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %67
57: ; preds = %47
%58 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%59 = load %struct.ifreq*, %struct.ifreq** %9
%60 = load i32, i32* %10
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %59, i64 %61
%63 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %62, i32 0, i32 0
%64 = call i32 @memcpy(%struct.sockaddr_in* %58, i32* %63, i32 4)
%65 = load %struct.ifreq*, %struct.ifreq** %9
%66 = call i32 @free(%struct.ifreq* %65)
store i32 1, i32* %4
br label %74
67: ; preds = %47
br label %68
68: ; preds = %67
%69 = load i32, i32* %10
%70 = add nsw i32 %69, 1
store i32 %70, i32* %10
br label %43
71: ; preds = %43
%72 = load %struct.ifreq*, %struct.ifreq** %9
%73 = call i32 @free(%struct.ifreq* %72)
store i32 -1, i32* %4
br label %74
74: ; preds = %71, %57, %34, %17
%75 = load i32, i32* %4
ret i32 %75
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.ifconf = type { i32, %struct.ifreq* }
%struct.ifreq = type { i32, i32 }
@SIOCGIFCONF = global i32 0
@.str = [7 x i8] c"malloc\00"
define i32 @get_device_ip(i32 %0, i8* %1, %struct.sockaddr_in* %2) {
%4 = alloca %struct.ifconf
%5 = bitcast %struct.ifconf* %4 to i8*
%6 = call i32 @memset(%struct.ifconf* %4, i32 0, i32 16)
%7 = load i32, i32* @SIOCGIFCONF
%8 = call i64 @ioctl(i32 %0, i32 %7, %struct.ifconf* %4)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %46
10: ; preds = %3
%11 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %4, i64 0, i32 0
%12 = load i32, i32* %11
%13 = shl nsw i32 %12, 1
%14 = call %struct.ifreq* @malloc(i32 %13)
%15 = icmp eq %struct.ifreq* %14, null
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %10
%20 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %4, i64 0, i32 1
store %struct.ifreq* %14, %struct.ifreq** %20
%21 = load i32, i32* @SIOCGIFCONF
%22 = call i64 @ioctl(i32 %0, i32 %21, %struct.ifconf* %4)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %43
24: ; preds = %19
%25 = load i32, i32* %11
%26 = icmp sgt i32 %25, 7
br i1 %26, label %27, label %43
27: ; preds = %24
%28 = lshr i32 %25, 3
%29 = zext i32 %28 to i64
br label %30
30: ; preds = %27, %40
%31 = phi i64 [ 0, %27 ], [ %41, %40 ]
%32 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %14, i64 %31, i32 1
%33 = load i32, i32* %32
%34 = call i64 @strcmp(i32 %33, i8* %1)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %30
%37 = and i64 %31, 4294967295
%38 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %14, i64 %37, i32 0
%39 = call i32 @memcpy(%struct.sockaddr_in* %2, i32* %38, i32 4)
br label %43
40: ; preds = %30
%41 = add nuw nsw i64 %31, 1
%42 = icmp eq i64 %41, %29
br i1 %42, label %43, label %30
43: ; preds = %40, %24, %19, %36
%44 = phi i32 [ 1, %36 ], [ -1, %19 ], [ -1, %24 ], [ -1, %40 ]
%45 = call i32 @free(%struct.ifreq* %14)
br label %46
46: ; preds = %43, %3
%47 = phi i32 [ -1, %3 ], [ %44, %43 ]
ret i32 %47
}