#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 bpf_sockopt {
  int *optval_end;
  int *optval;
  scalar_t__ level;
  scalar_t__ optname;
  int optlen;
  scalar_t__ retval;
};
typedef int __u8;

/* Variables and functions */
scalar_t__ IP_TOS;
scalar_t__ SOL_IP;
__attribute__((section("cgroup/getsockopt/child"), used)) int
_getsockopt_child(struct bpf_sockopt *ctx) {
  __u8 *optval_end = ctx->optval_end;
  __u8 *optval = ctx->optval;
  if (ctx->level != SOL_IP || ctx->optname != IP_TOS)
    return 1;
  if (optval + 1 > optval_end)
    return 0;
  if (optval[0] != 0x80)
    return 0;
  ctx->retval = 0;
  optval[0] = 0x90;
  ctx->optlen = 1;
  return 1;
}


x86 assembly (clang, -O0):
<_getsockopt_child>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    0x2eca(%rip),%rcx
cmp    (%rcx),%rax
jne    114f <_getsockopt_child+0x4f>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    0x2e9a(%rip),%rcx
cmp    (%rcx),%rax
je     115b <_getsockopt_child+0x5b>
movl   $0x1,-0x4(%rbp)
jmp    11bd <_getsockopt_child+0xbd>
mov    -0x20(%rbp),%rax
add    $0x4,%rax
cmp    -0x18(%rbp),%rax
jbe    1179 <_getsockopt_child+0x79>
movl   $0x0,-0x4(%rbp)
jmp    11bd <_getsockopt_child+0xbd>
mov    -0x20(%rbp),%rax
cmpl   $0x80,(%rax)
je     1195 <_getsockopt_child+0x95>
movl   $0x0,-0x4(%rbp)
jmp    11bd <_getsockopt_child+0xbd>
mov    -0x10(%rbp),%rax
movq   $0x0,0x28(%rax)
mov    -0x20(%rbp),%rax
movl   $0x90,(%rax)
mov    -0x10(%rbp),%rax
movl   $0x1,0x20(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<_getsockopt_child>:
mov    0x10(%rdi),%rcx
mov    0x2eed(%rip),%rdx
mov    $0x1,%eax
cmp    (%rdx),%rcx
jne    1156 <_getsockopt_child+0x56>
mov    0x18(%rdi),%rcx
mov    0x2ec0(%rip),%rdx
cmp    (%rdx),%rcx
jne    1156 <_getsockopt_child+0x56>
mov    0x8(%rdi),%rcx
lea    0x4(%rcx),%rdx
xor    %eax,%eax
cmp    (%rdi),%rdx
ja     1156 <_getsockopt_child+0x56>
cmpl   $0x80,(%rcx)
jne    1156 <_getsockopt_child+0x56>
movq   $0x0,0x28(%rdi)
movl   $0x90,(%rcx)
movl   $0x1,0x20(%rdi)
mov    $0x1,%eax
ret

LLVM IR (-O0):
%struct.bpf_sockopt = type { i32*, i32*, i64, i64, i32, i64 }
@SOL_IP = global i64 0
@IP_TOS = global i64 0
@llvm.compiler.used = appending global [1 x i8*] [i8* bitcast (i32 (%struct.bpf_sockopt*)* @_getsockopt_child to i8*)], section "llvm.metadata"
define i32 @_getsockopt_child(%struct.bpf_sockopt* %0) section "cgroup/getsockopt/child" {
%2 = alloca i32
%3 = alloca %struct.bpf_sockopt*
%4 = alloca i32*
%5 = alloca i32*
store %struct.bpf_sockopt* %0, %struct.bpf_sockopt** %3
%6 = load %struct.bpf_sockopt*, %struct.bpf_sockopt** %3
%7 = getelementptr inbounds %struct.bpf_sockopt, %struct.bpf_sockopt* %6, i32 0, i32 0
%8 = load i32*, i32** %7
store i32* %8, i32** %4
%9 = load %struct.bpf_sockopt*, %struct.bpf_sockopt** %3
%10 = getelementptr inbounds %struct.bpf_sockopt, %struct.bpf_sockopt* %9, i32 0, i32 1
%11 = load i32*, i32** %10
store i32* %11, i32** %5
%12 = load %struct.bpf_sockopt*, %struct.bpf_sockopt** %3
%13 = getelementptr inbounds %struct.bpf_sockopt, %struct.bpf_sockopt* %12, i32 0, i32 2
%14 = load i64, i64* %13
%15 = load i64, i64* @SOL_IP
%16 = icmp ne i64 %14, %15
br i1 %16, label %23, label %17
17: ; preds = %1
%18 = load %struct.bpf_sockopt*, %struct.bpf_sockopt** %3
%19 = getelementptr inbounds %struct.bpf_sockopt, %struct.bpf_sockopt* %18, i32 0, i32 3
%20 = load i64, i64* %19
%21 = load i64, i64* @IP_TOS
%22 = icmp ne i64 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %17, %1
store i32 1, i32* %2
br label %43
24: ; preds = %17
%25 = load i32*, i32** %5
%26 = getelementptr inbounds i32, i32* %25, i64 1
%27 = load i32*, i32** %4
%28 = icmp ugt i32* %26, %27
br i1 %28, label %29, label %30
29: ; preds = %24
store i32 0, i32* %2
br label %43
30: ; preds = %24
%31 = load i32*, i32** %5
%32 = getelementptr inbounds i32, i32* %31, i64 0
%33 = load i32, i32* %32
%34 = icmp ne i32 %33, 128
br i1 %34, label %35, label %36
35: ; preds = %30
store i32 0, i32* %2
br label %43
36: ; preds = %30
%37 = load %struct.bpf_sockopt*, %struct.bpf_sockopt** %3
%38 = getelementptr inbounds %struct.bpf_sockopt, %struct.bpf_sockopt* %37, i32 0, i32 5
store i64 0, i64* %38
%39 = load i32*, i32** %5
%40 = getelementptr inbounds i32, i32* %39, i64 0
store i32 144, i32* %40
%41 = load %struct.bpf_sockopt*, %struct.bpf_sockopt** %3
%42 = getelementptr inbounds %struct.bpf_sockopt, %struct.bpf_sockopt* %41, i32 0, i32 4
store i32 1, i32* %42
store i32 1, i32* %2
br label %43
43: ; preds = %36, %35, %29, %23
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
%struct.bpf_sockopt = type { i32*, i32*, i64, i64, i32, i64 }
@SOL_IP = global i64 0
@IP_TOS = global i64 0
@llvm.compiler.used = appending global [1 x i8*] [i8* bitcast (i32 (%struct.bpf_sockopt*)* @_getsockopt_child to i8*)], section "llvm.metadata"
define i32 @_getsockopt_child(%struct.bpf_sockopt* nocapture %0) section "cgroup/getsockopt/child" {
%2 = getelementptr inbounds %struct.bpf_sockopt, %struct.bpf_sockopt* %0, i64 0, i32 0
%3 = load i32*, i32** %2
%4 = getelementptr inbounds %struct.bpf_sockopt, %struct.bpf_sockopt* %0, i64 0, i32 1
%5 = load i32*, i32** %4
%6 = getelementptr inbounds %struct.bpf_sockopt, %struct.bpf_sockopt* %0, i64 0, i32 2
%7 = load i64, i64* %6
%8 = load i64, i64* @SOL_IP
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %24
10: ; preds = %1
%11 = getelementptr inbounds %struct.bpf_sockopt, %struct.bpf_sockopt* %0, i64 0, i32 3
%12 = load i64, i64* %11
%13 = load i64, i64* @IP_TOS
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %24
15: ; preds = %10
%16 = getelementptr inbounds i32, i32* %5, i64 1
%17 = icmp ugt i32* %16, %3
br i1 %17, label %24, label %18
18: ; preds = %15
%19 = load i32, i32* %5
%20 = icmp eq i32 %19, 128
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = getelementptr inbounds %struct.bpf_sockopt, %struct.bpf_sockopt* %0, i64 0, i32 5
store i64 0, i64* %22
store i32 144, i32* %5
%23 = getelementptr inbounds %struct.bpf_sockopt, %struct.bpf_sockopt* %0, i64 0, i32 4
store i32 1, i32* %23
br label %24
24: ; preds = %18, %15, %1, %10, %21
%25 = phi i32 [ 1, %21 ], [ 1, %10 ], [ 1, %1 ], [ 0, %15 ], [ 0, %18 ]
ret i32 %25
}