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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_DGRAM;
char *inet_ntoa(int /*<<< orphan*/);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(struct sockaddr_in *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(scalar_t__ *, char *, int);
char *get_ip(void) {
  int sock;
  struct sockaddr_in sin;
  struct ifreq ifr;
  sock = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock == -1) {
    perror("socket");
    return 0;
  }
  strncpy(ifr.ifr_name, "ens33", IFNAMSIZ);
  ifr.ifr_name[IFNAMSIZ - 1] = 0;
  if (ioctl(sock, SIOCGIFADDR, &ifr) < 0) {
    perror("ioctl");
    return 0;
  }
  memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
  if (inet_ntoa(sin.sin_addr))
    return inet_ntoa(sin.sin_addr);
  return "127.0.0.1";
}


x86 assembly (clang, -O0):
<get_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11a7 <get_ip+0x47>
lea    0xe6b(%rip),%rdi
call   1070 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1253 <get_ip+0xf3>
mov    -0x18(%rbp),%rdi
mov    0x2e16(%rip),%rax
mov    (%rax),%edx
lea    0xe4c(%rip),%rsi
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    0x2dfd(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0xc(%rbp),%edi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    1213 <get_ip+0xb3>
lea    0xe0c(%rip),%rdi
call   1070 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1253 <get_ip+0xf3>
lea    -0x20(%rbp),%rsi
lea    -0x10(%rbp),%rdi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    -0x10(%rbp),%edi
call   1040 <inet_ntoa@plt>
cmp    $0x0,%rax
je     1248 <get_ip+0xe8>
mov    -0x10(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x8(%rbp)
jmp    1253 <get_ip+0xf3>
lea    0xdc4(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ip>:
push   %rax
mov    0x2eb0(%rip),%rax
mov    (%rax),%edi
mov    0x2e9f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <socket@plt>
cmp    $0xffffffff,%eax
jne    115f <get_ip+0x2f>
lea    0xeaa(%rip),%rdi
call   1040 <perror@plt>
xor    %eax,%eax
pop    %rcx
ret
mov    0x2e6a(%rip),%rax
mov    (%rax),%edx
lea    0xe98(%rip),%rsi
call   1030 <strncpy@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.ifreq = type { i32, i64* }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [6 x i8] c"ens33\00"
@IFNAMSIZ = global i32 0
@SIOCGIFADDR = global i32 0
@.str.2 = [6 x i8] c"ioctl\00"
@.str.3 = [10 x i8] c"127.0.0.1\00"
define i8* @get_ip() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.ifreq
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %1
br label %41
12: ; preds = %0
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%14 = load i64*, i64** %13
%15 = load i32, i32* @IFNAMSIZ
%16 = call i32 @strncpy(i64* %14, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%18 = load i64*, i64** %17
%19 = load i32, i32* @IFNAMSIZ
%20 = sub nsw i32 %19, 1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i64, i64* %18, i64 %21
store i64 0, i64* %22
%23 = load i32, i32* %2
%24 = load i32, i32* @SIOCGIFADDR
%25 = call i64 @ioctl(i32 %23, i32 %24, %struct.ifreq* %4)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %12
%28 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %1
br label %41
29: ; preds = %12
%30 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
%31 = call i32 @memcpy(%struct.sockaddr_in* %3, i32* %30, i32 4)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i8* @inet_ntoa(i32 %33)
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %40
36: ; preds = %29
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%38 = load i32, i32* %37
%39 = call i8* @inet_ntoa(i32 %38)
store i8* %39, i8** %1
br label %41
40: ; preds = %29
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8** %1
br label %41
41: ; preds = %40, %36, %27, %10
%42 = load i8*, i8** %1
ret i8* %42
}

LLVM IR (-O2):
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [6 x i8] c"ens33\00"
@IFNAMSIZ = global i32 0
@SIOCGIFADDR = global i32 0
define i8* @get_ip() {
%1 = load i32, i32* @AF_INET
%2 = load i32, i32* @SOCK_DGRAM
%3 = tail call i32 @socket(i32 %1, i32 %2, i32 0)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
ret i8* null
7: ; preds = %0
%8 = load i32, i32* @IFNAMSIZ
%9 = tail call i32 @strncpy(i64* undef, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %8)
unreachable
}