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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SHUT_RDWR;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int GetIP(char *ifname, in_addr_t *ip, in_addr_t *mask) {
  struct ifreq ifr;
  int sock = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock < 0) {
    perror("socket");
    return 0;
  }
  strcpy(ifr.ifr_name, ifname);
  ifr.ifr_addr.sa_family = AF_INET;
  if (ioctl(sock, SIOCGIFADDR, &ifr) < 0) {
    perror("SIOCGIFADDR");
  }
  *ip = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
  shutdown(sock, SHUT_RDWR);
  return 0;
}


x86 assembly (clang, -O0):
<GetIP>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11a2 <GetIP+0x52>
lea    0xe6f(%rip),%rdi
call   1060 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1206 <GetIP+0xb6>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    11e5 <GetIP+0x95>
lea    0xe27(%rip),%rdi
call   1060 <perror@plt>
mov    -0x28(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%edi
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
call   1040 <shutdown@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetIP>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e74(%rip),%r15
mov    (%r15),%edi
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11c6 <GetIP+0x76>
mov    %eax,%ebp
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    (%r15),%eax
mov    %eax,(%rsp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1050 <ioctl@plt>
test   %rax,%rax
js     11d4 <GetIP+0x84>
mov    (%rsp),%eax
mov    %eax,(%r14)
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <shutdown@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe33(%rip),%rdi
call   1060 <perror@plt>
jmp    11b9 <GetIP+0x69>
lea    0xe2c(%rip),%rdi
call   1060 <perror@plt>
jmp    11a3 <GetIP+0x53>

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

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_4__, i32 }
%struct.TYPE_4__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [7 x i8] c"socket\00"
@SIOCGIFADDR = global i32 0
@.str.1 = [12 x i8] c"SIOCGIFADDR\00"
@SHUT_RDWR = global i32 0
define i32 @GetIP(i8* %0, i32* nocapture writeonly %1, i32* nocapture readnone %2) {
%4 = alloca %struct.ifreq
%5 = bitcast %struct.ifreq* %4 to i8*
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %25
12: ; preds = %3
%13 = tail call i32 @strcpy(i32 undef, i8* %0)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 0, i32 0
store i32 %14, i32* %15
%16 = load i32, i32* @SIOCGIFADDR
%17 = call i64 @ioctl(i32 %8, i32 %16, %struct.ifreq* %4)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %12
%22 = load i32, i32* %15
store i32 %22, i32* %1
%23 = load i32, i32* @SHUT_RDWR
%24 = call i32 @shutdown(i32 %8, i32 %23)
br label %25
25: ; preds = %21, %10
ret i32 0
}