#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 {
  char *ifr_name;
  TYPE_1__ ifr_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ close(int);
int get_sockfd();
char *inet_ntoa(int /*<<< orphan*/);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int GetIpAddressStr(char *address, char *ifname) {
  struct ifreq ifr;
  struct sockaddr_in *saddr;
  int fd;
  int succeeded = 0;
  fd = get_sockfd();
  if (fd >= 0) {
    strcpy(ifr.ifr_name, ifname);
    ifr.ifr_addr.sa_family = AF_INET;
    if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
      saddr = (struct sockaddr_in *)&ifr.ifr_addr;
      strcpy(address, inet_ntoa(saddr->sin_addr));
      succeeded = 1;
    } else {
      printf("Failure obtaining ip address of interface %s", ifname);
      succeeded = 0;
    }
    close(fd);
  }
  return succeeded;
}


x86 assembly (clang, -O0):
<GetIpAddressStr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x30(%rbp)
mov    $0x0,%al
call   1050 <get_sockfd@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jl     1219 <GetIpAddressStr+0xb9>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0x0,%rax
jne    11fa <GetIpAddressStr+0x9a>
lea    -0x20(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    -0x38(%rbp),%rdi
mov    %rax,%rsi
call   1030 <strcpy@plt>
movl   $0x1,-0x30(%rbp)
jmp    1211 <GetIpAddressStr+0xb1>
mov    -0x10(%rbp),%rsi
lea    0xdfb(%rip),%rdi
call   1060 <printf@plt>
movl   $0x0,-0x30(%rbp)
mov    -0x2c(%rbp),%edi
call   1080 <close@plt>
mov    -0x30(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetIpAddressStr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <get_sockfd@plt>
test   %eax,%eax
js     11e1 <GetIpAddressStr+0x81>
mov    %eax,%ebx
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1070 <ioctl@plt>
test   %rax,%rax
je     11c1 <GetIpAddressStr+0x61>
lea    0xe4b(%rip),%rdi
mov    %r15,%rsi
call   1060 <printf@plt>
xor    %ebp,%ebp
jmp    11da <GetIpAddressStr+0x7a>
mov    0x10(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %r14,%rdi
mov    %rax,%rsi
call   1030 <strcpy@plt>
mov    $0x1,%ebp
mov    %ebx,%edi
call   1080 <close@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i8*, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_in = type { i32 }
@AF_INET = global i32 0
@SIOCGIFADDR = global i32 0
@.str = [45 x i8] c"Failure obtaining ip address of interface %s\00"
define i32 @GetIpAddressStr(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.ifreq
%6 = alloca %struct.sockaddr_in*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %8
%9 = call i32 (...) @get_sockfd()
store i32 %9, i32* %7
%10 = load i32, i32* %7
%11 = icmp sge i32 %10, 0
br i1 %11, label %12, label %39
12: ; preds = %2
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 0
%14 = load i8*, i8** %13
%15 = load i8*, i8** %4
%16 = call i32 @strcpy(i8* %14, i8* %15)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 1
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
store i32 %17, i32* %19
%20 = load i32, i32* %7
%21 = load i32, i32* @SIOCGIFADDR
%22 = call i64 @ioctl(i32 %20, i32 %21, %struct.ifreq* %5)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %33
24: ; preds = %12
%25 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 1
%26 = bitcast %struct.TYPE_2__* %25 to %struct.sockaddr_in*
store %struct.sockaddr_in* %26, %struct.sockaddr_in** %6
%27 = load i8*, i8** %3
%28 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %28, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i8* @inet_ntoa(i32 %30)
%32 = call i32 @strcpy(i8* %27, i8* %31)
store i32 1, i32* %8
br label %36
33: ; preds = %12
%34 = load i8*, i8** %4
%35 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* %34)
store i32 0, i32* %8
br label %36
36: ; preds = %33, %24
%37 = load i32, i32* %7
%38 = call i32 @close(i32 %37)
br label %39
39: ; preds = %36, %2
%40 = load i32, i32* %8
ret i32 %40
}

LLVM IR (-O2):
%struct.ifreq = type { i8*, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SIOCGIFADDR = global i32 0
@.str = [45 x i8] c"Failure obtaining ip address of interface %s\00"
define i32 @GetIpAddressStr(i8* %0, i8* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = tail call i32 (...) @get_sockfd()
%6 = icmp sgt i32 %5, -1
br i1 %6, label %7, label %23
7: ; preds = %2
%8 = tail call i32 @strcpy(i8* undef, i8* %1)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1, i32 0
store i32 %9, i32* %10
%11 = load i32, i32* @SIOCGIFADDR
%12 = call i64 @ioctl(i32 %5, i32 %11, %struct.ifreq* %3)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %7
%15 = load i32, i32* %10
%16 = call i8* @inet_ntoa(i32 %15)
%17 = call i32 @strcpy(i8* %0, i8* %16)
br label %20
18: ; preds = %7
%19 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %20
20: ; preds = %18, %14
%21 = phi i32 [ 1, %14 ], [ 0, %18 ]
%22 = call i32 @close(i32 %5)
br label %23
23: ; preds = %20, %2
%24 = phi i32 [ %21, %20 ], [ 0, %2 ]
ret i32 %24
}