#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 */

/* Variables and functions */
scalar_t__ ENOSYS;
int MEMBARRIER_CMD_GLOBAL;
int /*<<< orphan*/ MEMBARRIER_CMD_QUERY;
scalar_t__ errno;
int /*<<< orphan*/ ksft_exit_fail_msg(char *);
int /*<<< orphan*/ ksft_exit_skip(char *);
int /*<<< orphan*/ ksft_test_result_fail(char *);
int /*<<< orphan*/ ksft_test_result_pass(char *);
int sys_membarrier(int /*<<< orphan*/, int);
int test_membarrier_query(void) {
  int flags = 0, ret;
  ret = sys_membarrier(MEMBARRIER_CMD_QUERY, flags);
  if (ret < 0) {
    if (errno == ENOSYS) {
      ksft_exit_skip("sys membarrier (CONFIG_MEMBARRIER) is disabled.\n");
    }
    ksft_exit_fail_msg("sys_membarrier() failed\n");
  }
  if (!(ret & MEMBARRIER_CMD_GLOBAL)) {
    ksft_test_result_fail("sys_membarrier() CMD_GLOBAL query failed\n");
    ksft_exit_fail_msg("sys_membarrier is not supported.\n");
  }
  ksft_test_result_pass("sys_membarrier available\n");
  return 0;
}


x86 assembly (clang, -O0):
<test_membarrier_query>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
call   1070 <sys_membarrier@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11af <test_membarrier_query+0x5f>
mov    0x2e44(%rip),%rax
mov    (%rax),%rax
mov    0x2e52(%rip),%rcx
cmp    (%rcx),%rax
jne    11a3 <test_membarrier_query+0x53>
lea    0xe62(%rip),%rdi
call   1050 <ksft_exit_skip@plt>
lea    0xe87(%rip),%rdi
call   1040 <ksft_exit_fail_msg@plt>
mov    -0x8(%rbp),%eax
mov    0x2e37(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    11dc <test_membarrier_query+0x8c>
lea    0xe7f(%rip),%rdi
call   1060 <ksft_test_result_fail@plt>
lea    0xe9d(%rip),%rdi
call   1040 <ksft_exit_fail_msg@plt>
lea    0xeb3(%rip),%rdi
call   1030 <ksft_test_result_pass@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_membarrier_query>:
push   %rbx
mov    0x2e80(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1070 <sys_membarrier@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1195 <test_membarrier_query+0x45>
mov    0x2e5a(%rip),%rax
mov    (%rax),%rax
mov    0x2e68(%rip),%rcx
cmp    (%rcx),%rax
jne    1189 <test_membarrier_query+0x39>
lea    0xe7c(%rip),%rdi
call   1050 <ksft_exit_skip@plt>
lea    0xea1(%rip),%rdi
call   1040 <ksft_exit_fail_msg@plt>
mov    0x2e54(%rip),%rax
test   %ebx,(%rax)
jne    11b8 <test_membarrier_query+0x68>
lea    0xea3(%rip),%rdi
call   1060 <ksft_test_result_fail@plt>
lea    0xec1(%rip),%rdi
call   1040 <ksft_exit_fail_msg@plt>
lea    0xed7(%rip),%rdi
call   1030 <ksft_test_result_pass@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@MEMBARRIER_CMD_QUERY = global i32 0
@errno = global i64 0
@ENOSYS = global i64 0
@.str = [49 x i8] c"sys membarrier (CONFIG_MEMBARRIER) is disabled.\0A\00"
@.str.1 = [25 x i8] c"sys_membarrier() failed\0A\00"
@MEMBARRIER_CMD_GLOBAL = global i32 0
@.str.2 = [42 x i8] c"sys_membarrier() CMD_GLOBAL query failed\0A\00"
@.str.3 = [34 x i8] c"sys_membarrier is not supported.\0A\00"
@.str.4 = [26 x i8] c"sys_membarrier available\0A\00"
define i32 @test_membarrier_query() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = load i32, i32* @MEMBARRIER_CMD_QUERY
%4 = load i32, i32* %1
%5 = call i32 @sys_membarrier(i32 %3, i32 %4)
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %16
8: ; preds = %0
%9 = load i64, i64* @errno
%10 = load i64, i64* @ENOSYS
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = call i32 @ksft_exit_skip(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %8
%15 = call i32 @ksft_exit_fail_msg(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %0
%17 = load i32, i32* %2
%18 = load i32, i32* @MEMBARRIER_CMD_GLOBAL
%19 = and i32 %17, %18
%20 = icmp ne i32 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %16
%22 = call i32 @ksft_test_result_fail(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @ksft_exit_fail_msg(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
br label %24
24: ; preds = %21, %16
%25 = call i32 @ksft_test_result_pass(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@MEMBARRIER_CMD_QUERY = global i32 0
@errno = global i64 0
@ENOSYS = global i64 0
@.str = [49 x i8] c"sys membarrier (CONFIG_MEMBARRIER) is disabled.\0A\00"
@.str.1 = [25 x i8] c"sys_membarrier() failed\0A\00"
@MEMBARRIER_CMD_GLOBAL = global i32 0
@.str.2 = [42 x i8] c"sys_membarrier() CMD_GLOBAL query failed\0A\00"
@.str.3 = [34 x i8] c"sys_membarrier is not supported.\0A\00"
@.str.4 = [26 x i8] c"sys_membarrier available\0A\00"
define i32 @test_membarrier_query() {
%1 = load i32, i32* @MEMBARRIER_CMD_QUERY
%2 = tail call i32 @sys_membarrier(i32 %1, i32 0)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %12
4: ; preds = %0
%5 = load i64, i64* @errno
%6 = load i64, i64* @ENOSYS
%7 = icmp eq i64 %5, %6
br i1 %7, label %8, label %10
8: ; preds = %4
%9 = tail call i32 @ksft_exit_skip(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %4
%11 = tail call i32 @ksft_exit_fail_msg(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = load i32, i32* @MEMBARRIER_CMD_GLOBAL
%14 = and i32 %13, %2
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = tail call i32 @ksft_test_result_fail(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
%18 = tail call i32 @ksft_exit_fail_msg(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
br label %19
19: ; preds = %16, %12
%20 = tail call i32 @ksft_test_result_pass(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
ret i32 0
}