#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_4__ TYPE_2__;
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_4__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_2__ sin_addr;
};
struct TYPE_3__ {
  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*/ IPPROTO_UDP;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
char *inet_ntoa(TYPE_2__);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, scalar_t__);
char *getMyIpv4(char *iface) {
  struct ifreq ifr;
  struct sockaddr_in thisHostAddr;
  int tmpSock;
  if (((tmpSock) = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
    perror("socket tmpSocket: ");
    fprintf(stderr, "errno: %d\n", errno);
    return NULL;
  }
  ifr.ifr_addr.sa_family = AF_INET;
  strncpy(ifr.ifr_name, iface, IFNAMSIZ - 1);
  ioctl(tmpSock, SIOCGIFADDR, &ifr);
  thisHostAddr.sin_addr.s_addr =
      ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
  if (close(tmpSock) < 0) {
    perror("close tmpSocket: ");
    fprintf(stderr, "errno: %d\n", errno);
  }
  return inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);
}


x86 assembly (clang, -O0):
<getMyIpv4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
mov    0x2e13(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11df <getMyIpv4+0x6f>
lea    0xe51(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%edx
lea    0xe46(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1268 <getMyIpv4+0xf8>
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2dc7(%rip),%rax
mov    (%rax),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1050 <ioctl@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
jge    125c <getMyIpv4+0xec>
lea    0xde5(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
mov    0x2d6a(%rip),%rax
mov    (%rax),%edx
lea    0xdbc(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getMyIpv4>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e5d(%rip),%r15
mov    (%r15),%edi
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
test   %eax,%eax
js     11f4 <getMyIpv4+0x84>
mov    %eax,%ebx
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%rdx
add    $0xffffffffffffffff,%rdx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1050 <ioctl@plt>
mov    %ebx,%edi
call   1060 <close@plt>
test   %rax,%rax
js     1222 <getMyIpv4+0xb2>
mov    0x8(%rsp),%edi
call   1040 <inet_ntoa@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe05(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
mov    0x2da8(%rip),%rax
mov    (%rax),%edx
lea    0xdfa(%rip),%rsi
call   1070 <fprintf@plt>
xor    %eax,%eax
jmp    11ea <getMyIpv4+0x7a>
lea    0xdf5(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
mov    0x2d7a(%rip),%rax
mov    (%rax),%edx
lea    0xdcc(%rip),%rsi
call   1070 <fprintf@plt>
jmp    11e1 <getMyIpv4+0x71>

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_3__, i32 }
%struct.TYPE_3__ = type { i32 }
%struct.sockaddr_in = type { %struct.TYPE_4__ }
%struct.TYPE_4__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [19 x i8] c"socket tmpSocket: \00"
@stderr = global i32 0
@.str.1 = [11 x i8] c"errno: %d\0A\00"
@errno = global i32 0
@IFNAMSIZ = global i64 0
@SIOCGIFADDR = global i32 0
@.str.2 = [18 x i8] c"close tmpSocket: \00"
define i8* @getMyIpv4(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.ifreq
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = load i32, i32* @IPPROTO_UDP
%10 = call i32 @socket(i32 %7, i32 %8, i32 %9)
store i32 %10, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %15)
store i8* null, i8** %2
br label %52
17: ; preds = %1
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
%20 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %19, i32 0, i32 0
store i32 %18, i32* %20
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%22 = load i32, i32* %21
%23 = load i8*, i8** %3
%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* %6
%28 = load i32, i32* @SIOCGIFADDR
%29 = call i32 @ioctl(i32 %27, i32 %28, %struct.ifreq* %4)
%30 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
%31 = bitcast %struct.TYPE_3__* %30 to %struct.sockaddr_in*
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %31, i32 0, i32 0
%33 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %32, i32 0, i32 0
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%36 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %35, i32 0, i32 0
store i32 %34, i32* %36
%37 = load i32, i32* %6
%38 = call i64 @close(i32 %37)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %17
%41 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%42 = load i32, i32* @stderr
%43 = load i32, i32* @errno
%44 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %43)
br label %45
45: ; preds = %40, %17
%46 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
%47 = bitcast %struct.TYPE_3__* %46 to %struct.sockaddr_in*
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %47, i32 0, i32 0
%49 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %48, i32 0, i32 0
%50 = load i32, i32* %49
%51 = call i8* @inet_ntoa(i32 %50)
store i8* %51, i8** %2
br label %52
52: ; preds = %45, %12
%53 = load i8*, i8** %2
ret i8* %53
}

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_3__, i32 }
%struct.TYPE_3__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [19 x i8] c"socket tmpSocket: \00"
@stderr = global i32 0
@.str.1 = [11 x i8] c"errno: %d\0A\00"
@errno = global i32 0
@IFNAMSIZ = global i64 0
@SIOCGIFADDR = global i32 0
@.str.2 = [18 x i8] c"close tmpSocket: \00"
define i8* @getMyIpv4(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_DGRAM
%6 = load i32, i32* @IPPROTO_UDP
%7 = tail call i32 @socket(i32 %4, i32 %5, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %1
%10 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %12)
br label %32
14: ; preds = %1
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0, i32 0
store i32 %15, i32* %16
%17 = load i64, i64* @IFNAMSIZ
%18 = add nsw i64 %17, -1
%19 = tail call i32 @strncpy(i32 undef, i8* %0, i64 %18)
%20 = load i32, i32* @SIOCGIFADDR
%21 = call i32 @ioctl(i32 %7, i32 %20, %struct.ifreq* %2)
%22 = call i64 @close(i32 %7)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %14
%25 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %24, %14
%30 = load i32, i32* %16
%31 = call i8* @inet_ntoa(i32 %30)
br label %32
32: ; preds = %29, %9
%33 = phi i8* [ null, %9 ], [ %31, %29 ]
ret i8* %33
}