#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*/);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, scalar_t__);
int main(void) {
  char interface_name[256];
  struct ifreq sifr;
  int soc;
  char *ip_str;
  struct sockaddr_in *addr_in;
  printf("interface_name: ");
  scanf("%s", interface_name);
  soc = socket(AF_INET, SOCK_DGRAM, 0);
  if (soc == -1) {
    perror("socket error!");
    return -1;
  }
  sifr.ifr_addr.sa_family = AF_INET;
  strncpy(sifr.ifr_name, interface_name, IFNAMSIZ - 1);
  if (ioctl(soc, SIOCGIFADDR, &sifr) < 0) {
    perror("ioctl error!");
    close(soc);
    return -2;
  }
  addr_in = (struct sockaddr_in *)&sifr.ifr_addr;
  ip_str = inet_ntoa(addr_in->sin_addr);
  printf("%s ip address = %s\n", interface_name, ip_str);
  close(soc);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x110(%rbp),%rsi
lea    0xe63(%rip),%rdi
call   1080 <scanf@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x11c(%rbp)
cmpl   $0xffffffff,-0x11c(%rbp)
jne    11f7 <main+0x77>
lea    0xe2e(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bf <main+0x13f>
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x118(%rbp)
mov    -0x114(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    0x2dae(%rip),%rax
mov    (%rax),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x11c(%rbp),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
lea    -0x118(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    126e <main+0xee>
lea    0xdd0(%rip),%rdi
call   1090 <perror@plt>
mov    -0x11c(%rbp),%edi
call   1070 <close@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12bf <main+0x13f>
lea    -0x118(%rbp),%rax
mov    %rax,-0x130(%rbp)
mov    -0x130(%rbp),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x128(%rbp)
lea    -0x110(%rbp),%rsi
mov    -0x128(%rbp),%rdx
lea    0xd89(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x11c(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x118,%rsp
lea    0xe70(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe73(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1080 <scanf@plt>
mov    0x2e31(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1236 <main+0xb6>
mov    %eax,%ebx
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    0x2df1(%rip),%rax
mov    (%rax),%rdx
add    $0xffffffffffffffff,%rdx
lea    0x10(%rsp),%rsi
call   1030 <strncpy@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %rax,%rax
js     1249 <main+0xc9>
mov    0x8(%rsp),%edi
call   1040 <inet_ntoa@plt>
lea    0xe1d(%rip),%rdi
xor    %ebp,%ebp
lea    0x10(%rsp),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    %ebp,%eax
add    $0x118,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdd7(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebp
jmp    122a <main+0xaa>
lea    0xdd2(%rip),%rdi
call   1090 <perror@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0xfffffffe,%ebp
jmp    122a <main+0xaa>

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

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