#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 */

/* Type definitions */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int inet_get_local_ip(char *ip, size_t ip_len) {
  FILE *process = NULL;
  char local_ip[17];
  process = popen("/sbin/ifconfig `ip route show | grep default | awk '{print "
                  "$5}'` | grep 'inet addr' | cut -d: -f2 | awk '{print $1}'",
                  "r");
  if (process == NULL) {
    return -1;
  }
  if (fgets(local_ip, 17, process) == NULL) {
    return -2;
  }
  strncpy(ip, local_ip, 15);
  ip[strlen(local_ip) - 1] = '\0';
  if (pclose(process) == -1) {
    return -3;
  }
  return strlen(ip);
}


x86 assembly (clang, -O0):
<inet_get_local_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    0xe91(%rip),%rdi
lea    0xeff(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1196 <inet_get_local_ip+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    121b <inet_get_local_ip+0xcb>
lea    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x11,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
jne    11be <inet_get_local_ip+0x6e>
movl   $0xfffffffe,-0x4(%rbp)
jmp    121b <inet_get_local_ip+0xcb>
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    $0xf,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    -0x40(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
call   1050 <pclose@plt>
cmp    $0xffffffff,%eax
jne    120f <inet_get_local_ip+0xbf>
movl   $0xfffffffd,-0x4(%rbp)
jmp    121b <inet_get_local_ip+0xcb>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inet_get_local_ip>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%r14
lea    0xe9d(%rip),%rdi
lea    0xf0b(%rip),%rsi
call   1070 <popen@plt>
test   %rax,%rax
je     11c6 <inet_get_local_ip+0x76>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x11,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     11cd <inet_get_local_ip+0x7d>
mov    %rsp,%r15
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0xf,%edx
call   1030 <strncpy@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%r14,1)
mov    %rbx,%rdi
call   1050 <pclose@plt>
cmp    $0xffffffff,%eax
je     11d4 <inet_get_local_ip+0x84>
mov    %r14,%rdi
call   1040 <strlen@plt>
jmp    11d9 <inet_get_local_ip+0x89>
mov    $0xffffffff,%eax
jmp    11d9 <inet_get_local_ip+0x89>
mov    $0xfffffffe,%eax
jmp    11d9 <inet_get_local_ip+0x89>
mov    $0xfffffffd,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [117 x i8] c"/sbin/ifconfig `ip route show | grep default | awk '{print $5}'` | grep 'inet addr' | cut -d: -f2 | awk '{print $1}'\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @inet_get_local_ip(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca [17 x i8]
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32* null, i32** %6
%8 = call i32* @popen(i8* getelementptr inbounds ([117 x i8], [117 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %6
%9 = load i32*, i32** %6
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %35
12: ; preds = %2
%13 = getelementptr inbounds [17 x i8], [17 x i8]* %7, i64 0, i64 0
%14 = load i32*, i32** %6
%15 = call i32* @fgets(i8* %13, i32 17, i32* %14)
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %18
17: ; preds = %12
store i32 -2, i32* %3
br label %35
18: ; preds = %12
%19 = load i8*, i8** %4
%20 = getelementptr inbounds [17 x i8], [17 x i8]* %7, i64 0, i64 0
%21 = call i32 @strncpy(i8* %19, i8* %20, i32 15)
%22 = load i8*, i8** %4
%23 = getelementptr inbounds [17 x i8], [17 x i8]* %7, i64 0, i64 0
%24 = call i32 @strlen(i8* %23)
%25 = sub nsw i32 %24, 1
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %22, i64 %26
store i8 0, i8* %27
%28 = load i32*, i32** %6
%29 = call i32 @pclose(i32* %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %18
store i32 -3, i32* %3
br label %35
32: ; preds = %18
%33 = load i8*, i8** %4
%34 = call i32 @strlen(i8* %33)
store i32 %34, i32* %3
br label %35
35: ; preds = %32, %31, %17, %11
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
@.str = [117 x i8] c"/sbin/ifconfig `ip route show | grep default | awk '{print $5}'` | grep 'inet addr' | cut -d: -f2 | awk '{print $1}'\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @inet_get_local_ip(i8* %0, i64 %1) {
%3 = alloca [17 x i8]
%4 = getelementptr inbounds [17 x i8], [17 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @popen(i8* getelementptr inbounds ([117 x i8], [117 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %20, label %7
7: ; preds = %2
%8 = call i32* @fgets(i8* %4, i32 17, i32* %5)
%9 = icmp eq i32* %8, null
br i1 %9, label %20, label %10
10: ; preds = %7
%11 = call i32 @strncpy(i8* %0, i8* %4, i32 15)
%12 = call i32 @strlen(i8* %4)
%13 = add nsw i32 %12, -1
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %0, i64 %14
store i8 0, i8* %15
%16 = call i32 @pclose(i32* %5)
%17 = icmp eq i32 %16, -1
br i1 %17, label %20, label %18
18: ; preds = %10
%19 = call i32 @strlen(i8* %0)
br label %20
20: ; preds = %10, %7, %2, %18
%21 = phi i32 [ %19, %18 ], [ -1, %2 ], [ -2, %7 ], [ -3, %10 ]
ret i32 %21
}