#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct TYPE_2__ {
  int /*<<< orphan*/ sa_family;
};
struct ifreq {
  TYPE_1__ ifr_addr;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
char *inet_ntoa(int /*<<< orphan*/);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, scalar_t__);
int main() {
  int fd;
  struct ifreq ifr;
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  ifr.ifr_addr.sa_family = AF_INET;
  strncpy(ifr.ifr_name, "eth0", IFNAMSIZ - 1);
  int ret = ioctl(fd, SIOCGIFADDR, &ifr);
  if (ret < 0) {
    perror("ioctl");
  } else {
    printf("%s\n", inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e17(%rip),%rax
mov    (%rax),%rdx
sub    $0x1,%rdx
lea    0xe41(%rip),%rsi
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%edi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11f7 <main+0x87>
lea    0xe18(%rip),%rdi
call   1080 <perror@plt>
jmp    120e <main+0x9e>
mov    -0x10(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xe02(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e65(%rip),%r14
mov    (%r14),%edi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,(%rsp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%rdx
add    $0xffffffffffffffff,%rdx
lea    0xe55(%rip),%rsi
call   1030 <strncpy@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %eax,%eax
js     11ef <main+0x7f>
mov    (%rsp),%edi
call   1040 <inet_ntoa@plt>
lea    0xe35(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe0f(%rip),%rdi
call   1080 <perror@plt>
jmp    11de <main+0x6e>

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_in = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [5 x i8] c"eth0\00"
@IFNAMSIZ = global i64 0
@SIOCGIFADDR = global i32 0
@.str.1 = [6 x i8] c"ioctl\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.ifreq
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %2
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i32 0, i32 0
%10 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %9, i32 0, i32 0
store i32 %8, i32* %10
%11 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i32 0, i32 1
%12 = load i32, i32* %11
%13 = load i64, i64* @IFNAMSIZ
%14 = sub 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* %2
%17 = load i32, i32* @SIOCGIFADDR
%18 = call i32 @ioctl(i32 %16, i32 %17, %struct.ifreq* %3)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %0
%22 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %30
23: ; preds = %0
%24 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i32 0, i32 0
%25 = bitcast %struct.TYPE_2__* %24 to %struct.sockaddr_in*
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %25, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i8* @inet_ntoa(i32 %27)
%29 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %28)
br label %30
30: ; preds = %23, %21
%31 = load i32, i32* %2
%32 = call i32 @close(i32 %31)
ret i32 0
}

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