#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*/ cmd;
typedef int /*<<< orphan*/ buff;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ snprintf(char *, int const, char *, char const *);
int localip(const char *interface, char *ip, const int ip_size) {
  char cmd[256], buff[64] = "";
  FILE *fd;
  snprintf(cmd, sizeof(cmd),
           "ifconfig %s | grep 'inet addr'"
           "| awk '{print $2}' | awk -F: '{printf $2}'",
           interface);
  fd = popen(cmd, "r");
  if (fd == NULL) {
    pclose(fd);
    return -1;
  }
  if (fgets(buff, sizeof(buff), fd) == NULL) {
    pclose(fd);
    return -1;
  }
  snprintf(ip, ip_size, "%s", buff);
  pclose(fd);
  return 0;
}


x86 assembly (clang, -O0):
<localip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x170,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    -0x160(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1050 <memset@plt>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x100,%esi
lea    0xe70(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x120(%rbp),%rdi
lea    0xea6(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x168(%rbp)
cmpq   $0x0,-0x168(%rbp)
jne    11d5 <localip+0x85>
mov    -0x168(%rbp),%rdi
call   1030 <pclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123c <localip+0xec>
lea    -0x160(%rbp),%rdi
mov    -0x168(%rbp),%rdx
mov    $0x40,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
jne    120f <localip+0xbf>
mov    -0x168(%rbp),%rdi
call   1030 <pclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123c <localip+0xec>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
lea    -0x160(%rbp),%rcx
lea    0xe27(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x168(%rbp),%rdi
call   1030 <pclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x170,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<localip>:
push   %rbp
push   %r14
push   %rbx
sub    $0x140,%rsp
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,%rcx
xorps  %xmm0,%xmm0
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
lea    0xe8f(%rip),%rdx
lea    0x40(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x100,%esi
call   1040 <snprintf@plt>
lea    0xebf(%rip),%rsi
mov    %rbp,%rdi
call   1060 <popen@plt>
test   %rax,%rax
je     11d0 <localip+0x90>
mov    %rax,%rbp
mov    %rsp,%rdi
mov    $0x40,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11d4 <localip+0x94>
lea    0xe95(%rip),%rdx
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <snprintf@plt>
mov    %rbp,%rdi
call   1030 <pclose@plt>
xor    %eax,%eax
jmp    11e1 <localip+0xa1>
xor    %edi,%edi
jmp    11d7 <localip+0x97>
mov    %rbp,%rdi
call   1030 <pclose@plt>
mov    $0xffffffff,%eax
add    $0x140,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [73 x i8] c"ifconfig %s | grep 'inet addr'| awk '{print $2}' | awk -F: '{printf $2}'\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%s\00"
define i32 @localip(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [256 x i8]
%9 = alloca [64 x i8]
%10 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = bitcast [64 x i8]* %9 to i8*
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%13 = load i8*, i8** %5
%14 = call i32 @snprintf(i8* %12, i32 256, i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%16 = call i32* @popen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %16, i32** %10
%17 = load i32*, i32** %10
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %3
%20 = load i32*, i32** %10
%21 = call i32 @pclose(i32* %20)
store i32 -1, i32* %4
br label %37
22: ; preds = %3
%23 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%24 = load i32*, i32** %10
%25 = call i32* @fgets(i8* %23, i32 64, i32* %24)
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = load i32*, i32** %10
%29 = call i32 @pclose(i32* %28)
store i32 -1, i32* %4
br label %37
30: ; preds = %22
%31 = load i8*, i8** %6
%32 = load i32, i32* %7
%33 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%34 = call i32 @snprintf(i8* %31, i32 %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %33)
%35 = load i32*, i32** %10
%36 = call i32 @pclose(i32* %35)
store i32 0, i32* %4
br label %37
37: ; preds = %30, %27, %19
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
@.str = [73 x i8] c"ifconfig %s | grep 'inet addr'| awk '{print $2}' | awk -F: '{printf $2}'\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%s\00"
define i32 @localip(i8* %0, i8* %1, i32 %2) {
%4 = alloca [256 x i8]
%5 = alloca [64 x i8]
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%8 = call i32 @snprintf(i8* %6, i32 256, i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str, i64 0, i64 0), i8* %0)
%9 = call i32* @popen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = call i32 @pclose(i32* null)
br label %21
13: ; preds = %3
%14 = call i32* @fgets(i8* %7, i32 64, i32* %9)
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @pclose(i32* %9)
br label %21
18: ; preds = %13
%19 = call i32 @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %7)
%20 = call i32 @pclose(i32* %9)
br label %21
21: ; preds = %18, %16, %11
%22 = phi i32 [ -1, %11 ], [ -1, %16 ], [ 0, %18 ]
ret i32 %22
}