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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int get_ip(char *iface) {
  struct sockaddr_in *addr;
  struct ifreq req;
  int fd, ret;
  if (iface == NULL)
    return 0;
  strncpy(req.ifr_name, iface, IFNAMSIZ);
  fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd < 0)
    return 0;
  ret = ioctl(fd, SIOCGIFADDR, &req);
  close(fd);
  if (ret < 0)
    return 0;
  addr = (struct sockaddr_in *)&(req.ifr_addr);
  return addr->sin_addr.s_addr;
}


x86 assembly (clang, -O0):
<get_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1163 <get_ip+0x23>
movl   $0x0,-0x4(%rbp)
jmp    11f1 <get_ip+0xb1>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e5f(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11aa <get_ip+0x6a>
movl   $0x0,-0x4(%rbp)
jmp    11f1 <get_ip+0xb1>
mov    -0x24(%rbp),%edi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x28(%rbp)
jge    11e0 <get_ip+0xa0>
movl   $0x0,-0x4(%rbp)
jmp    11f1 <get_ip+0xb1>
lea    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ip>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %rdi,%rdi
je     11a9 <get_ip+0x69>
mov    0x2e7c(%rip),%rax
mov    (%rax),%edx
mov    %rdi,%rsi
call   1030 <strncpy@plt>
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   1060 <socket@plt>
test   %eax,%eax
js     11ab <get_ip+0x6b>
mov    %eax,%ebp
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
mov    %eax,%r14d
mov    %ebp,%edi
call   1050 <close@plt>
test   %r14d,%r14d
js     11ab <get_ip+0x6b>
mov    0x8(%rsp),%ebx
jmp    11ab <get_ip+0x6b>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@IFNAMSIZ = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SIOCGIFADDR = global i32 0
define i32 @get_ip(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = icmp eq i8* %0, null
br i1 %4, label %20, label %5
5: ; preds = %1
%6 = load i32, i32* @IFNAMSIZ
%7 = tail call i32 @strncpy(i32 undef, i8* %0, i32 %6)
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %20, label %12
12: ; preds = %5
%13 = load i32, i32* @SIOCGIFADDR
%14 = call i32 @ioctl(i32 %10, i32 %13, %struct.ifreq* %2)
%15 = call i32 @close(i32 %10)
%16 = icmp slt i32 %14, 0
br i1 %16, label %20, label %17
17: ; preds = %12
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%19 = load i32, i32* %18
br label %20
20: ; preds = %12, %5, %1, %17
%21 = phi i32 [ %19, %17 ], [ 0, %1 ], [ 0, %5 ], [ 0, %12 ]
ret i32 %21
}