#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 */
typedef int /*<<< orphan*/ uint32_t;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
};
struct ifreq {
  int /*<<< orphan*/ ifr_addr;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
uint32_t getip(const char *dev) {
  struct ifreq ifr;
  int res;
  int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  if (sockfd < 0)
    return 0;
  memset(&ifr, 0, sizeof(struct ifreq));
  strcpy(ifr.ifr_name, dev);
  res = ioctl(sockfd, SIOCGIFADDR, &ifr);
  close(sockfd);
  if (res < 0)
    return 0;
  return ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
}


x86 assembly (clang, -O0):
<getip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    118e <getip+0x3e>
movl   $0x0,-0x4(%rbp)
jmp    11e6 <getip+0x96>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%edi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
cmpl   $0x0,-0x1c(%rbp)
jge    11e0 <getip+0x90>
movl   $0x0,-0x4(%rbp)
jmp    11e6 <getip+0x96>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getip>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e7f(%rip),%rax
mov    (%rax),%edi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11c0 <getip+0x70>
mov    %eax,%ebp
mov    %rsp,%r15
xor    %ebx,%ebx
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x4(%rsp),%edi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r15,%rdx
call   1050 <ioctl@plt>
mov    %eax,%r14d
mov    %ebp,%edi
call   1060 <close@plt>
test   %r14d,%r14d
js     11c0 <getip+0x70>
mov    (%rsp),%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFADDR = global i32 0
define i32 @getip(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.ifreq
%5 = alloca i32
%6 = alloca i32
store i8* %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)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 0, i32* %2
br label %33
13: ; preds = %1
%14 = call i32 @memset(%struct.ifreq* %4, i32 0, i32 8)
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%16 = load i32, i32* %15
%17 = load i8*, i8** %3
%18 = call i32 @strcpy(i32 %16, i8* %17)
%19 = load i32, i32* %6
%20 = load i32, i32* @SIOCGIFADDR
%21 = call i32 @ioctl(i32 %19, i32 %20, %struct.ifreq* %4)
store i32 %21, i32* %5
%22 = load i32, i32* %6
%23 = call i32 @close(i32 %22)
%24 = load i32, i32* %5
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %13
store i32 0, i32* %2
br label %33
27: ; preds = %13
%28 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
%29 = bitcast i32* %28 to %struct.sockaddr_in*
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %29, i32 0, i32 0
%31 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %30, i32 0, i32 0
%32 = load i32, i32* %31
store i32 %32, i32* %2
br label %33
33: ; preds = %27, %26, %12
%34 = load i32, i32* %2
ret i32 %34
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFADDR = global i32 0
define i32 @getip(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 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %20, label %8
8: ; preds = %1
%9 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 8)
%10 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%11 = load i32, i32* %10
%12 = call i32 @strcpy(i32 %11, i8* %0)
%13 = load i32, i32* @SIOCGIFADDR
%14 = call i32 @ioctl(i32 %6, i32 %13, %struct.ifreq* %2)
%15 = call i32 @close(i32 %6)
%16 = icmp slt i32 %14, 0
br i1 %16, label %20, label %17
17: ; preds = %8
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%19 = load i32, i32* %18
br label %20
20: ; preds = %8, %1, %17
%21 = phi i32 [ %19, %17 ], [ 0, %1 ], [ 0, %8 ]
ret i32 %21
}