#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 in_addr {
  int dummy;
};
struct sockaddr_in {
  struct in_addr sin_addr;
};
struct sockaddr {
  int /*<<< orphan*/ sa_family;
};
struct ifreq {
  struct sockaddr ifr_addr;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(struct in_addr *, struct in_addr *, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int interface_ip(const char *interface, struct in_addr *ip) {
  struct ifreq request;
  struct sockaddr *socket_addr;
  struct sockaddr_in *socket_addr_in;
  struct in_addr *ip_addr;
  int sockfd;
  if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
    if (1) {
      perror("socket");
    }
    fprintf(stderr, "error: Failed to open a socket\n");
    return -1;
  }
  request.ifr_addr.sa_family = AF_INET;
  strncpy(request.ifr_name, interface, sizeof(request.ifr_name));
  if (ioctl(sockfd, SIOCGIFADDR, &request) == -1) {
    if (1) {
      perror("ioctl");
    }
    fprintf(stderr, "error: Interface does not exists\n");
    return -1;
  }
  socket_addr = &request.ifr_addr;
  socket_addr_in = (struct sockaddr_in *)socket_addr;
  ip_addr = &socket_addr_in->sin_addr;
  memcpy(ip, ip_addr, sizeof(struct in_addr));
  return 0;
}


x86 assembly (clang, -O0):
<interface_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    0x2e2f(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0xffffffff,%eax
jne    11c9 <interface_ip+0x69>
lea    0xe5d(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1262 <interface_ip+0x102>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x3c(%rbp),%edi
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1231 <interface_ip+0xd1>
lea    0xe1c(%rip),%rdi
call   1070 <perror@plt>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
lea    0xe0d(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1262 <interface_ip+0x102>
lea    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<interface_ip>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e6c(%rip),%r15
mov    (%r15),%edi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11dd <interface_ip+0x7d>
mov    %eax,%ebp
mov    (%r15),%eax
mov    %eax,(%rsp)
mov    %rbx,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11fb <interface_ip+0x9b>
mov    %rsp,%rsi
mov    %r14,%rdi
mov    $0x4,%edx
call   1060 <memcpy@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe1c(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
lea    0xe0e(%rip),%rsi
jmp    1217 <interface_ip+0xb7>
lea    0xe25(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
lea    0xe16(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    11d2 <interface_ip+0x72>

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.ifreq = type { %struct.sockaddr, i32 }
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { %struct.in_addr }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [7 x i8] c"socket\00"
@stderr = global i32 0
@.str.1 = [32 x i8] c"error: Failed to open a socket\0A\00"
@SIOCGIFADDR = global i32 0
@.str.2 = [6 x i8] c"ioctl\00"
@.str.3 = [34 x i8] c"error: Interface does not exists\0A\00"
define i32 @interface_ip(i8* %0, %struct.in_addr* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.in_addr*
%6 = alloca %struct.ifreq
%7 = alloca %struct.sockaddr*
%8 = alloca %struct.sockaddr_in*
%9 = alloca %struct.in_addr*
%10 = alloca i32
store i8* %0, i8** %4
store %struct.in_addr* %1, %struct.in_addr** %5
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_DGRAM
%13 = load i32, i32* @IPPROTO_UDP
%14 = call i32 @socket(i32 %11, i32 %12, i32 %13)
store i32 %14, i32* %10
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %45
20: ; preds = %2
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%23 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %22, i32 0, i32 0
store i32 %21, i32* %23
%24 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%25 = load i32, i32* %24
%26 = load i8*, i8** %4
%27 = call i32 @strncpy(i32 %25, i8* %26, i32 4)
%28 = load i32, i32* %10
%29 = load i32, i32* @SIOCGIFADDR
%30 = call i32 @ioctl(i32 %28, i32 %29, %struct.ifreq* %6)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %20
%33 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %45
36: ; preds = %20
%37 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
store %struct.sockaddr* %37, %struct.sockaddr** %7
%38 = load %struct.sockaddr*, %struct.sockaddr** %7
%39 = bitcast %struct.sockaddr* %38 to %struct.sockaddr_in*
store %struct.sockaddr_in* %39, %struct.sockaddr_in** %8
%40 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %40, i32 0, i32 0
store %struct.in_addr* %41, %struct.in_addr** %9
%42 = load %struct.in_addr*, %struct.in_addr** %5
%43 = load %struct.in_addr*, %struct.in_addr** %9
%44 = call i32 @memcpy(%struct.in_addr* %42, %struct.in_addr* %43, i32 4)
store i32 0, i32* %3
br label %45
45: ; preds = %36, %32, %16
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.ifreq = type { %struct.sockaddr, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [7 x i8] c"socket\00"
@stderr = global i32 0
@.str.1 = [32 x i8] c"error: Failed to open a socket\0A\00"
@SIOCGIFADDR = global i32 0
@.str.2 = [6 x i8] c"ioctl\00"
@.str.3 = [34 x i8] c"error: Interface does not exists\0A\00"
define i32 @interface_ip(i8* %0, %struct.in_addr* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = load i32, i32* @IPPROTO_UDP
%8 = tail call i32 @socket(i32 %5, i32 %6, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %28
14: ; preds = %2
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0, i32 0
store i32 %15, i32* %16
%17 = tail call i32 @strncpy(i32 undef, i8* %0, i32 4)
%18 = load i32, i32* @SIOCGIFADDR
%19 = call i32 @ioctl(i32 %8, i32 %18, %struct.ifreq* %3)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %14
%22 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
br label %28
25: ; preds = %14
%26 = bitcast %struct.ifreq* %3 to %struct.in_addr*
%27 = call i32 @memcpy(%struct.in_addr* %1, %struct.in_addr* %26, i32 4)
br label %28
28: ; preds = %25, %21, %10
%29 = phi i32 [ -1, %10 ], [ -1, %21 ], [ 0, %25 ]
ret i32 %29
}