#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 /*<<< orphan*/ tcp_mem;
struct bpf_sysctl {
  scalar_t__ write;
};

/* Variables and functions */
int bpf_strtoul(char *, int, int /*<<< orphan*/, unsigned long *);
int bpf_sysctl_get_current_value(struct bpf_sysctl *, char *, int);
int /*<<< orphan*/ is_tcp_mem(struct bpf_sysctl *);
__attribute__((section("cgroup/sysctl"), used)) int
sysctl_tcp_mem(struct bpf_sysctl *ctx) {
  unsigned long tcp_mem[28] = {};
  char value[(28 * 7)];
  unsigned char i, off = 0;
  int ret;
  if (ctx->write)
    return 0;
  if (!is_tcp_mem(ctx))
    return 0;
  ret = bpf_sysctl_get_current_value(ctx, value, (28 * 7));
  if (ret < 0 || ret >= (28 * 7))
    return 0;
#pragma clang loop unroll(disable)
  for (i = 0; i < (sizeof(tcp_mem) / sizeof((tcp_mem)[0])); ++i) {
    ret = bpf_strtoul(value + off, 7, 0, tcp_mem + i);
    if (ret <= 0 || ret > 7)
      return 0;
    off += ret & 7;
  }
  return tcp_mem[0] < tcp_mem[1] && tcp_mem[1] < tcp_mem[2];
}


x86 assembly (clang, -O0):
<sysctl_tcp_mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1d0,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0xf0(%rbp),%rdi
xor    %esi,%esi
mov    $0xe0,%edx
call   1030 <memset@plt>
movb   $0x0,-0x1c2(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     1183 <sysctl_tcp_mem+0x43>
movl   $0x0,-0x4(%rbp)
jmp    12cb <sysctl_tcp_mem+0x18b>
mov    -0x10(%rbp),%rdi
call   1040 <is_tcp_mem@plt>
cmp    $0x0,%eax
jne    11a1 <sysctl_tcp_mem+0x61>
movl   $0x0,-0x4(%rbp)
jmp    12cb <sysctl_tcp_mem+0x18b>
mov    -0x10(%rbp),%rdi
lea    -0x1c0(%rbp),%rsi
mov    $0xc4,%edx
call   1060 <bpf_sysctl_get_current_value@plt>
mov    %eax,-0x1c8(%rbp)
cmpl   $0x0,-0x1c8(%rbp)
jl     11d9 <sysctl_tcp_mem+0x99>
cmpl   $0xc4,-0x1c8(%rbp)
jl     11e5 <sysctl_tcp_mem+0xa5>
movl   $0x0,-0x4(%rbp)
jmp    12cb <sysctl_tcp_mem+0x18b>
movb   $0x0,-0x1c1(%rbp)
movzbl -0x1c1(%rbp),%eax
cmp    $0x1c,%rax
jae    128a <sysctl_tcp_mem+0x14a>
lea    -0x1c0(%rbp),%rdi
movzbl -0x1c2(%rbp),%eax
cltq
add    %rax,%rdi
lea    -0xf0(%rbp),%rcx
movzbl -0x1c1(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rcx
mov    $0x7,%esi
xor    %edx,%edx
call   1050 <bpf_strtoul@plt>
mov    %eax,-0x1c8(%rbp)
cmpl   $0x0,-0x1c8(%rbp)
jle    1253 <sysctl_tcp_mem+0x113>
cmpl   $0x7,-0x1c8(%rbp)
jle    125f <sysctl_tcp_mem+0x11f>
movl   $0x0,-0x4(%rbp)
jmp    12cb <sysctl_tcp_mem+0x18b>
mov    -0x1c8(%rbp),%ecx
and    $0x7,%ecx
movzbl -0x1c2(%rbp),%eax
add    %ecx,%eax
mov    %al,-0x1c2(%rbp)
mov    -0x1c1(%rbp),%al
add    $0x1,%al
mov    %al,-0x1c1(%rbp)
jmp    11ec <sysctl_tcp_mem+0xac>
mov    -0xf0(%rbp),%rcx
xor    %eax,%eax
cmp    -0xe8(%rbp),%rcx
mov    %al,-0x1c9(%rbp)
jae    12bd <sysctl_tcp_mem+0x17d>
mov    -0xe8(%rbp),%rax
cmp    -0xe0(%rbp),%rax
setb   %al
mov    %al,-0x1c9(%rbp)
mov    -0x1c9(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1d0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysctl_tcp_mem>:
push   %r15
push   %r14
push   %rbx
sub    $0x1b0,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
xor    %r14d,%r14d
cmpq   $0x0,(%rdi)
je     11af <sysctl_tcp_mem+0x7f>
mov    %r14d,%eax
add    $0x1b0,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rdi,%rbx
call   1030 <is_tcp_mem@plt>
test   %eax,%eax
je     119f <sysctl_tcp_mem+0x6f>
lea    0xe0(%rsp),%rsi
mov    %rbx,%rdi
mov    $0xc4,%edx
call   1050 <bpf_sysctl_get_current_value@plt>
cmp    $0xc3,%eax
ja     119f <sysctl_tcp_mem+0x6f>
xor    %r14d,%r14d
xor    %ebx,%ebx
xor    %eax,%eax
xchg   %ax,%ax
movzbl %al,%r15d
lea    (%rsp,%r15,1),%rdi
add    $0xe0,%rdi
lea    (%rsp,%rbx,1),%rcx
mov    $0x7,%esi
xor    %edx,%edx
call   1040 <bpf_strtoul@plt>
lea    -0x8(%rax),%ecx
cmp    $0xfffffff9,%ecx
jb     119f <sysctl_tcp_mem+0x6f>
add    %r15d,%eax
add    $0x8,%rbx
cmp    $0xe0,%rbx
jne    11e0 <sysctl_tcp_mem+0xb0>
mov    0x8(%rsp),%rax
cmp    %rax,(%rsp)
setb   %cl
cmp    0x10(%rsp),%rax
setb   %al
and    %cl,%al
movzbl %al,%r14d
jmp    119f <sysctl_tcp_mem+0x6f>

LLVM IR (-O0):
%struct.bpf_sysctl = type { i64 }
@llvm.compiler.used = appending global [1 x i8*] [i8* bitcast (i32 (%struct.bpf_sysctl*)* @sysctl_tcp_mem to i8*)], section "llvm.metadata"
define i32 @sysctl_tcp_mem(%struct.bpf_sysctl* %0) section "cgroup/sysctl" {
%2 = alloca i32
%3 = alloca %struct.bpf_sysctl*
%4 = alloca [28 x i64]
%5 = alloca [196 x i8]
%6 = alloca i8
%7 = alloca i8
%8 = alloca i32
store %struct.bpf_sysctl* %0, %struct.bpf_sysctl** %3
%9 = bitcast [28 x i64]* %4 to i8*
store i8 0, i8* %7
%10 = load %struct.bpf_sysctl*, %struct.bpf_sysctl** %3
%11 = getelementptr inbounds %struct.bpf_sysctl, %struct.bpf_sysctl* %10, i32 0, i32 0
%12 = load i64, i64* %11
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %1
store i32 0, i32* %2
br label %78
15: ; preds = %1
%16 = load %struct.bpf_sysctl*, %struct.bpf_sysctl** %3
%17 = call i32 @is_tcp_mem(%struct.bpf_sysctl* %16)
%18 = icmp ne i32 %17, 0
br i1 %18, label %20, label %19
19: ; preds = %15
store i32 0, i32* %2
br label %78
20: ; preds = %15
%21 = load %struct.bpf_sysctl*, %struct.bpf_sysctl** %3
%22 = getelementptr inbounds [196 x i8], [196 x i8]* %5, i64 0, i64 0
%23 = call i32 @bpf_sysctl_get_current_value(%struct.bpf_sysctl* %21, i8* %22, i32 196)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp slt i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %20
%27 = load i32, i32* %8
%28 = icmp sge i32 %27, 196
br i1 %28, label %29, label %30
29: ; preds = %26, %20
store i32 0, i32* %2
br label %78
30: ; preds = %26
store i8 0, i8* %6
br label %31
31: ; preds = %60, %30
%32 = load i8, i8* %6
%33 = zext i8 %32 to i64
%34 = icmp ult i64 %33, 28
br i1 %34, label %35, label %63
35: ; preds = %31
%36 = getelementptr inbounds [196 x i8], [196 x i8]* %5, i64 0, i64 0
%37 = load i8, i8* %7
%38 = zext i8 %37 to i32
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %36, i64 %39
%41 = getelementptr inbounds [28 x i64], [28 x i64]* %4, i64 0, i64 0
%42 = load i8, i8* %6
%43 = zext i8 %42 to i32
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i64, i64* %41, i64 %44
%46 = call i32 @bpf_strtoul(i8* %40, i32 7, i32 0, i64* %45)
store i32 %46, i32* %8
%47 = load i32, i32* %8
%48 = icmp sle i32 %47, 0
br i1 %48, label %52, label %49
49: ; preds = %35
%50 = load i32, i32* %8
%51 = icmp sgt i32 %50, 7
br i1 %51, label %52, label %53
52: ; preds = %49, %35
store i32 0, i32* %2
br label %78
53: ; preds = %49
%54 = load i32, i32* %8
%55 = and i32 %54, 7
%56 = load i8, i8* %7
%57 = zext i8 %56 to i32
%58 = add nsw i32 %57, %55
%59 = trunc i32 %58 to i8
store i8 %59, i8* %7
br label %60
60: ; preds = %53
%61 = load i8, i8* %6
%62 = add i8 %61, 1
store i8 %62, i8* %6
br label %31
63: ; preds = %31
%64 = getelementptr inbounds [28 x i64], [28 x i64]* %4, i64 0, i64 0
%65 = load i64, i64* %64
%66 = getelementptr inbounds [28 x i64], [28 x i64]* %4, i64 0, i64 1
%67 = load i64, i64* %66
%68 = icmp ult i64 %65, %67
br i1 %68, label %69, label %75
69: ; preds = %63
%70 = getelementptr inbounds [28 x i64], [28 x i64]* %4, i64 0, i64 1
%71 = load i64, i64* %70
%72 = getelementptr inbounds [28 x i64], [28 x i64]* %4, i64 0, i64 2
%73 = load i64, i64* %72
%74 = icmp ult i64 %71, %73
br label %75
75: ; preds = %69, %63
%76 = phi i1 [ false, %63 ], [ %74, %69 ]
%77 = zext i1 %76 to i32
store i32 %77, i32* %2
br label %78
78: ; preds = %75, %52, %29, %19, %14
%79 = load i32, i32* %2
ret i32 %79
}

LLVM IR (-O2):
%struct.bpf_sysctl = type { i64 }
@llvm.compiler.used = appending global [1 x i8*] [i8* bitcast (i32 (%struct.bpf_sysctl*)* @sysctl_tcp_mem to i8*)], section "llvm.metadata"
define i32 @sysctl_tcp_mem(%struct.bpf_sysctl* %0) section "cgroup/sysctl" {
%2 = alloca [28 x i64]
%3 = alloca [196 x i8]
%4 = bitcast [28 x i64]* %2 to i8*
%5 = getelementptr inbounds [196 x i8], [196 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds %struct.bpf_sysctl, %struct.bpf_sysctl* %0, i64 0, i32 0
%7 = load i64, i64* %6
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %40
9: ; preds = %1
%10 = tail call i32 @is_tcp_mem(%struct.bpf_sysctl* %0)
%11 = icmp eq i32 %10, 0
br i1 %11, label %40, label %12
12: ; preds = %9
%13 = call i32 @bpf_sysctl_get_current_value(%struct.bpf_sysctl* %0, i8* %5, i32 196)
%14 = icmp ugt i32 %13, 195
br i1 %14, label %40, label %15
15: ; preds = %12, %25
%16 = phi i64 [ %27, %25 ], [ 0, %12 ]
%17 = phi i32 [ %26, %25 ], [ 0, %12 ]
%18 = and i32 %17, 255
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds [196 x i8], [196 x i8]* %3, i64 0, i64 %19
%21 = getelementptr inbounds [28 x i64], [28 x i64]* %2, i64 0, i64 %16
%22 = call i32 @bpf_strtoul(i8* %20, i32 7, i32 0, i64* %21)
%23 = add i32 %22, -8
%24 = icmp ult i32 %23, -7
br i1 %24, label %40, label %25
25: ; preds = %15
%26 = add nuw nsw i32 %22, %18
%27 = add nuw nsw i64 %16, 1
%28 = icmp eq i64 %27, 28
br i1 %28, label %29, label %15
29: ; preds = %25
%30 = getelementptr inbounds [28 x i64], [28 x i64]* %2, i64 0, i64 0
%31 = load i64, i64* %30
%32 = getelementptr inbounds [28 x i64], [28 x i64]* %2, i64 0, i64 1
%33 = load i64, i64* %32
%34 = icmp ult i64 %31, %33
%35 = getelementptr inbounds [28 x i64], [28 x i64]* %2, i64 0, i64 2
%36 = load i64, i64* %35
%37 = icmp ult i64 %33, %36
%38 = select i1 %34, i1 %37, i1 false
%39 = zext i1 %38 to i32
br label %40
40: ; preds = %15, %12, %9, %1, %29
%41 = phi i32 [ %39, %29 ], [ 0, %1 ], [ 0, %9 ], [ 0, %12 ], [ 0, %15 ]
ret i32 %41
}