#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;
};
typedef int __u8;

/* Variables and functions */
scalar_t__ IP_TOS;
scalar_t__ SOL_IP;
__attribute__((section("cgroup/setsockopt"), used)) int
_setsockopt(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;
  optval[0] += 0x10;
  ctx->optlen = 1;
  return 1;
}


x86 assembly (clang, -O0):
<_setsockopt>:
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 <_setsockopt+0x4f>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    0x2e9a(%rip),%rcx
cmp    (%rcx),%rax
je     115b <_setsockopt+0x5b>
movl   $0x1,-0x4(%rbp)
jmp    1196 <_setsockopt+0x96>
mov    -0x20(%rbp),%rax
add    $0x4,%rax
cmp    -0x18(%rbp),%rax
jbe    1179 <_setsockopt+0x79>
movl   $0x0,-0x4(%rbp)
jmp    1196 <_setsockopt+0x96>
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0x10,%ecx
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
movl   $0x1,0x20(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<_setsockopt>:
mov    0x10(%rdi),%rcx
mov    0x2eed(%rip),%rdx
mov    $0x1,%eax
cmp    (%rdx),%rcx
jne    1143 <_setsockopt+0x43>
mov    0x18(%rdi),%rcx
mov    0x2ec0(%rip),%rdx
cmp    (%rdx),%rcx
jne    1143 <_setsockopt+0x43>
mov    0x8(%rdi),%rcx
lea    0x4(%rcx),%rdx
xor    %eax,%eax
cmp    (%rdi),%rdx
ja     1143 <_setsockopt+0x43>
addl   $0x10,(%rcx)
movl   $0x1,0x20(%rdi)
mov    $0x1,%eax
ret

LLVM IR (-O0):
%struct.bpf_sockopt = type { i32*, i32*, i64, i64, i32 }
@SOL_IP = global i64 0
@IP_TOS = global i64 0
@llvm.compiler.used = appending global [1 x i8*] [i8* bitcast (i32 (%struct.bpf_sockopt*)* @_setsockopt to i8*)], section "llvm.metadata"
define i32 @_setsockopt(%struct.bpf_sockopt* %0) section "cgroup/setsockopt" {
%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 %37
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 %37
30: ; preds = %24
%31 = load i32*, i32** %5
%32 = getelementptr inbounds i32, i32* %31, i64 0
%33 = load i32, i32* %32
%34 = add nsw i32 %33, 16
store i32 %34, i32* %32
%35 = load %struct.bpf_sockopt*, %struct.bpf_sockopt** %3
%36 = getelementptr inbounds %struct.bpf_sockopt, %struct.bpf_sockopt* %35, i32 0, i32 4
store i32 1, i32* %36
store i32 1, i32* %2
br label %37
37: ; preds = %30, %29, %23
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
%struct.bpf_sockopt = type { i32*, i32*, i64, i64, i32 }
@SOL_IP = global i64 0
@IP_TOS = global i64 0
@llvm.compiler.used = appending global [1 x i8*] [i8* bitcast (i32 (%struct.bpf_sockopt*)* @_setsockopt to i8*)], section "llvm.metadata"
define i32 @_setsockopt(%struct.bpf_sockopt* nocapture %0) section "cgroup/setsockopt" {
%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 %22
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 %22
15: ; preds = %10
%16 = getelementptr inbounds i32, i32* %5, i64 1
%17 = icmp ugt i32* %16, %3
br i1 %17, label %22, label %18
18: ; preds = %15
%19 = load i32, i32* %5
%20 = add nsw i32 %19, 16
store i32 %20, i32* %5
%21 = getelementptr inbounds %struct.bpf_sockopt, %struct.bpf_sockopt* %0, i64 0, i32 4
store i32 1, i32* %21
br label %22
22: ; preds = %15, %1, %10, %18
%23 = phi i32 [ 1, %18 ], [ 1, %10 ], [ 1, %1 ], [ 0, %15 ]
ret i32 %23
}