#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;
typedef int /*<<< orphan*/ buffer;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int, int, int, int);
int sscanf(char *, char *, int *, int *, int *, int *, char *, char *, char *);
int getarp(uint32_t ip) {
  int found = 1;
  char searchfor[16];
  sprintf(searchfor, "%d.%d.%d.%d", ip & 0xff, (ip >> 8) & 0xff,
          (ip >> 16) & 0xff, ip >> 24);
  FILE *farp = fopen("/proc/net/arp", "r");
  if (farp) {
    char buffer[512];
    char *l = fgets(buffer, sizeof(buffer), farp);
    while (l) {
      l = fgets(buffer, sizeof(buffer), farp);
      if (l) {
        int p[4];
        char type[16];
        char flags[16];
        char hw[32];
        if (7 == sscanf(buffer, "%u.%u.%u.%u %15s %15s %31s", &p[0], &p[1],
                        &p[2], &p[3], type, flags, hw)) {
          uint32_t searchip = p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
          if (ip == searchip) {
            printf("id: %s\n", hw);
            found = 0;
            break;
          }
        }
      }
    }
    fclose(farp);
  }
  return found;
}


x86 assembly (clang, -O0):
<getarp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2b0,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x1,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0x4(%rbp),%edx
and    $0xff,%edx
mov    -0x4(%rbp),%ecx
sar    $0x8,%ecx
and    $0xff,%ecx
mov    -0x4(%rbp),%r8d
sar    $0x10,%r8d
and    $0xff,%r8d
mov    -0x4(%rbp),%r9d
sar    $0x18,%r9d
lea    0xe54(%rip),%rsi
call   1080 <sprintf@plt>
lea    0xe54(%rip),%rdi
lea    0xe5b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1307 <getarp+0x1a7>
lea    -0x230(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x200,%esi
call   1050 <fgets@plt>
mov    %rax,-0x238(%rbp)
cmpq   $0x0,-0x238(%rbp)
je     12fe <getarp+0x19e>
lea    -0x230(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x200,%esi
call   1050 <fgets@plt>
mov    %rax,-0x238(%rbp)
cmpq   $0x0,-0x238(%rbp)
je     12f9 <getarp+0x199>
lea    -0x230(%rbp),%rdi
lea    -0x250(%rbp),%rdx
lea    -0x250(%rbp),%rcx
add    $0x4,%rcx
lea    -0x250(%rbp),%r8
add    $0x8,%r8
lea    -0x250(%rbp),%r9
add    $0xc,%r9
lea    -0x260(%rbp),%r11
lea    -0x270(%rbp),%r10
lea    -0x290(%rbp),%rax
lea    0xdaa(%rip),%rsi
mov    %r11,(%rsp)
mov    %r10,0x8(%rsp)
mov    %rax,0x10(%rsp)
call   1060 <sscanf@plt>
mov    %eax,%ecx
mov    $0x7,%eax
cmp    %ecx,%eax
jne    12f4 <getarp+0x194>
mov    -0x250(%rbp),%eax
mov    -0x24c(%rbp),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
mov    -0x248(%rbp),%ecx
shl    $0x10,%ecx
or     %ecx,%eax
mov    -0x244(%rbp),%ecx
shl    $0x18,%ecx
or     %ecx,%eax
mov    %eax,-0x294(%rbp)
mov    -0x4(%rbp),%eax
cmp    -0x294(%rbp),%eax
jne    12ef <getarp+0x18f>
lea    -0x290(%rbp),%rsi
lea    0xd59(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x8(%rbp)
jmp    12fe <getarp+0x19e>
jmp    12f4 <getarp+0x194>
jmp    12f9 <getarp+0x199>
jmp    11ef <getarp+0x8f>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x8(%rbp),%eax
add    $0x2b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getarp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x278,%rsp
mov    %edi,%ebx
movzbl %bl,%edx
movzbl %bh,%ecx
mov    %edi,%eax
shr    $0x10,%eax
movzbl %al,%r8d
mov    %edi,0x1c(%rsp)
mov    %edi,%eax
sar    $0x18,%eax
lea    0xe6e(%rip),%rsi
lea    0x60(%rsp),%rdi
mov    %eax,%r9d
call   1080 <sprintf@plt>
lea    0xe66(%rip),%rdi
lea    0xe6d(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     12a0 <getarp+0x140>
mov    %rax,%rbx
lea    0x70(%rsp),%rdi
mov    $0x200,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
mov    $0x1,%ebp
test   %rax,%rax
je     12ac <getarp+0x14c>
lea    0x70(%rsp),%rdi
mov    $0x200,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     12ac <getarp+0x14c>
lea    0x40(%rsp),%r13
lea    0x50(%rsp),%r12
lea    0xe12(%rip),%rbp
lea    0x70(%rsp),%r14
mov    %rsp,%r15
jmp    1235 <getarp+0xd5>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14,%rdi
mov    $0x200,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     12a7 <getarp+0x147>
sub    $0x8,%rsp
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
lea    0xc(%rsp),%rcx
lea    0x10(%rsp),%r8
lea    0x14(%rsp),%r9
lea    0x28(%rsp),%rax
push   %rax
push   %r13
push   %r12
call   1060 <sscanf@plt>
add    $0x20,%rsp
cmp    $0x7,%eax
jne    1220 <getarp+0xc0>
mov    0x4(%rsp),%eax
mov    0x8(%rsp),%ecx
shl    $0x8,%eax
or     (%rsp),%eax
shl    $0x10,%ecx
mov    0xc(%rsp),%edx
shl    $0x18,%edx
or     %ecx,%edx
or     %eax,%edx
cmp    0x1c(%rsp),%edx
jne    1220 <getarp+0xc0>
lea    0xda5(%rip),%rdi
lea    0x20(%rsp),%rsi
call   1040 <printf@plt>
xor    %ebp,%ebp
jmp    12ac <getarp+0x14c>
mov    $0x1,%ebp
jmp    12b4 <getarp+0x154>
mov    $0x1,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
add    $0x278,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"%d.%d.%d.%d\00"
@.str.1 = [14 x i8] c"/proc/net/arp\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [27 x i8] c"%u.%u.%u.%u %15s %15s %31s\00"
@.str.4 = [8 x i8] c"id: %s\0A\00"
define i32 @getarp(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [16 x i8]
%5 = alloca i32*
%6 = alloca [512 x i8]
%7 = alloca i8*
%8 = alloca [4 x i32]
%9 = alloca [16 x i8]
%10 = alloca [16 x i8]
%11 = alloca [32 x i8]
%12 = alloca i32
store i32 %0, i32* %2
store i32 1, i32* %3
%13 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%14 = load i32, i32* %2
%15 = and i32 %14, 255
%16 = load i32, i32* %2
%17 = ashr i32 %16, 8
%18 = and i32 %17, 255
%19 = load i32, i32* %2
%20 = ashr i32 %19, 16
%21 = and i32 %20, 255
%22 = load i32, i32* %2
%23 = ashr i32 %22, 24
%24 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %18, i32 %21, i32 %23)
%25 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %25, i32** %5
%26 = load i32*, i32** %5
%27 = icmp ne i32* %26, null
br i1 %27, label %28, label %79
28: ; preds = %1
%29 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%30 = load i32*, i32** %5
%31 = call i8* @fgets(i8* %29, i32 512, i32* %30)
store i8* %31, i8** %7
br label %32
32: ; preds = %75, %28
%33 = load i8*, i8** %7
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %76
35: ; preds = %32
%36 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%37 = load i32*, i32** %5
%38 = call i8* @fgets(i8* %36, i32 512, i32* %37)
store i8* %38, i8** %7
%39 = load i8*, i8** %7
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %75
41: ; preds = %35
%42 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%43 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 0
%44 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 1
%45 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 2
%46 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 3
%47 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%48 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%49 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 0
%50 = call i32 @sscanf(i8* %42, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32* %43, i32* %44, i32* %45, i32* %46, i8* %47, i8* %48, i8* %49)
%51 = icmp eq i32 7, %50
br i1 %51, label %52, label %74
52: ; preds = %41
%53 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 0
%54 = load i32, i32* %53
%55 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 1
%56 = load i32, i32* %55
%57 = shl i32 %56, 8
%58 = or i32 %54, %57
%59 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 2
%60 = load i32, i32* %59
%61 = shl i32 %60, 16
%62 = or i32 %58, %61
%63 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 3
%64 = load i32, i32* %63
%65 = shl i32 %64, 24
%66 = or i32 %62, %65
store i32 %66, i32* %12
%67 = load i32, i32* %2
%68 = load i32, i32* %12
%69 = icmp eq i32 %67, %68
br i1 %69, label %70, label %73
70: ; preds = %52
%71 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 0
%72 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %71)
store i32 0, i32* %3
br label %76
73: ; preds = %52
br label %74
74: ; preds = %73, %41
br label %75
75: ; preds = %74, %35
br label %32
76: ; preds = %70, %32
%77 = load i32*, i32** %5
%78 = call i32 @fclose(i32* %77)
br label %79
79: ; preds = %76, %1
%80 = load i32, i32* %3
ret i32 %80
}

LLVM IR (-O2):
@.str = [12 x i8] c"%d.%d.%d.%d\00"
@.str.1 = [14 x i8] c"/proc/net/arp\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [27 x i8] c"%u.%u.%u.%u %15s %15s %31s\00"
@.str.4 = [8 x i8] c"id: %s\0A\00"
define i32 @getarp(i32 %0) {
%2 = alloca [16 x i8]
%3 = alloca [512 x i8]
%4 = alloca [4 x i32]
%5 = alloca [16 x i8]
%6 = alloca [16 x i8]
%7 = alloca [32 x i8]
%8 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%9 = and i32 %0, 255
%10 = lshr i32 %0, 8
%11 = and i32 %10, 255
%12 = lshr i32 %0, 16
%13 = and i32 %12, 255
%14 = ashr i32 %0, 24
%15 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %11, i32 %13, i32 %14)
%16 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %56, label %18
18: ; preds = %1
%19 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%20 = call i8* @fgets(i8* %19, i32 512, i32* %16)
%21 = icmp eq i8* %20, null
br i1 %21, label %53, label %22
22: ; preds = %18
%23 = bitcast [4 x i32]* %4 to i8*
%24 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%25 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%26 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%27 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 0
%28 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 1
%29 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 2
%30 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 3
%31 = call i8* @fgets(i8* %19, i32 512, i32* %16)
%32 = icmp eq i8* %31, null
br i1 %32, label %53, label %33
33: ; preds = %22, %48
%34 = call i32 @sscanf(i8* %19, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32* %27, i32* %28, i32* %29, i32* %30, i8* %24, i8* %25, i8* %26)
%35 = icmp eq i32 %34, 7
br i1 %35, label %36, label %48
36: ; preds = %33
%37 = load i32, i32* %27
%38 = load i32, i32* %28
%39 = shl i32 %38, 8
%40 = or i32 %39, %37
%41 = load i32, i32* %29
%42 = shl i32 %41, 16
%43 = or i32 %40, %42
%44 = load i32, i32* %30
%45 = shl i32 %44, 24
%46 = or i32 %43, %45
%47 = icmp eq i32 %46, %0
br i1 %47, label %51, label %48
48: ; preds = %36, %33
%49 = call i8* @fgets(i8* %19, i32 512, i32* %16)
%50 = icmp eq i8* %49, null
br i1 %50, label %53, label %33
51: ; preds = %36
%52 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %26)
br label %53
53: ; preds = %48, %22, %18, %51
%54 = phi i32 [ 0, %51 ], [ 1, %18 ], [ 1, %22 ], [ 1, %48 ]
%55 = call i32 @fclose(i32* %16)
br label %56
56: ; preds = %53, %1
%57 = phi i32 [ %54, %53 ], [ 1, %1 ]
ret i32 %57
}