#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 timeval {
  int tv_sec;
  int tv_usec;
  int /*<<< orphan*/ member_1;
  int /*<<< orphan*/ member_0;
};
typedef int /*<<< orphan*/ rfds;
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ __be32;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int open_arp();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ process_arp();
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
scalar_t__ send_garp(int, int /*<<< orphan*/, char *, int /*<<< orphan*/,
                     char *);
void loop(int ifindex, __be32 sip, char *sha, __be32 tip, char *tha) {
  int s, ret;
  struct timeval timeout, now, last = {0, 0};
  fd_set rfds, readers;
  printf("ARP\n");
  if ((s = open_arp()) < 0)
    exit(-1);
  FD_ZERO(&readers);
  FD_SET(s, &readers);
  for (;;) {
    ret = gettimeofday(&now, NULL);
    if (ret == -1)
      ({
        perror("gettimeofday");
        exit(-2);
      });
    timeout.tv_sec = last.tv_sec + 1 - now.tv_sec;
    timeout.tv_usec = last.tv_usec - now.tv_usec;
    ({
      (timeout).tv_sec--;
      (timeout).tv_usec += 1000000;
      (timeout).tv_sec += (timeout).tv_usec / 1000000;
      (timeout).tv_usec = (timeout).tv_usec % 1000000;
    });
    if (timeout.tv_sec < 0) {
      last = now;
      timeout.tv_sec = 1;
      timeout.tv_usec = 0;
      if (send_garp(ifindex, sip, sha, tip, tha) == 0)
        printf("sent\n");
    }
    memcpy(&rfds, &readers, sizeof(rfds));
    ret = select(s + 1, &rfds, NULL, NULL, &timeout);
    if (ret == -1 && errno != EINTR)
      ({
        perror("select");
        exit(-2);
      });
    if (ret == 1)
      process_arp();
  }
}


x86 assembly (clang, -O0):
<loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8,-0x20(%rbp)
lea    -0x58(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <memset@plt>
lea    0xe10(%rip),%rdi
call   1060 <printf@plt>
mov    $0x0,%al
call   1040 <open_arp@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1212 <loop+0x52>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
lea    -0x60(%rbp),%rdi
call   10a0 <FD_ZERO@plt>
mov    -0x24(%rbp),%edi
lea    -0x60(%rbp),%rsi
call   10e0 <FD_SET@plt>
lea    -0x48(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <gettimeofday@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1257 <loop+0x97>
lea    0xdbd(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xfffffffe,%edi
call   10d0 <exit@plt>
mov    -0x58(%rbp),%eax
add    $0x1,%eax
sub    -0x48(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x54(%rbp),%eax
sub    -0x44(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x38(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x38(%rbp)
mov    -0x34(%rbp),%eax
add    $0xf4240,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
add    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x34(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %edx,-0x34(%rbp)
mov    %edx,-0x68(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    12fb <loop+0x13b>
mov    -0x48(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
movl   $0x1,-0x38(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
mov    -0x20(%rbp),%r8
call   1050 <send_garp@plt>
cmp    $0x0,%rax
jne    12f6 <loop+0x136>
lea    0xd21(%rip),%rdi
call   1060 <printf@plt>
jmp    12fb <loop+0x13b>
lea    -0x5c(%rbp),%rdi
lea    -0x60(%rbp),%rsi
mov    $0x4,%edx
call   1090 <memcpy@plt>
mov    -0x24(%rbp),%edi
add    $0x1,%edi
lea    -0x5c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x38(%rbp),%r8
mov    %rcx,%rdx
call   10b0 <select@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1364 <loop+0x1a4>
mov    0x2ca5(%rip),%rax
mov    (%rax),%rax
mov    0x2c93(%rip),%rcx
cmp    (%rcx),%rax
je     1364 <loop+0x1a4>
lea    0xcc3(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xfffffffe,%edi
call   10d0 <exit@plt>
cmpl   $0x1,-0x28(%rbp)
jne    1375 <loop+0x1b5>
mov    $0x0,%al
call   1030 <process_arp@plt>
jmp    1227 <loop+0x67>

x86 assembly (clang, -O2):
<loop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %r8,%r14
mov    %ecx,%r15d
mov    %rdx,%r12
mov    %esi,%r13d
mov    %edi,%ebx
lea    0xe41(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1050 <open_arp@plt>
test   %eax,%eax
jns    11ed <loop+0x3d>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    %eax,%ebp
mov    %ebx,(%rsp)
mov    %r13d,0x4(%rsp)
mov    %r12,0x10(%rsp)
mov    %r15d,0x8(%rsp)
mov    %r14,0x18(%rsp)
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1090 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r14,%rsi
call   10d0 <FD_SET@plt>
lea    0x20(%rsp),%rdi
xor    %esi,%esi
call   1070 <gettimeofday@plt>
cmp    $0xffffffff,%eax
jne    1244 <loop+0x94>
lea    0xdcb(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xfffffffe,%edi
call   10c0 <exit@plt>
add    $0x1,%ebp
lea    0x44(%rsp),%r14
xor    %r12d,%r12d
xor    %r13d,%r13d
jmp    1278 <loop+0xc8>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %eax,%eax
call   1040 <process_arp@plt>
lea    0x20(%rsp),%rdi
xor    %esi,%esi
call   1070 <gettimeofday@plt>
cmp    $0xffffffff,%eax
je     122e <loop+0x7e>
mov    0x20(%rsp),%ebx
mov    0x24(%rsp),%r15d
mov    %r12d,%eax
sub    %r15d,%eax
mov    %r13d,%ecx
sub    %ebx,%ecx
add    $0xf4240,%eax
cltq
imul   $0x431bde83,%rax,%rdx
mov    %rdx,%rsi
shr    $0x3f,%rsi
sar    $0x32,%rdx
add    %esi,%edx
imul   $0xf4240,%edx,%esi
sub    %esi,%eax
add    %ecx,%edx
mov    %edx,0x30(%rsp)
mov    %eax,0x34(%rsp)
jns    12f5 <loop+0x145>
movq   $0x1,0x30(%rsp)
mov    (%rsp),%edi
mov    0x4(%rsp),%esi
mov    0x10(%rsp),%rdx
mov    0x8(%rsp),%ecx
mov    0x18(%rsp),%r8
call   1060 <send_garp@plt>
test   %rax,%rax
jne    12ef <loop+0x13f>
lea    0xd2e(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%r13d
mov    %r15d,%r12d
mov    %r14,%rdi
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
call   1080 <memcpy@plt>
mov    %ebp,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x30(%rsp),%r8
call   10a0 <select@plt>
cmp    $0x1,%eax
je     1260 <loop+0xb0>
cmp    $0xffffffff,%eax
jne    1267 <loop+0xb7>
mov    0x2cad(%rip),%rax
mov    (%rax),%rax
mov    0x2c9b(%rip),%rcx
cmp    (%rcx),%rax
je     1267 <loop+0xb7>
lea    0xcc0(%rip),%rdi
jmp    1235 <loop+0x85>

LLVM IR (-O0):
%struct.timeval = type { i32, i32, i32, i32 }
@.str = [5 x i8] c"ARP\0A\00"
@.str.1 = [13 x i8] c"gettimeofday\00"
@.str.2 = [6 x i8] c"sent\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.3 = [7 x i8] c"select\00"
define void @loop(i32 %0, i32 %1, i8* %2, i32 %3, i8* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.timeval
%14 = alloca %struct.timeval
%15 = alloca %struct.timeval
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i8* %4, i8** %10
%21 = bitcast %struct.timeval* %15 to i8*
%22 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%23 = call i32 (...) @open_arp()
store i32 %23, i32* %11
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %5
%26 = call i32 @exit(i32 -1)
unreachable
27: ; preds = %5
%28 = call i32 @FD_ZERO(i32* %17)
%29 = load i32, i32* %11
%30 = call i32 @FD_SET(i32 %29, i32* %17)
br label %31
31: ; preds = %106, %27
%32 = call i32 @gettimeofday(%struct.timeval* %14, i32* null)
store i32 %32, i32* %12
%33 = load i32, i32* %12
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32 @exit(i32 -2)
unreachable
38: ; preds = %31
%39 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 0
%40 = load i32, i32* %39
%41 = add nsw i32 %40, 1
%42 = getelementptr inbounds %struct.timeval, %struct.timeval* %14, i32 0, i32 0
%43 = load i32, i32* %42
%44 = sub nsw i32 %41, %43
%45 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
store i32 %44, i32* %45
%46 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 1
%47 = load i32, i32* %46
%48 = getelementptr inbounds %struct.timeval, %struct.timeval* %14, i32 0, i32 1
%49 = load i32, i32* %48
%50 = sub nsw i32 %47, %49
%51 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 1
store i32 %50, i32* %51
%52 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
%53 = load i32, i32* %52
%54 = add nsw i32 %53, -1
store i32 %54, i32* %52
%55 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 1
%56 = load i32, i32* %55
%57 = add nsw i32 %56, 1000000
store i32 %57, i32* %55
%58 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 1
%59 = load i32, i32* %58
%60 = sdiv i32 %59, 1000000
%61 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
%62 = load i32, i32* %61
%63 = add nsw i32 %62, %60
store i32 %63, i32* %61
%64 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 1
%65 = load i32, i32* %64
%66 = srem i32 %65, 1000000
%67 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 1
store i32 %66, i32* %67
store i32 %66, i32* %19
%68 = load i32, i32* %19
%69 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
%70 = load i32, i32* %69
%71 = icmp slt i32 %70, 0
br i1 %71, label %72, label %87
72: ; preds = %38
%73 = bitcast %struct.timeval* %15 to i8*
%74 = bitcast %struct.timeval* %14 to i8*
%75 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
store i32 1, i32* %75
%76 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 1
store i32 0, i32* %76
%77 = load i32, i32* %6
%78 = load i32, i32* %7
%79 = load i8*, i8** %8
%80 = load i32, i32* %9
%81 = load i8*, i8** %10
%82 = call i64 @send_garp(i32 %77, i32 %78, i8* %79, i32 %80, i8* %81)
%83 = icmp eq i64 %82, 0
br i1 %83, label %84, label %86
84: ; preds = %72
%85 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %86
86: ; preds = %84, %72
br label %87
87: ; preds = %86, %38
%88 = call i32 @memcpy(i32* %16, i32* %17, i32 4)
%89 = load i32, i32* %11
%90 = add nsw i32 %89, 1
%91 = call i32 @select(i32 %90, i32* %16, i32* null, i32* null, %struct.timeval* %13)
store i32 %91, i32* %12
%92 = load i32, i32* %12
%93 = icmp eq i32 %92, -1
br i1 %93, label %94, label %101
94: ; preds = %87
%95 = load i64, i64* @errno
%96 = load i64, i64* @EINTR
%97 = icmp ne i64 %95, %96
br i1 %97, label %98, label %101
98: ; preds = %94
%99 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%100 = call i32 @exit(i32 -2)
unreachable
101: ; preds = %94, %87
%102 = load i32, i32* %12
%103 = icmp eq i32 %102, 1
br i1 %103, label %104, label %106
104: ; preds = %101
%105 = call i32 (...) @process_arp()
br label %106
106: ; preds = %104, %101
br label %31
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32, i32, i32 }
@.str.1 = [13 x i8] c"gettimeofday\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.3 = [7 x i8] c"select\00"
@str = [4 x i8] c"ARP\00"
@str.4 = [5 x i8] c"sent\00"
define void @loop(i32 %0, i32 %1, i8* %2, i32 %3, i8* %4) {
%6 = alloca %struct.timeval
%7 = alloca %struct.timeval
%8 = alloca i32
%9 = alloca i32
%10 = bitcast %struct.timeval* %6 to i8*
%11 = bitcast %struct.timeval* %7 to i8*
%12 = bitcast i32* %8 to i8*
%13 = bitcast i32* %9 to i8*
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str, i64 0, i64 0))
%15 = tail call i32 (...) @open_arp()
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %5
%18 = tail call i32 @exit(i32 -1)
unreachable
19: ; preds = %5
%20 = call i32 @FD_ZERO(i32* %9)
%21 = call i32 @FD_SET(i32 %15, i32* %9)
%22 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
%23 = icmp eq i32 %22, -1
br i1 %23, label %30, label %24
24: ; preds = %19
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 0
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 1
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
%29 = add nuw nsw i32 %15, 1
br label %33
30: ; preds = %64, %19
%31 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%32 = call i32 @exit(i32 -2)
unreachable
33: ; preds = %24, %64
%34 = phi i32 [ 0, %24 ], [ %52, %64 ]
%35 = phi i32 [ 0, %24 ], [ %51, %64 ]
%36 = load i32, i32* %25
%37 = load i32, i32* %27
%38 = sub nsw i32 %34, %37
%39 = sub i32 %35, %36
%40 = add nsw i32 %38, 1000000
%41 = sdiv i32 %40, 1000000
%42 = add nsw i32 %41, %39
store i32 %42, i32* %26
%43 = srem i32 %40, 1000000
store i32 %43, i32* %28
%44 = icmp slt i32 %42, 0
br i1 %44, label %45, label %50
45: ; preds = %33
store i32 1, i32* %26
store i32 0, i32* %28
%46 = call i64 @send_garp(i32 %0, i32 %1, i8* %2, i32 %3, i8* %4)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.4, i64 0, i64 0))
br label %50
50: ; preds = %45, %48, %33
%51 = phi i32 [ %36, %48 ], [ %36, %45 ], [ %35, %33 ]
%52 = phi i32 [ %37, %48 ], [ %37, %45 ], [ %34, %33 ]
%53 = call i32 @memcpy(i32* %8, i32* %9, i32 4)
%54 = call i32 @select(i32 %29, i32* %8, i32* null, i32* null, %struct.timeval* %6)
switch i32 %54, label %64 [
i32 -1, label %55
i32 1, label %62
]
55: ; preds = %50
%56 = load i64, i64* @errno
%57 = load i64, i64* @EINTR
%58 = icmp eq i64 %56, %57
br i1 %58, label %64, label %59
59: ; preds = %55
%60 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%61 = call i32 @exit(i32 -2)
unreachable
62: ; preds = %50
%63 = call i32 (...) @process_arp()
br label %64
64: ; preds = %55, %50, %62
%65 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
%66 = icmp eq i32 %65, -1
br i1 %66, label %30, label %33
}