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

/* Variables and functions */
int /*<<< orphan*/ IFNAMSIZ;
scalar_t__ INADDR_NONE;
int /*<<< orphan*/ LOG_DEBUG;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
scalar_t__ inet_addr(char const *);
int /*<<< orphan*/ inet_ntoa(TYPE_1__);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *, int *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *,
                           int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, ...);
in_addr_t GetIfAddrIPv4(const char *ifaddr) {
  in_addr_t addr;
  int s;
  struct ifreq ifr;
  int ifrlen;
  addr = inet_addr(ifaddr);
  if (addr != INADDR_NONE) {
    return addr;
  }
  s = socket(PF_INET, SOCK_DGRAM, 0);
  if (s < 0) {
    syslog(LOG_ERR, "socket(PF_INET, SOCK_DGRAM): %m");
    return INADDR_NONE;
  }
  memset(&ifr, 0, sizeof(struct ifreq));
  strncpy(ifr.ifr_name, ifaddr, IFNAMSIZ);
  if (ioctl(s, SIOCGIFADDR, &ifr, &ifrlen) < 0) {
    syslog(LOG_ERR, "ioctl(s, SIOCGIFADDR, ...): %m");
    close(s);
    return INADDR_NONE;
  }
  syslog(LOG_DEBUG, "GetIfAddrIPv4(%s) = %s", ifaddr,
         inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
  addr = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
  close(s);
  return addr;
}


x86 assembly (clang, -O0):
<GetIfAddrIPv4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <inet_addr@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e3c(%rip),%rcx
cmp    (%rcx),%rax
je     11ba <GetIfAddrIPv4+0x3a>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12d0 <GetIfAddrIPv4+0x150>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    120a <GetIfAddrIPv4+0x8a>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rsi
mov    $0x0,%al
call   1090 <syslog@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    12d0 <GetIfAddrIPv4+0x150>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2da0(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
lea    -0x2c(%rbp),%rcx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1284 <GetIfAddrIPv4+0x104>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
lea    0xdbe(%rip),%rsi
mov    $0x0,%al
call   1090 <syslog@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    0x2d68(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    12d0 <GetIfAddrIPv4+0x150>
mov    0x2d2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <inet_ntoa@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %eax,%ecx
lea    0xd8e(%rip),%rsi
mov    $0x0,%al
call   1090 <syslog@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetIfAddrIPv4>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
call   1080 <inet_addr@plt>
mov    %rax,%rbp
mov    0x2e44(%rip),%r15
cmp    (%r15),%rax
jne    1282 <GetIfAddrIPv4+0x102>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1247 <GetIfAddrIPv4+0xc7>
mov    %eax,%r14d
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0xc(%rsp),%edi
mov    0x2de0(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%esi
lea    0x14(%rsp),%rcx
mov    %r14d,%edi
mov    %rbp,%rdx
call   1060 <ioctl@plt>
test   %rax,%rax
js     1260 <GetIfAddrIPv4+0xe0>
mov    0x2da1(%rip),%rax
mov    (%rax),%ebp
mov    0x8(%rsp),%rdi
call   1040 <inet_ntoa@plt>
lea    0xe15(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1090 <syslog@plt>
mov    0x8(%rsp),%rbp
mov    %r14d,%edi
call   1070 <close@plt>
jmp    1282 <GetIfAddrIPv4+0x102>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
lea    0xda9(%rip),%rsi
xor    %eax,%eax
call   1090 <syslog@plt>
jmp    127f <GetIfAddrIPv4+0xff>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
lea    0xdb0(%rip),%rsi
xor    %eax,%eax
call   1090 <syslog@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    (%r15),%rbp
mov    %rbp,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
@INADDR_NONE = global i64 0
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@LOG_ERR = global i32 0
@.str = [32 x i8] c"socket(PF_INET, SOCK_DGRAM): %m\00"
@IFNAMSIZ = global i32 0
@SIOCGIFADDR = global i32 0
@.str.1 = [31 x i8] c"ioctl(s, SIOCGIFADDR, ...): %m\00"
@LOG_DEBUG = global i32 0
@.str.2 = [23 x i8] c"GetIfAddrIPv4(%s) = %s\00"
define i64 @GetIfAddrIPv4(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca %struct.ifreq
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i64 @inet_addr(i8* %8)
store i64 %9, i64* %4
%10 = load i64, i64* %4
%11 = load i64, i64* @INADDR_NONE
%12 = icmp ne i64 %10, %11
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = load i64, i64* %4
store i64 %14, i64* %2
br label %60
15: ; preds = %1
%16 = load i32, i32* @PF_INET
%17 = load i32, i32* @SOCK_DGRAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %15
%22 = load i32, i32* @LOG_ERR
%23 = call i32 (i32, i8*, ...) @syslog(i32 %22, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%24 = load i64, i64* @INADDR_NONE
store i64 %24, i64* %2
br label %60
25: ; preds = %15
%26 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 8)
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%28 = load i32, i32* %27
%29 = load i8*, i8** %3
%30 = load i32, i32* @IFNAMSIZ
%31 = call i32 @strncpy(i32 %28, i8* %29, i32 %30)
%32 = load i32, i32* %5
%33 = load i32, i32* @SIOCGIFADDR
%34 = call i64 @ioctl(i32 %32, i32 %33, %struct.ifreq* %6, i32* %7)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %25
%37 = load i32, i32* @LOG_ERR
%38 = call i32 (i32, i8*, ...) @syslog(i32 %37, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%39 = load i32, i32* %5
%40 = call i32 @close(i32 %39)
%41 = load i64, i64* @INADDR_NONE
store i64 %41, i64* %2
br label %60
42: ; preds = %25
%43 = load i32, i32* @LOG_DEBUG
%44 = load i8*, i8** %3
%45 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%46 = bitcast i32* %45 to %struct.sockaddr_in*
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %46, i32 0, i32 0
%48 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %47, i32 0, i32 0
%49 = load i64, i64* %48
%50 = call i32 @inet_ntoa(i64 %49)
%51 = call i32 (i32, i8*, ...) @syslog(i32 %43, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %44, i32 %50)
%52 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%53 = bitcast i32* %52 to %struct.sockaddr_in*
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %53, i32 0, i32 0
%55 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %54, i32 0, i32 0
%56 = load i64, i64* %55
store i64 %56, i64* %4
%57 = load i32, i32* %5
%58 = call i32 @close(i32 %57)
%59 = load i64, i64* %4
store i64 %59, i64* %2
br label %60
60: ; preds = %42, %36, %21, %13
%61 = load i64, i64* %2
ret i64 %61
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
%struct.ifreq = type { i32, i32 }
@INADDR_NONE = global i64 0
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@LOG_ERR = global i32 0
@.str = [32 x i8] c"socket(PF_INET, SOCK_DGRAM): %m\00"
@IFNAMSIZ = global i32 0
@SIOCGIFADDR = global i32 0
@.str.1 = [31 x i8] c"ioctl(s, SIOCGIFADDR, ...): %m\00"
@LOG_DEBUG = global i32 0
@.str.2 = [23 x i8] c"GetIfAddrIPv4(%s) = %s\00"
define i64 @GetIfAddrIPv4(i8* %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to %struct.ifreq*
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = bitcast i32* %4 to i8*
%7 = tail call i64 @inet_addr(i8* %0)
%8 = load i64, i64* @INADDR_NONE
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %41
10: ; preds = %1
%11 = load i32, i32* @PF_INET
%12 = load i32, i32* @SOCK_DGRAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %10
%16 = load i32, i32* @LOG_ERR
%17 = tail call i32 (i32, i8*, ...) @syslog(i32 %16, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%18 = load i64, i64* @INADDR_NONE
br label %41
19: ; preds = %10
%20 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 8)
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%22 = load i32, i32* %21
%23 = load i32, i32* @IFNAMSIZ
%24 = call i32 @strncpy(i32 %22, i8* %0, i32 %23)
%25 = load i32, i32* @SIOCGIFADDR
%26 = call i64 @ioctl(i32 %13, i32 %25, %struct.ifreq* %3, i32* %4)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %19
%29 = load i32, i32* @LOG_ERR
%30 = call i32 (i32, i8*, ...) @syslog(i32 %29, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @close(i32 %13)
%32 = load i64, i64* @INADDR_NONE
br label %41
33: ; preds = %19
%34 = load i32, i32* @LOG_DEBUG
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
%36 = load i64, i64* %35
%37 = call i32 @inet_ntoa(i64 %36)
%38 = call i32 (i32, i8*, ...) @syslog(i32 %34, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %37)
%39 = load i64, i64* %35
%40 = call i32 @close(i32 %13)
br label %41
41: ; preds = %1, %33, %28, %15
%42 = phi i64 [ %18, %15 ], [ %32, %28 ], [ %39, %33 ], [ %7, %1 ]
ret i64 %42
}