#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 */
typedef int uint32_t;

/* Variables and functions */
scalar_t__ bind_arp(int, int *);
int /*<<< orphan*/ close(int);
scalar_t__ get_if_info(char const *, int *, char *, int *);
int inet_addr(char const *);
int /*<<< orphan*/ printf(char *, ...);
int read_arp(int);
scalar_t__ send_arp(int, int, char *, int, int);
int test_arping(const char *ifname, const char *ip) {
  int ret = -1;
  uint32_t dst = inet_addr(ip);
  if (dst == 0 || dst == 0xffffffff) {
    printf("Invalid source IP\n");
    return 1;
  }
  int src;
  int ifindex;
  char mac[6];
  if (get_if_info(ifname, &src, mac, &ifindex)) {
    printf("get_if_info failed, interface %s not found or no IP set?", ifname);
    printf("\n");
    ;
    goto out;
  }
  int arp_fd;
  if (bind_arp(ifindex, &arp_fd)) {
    printf("Failed to bind_arp()");
    printf("\n");
    ;
    goto out;
  }
  if (send_arp(arp_fd, ifindex, mac, src, dst)) {
    printf("Failed to send_arp");
    printf("\n");
    ;
    goto out;
  }
  while (1) {
    int r = read_arp(arp_fd);
    if (r == 0) {
      printf("Got reply, break out");
      printf("\n");
      ;
      break;
    }
  }
  ret = 0;
out:
  if (arp_fd) {
    close(arp_fd);
    arp_fd = 0;
  }
  return ret;
}


x86 assembly (clang, -O0):
<test_arping>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     11a7 <test_arping+0x37>
cmpl   $0xffffffff,-0x20(%rbp)
jne    11c1 <test_arping+0x51>
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e3 <test_arping+0x173>
mov    -0x10(%rbp),%rdi
lea    -0x2e(%rbp),%rdx
lea    -0x24(%rbp),%rsi
lea    -0x28(%rbp),%rcx
call   1080 <get_if_info@plt>
cmp    $0x0,%rax
je     1205 <test_arping+0x95>
mov    -0x10(%rbp),%rsi
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12c4 <test_arping+0x154>
mov    -0x28(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   1090 <bind_arp@plt>
cmp    $0x0,%rax
je     123c <test_arping+0xcc>
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12c4 <test_arping+0x154>
mov    -0x34(%rbp),%edi
mov    -0x28(%rbp),%esi
lea    -0x2e(%rbp),%rdx
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%r8d
call   1070 <send_arp@plt>
cmp    $0x0,%rax
je     127d <test_arping+0x10d>
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xda0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12c4 <test_arping+0x154>
jmp    1282 <test_arping+0x112>
mov    -0x34(%rbp),%edi
call   1040 <read_arp@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jne    12b8 <test_arping+0x148>
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xd65(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12bd <test_arping+0x14d>
jmp    1282 <test_arping+0x112>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     12dd <test_arping+0x16d>
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_arping>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1080 <inet_addr@plt>
mov    %eax,%r14d
lea    0x1(%r14),%eax
cmp    $0x1,%eax
ja     11c4 <test_arping+0x34>
lea    0xec1(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebx
jmp    125f <test_arping+0xcf>
lea    0xc(%rsp),%rsi
lea    0x12(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
call   10a0 <get_if_info@plt>
test   %rax,%rax
je     11f3 <test_arping+0x63>
lea    0xe19(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    123b <test_arping+0xab>
mov    0x8(%rsp),%edi
lea    0x4(%rsp),%rsi
call   10b0 <bind_arp@plt>
test   %rax,%rax
je     120f <test_arping+0x7f>
lea    0xe2c(%rip),%rdi
jmp    1234 <test_arping+0xa4>
mov    0x4(%rsp),%edi
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%ecx
lea    0x12(%rsp),%rdx
mov    %r14d,%r8d
call   1090 <send_arp@plt>
test   %rax,%rax
je     1270 <test_arping+0xe0>
lea    0xe1a(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%ebx
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x4(%rsp),%edi
test   %edi,%edi
je     125f <test_arping+0xcf>
call   1070 <close@plt>
movl   $0x0,0x4(%rsp)
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
nopl   0x0(%rax)
mov    0x4(%rsp),%edi
call   1060 <read_arp@plt>
test   %eax,%eax
jne    1270 <test_arping+0xe0>
lea    0xddd(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1240 <test_arping+0xb0>

LLVM IR (-O0):
@.str = [19 x i8] c"Invalid source IP\0A\00"
@.str.1 = [57 x i8] c"get_if_info failed, interface %s not found or no IP set?\00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [21 x i8] c"Failed to bind_arp()\00"
@.str.4 = [19 x i8] c"Failed to send_arp\00"
@.str.5 = [21 x i8] c"Got reply, break out\00"
define i32 @test_arping(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [6 x i8]
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 -1, i32* %6
%13 = load i8*, i8** %5
%14 = call i32 @inet_addr(i8* %13)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp eq i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %2
%18 = load i32, i32* %7
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %17, %2
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %68
22: ; preds = %17
%23 = load i8*, i8** %4
%24 = getelementptr inbounds [6 x i8], [6 x i8]* %10, i64 0, i64 0
%25 = call i64 @get_if_info(i8* %23, i32* %8, i8* %24, i32* %9)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %22
%28 = load i8*, i8** %4
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %60
31: ; preds = %22
%32 = load i32, i32* %9
%33 = call i64 @bind_arp(i32 %32, i32* %11)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %60
38: ; preds = %31
%39 = load i32, i32* %11
%40 = load i32, i32* %9
%41 = getelementptr inbounds [6 x i8], [6 x i8]* %10, i64 0, i64 0
%42 = load i32, i32* %8
%43 = load i32, i32* %7
%44 = call i64 @send_arp(i32 %39, i32 %40, i8* %41, i32 %42, i32 %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %38
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %60
49: ; preds = %38
br label %50
50: ; preds = %49, %58
%51 = load i32, i32* %11
%52 = call i32 @read_arp(i32 %51)
store i32 %52, i32* %12
%53 = load i32, i32* %12
%54 = icmp eq i32 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %50
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %59
58: ; preds = %50
br label %50
59: ; preds = %55
store i32 0, i32* %6
br label %60
60: ; preds = %59, %46, %35, %27
%61 = load i32, i32* %11
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %66
63: ; preds = %60
%64 = load i32, i32* %11
%65 = call i32 @close(i32 %64)
store i32 0, i32* %11
br label %66
66: ; preds = %63, %60
%67 = load i32, i32* %6
store i32 %67, i32* %3
br label %68
68: ; preds = %66, %20
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
@.str.1 = [57 x i8] c"get_if_info failed, interface %s not found or no IP set?\00"
@.str.3 = [21 x i8] c"Failed to bind_arp()\00"
@.str.4 = [19 x i8] c"Failed to send_arp\00"
@.str.5 = [21 x i8] c"Got reply, break out\00"
@str = [18 x i8] c"Invalid source IP\00"
define i32 @test_arping(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [6 x i8]
%6 = alloca i32
%7 = tail call i32 @inet_addr(i8* %1)
%8 = add i32 %7, 1
%9 = icmp ult i32 %8, 2
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %48
12: ; preds = %2
%13 = bitcast i32* %3 to i8*
%14 = bitcast i32* %4 to i8*
%15 = getelementptr inbounds [6 x i8], [6 x i8]* %5, i64 0, i64 0
%16 = call i64 @get_if_info(i8* %0, i32* %3, i8* %15, i32* %4)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %12
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %40
20: ; preds = %12
%21 = load i32, i32* %4
%22 = call i64 @bind_arp(i32 %21, i32* %6)
%23 = icmp eq i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %20
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %40
26: ; preds = %20
%27 = load i32, i32* %6
%28 = load i32, i32* %4
%29 = load i32, i32* %3
%30 = call i64 @send_arp(i32 %27, i32 %28, i8* %15, i32 %29, i32 %7)
%31 = icmp eq i64 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %26
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
br label %40
34: ; preds = %26, %34
%35 = load i32, i32* %6
%36 = call i32 @read_arp(i32 %35)
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %34
38: ; preds = %34
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %40
40: ; preds = %38, %32, %24, %18
%41 = phi i32 [ -1, %18 ], [ -1, %24 ], [ -1, %32 ], [ 0, %38 ]
%42 = call i32 @putchar(i32 10)
%43 = load i32, i32* %6
%44 = icmp eq i32 %43, 0
br i1 %44, label %47, label %45
45: ; preds = %40
%46 = call i32 @close(i32 %43)
store i32 0, i32* %6
br label %47
47: ; preds = %45, %40
br label %48
48: ; preds = %47, %10
%49 = phi i32 [ 1, %10 ], [ %41, %47 ]
ret i32 %49
}