#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 {
  char *ifr_name;
  int /*<<< orphan*/ ifr_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_DGRAM;
char *inet_ntoa(int /*<<< orphan*/);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, scalar_t__);
int ip_addr(char *p_str) {
  int inet_sock;
  struct ifreq ifr;
  inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
  strncpy(ifr.ifr_name, "eth0", strlen("eth0") + 1);
  if (ioctl(inet_sock, SIOCGIFADDR, &ifr) < 0) {
    perror("ioctl");
    p_str = NULL;
    return -1;
  }
  char *p_ipstr = inet_ntoa(((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr);
  strncpy(p_str, p_ipstr, strlen(p_ipstr) + 1);
  return 0;
}


x86 assembly (clang, -O0):
<ip_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    0xe69(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    %rax,%rdx
add    $0x1,%rdx
lea    0xe52(%rip),%rsi
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    11f2 <ip_addr+0x92>
lea    0xe2c(%rip),%rdi
call   1070 <perror@plt>
movq   $0x0,-0x10(%rbp)
movl   $0xffffffff,-0x4(%rbp)
jmp    1232 <ip_addr+0xd2>
mov    -0x20(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <strncpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ip_addr>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
xor    %r14d,%r14d
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebp
lea    0xe70(%rip),%rsi
mov    $0x5,%edx
call   1030 <strncpy@plt>
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1060 <ioctl@plt>
test   %rax,%rax
js     11e1 <ip_addr+0x81>
mov    0x8(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1030 <strncpy@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe1d(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%r14d
jmp    11d5 <ip_addr+0x75>

LLVM IR (-O0):
%struct.ifreq = type { i8*, i32 }
%struct.sockaddr_in = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [5 x i8] c"eth0\00"
@SIOCGIFADDR = global i32 0
@.str.1 = [6 x i8] c"ioctl\00"
define i32 @ip_addr(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.ifreq
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %4
%10 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 0
%11 = load i8*, i8** %10
%12 = call i64 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%13 = add nsw i64 %12, 1
%14 = call i32 @strncpy(i8* %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 %13)
%15 = load i32, i32* %4
%16 = load i32, i32* @SIOCGIFADDR
%17 = call i64 @ioctl(i32 %15, i32 %16, %struct.ifreq* %5)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %1
%20 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %3
store i32 -1, i32* %2
br label %33
21: ; preds = %1
%22 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 1
%23 = bitcast i32* %22 to %struct.sockaddr_in*
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i8* @inet_ntoa(i32 %25)
store i8* %26, i8** %6
%27 = load i8*, i8** %3
%28 = load i8*, i8** %6
%29 = load i8*, i8** %6
%30 = call i64 @strlen(i8* %29)
%31 = add nsw i64 %30, 1
%32 = call i32 @strncpy(i8* %27, i8* %28, i64 %31)
store i32 0, i32* %2
br label %33
33: ; preds = %21, %19
%34 = load i32, i32* %2
ret i32 %34
}

LLVM IR (-O2):
%struct.ifreq = type { i8*, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [5 x i8] c"eth0\00"
@SIOCGIFADDR = global i32 0
@.str.1 = [6 x i8] c"ioctl\00"
define i32 @ip_addr(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_DGRAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = tail call i32 @strncpy(i8* undef, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 5)
%8 = load i32, i32* @SIOCGIFADDR
%9 = call i64 @ioctl(i32 %6, i32 %8, %struct.ifreq* %2)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %20
13: ; preds = %1
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%15 = load i32, i32* %14
%16 = call i8* @inet_ntoa(i32 %15)
%17 = call i64 @strlen(i8* dereferenceable(1) %16)
%18 = add nsw i64 %17, 1
%19 = call i32 @strncpy(i8* %0, i8* %16, i64 %18)
br label %20
20: ; preds = %13, %11
%21 = phi i32 [ -1, %11 ], [ 0, %13 ]
ret i32 %21
}