#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 tpacket_stats_v3 {
  scalar_t__ tp_freeze_q_cnt;
  scalar_t__ tp_packets;
  scalar_t__ tp_drops;
};
typedef int /*<<< orphan*/ stats;
typedef int socklen_t;

/* Variables and functions */
int /*<<< orphan*/ PACKET_STATISTICS;
int /*<<< orphan*/ SOL_PACKET;
int getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
               struct tpacket_stats_v3 *, int *);
void gather_socket_stats(int fd, struct tpacket_stats_v3 *aggregate_stats) {
  if (fd == 0) {
    return;
  }
  struct tpacket_stats_v3 stats;
  socklen_t len = sizeof(stats);
  int err = getsockopt(fd, SOL_PACKET, PACKET_STATISTICS, &stats, &len);
  if (err < 0) {
    return;
  }
  aggregate_stats->tp_drops += stats.tp_drops;
  aggregate_stats->tp_packets += stats.tp_packets;
  aggregate_stats->tp_freeze_q_cnt += stats.tp_freeze_q_cnt;
}


x86 assembly (clang, -O0):
<gather_socket_stats>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    112e <gather_socket_stats+0x1e>
jmp    1197 <gather_socket_stats+0x87>
movl   $0x18,-0x2c(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2eb1(%rip),%rax
mov    (%rax),%esi
mov    0x2e90(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
lea    -0x2c(%rbp),%r8
call   1030 <getsockopt@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    1169 <gather_socket_stats+0x59>
jmp    1197 <gather_socket_stats+0x87>
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
add    0x10(%rax),%rcx
mov    %rcx,0x10(%rax)
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
add    0x8(%rax),%rcx
mov    %rcx,0x8(%rax)
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gather_socket_stats>:
test   %edi,%edi
je     1165 <gather_socket_stats+0x55>
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%rbx
movl   $0x18,0xc(%rsp)
mov    0x2ec5(%rip),%rax
mov    (%rax),%esi
mov    0x2ea4(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rcx
lea    0xc(%rsp),%r8
call   1030 <getsockopt@plt>
test   %eax,%eax
js     1160 <gather_socket_stats+0x50>
mov    0x20(%rsp),%rax
add    %rax,0x10(%rbx)
movdqu (%rbx),%xmm0
paddq  0x10(%rsp),%xmm0
movdqu %xmm0,(%rbx)
add    $0x30,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.tpacket_stats_v3 = type { i64, i64, i64 }
@SOL_PACKET = global i32 0
@PACKET_STATISTICS = global i32 0
define void @gather_socket_stats(i32 %0, %struct.tpacket_stats_v3* %1) {
%3 = alloca i32
%4 = alloca %struct.tpacket_stats_v3*
%5 = alloca %struct.tpacket_stats_v3
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store %struct.tpacket_stats_v3* %1, %struct.tpacket_stats_v3** %4
%8 = load i32, i32* %3
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %2
br label %38
11: ; preds = %2
store i32 24, i32* %6
%12 = load i32, i32* %3
%13 = load i32, i32* @SOL_PACKET
%14 = load i32, i32* @PACKET_STATISTICS
%15 = call i32 @getsockopt(i32 %12, i32 %13, i32 %14, %struct.tpacket_stats_v3* %5, i32* %6)
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %11
br label %38
19: ; preds = %11
%20 = getelementptr inbounds %struct.tpacket_stats_v3, %struct.tpacket_stats_v3* %5, i32 0, i32 2
%21 = load i64, i64* %20
%22 = load %struct.tpacket_stats_v3*, %struct.tpacket_stats_v3** %4
%23 = getelementptr inbounds %struct.tpacket_stats_v3, %struct.tpacket_stats_v3* %22, i32 0, i32 2
%24 = load i64, i64* %23
%25 = add nsw i64 %24, %21
store i64 %25, i64* %23
%26 = getelementptr inbounds %struct.tpacket_stats_v3, %struct.tpacket_stats_v3* %5, i32 0, i32 1
%27 = load i64, i64* %26
%28 = load %struct.tpacket_stats_v3*, %struct.tpacket_stats_v3** %4
%29 = getelementptr inbounds %struct.tpacket_stats_v3, %struct.tpacket_stats_v3* %28, i32 0, i32 1
%30 = load i64, i64* %29
%31 = add nsw i64 %30, %27
store i64 %31, i64* %29
%32 = getelementptr inbounds %struct.tpacket_stats_v3, %struct.tpacket_stats_v3* %5, i32 0, i32 0
%33 = load i64, i64* %32
%34 = load %struct.tpacket_stats_v3*, %struct.tpacket_stats_v3** %4
%35 = getelementptr inbounds %struct.tpacket_stats_v3, %struct.tpacket_stats_v3* %34, i32 0, i32 0
%36 = load i64, i64* %35
%37 = add nsw i64 %36, %33
store i64 %37, i64* %35
br label %38
38: ; preds = %19, %18, %10
ret void
}

LLVM IR (-O2):
%struct.tpacket_stats_v3 = type { i64, i64, i64 }
@SOL_PACKET = global i32 0
@PACKET_STATISTICS = global i32 0
define void @gather_socket_stats(i32 %0, %struct.tpacket_stats_v3* nocapture %1) {
%3 = alloca %struct.tpacket_stats_v3
%4 = alloca i32
%5 = icmp eq i32 %0, 0
br i1 %5, label %26, label %6
6: ; preds = %2
%7 = bitcast %struct.tpacket_stats_v3* %3 to i8*
%8 = bitcast i32* %4 to i8*
store i32 24, i32* %4
%9 = load i32, i32* @SOL_PACKET
%10 = load i32, i32* @PACKET_STATISTICS
%11 = call i32 @getsockopt(i32 %0, i32 %9, i32 %10, %struct.tpacket_stats_v3* %3, i32* %4)
%12 = icmp slt i32 %11, 0
br i1 %12, label %25, label %13
13: ; preds = %6
%14 = getelementptr inbounds %struct.tpacket_stats_v3, %struct.tpacket_stats_v3* %3, i64 0, i32 2
%15 = load i64, i64* %14
%16 = getelementptr inbounds %struct.tpacket_stats_v3, %struct.tpacket_stats_v3* %1, i64 0, i32 2
%17 = load i64, i64* %16
%18 = add nsw i64 %17, %15
store i64 %18, i64* %16
%19 = bitcast %struct.tpacket_stats_v3* %3 to <2 x i64>*
%20 = load <2 x i64>, <2 x i64>* %19
%21 = bitcast %struct.tpacket_stats_v3* %1 to <2 x i64>*
%22 = load <2 x i64>, <2 x i64>* %21
%23 = add nsw <2 x i64> %22, %20
%24 = bitcast %struct.tpacket_stats_v3* %1 to <2 x i64>*
store <2 x i64> %23, <2 x i64>* %24
br label %25
25: ; preds = %6, %13
br label %26
26: ; preds = %2, %25
ret void
}