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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
char *inet_ntoa(int /*<<< orphan*/);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int get_local_ip(char *ifc, char *ip) {
  if (ip == NULL) {
    return -1;
  }
  int sock_get_ip;
  char ipaddr[50];
  struct sockaddr_in *sin;
  struct ifreq ifr_ip;
  if ((sock_get_ip = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    printf("socket create failse...GetLocalIp!\n");
    return -1;
  }
  memset(&ifr_ip, 0, sizeof(ifr_ip));
  strncpy(ifr_ip.ifr_name, ifc, sizeof(ifr_ip.ifr_name) - 1);
  if (ioctl(sock_get_ip, SIOCGIFADDR, &ifr_ip) < 0) {
    return -1;
  }
  sin = (struct sockaddr_in *)&ifr_ip.ifr_addr;
  strcpy(ipaddr, inet_ntoa(sin->sin_addr));
  printf("local ip:%s\n", ipaddr);
  close(sock_get_ip);
  strcpy(ip, ipaddr);
  return 0;
}


x86 assembly (clang, -O0):
<get_local_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a7 <get_local_ip+0x27>
movl   $0xffffffff,-0x4(%rbp)
jmp    1287 <get_local_ip+0x107>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    11e6 <get_local_ip+0x66>
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1287 <get_local_ip+0x107>
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    -0x5c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
lea    -0x60(%rbp),%rdx
call   1080 <ioctl@plt>
cmp    $0x0,%rax
jge    1232 <get_local_ip+0xb2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1287 <get_local_ip+0x107>
lea    -0x60(%rbp),%rax
mov    %rax,-0x58(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x58(%rbp),%rax
mov    (%rax),%edi
call   1050 <inet_ntoa@plt>
mov    -0x68(%rbp),%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
lea    -0x50(%rbp),%rsi
lea    0xdc0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1090 <close@plt>
mov    -0x18(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1040 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_local_ip>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x50,%rsp
test   %rsi,%rsi
je     1259 <get_local_ip+0xc9>
mov    %rsi,%r14
mov    %rdi,%rbp
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     124d <get_local_ip+0xbd>
mov    %eax,%ebx
lea    0x8(%rsp),%r12
xor    %r15d,%r15d
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1080 <memset@plt>
mov    0xc(%rsp),%edi
mov    %rbp,%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r12,%rdx
call   1090 <ioctl@plt>
test   %rax,%rax
js     1259 <get_local_ip+0xc9>
mov    0x8(%rsp),%edi
call   1060 <inet_ntoa@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
lea    0xdd1(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebx,%edi
call   10a0 <close@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
call   1040 <strcpy@plt>
jmp    125f <get_local_ip+0xcf>
lea    0xdb9(%rip),%rdi
call   1050 <puts@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [36 x i8] c"socket create failse...GetLocalIp!\0A\00"
@SIOCGIFADDR = global i32 0
@.str.1 = [13 x i8] c"local ip:%s\0A\00"
define i32 @get_local_ip(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [50 x i8]
%8 = alloca %struct.sockaddr_in*
%9 = alloca %struct.ifreq
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %5
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 -1, i32* %3
br label %47
13: ; preds = %2
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %6
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %47
20: ; preds = %13
%21 = call i32 @memset(%struct.ifreq* %9, i32 0, i32 8)
%22 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 1
%23 = load i32, i32* %22
%24 = load i8*, i8** %4
%25 = call i32 @strncpy(i32 %23, i8* %24, i32 3)
%26 = load i32, i32* %6
%27 = load i32, i32* @SIOCGIFADDR
%28 = call i64 @ioctl(i32 %26, i32 %27, %struct.ifreq* %9)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %20
store i32 -1, i32* %3
br label %47
31: ; preds = %20
%32 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 0
%33 = bitcast i32* %32 to %struct.sockaddr_in*
store %struct.sockaddr_in* %33, %struct.sockaddr_in** %8
%34 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
%35 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i8* @inet_ntoa(i32 %37)
%39 = call i32 @strcpy(i8* %34, i8* %38)
%40 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %40)
%42 = load i32, i32* %6
%43 = call i32 @close(i32 %42)
%44 = load i8*, i8** %5
%45 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
%46 = call i32 @strcpy(i8* %44, i8* %45)
store i32 0, i32* %3
br label %47
47: ; preds = %31, %30, %18, %12
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SIOCGIFADDR = global i32 0
@.str.1 = [13 x i8] c"local ip:%s\0A\00"
@str = [35 x i8] c"socket create failse...GetLocalIp!\00"
define i32 @get_local_ip(i8* %0, i8* %1) {
%3 = alloca [50 x i8]
%4 = alloca %struct.ifreq
%5 = icmp eq i8* %1, null
br i1 %5, label %33, label %6
6: ; preds = %2
%7 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%8 = bitcast %struct.ifreq* %4 to i8*
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %6
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
br label %31
15: ; preds = %6
%16 = call i32 @memset(%struct.ifreq* %4, i32 0, i32 8)
%17 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 1
%18 = load i32, i32* %17
%19 = call i32 @strncpy(i32 %18, i8* %0, i32 3)
%20 = load i32, i32* @SIOCGIFADDR
%21 = call i64 @ioctl(i32 %11, i32 %20, %struct.ifreq* %4)
%22 = icmp slt i64 %21, 0
br i1 %22, label %31, label %23
23: ; preds = %15
%24 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 0
%25 = load i32, i32* %24
%26 = call i8* @inet_ntoa(i32 %25)
%27 = call i32 @strcpy(i8* %7, i8* %26)
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%29 = call i32 @close(i32 %11)
%30 = call i32 @strcpy(i8* %1, i8* %7)
br label %31
31: ; preds = %15, %23, %13
%32 = phi i32 [ -1, %13 ], [ 0, %23 ], [ -1, %15 ]
br label %33
33: ; preds = %2, %31
%34 = phi i32 [ %32, %31 ], [ -1, %2 ]
ret i32 %34
}