#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int errno;
char *inet_ntoa(int /*<<< orphan*/);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(scalar_t__ *, char *, size_t);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int);
size_t strlen(char *);
void main() {
  struct ifreq ifr;
  char *if_name = "eth0";
  size_t if_name_len = strlen(if_name);
  if (if_name_len < sizeof(ifr.ifr_name)) {
    memcpy(ifr.ifr_name, if_name, if_name_len);
    ifr.ifr_name[if_name_len] = 0;
  } else {
    printf("interface name is too long");
  }
  int fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd == -1) {
    printf("%s", strerror(errno));
  }
  if (ioctl(fd, SIOCGIFADDR, &ifr) == -1) {
    int temp_errno = errno;
    close(fd);
    printf("%s", strerror(temp_errno));
  }
  close(fd);
  struct sockaddr_in *ipaddr = (struct sockaddr_in *)&ifr.ifr_addr;
  printf("IP address: %s\n", inet_ntoa(ipaddr->sin_addr));
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xe71(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x8,-0x20(%rbp)
jae    11d1 <main+0x51>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1080 <memcpy@plt>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
jmp    11df <main+0x5f>
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1224 <main+0xa4>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x24(%rbp),%edi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    126f <main+0xef>
mov    0x2d7f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
mov    -0x28(%rbp),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
lea    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xd8f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xee8(%rip),%rsi
mov    $0x4,%edx
call   1030 <memcpy@plt>

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

LLVM IR (-O2):
@.str = [5 x i8] c"eth0\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@errno = global i32 0
@SIOCGIFADDR = global i32 0
define void @main() {
%1 = tail call i32 @memcpy(i64* undef, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 4)
unreachable
}