#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 */
typedef int /*<<< orphan*/ uint8_t;
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct TYPE_2__ {
  int /*<<< orphan*/ sa_family;
};
struct ifreq {
  TYPE_1__ ifr_addr;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strlen(char const *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, scalar_t__);
void GetIpFromDevice(uint8_t ip[4], const char DeviceName[]) {
  int fd;
  struct ifreq ifr;
  assert(strlen(DeviceName) <= IFNAMSIZ);
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  assert(fd > 0);
  strncpy(ifr.ifr_name, DeviceName, IFNAMSIZ);
  ifr.ifr_addr.sa_family = AF_INET;
  if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
    struct sockaddr_in *p = (void *)&(ifr.ifr_addr);
    memcpy(ip, &(p->sin_addr), 4);
  } else {
    memset(ip, 0x00, 4);
  }
  close(fd);
  return;
}


x86 assembly (clang, -O0):
<GetIpFromDevice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    0x2e28(%rip),%rcx
cmp    (%rcx),%rax
setle  %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2ddd(%rip),%rax
mov    (%rax),%rdx
call   1030 <strncpy@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jne    123d <GetIpFromDevice+0xbd>
lea    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1090 <memcpy@plt>
jmp    124d <GetIpFromDevice+0xcd>
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetIpFromDevice>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    0x2e27(%rip),%r15
xor    %edi,%edi
cmp    (%r15),%rax
setle  %dil
call   1080 <assert@plt>
mov    0x2e2a(%rip),%r12
mov    (%r12),%edi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
xor    %edi,%edi
test   %eax,%eax
setg   %dil
call   1080 <assert@plt>
mov    (%r15),%rdx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1060 <ioctl@plt>
test   %rax,%rax
je     1217 <GetIpFromDevice+0x97>
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
jmp    1229 <GetIpFromDevice+0xa9>
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
mov    $0x4,%edx
call   1090 <memcpy@plt>
mov    %ebp,%edi
call   1070 <close@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_in = type { i32 }
@IFNAMSIZ = global i64 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFADDR = global i32 0
define void @GetIpFromDevice(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.ifreq
%7 = alloca %struct.sockaddr_in*
store i32* %0, i32** %3
store i8* %1, i8** %4
%8 = load i8*, i8** %4
%9 = call i64 @strlen(i8* %8)
%10 = load i64, i64* @IFNAMSIZ
%11 = icmp sle i64 %9, %10
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_DGRAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = icmp sgt i32 %17, 0
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%22 = load i32, i32* %21
%23 = load i8*, i8** %4
%24 = load i64, i64* @IFNAMSIZ
%25 = call i32 @strncpy(i32 %22, i8* %23, i64 %24)
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = load i32, i32* %5
%30 = load i32, i32* @SIOCGIFADDR
%31 = call i64 @ioctl(i32 %29, i32 %30, %struct.ifreq* %6)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %41
33: ; preds = %2
%34 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%35 = bitcast %struct.TYPE_2__* %34 to i8*
%36 = bitcast i8* %35 to %struct.sockaddr_in*
store %struct.sockaddr_in* %36, %struct.sockaddr_in** %7
%37 = load i32*, i32** %3
%38 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %38, i32 0, i32 0
%40 = call i32 @memcpy(i32* %37, i32* %39, i32 4)
br label %44
41: ; preds = %2
%42 = load i32*, i32** %3
%43 = call i32 @memset(i32* %42, i32 0, i32 4)
br label %44
44: ; preds = %41, %33
%45 = load i32, i32* %5
%46 = call i32 @close(i32 %45)
ret void
}

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@IFNAMSIZ = global i64 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFADDR = global i32 0
define void @GetIpFromDevice(i32* %0, i8* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%6 = load i64, i64* @IFNAMSIZ
%7 = icmp sle i64 %5, %6
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_DGRAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp sgt i32 %12, 0
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
%16 = load i64, i64* @IFNAMSIZ
%17 = tail call i32 @strncpy(i32 undef, i8* %1, i64 %16)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* @SIOCGIFADDR
%21 = call i64 @ioctl(i32 %12, i32 %20, %struct.ifreq* %3)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = call i32 @memcpy(i32* %0, i32* %19, i32 4)
br label %27
25: ; preds = %2
%26 = call i32 @memset(i32* %0, i32 0, i32 4)
br label %27
27: ; preds = %25, %23
%28 = call i32 @close(i32 %12)
ret void
}