#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 */
struct TYPE_3__ {
  unsigned long s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
};
struct in_addr {
  int dummy;
};
struct TYPE_4__ {
  int /*<<< orphan*/ sa_family;
};
struct ifreq {
  TYPE_2__ ifr_addr;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IF_NAMESIZE;
int /*<<< orphan*/ IPPROTO_IP;
int MAX(int /*<<< orphan*/, int);
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int strnlen(char *, int);
unsigned long get_ip_by_spec(char *spec) {
  int fd;
  int badlen;
  struct ifreq ifr;
  struct in_addr addr;
  badlen = MAX(IF_NAMESIZE, 16);
  if (strnlen(spec, badlen) < badlen) {
    fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
    if (fd < 0)
      return 0L;
    memset(&ifr, 0, sizeof(struct ifreq));
    strcpy(ifr.ifr_name, spec);
    ifr.ifr_addr.sa_family = AF_INET;
    if (ioctl(fd, SIOCGIFADDR, &ifr) >= 0) {
      close(fd);
      return (((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr);
    }
    close(fd);
  }
  return 0L;
}


x86 assembly (clang, -O0):
<get_ip_by_spec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
mov    $0x10,%esi
call   1040 <MAX@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1070 <strnlen@plt>
cmp    -0x18(%rbp),%eax
jge    1245 <get_ip_by_spec+0xd5>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
mov    0x2e18(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11e1 <get_ip_by_spec+0x71>
movq   $0x0,-0x8(%rbp)
jmp    124d <get_ip_by_spec+0xdd>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jl     123d <get_ip_by_spec+0xcd>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    124d <get_ip_by_spec+0xdd>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ip_by_spec>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    $0x10,%esi
call   1040 <MAX@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %eax,%esi
call   1070 <strnlen@plt>
cmp    %ebp,%eax
jge    121e <get_ip_by_spec+0xae>
mov    0x2e37(%rip),%r12
mov    (%r12),%edi
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
test   %eax,%eax
js     121e <get_ip_by_spec+0xae>
mov    %eax,%ebp
lea    0x8(%rsp),%r15
xor    %r14d,%r14d
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r15,%rdx
call   1060 <ioctl@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1080 <close@plt>
test   %rbx,%rbx
js     1221 <get_ip_by_spec+0xb1>
mov    0x8(%rsp),%r14
jmp    1221 <get_ip_by_spec+0xb1>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_3__ }
%struct.TYPE_3__ = type { i64 }
%struct.ifreq = type { %struct.TYPE_4__, i32 }
%struct.TYPE_4__ = type { i32 }
@IF_NAMESIZE = global i32 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_IP = global i32 0
@SIOCGIFADDR = global i32 0
define i64 @get_ip_by_spec(i8* %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to %struct.ifreq*
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = load i32, i32* @IF_NAMESIZE
%6 = tail call i32 @MAX(i32 %5, i32 16)
%7 = tail call i32 @strnlen(i8* %0, i32 %6)
%8 = icmp slt i32 %7, %6
br i1 %8, label %9, label %29
9: ; preds = %1
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_DGRAM
%12 = load i32, i32* @IPPROTO_IP
%13 = tail call i32 @socket(i32 %10, i32 %11, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %29, label %15
15: ; preds = %9
%16 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 8)
%17 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%18 = load i32, i32* %17
%19 = call i32 @strcpy(i32 %18, i8* %0)
%20 = load i32, i32* @AF_INET
%21 = bitcast %struct.sockaddr_in* %2 to i32*
store i32 %20, i32* %21
%22 = load i32, i32* @SIOCGIFADDR
%23 = call i64 @ioctl(i32 %13, i32 %22, %struct.ifreq* %3)
%24 = icmp sgt i64 %23, -1
%25 = call i32 @close(i32 %13)
br i1 %24, label %26, label %29
26: ; preds = %15
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
%28 = load i64, i64* %27
br label %29
29: ; preds = %15, %1, %9, %26
%30 = phi i64 [ %28, %26 ], [ 0, %9 ], [ 0, %1 ], [ 0, %15 ]
ret i64 %30
}