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

/* Variables and functions */
int /*<<< orphan*/ SIOCGIFADDR;
char *inet_ntoa(int /*<<< orphan*/);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int print_if_addr(int fd, char *if_name) {
  struct sockaddr_in *ip;
  struct ifreq ifr;
  strcpy(ifr.ifr_name, if_name);
  if (ioctl(fd, SIOCGIFADDR, &ifr) < 0) {
    perror("ioctl SIOCGIFADDR error");
    return -1;
  }
  ip = (struct sockaddr_in *)&ifr.ifr_addr;
  printf(" IP : %s\n", inet_ntoa(ip->sin_addr));
  return 0;
}


x86 assembly (clang, -O0):
<print_if_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%edi
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    11a2 <print_if_addr+0x52>
lea    0xe6f(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cb <print_if_addr+0x7b>
lea    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xe59(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_if_addr>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
call   1030 <strcpy@plt>
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %rax,%rax
js     1196 <print_if_addr+0x46>
mov    0x8(%rsp),%edi
call   1040 <inet_ntoa@plt>
lea    0xe92(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe63(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
jmp    1190 <print_if_addr+0x40>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.ifreq = type { i32, i32 }
@SIOCGIFADDR = global i32 0
@.str = [24 x i8] c"ioctl SIOCGIFADDR error\00"
@.str.1 = [10 x i8] c" IP : %s\0A\00"
define i32 @print_if_addr(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.sockaddr_in*
%7 = alloca %struct.ifreq
store i32 %0, i32* %4
store i8* %1, i8** %5
%8 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 1
%9 = load i32, i32* %8
%10 = load i8*, i8** %5
%11 = call i32 @strcpy(i32 %9, i8* %10)
%12 = load i32, i32* %4
%13 = load i32, i32* @SIOCGIFADDR
%14 = call i64 @ioctl(i32 %12, i32 %13, %struct.ifreq* %7)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %26
18: ; preds = %2
%19 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%20 = bitcast i32* %19 to %struct.sockaddr_in*
store %struct.sockaddr_in* %20, %struct.sockaddr_in** %6
%21 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %21, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i8* @inet_ntoa(i32 %23)
%25 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %24)
store i32 0, i32* %3
br label %26
26: ; preds = %18, %16
%27 = load i32, i32* %3
ret i32 %27
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@SIOCGIFADDR = global i32 0
@.str = [24 x i8] c"ioctl SIOCGIFADDR error\00"
@.str.1 = [10 x i8] c" IP : %s\0A\00"
define i32 @print_if_addr(i32 %0, i8* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = tail call i32 @strcpy(i32 undef, i8* %1)
%6 = load i32, i32* @SIOCGIFADDR
%7 = call i64 @ioctl(i32 %0, i32 %6, %struct.ifreq* %3)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %16
11: ; preds = %2
%12 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%13 = load i32, i32* %12
%14 = call i8* @inet_ntoa(i32 %13)
%15 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %11, %9
%17 = phi i32 [ -1, %9 ], [ 0, %11 ]
ret i32 %17
}