#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[3] = {0, 0, 0};
  char value[0x40];
  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, 0x40);
  if (ret < 0 || ret >= 0x40)
    return 0;
#pragma clang loop unroll(full)
  for (i = 0; i < (sizeof(tcp_mem) / sizeof((tcp_mem)[0])); ++i) {
    ret = bpf_strtoul(value + off, 0xF, 0, tcp_mem + i);
    if (ret <= 0 || ret > 0xF)
      return 0;
    off += ret & 0xF;
  }
  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    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
movb   $0x0,-0x72(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     117d <sysctl_tcp_mem+0x3d>
movl   $0x0,-0x4(%rbp)
jmp    1277 <sysctl_tcp_mem+0x137>
mov    -0x10(%rbp),%rdi
call   1040 <is_tcp_mem@plt>
cmp    $0x0,%eax
jne    119b <sysctl_tcp_mem+0x5b>
movl   $0x0,-0x4(%rbp)
jmp    1277 <sysctl_tcp_mem+0x137>
mov    -0x10(%rbp),%rdi
lea    -0x70(%rbp),%rsi
mov    $0x40,%edx
call   1060 <bpf_sysctl_get_current_value@plt>
mov    %eax,-0x78(%rbp)
cmpl   $0x0,-0x78(%rbp)
jl     11c4 <sysctl_tcp_mem+0x84>
cmpl   $0x40,-0x78(%rbp)
jl     11d0 <sysctl_tcp_mem+0x90>
movl   $0x0,-0x4(%rbp)
jmp    1277 <sysctl_tcp_mem+0x137>
movb   $0x0,-0x71(%rbp)
movzbl -0x71(%rbp),%eax
cmp    $0x3,%rax
jae    124b <sysctl_tcp_mem+0x10b>
lea    -0x70(%rbp),%rdi
movzbl -0x72(%rbp),%eax
cltq
add    %rax,%rdi
lea    -0x30(%rbp),%rcx
movzbl -0x71(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rcx
mov    $0xf,%esi
xor    %edx,%edx
call   1050 <bpf_strtoul@plt>
mov    %eax,-0x78(%rbp)
cmpl   $0x0,-0x78(%rbp)
jle    1223 <sysctl_tcp_mem+0xe3>
cmpl   $0xf,-0x78(%rbp)
jle    122f <sysctl_tcp_mem+0xef>
movl   $0x0,-0x4(%rbp)
jmp    1277 <sysctl_tcp_mem+0x137>
mov    -0x78(%rbp),%ecx
and    $0xf,%ecx
movzbl -0x72(%rbp),%eax
add    %ecx,%eax
mov    %al,-0x72(%rbp)
mov    -0x71(%rbp),%al
add    $0x1,%al
mov    %al,-0x71(%rbp)
jmp    11d4 <sysctl_tcp_mem+0x94>
mov    -0x30(%rbp),%rcx
xor    %eax,%eax
cmp    -0x28(%rbp),%rcx
mov    %al,-0x79(%rbp)
jae    126c <sysctl_tcp_mem+0x12c>
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
setb   %al
mov    %al,-0x79(%rbp)
mov    -0x79(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysctl_tcp_mem>:
push   %rbp
push   %r14
push   %rbx
sub    $0x60,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movq   $0x0,0x10(%rsp)
xor    %ebp,%ebp
cmpq   $0x0,(%rdi)
je     115b <sysctl_tcp_mem+0x2b>
mov    %ebp,%eax
add    $0x60,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %rdi,%rbx
call   1030 <is_tcp_mem@plt>
test   %eax,%eax
je     1150 <sysctl_tcp_mem+0x20>
lea    0x20(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x40,%edx
call   1050 <bpf_sysctl_get_current_value@plt>
cmp    $0x3f,%eax
ja     1150 <sysctl_tcp_mem+0x20>
lea    0x20(%rsp),%rdi
xor    %ebp,%ebp
mov    %rsp,%rcx
mov    $0xf,%esi
xor    %edx,%edx
call   1040 <bpf_strtoul@plt>
mov    %eax,%r14d
lea    -0x10(%r14),%eax
cmp    $0xfffffff1,%eax
jb     1150 <sysctl_tcp_mem+0x20>
mov    %r14d,%eax
lea    (%rsp,%rax,1),%rdi
add    $0x20,%rdi
lea    0x8(%rsp),%rcx
xor    %ebp,%ebp
mov    $0xf,%esi
xor    %edx,%edx
call   1040 <bpf_strtoul@plt>
lea    -0x10(%rax),%ecx
cmp    $0xfffffff1,%ecx
jb     1150 <sysctl_tcp_mem+0x20>
add    %r14d,%eax
lea    (%rsp,%rax,1),%rdi
add    $0x20,%rdi
lea    0x10(%rsp),%rcx
xor    %ebp,%ebp
mov    $0xf,%esi
xor    %edx,%edx
call   1040 <bpf_strtoul@plt>
add    $0xfffffff0,%eax
cmp    $0xfffffff1,%eax
jb     1150 <sysctl_tcp_mem+0x20>
mov    0x8(%rsp),%rax
cmp    %rax,(%rsp)
setb   %cl
cmp    0x10(%rsp),%rax
setb   %al
and    %cl,%al
movzbl %al,%ebp
jmp    1150 <sysctl_tcp_mem+0x20>

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 [3 x i64]
%5 = alloca [64 x i8]
%6 = alloca i8
%7 = alloca i8
%8 = alloca i32
store %struct.bpf_sysctl* %0, %struct.bpf_sysctl** %3
%9 = bitcast [3 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 [64 x i8], [64 x i8]* %5, i64 0, i64 0
%23 = call i32 @bpf_sysctl_get_current_value(%struct.bpf_sysctl* %21, i8* %22, i32 64)
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, 64
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, 3
br i1 %34, label %35, label %63
35: ; preds = %31
%36 = getelementptr inbounds [64 x i8], [64 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 [3 x i64], [3 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 15, 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, 15
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, 15
%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 [3 x i64], [3 x i64]* %4, i64 0, i64 0
%65 = load i64, i64* %64
%66 = getelementptr inbounds [3 x i64], [3 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 [3 x i64], [3 x i64]* %4, i64 0, i64 1
%71 = load i64, i64* %70
%72 = getelementptr inbounds [3 x i64], [3 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 [3 x i64]
%3 = alloca [64 x i8]
%4 = bitcast [3 x i64]* %2 to i8*
%5 = getelementptr inbounds [64 x i8], [64 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 %43
9: ; preds = %1
%10 = tail call i32 @is_tcp_mem(%struct.bpf_sysctl* %0)
%11 = icmp eq i32 %10, 0
br i1 %11, label %43, label %12
12: ; preds = %9
%13 = call i32 @bpf_sysctl_get_current_value(%struct.bpf_sysctl* %0, i8* %5, i32 64)
%14 = icmp ugt i32 %13, 63
br i1 %14, label %43, label %15
15: ; preds = %12
%16 = getelementptr inbounds [3 x i64], [3 x i64]* %2, i64 0, i64 0
%17 = call i32 @bpf_strtoul(i8* %5, i32 15, i32 0, i64* %16)
%18 = add i32 %17, -16
%19 = icmp ult i32 %18, -15
br i1 %19, label %43, label %20
20: ; preds = %15
%21 = zext i32 %17 to i64
%22 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 %21
%23 = getelementptr inbounds [3 x i64], [3 x i64]* %2, i64 0, i64 1
%24 = call i32 @bpf_strtoul(i8* %22, i32 15, i32 0, i64* %23)
%25 = add i32 %24, -16
%26 = icmp ult i32 %25, -15
br i1 %26, label %43, label %27
27: ; preds = %20
%28 = add nuw nsw i32 %24, %17
%29 = zext i32 %28 to i64
%30 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 %29
%31 = getelementptr inbounds [3 x i64], [3 x i64]* %2, i64 0, i64 2
%32 = call i32 @bpf_strtoul(i8* %30, i32 15, i32 0, i64* %31)
%33 = add i32 %32, -16
%34 = icmp ult i32 %33, -15
br i1 %34, label %43, label %35
35: ; preds = %27
%36 = load i64, i64* %16
%37 = load i64, i64* %23
%38 = icmp ult i64 %36, %37
%39 = load i64, i64* %31
%40 = icmp ult i64 %37, %39
%41 = select i1 %38, i1 %40, i1 false
%42 = zext i1 %41 to i32
br label %43
43: ; preds = %15, %20, %27, %12, %9, %1, %35
%44 = phi i32 [ %42, %35 ], [ 0, %1 ], [ 0, %9 ], [ 0, %12 ], [ 0, %27 ], [ 0, %20 ], [ 0, %15 ]
ret i32 %44
}