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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, scalar_t__);
int get_local_ip(char *ip) {
  int sockfd;
  struct ifreq ifr;
  struct sockaddr_in *sa = (struct sockaddr_in *)&ifr.ifr_addr;
  if (0 > (sockfd = socket(AF_INET, SOCK_STREAM, 0)))
    return 0;
  memset(&ifr, 0, sizeof(ifr));
  strncpy(ifr.ifr_name, "eth0", IFNAMSIZ - 1);
  sa->sin_family = AF_INET;
  if (!ioctl(sockfd, SIOCGIFADDR, &ifr)) {
    strcpy(ip, inet_ntoa(sa->sin_addr));
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<get_local_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x20(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jle    11ac <get_local_ip+0x4c>
movl   $0x0,-0x4(%rbp)
jmp    1238 <get_local_ip+0xd8>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    -0x20(%rbp),%edi
mov    0x2e0a(%rip),%rax
mov    (%rax),%rdx
sub    $0x1,%rdx
lea    0xe2c(%rip),%rsi
call   1030 <strncpy@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x14(%rbp),%edi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0x0,%eax
jne    1231 <get_local_ip+0xd1>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1050 <inet_ntoa@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%esi
call   1040 <strcpy@plt>
movl   $0x1,-0x4(%rbp)
jmp    1238 <get_local_ip+0xd8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_local_ip>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e6a(%rip),%r12
mov    (%r12),%edi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11fd <get_local_ip+0x9d>
mov    %eax,%ebx
lea    0x8(%rsp),%r15
xor    %ebp,%ebp
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x8(%rsp),%edi
mov    0x2e1d(%rip),%rax
mov    (%rax),%rdx
add    $0xffffffffffffffff,%rdx
lea    0xe3f(%rip),%rsi
call   1030 <strncpy@plt>
mov    (%r12),%eax
mov    %eax,0x10(%rsp)
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r15,%rdx
call   1070 <ioctl@plt>
test   %eax,%eax
jne    11fd <get_local_ip+0x9d>
mov    0xc(%rsp),%edi
call   1050 <inet_ntoa@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1040 <strcpy@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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