#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_4__ TYPE_2__;
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ u_char;
struct TYPE_4__ {
  int s_addr;
};
struct sockaddr_in {
  TYPE_2__ sin_addr;
};
struct TYPE_3__ {
  int /*<<< orphan*/ sa_data;
};
struct ifreq {
  int /*<<< orphan*/ ifr_addr;
  TYPE_1__ ifr_hwaddr;
  int /*<<< orphan*/ ifr_name;
};
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SIOCGIFHWADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ shutdown(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
void read_iface_config(u_char *addr, int *ip, const char *iface) {
  struct ifreq ifr;
  int s;
  s = socket(PF_INET, SOCK_DGRAM, 0);
  memset(&ifr, 0, sizeof(ifr));
  strcpy(ifr.ifr_name, iface);
  ioctl(s, SIOCGIFHWADDR, &ifr);
  shutdown(s, 1);
  if (addr != NULL)
    memcpy(addr, ifr.ifr_hwaddr.sa_data, 6);
  s = socket(PF_INET, SOCK_STREAM, 0);
  ioctl(s, SIOCGIFADDR, &ifr);
  shutdown(s, 1);
  if (ip != NULL)
    *ip = ((struct sockaddr_in *)(&(ifr.ifr_addr)))->sin_addr.s_addr;
}


x86 assembly (clang, -O0):
<read_iface_config>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x2c(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x2c(%rbp),%edi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <ioctl@plt>
mov    -0x2c(%rbp),%edi
mov    $0x1,%esi
call   1040 <shutdown@plt>
cmpq   $0x0,-0x8(%rbp)
je     11ea <read_iface_config+0x8a>
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    $0x6,%edx
call   1070 <memcpy@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <ioctl@plt>
mov    -0x2c(%rbp),%edi
mov    $0x1,%esi
call   1040 <shutdown@plt>
cmpq   $0x0,-0x10(%rbp)
je     123c <read_iface_config+0xdc>
mov    -0x28(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_iface_config>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e42(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x10(%rsp),%edi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r12,%rdx
call   1060 <ioctl@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1040 <shutdown@plt>
test   %r15,%r15
je     11e9 <read_iface_config+0x89>
mov    0xc(%rsp),%esi
mov    %r15,%rdi
mov    $0x6,%edx
call   1070 <memcpy@plt>
mov    0x0(%r13),%edi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebx
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1040 <shutdown@plt>
test   %r14,%r14
je     122c <read_iface_config+0xcc>
mov    0x8(%rsp),%eax
mov    %eax,(%r14)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, %struct.TYPE_3__, i32 }
%struct.TYPE_3__ = type { i32 }
%struct.sockaddr_in = type { %struct.TYPE_4__ }
%struct.TYPE_4__ = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFHWADDR = global i32 0
@SOCK_STREAM = global i32 0
@SIOCGIFADDR = global i32 0
define void @read_iface_config(i32* %0, i32* %1, i8* %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca %struct.ifreq
%8 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i8* %2, i8** %6
%9 = load i32, i32* @PF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %8
%12 = call i32 @memset(%struct.ifreq* %7, i32 0, i32 12)
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 2
%14 = load i32, i32* %13
%15 = load i8*, i8** %6
%16 = call i32 @strcpy(i32 %14, i8* %15)
%17 = load i32, i32* %8
%18 = load i32, i32* @SIOCGIFHWADDR
%19 = call i32 @ioctl(i32 %17, i32 %18, %struct.ifreq* %7)
%20 = load i32, i32* %8
%21 = call i32 @shutdown(i32 %20, i32 1)
%22 = load i32*, i32** %4
%23 = icmp ne i32* %22, null
br i1 %23, label %24, label %30
24: ; preds = %3
%25 = load i32*, i32** %4
%26 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 1
%27 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %26, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @memcpy(i32* %25, i32 %28, i32 6)
br label %30
30: ; preds = %24, %3
%31 = load i32, i32* @PF_INET
%32 = load i32, i32* @SOCK_STREAM
%33 = call i32 @socket(i32 %31, i32 %32, i32 0)
store i32 %33, i32* %8
%34 = load i32, i32* %8
%35 = load i32, i32* @SIOCGIFADDR
%36 = call i32 @ioctl(i32 %34, i32 %35, %struct.ifreq* %7)
%37 = load i32, i32* %8
%38 = call i32 @shutdown(i32 %37, i32 1)
%39 = load i32*, i32** %5
%40 = icmp ne i32* %39, null
br i1 %40, label %41, label %48
41: ; preds = %30
%42 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%43 = bitcast i32* %42 to %struct.sockaddr_in*
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %43, i32 0, i32 0
%45 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %44, i32 0, i32 0
%46 = load i32, i32* %45
%47 = load i32*, i32** %5
store i32 %46, i32* %47
br label %48
48: ; preds = %41, %30
ret void
}

LLVM IR (-O2):
%struct.ifreq = type { i32, %struct.TYPE_3__, i32 }
%struct.TYPE_3__ = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFHWADDR = global i32 0
@SOCK_STREAM = global i32 0
@SIOCGIFADDR = global i32 0
define void @read_iface_config(i32* %0, i32* writeonly %1, i8* %2) {
%4 = alloca %struct.ifreq
%5 = bitcast %struct.ifreq* %4 to i8*
%6 = load i32, i32* @PF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = call i32 @memset(%struct.ifreq* %4, i32 0, i32 12)
%10 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 2
%11 = load i32, i32* %10
%12 = call i32 @strcpy(i32 %11, i8* %2)
%13 = load i32, i32* @SIOCGIFHWADDR
%14 = call i32 @ioctl(i32 %8, i32 %13, %struct.ifreq* %4)
%15 = call i32 @shutdown(i32 %8, i32 1)
%16 = icmp eq i32* %0, null
br i1 %16, label %21, label %17
17: ; preds = %3
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 1, i32 0
%19 = load i32, i32* %18
%20 = call i32 @memcpy(i32* %0, i32 %19, i32 6)
br label %21
21: ; preds = %17, %3
%22 = load i32, i32* @PF_INET
%23 = load i32, i32* @SOCK_STREAM
%24 = call i32 @socket(i32 %22, i32 %23, i32 0)
%25 = load i32, i32* @SIOCGIFADDR
%26 = call i32 @ioctl(i32 %24, i32 %25, %struct.ifreq* %4)
%27 = call i32 @shutdown(i32 %24, i32 1)
%28 = icmp eq i32* %1, null
br i1 %28, label %32, label %29
29: ; preds = %21
%30 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 0
%31 = load i32, i32* %30
store i32 %31, i32* %1
br label %32
32: ; preds = %29, %21
ret void
}