#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int target_fd;
  int attach_type;
  void *prog_cnt;
  void *attach_flags;
  int /*<<< orphan*/ prog_ids;
  void *query_flags;
};
union bpf_attr {
  TYPE_1__ query;
};
typedef enum bpf_attach_type {
  ____Placeholder_bpf_attach_type
} bpf_attach_type;
typedef int /*<<< orphan*/ attr;
typedef void *__u32;

/* Variables and functions */
int /*<<< orphan*/ BPF_PROG_QUERY;
int /*<<< orphan*/ memset(union bpf_attr *, int /*<<< orphan*/, int);
int /*<<< orphan*/ ptr_to_u64(void **);
int sys_bpf(int /*<<< orphan*/, union bpf_attr *, int);
int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags,
                   __u32 *attach_flags, __u32 *prog_ids, __u32 *prog_cnt) {
  union bpf_attr attr;
  int ret;
  memset(&attr, 0, sizeof(attr));
  attr.query.target_fd = target_fd;
  attr.query.attach_type = type;
  attr.query.query_flags = query_flags;
  attr.query.prog_cnt = *prog_cnt;
  attr.query.prog_ids = ptr_to_u64(prog_ids);
  ret = sys_bpf(BPF_PROG_QUERY, &attr, sizeof(attr));
  if (attach_flags)
    *attach_flags = attr.query.attach_flags;
  *prog_cnt = attr.query.prog_cnt;
  return ret;
}


x86 assembly (clang, -O0):
<bpf_prog_query>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    %r9,-0x28(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <ptr_to_u64@plt>
mov    %eax,-0x38(%rbp)
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
lea    -0x50(%rbp),%rsi
mov    $0x28,%edx
call   1030 <sys_bpf@plt>
mov    %eax,-0x54(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11b9 <bpf_prog_query+0x89>
mov    -0x40(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x48(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x54(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bpf_prog_query>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9,%r15
mov    %r8,%r12
mov    %rcx,%r14
mov    %rdx,%r13
mov    %esi,%ebp
mov    %edi,%ebx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    %ebx,(%rsp)
mov    %ebp,0x4(%rsp)
mov    %r13,0x20(%rsp)
mov    (%r15),%rax
mov    %rax,0x8(%rsp)
mov    %r12,%rdi
call   1040 <ptr_to_u64@plt>
mov    %eax,0x18(%rsp)
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
mov    $0x28,%edx
call   1030 <sys_bpf@plt>
test   %r14,%r14
je     11a0 <bpf_prog_query+0x70>
mov    0x10(%rsp),%rcx
mov    %rcx,(%r14)
mov    0x8(%rsp),%rcx
mov    %rcx,(%r15)
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%union.bpf_attr = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32, i32, i8*, i8*, i32, i8* }
@BPF_PROG_QUERY = global i32 0
define i32 @bpf_prog_query(i32 %0, i32 %1, i8* %2, i8** %3, i8** %4, i8** %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8**
%11 = alloca i8**
%12 = alloca i8**
%13 = alloca %union.bpf_attr
%14 = alloca i32
store i32 %0, i32* %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i8** %3, i8*** %10
store i8** %4, i8*** %11
store i8** %5, i8*** %12
%15 = call i32 @memset(%union.bpf_attr* %13, i32 0, i32 40)
%16 = load i32, i32* %7
%17 = bitcast %union.bpf_attr* %13 to %struct.TYPE_2__*
%18 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %17, i32 0, i32 0
store i32 %16, i32* %18
%19 = load i32, i32* %8
%20 = bitcast %union.bpf_attr* %13 to %struct.TYPE_2__*
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 1
store i32 %19, i32* %21
%22 = load i8*, i8** %9
%23 = bitcast %union.bpf_attr* %13 to %struct.TYPE_2__*
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 5
store i8* %22, i8** %24
%25 = load i8**, i8*** %12
%26 = load i8*, i8** %25
%27 = bitcast %union.bpf_attr* %13 to %struct.TYPE_2__*
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 2
store i8* %26, i8** %28
%29 = load i8**, i8*** %11
%30 = call i32 @ptr_to_u64(i8** %29)
%31 = bitcast %union.bpf_attr* %13 to %struct.TYPE_2__*
%32 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %31, i32 0, i32 4
store i32 %30, i32* %32
%33 = load i32, i32* @BPF_PROG_QUERY
%34 = call i32 @sys_bpf(i32 %33, %union.bpf_attr* %13, i32 40)
store i32 %34, i32* %14
%35 = load i8**, i8*** %10
%36 = icmp ne i8** %35, null
br i1 %36, label %37, label %42
37: ; preds = %6
%38 = bitcast %union.bpf_attr* %13 to %struct.TYPE_2__*
%39 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %38, i32 0, i32 3
%40 = load i8*, i8** %39
%41 = load i8**, i8*** %10
store i8* %40, i8** %41
br label %42
42: ; preds = %37, %6
%43 = bitcast %union.bpf_attr* %13 to %struct.TYPE_2__*
%44 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %43, i32 0, i32 2
%45 = load i8*, i8** %44
%46 = load i8**, i8*** %12
store i8* %45, i8** %46
%47 = load i32, i32* %14
ret i32 %47
}

LLVM IR (-O2):
%union.bpf_attr = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32, i32, i8*, i8*, i32, i8* }
@BPF_PROG_QUERY = global i32 0
define i32 @bpf_prog_query(i32 %0, i32 %1, i8* %2, i8** writeonly %3, i8** %4, i8** nocapture %5) {
%7 = alloca %union.bpf_attr
%8 = bitcast %union.bpf_attr* %7 to i8*
%9 = call i32 @memset(%union.bpf_attr* %7, i32 0, i32 40)
%10 = getelementptr inbounds %union.bpf_attr, %union.bpf_attr* %7, i64 0, i32 0, i32 0
store i32 %0, i32* %10
%11 = getelementptr inbounds %union.bpf_attr, %union.bpf_attr* %7, i64 0, i32 0, i32 1
store i32 %1, i32* %11
%12 = getelementptr inbounds %union.bpf_attr, %union.bpf_attr* %7, i64 0, i32 0, i32 5
store i8* %2, i8** %12
%13 = load i8*, i8** %5
%14 = getelementptr inbounds %union.bpf_attr, %union.bpf_attr* %7, i64 0, i32 0, i32 2
store i8* %13, i8** %14
%15 = call i32 @ptr_to_u64(i8** %4)
%16 = getelementptr inbounds %union.bpf_attr, %union.bpf_attr* %7, i64 0, i32 0, i32 4
store i32 %15, i32* %16
%17 = load i32, i32* @BPF_PROG_QUERY
%18 = call i32 @sys_bpf(i32 %17, %union.bpf_attr* %7, i32 40)
%19 = icmp eq i8** %3, null
br i1 %19, label %23, label %20
20: ; preds = %6
%21 = getelementptr inbounds %union.bpf_attr, %union.bpf_attr* %7, i64 0, i32 0, i32 3
%22 = load i8*, i8** %21
store i8* %22, i8** %3
br label %23
23: ; preds = %20, %6
%24 = load i8*, i8** %14
store i8* %24, i8** %5
ret i32 %18
}