#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;
  scalar_t__ *ifr_name;
};
typedef int /*<<< orphan*/ sin;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
char *inet_ntoa(int /*<<< orphan*/);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(struct sockaddr_in *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(scalar_t__ *, char const *, int);
int get_eth_ip(const char *eth_inf, char *ip) {
  int sd;
  struct sockaddr_in sin;
  struct ifreq ifr;
  sd = socket(AF_INET, SOCK_DGRAM, 0);
  if (-1 == sd) {
    printf("socket error: %s\n", strerror(errno));
    return -1;
  }
  strncpy(ifr.ifr_name, eth_inf, IFNAMSIZ);
  ifr.ifr_name[IFNAMSIZ - 1] = 0;
  if (ioctl(sd, SIOCGIFADDR, &ifr) < 0) {
    printf("ioctl error: %s\n", strerror(errno));
    close(sd);
    return -1;
  }
  memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
  snprintf(ip, 16, "%s", inet_ntoa(sin.sin_addr));
  close(sd);
  return 0;
}


x86 assembly (clang, -O0):
<get_eth_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x1c(%rbp)
mov    $0xffffffff,%eax
cmp    -0x1c(%rbp),%eax
jne    11f3 <get_eth_ip+0x63>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xe1e(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bd <get_eth_ip+0x12d>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2dc6(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
mov    0x2db4(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x1c(%rbp),%edi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0x0,%rax
jge    1274 <get_eth_ip+0xe4>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xdb7(%rip),%rdi
call   1050 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bd <get_eth_ip+0x12d>
lea    -0x30(%rbp),%rsi
lea    -0x20(%rbp),%rdi
mov    $0x4,%edx
call   1090 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    -0x38(%rbp),%rdi
mov    %rax,%rcx
mov    $0x10,%esi
lea    0xd7a(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_eth_ip>:
push   %rbx
mov    %rdi,%rbx
mov    0x2e9d(%rip),%rax
mov    (%rax),%edi
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
cmp    $0xffffffff,%eax
jne    1186 <get_eth_ip+0x46>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe89(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret
mov    0x2e43(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rsi
call   1030 <strncpy@plt>

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

LLVM IR (-O2):
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [18 x i8] c"socket error: %s\0A\00"
@errno = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFADDR = global i32 0
define i32 @get_eth_ip(i8* %0, i8* %1) {
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_DGRAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = load i32, i32* @errno
%9 = tail call i8* @strerror(i32 %8)
%10 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %9)
ret i32 -1
11: ; preds = %2
%12 = load i32, i32* @IFNAMSIZ
%13 = tail call i32 @strncpy(i64* undef, i8* %0, i32 %12)
unreachable
}