#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 tpacket2_hdr {
  int tp_status;
};

/* Variables and functions */
int TP_STATUS_USER;
int getpagesize();
int sock_fanout_read_ring(int fd, void *ring) {
  struct tpacket2_hdr *header = ring;
  int count = 0;
  while (count < 20 && header->tp_status & TP_STATUS_USER) {
    count++;
    header = ring + (count * getpagesize());
  }
  return count;
}


x86 assembly (clang, -O0):
<sock_fanout_read_ring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0x14,-0x1c(%rbp)
mov    %al,-0x1d(%rbp)
jge    1155 <sock_fanout_read_ring+0x45>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    0x2e9e(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1165 <sock_fanout_read_ring+0x55>
jmp    119e <sock_fanout_read_ring+0x8e>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1030 <getpagesize@plt>
mov    -0x2c(%rbp),%ecx
mov    %eax,%edx
mov    -0x28(%rbp),%rax
imul   %edx,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    112e <sock_fanout_read_ring+0x1e>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_fanout_read_ring>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    $0x1,%ebx
mov    0x2ec4(%rip),%r15
mov    %rsi,%rax
nopw   0x0(%rax,%rax,1)
mov    (%r15),%ecx
test   %ecx,(%rax)
je     1155 <sock_fanout_read_ring+0x45>
xor    %eax,%eax
call   1030 <getpagesize@plt>
imul   %ebx,%eax
cltq
add    %r14,%rax
add    $0x1,%ebx
cmp    $0x15,%ebx
jne    1130 <sock_fanout_read_ring+0x20>
mov    $0x14,%ebx
jmp    1158 <sock_fanout_read_ring+0x48>
add    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.tpacket2_hdr = type { i32 }
@TP_STATUS_USER = global i32 0
define i32 @sock_fanout_read_ring(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.tpacket2_hdr*
%6 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%7 = load i8*, i8** %4
%8 = bitcast i8* %7 to %struct.tpacket2_hdr*
store %struct.tpacket2_hdr* %8, %struct.tpacket2_hdr** %5
store i32 0, i32* %6
br label %9
9: ; preds = %21, %2
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 20
br i1 %11, label %12, label %19
12: ; preds = %9
%13 = load %struct.tpacket2_hdr*, %struct.tpacket2_hdr** %5
%14 = getelementptr inbounds %struct.tpacket2_hdr, %struct.tpacket2_hdr* %13, i32 0, i32 0
%15 = load i32, i32* %14
%16 = load i32, i32* @TP_STATUS_USER
%17 = and i32 %15, %16
%18 = icmp ne i32 %17, 0
br label %19
19: ; preds = %12, %9
%20 = phi i1 [ false, %9 ], [ %18, %12 ]
br i1 %20, label %21, label %31
21: ; preds = %19
%22 = load i32, i32* %6
%23 = add nsw i32 %22, 1
store i32 %23, i32* %6
%24 = load i8*, i8** %4
%25 = load i32, i32* %6
%26 = call i32 (...) @getpagesize()
%27 = mul nsw i32 %25, %26
%28 = sext i32 %27 to i64
%29 = getelementptr i8, i8* %24, i64 %28
%30 = bitcast i8* %29 to %struct.tpacket2_hdr*
store %struct.tpacket2_hdr* %30, %struct.tpacket2_hdr** %5
br label %9
31: ; preds = %19
%32 = load i32, i32* %6
ret i32 %32
}

LLVM IR (-O2):
@TP_STATUS_USER = global i32 0
define i32 @sock_fanout_read_ring(i32 %0, i8* nocapture %1) {
br label %3
3: ; preds = %2, %11
%4 = phi i32 [ 0, %2 ], [ %12, %11 ]
%5 = phi i8* [ %1, %2 ], [ %16, %11 ]
%6 = bitcast i8* %5 to i32*
%7 = load i32, i32* %6
%8 = load i32, i32* @TP_STATUS_USER
%9 = and i32 %8, %7
%10 = icmp eq i32 %9, 0
br i1 %10, label %18, label %11
11: ; preds = %3
%12 = add nuw nsw i32 %4, 1
%13 = tail call i32 (...) @getpagesize()
%14 = mul nsw i32 %13, %12
%15 = sext i32 %14 to i64
%16 = getelementptr i8, i8* %1, i64 %15
%17 = icmp eq i32 %12, 20
br i1 %17, label %18, label %3
18: ; preds = %11, %3
%19 = phi i32 [ 20, %11 ], [ %4, %3 ]
ret i32 %19
}