#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_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_exit_skip("sys_membarrier unsupported: CMD_GLOBAL not found.\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    0x2e82(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
call   1060 <sys_membarrier@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    119f <test_membarrier_query+0x5f>
mov    0x2e54(%rip),%rax
mov    (%rax),%rax
mov    0x2e62(%rip),%rcx
cmp    (%rcx),%rax
jne    1193 <test_membarrier_query+0x53>
lea    0xe72(%rip),%rdi
call   1050 <ksft_exit_skip@plt>
lea    0xe97(%rip),%rdi
call   1040 <ksft_exit_fail_msg@plt>
mov    -0x8(%rbp),%eax
mov    0x2e47(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    11c0 <test_membarrier_query+0x80>
lea    0xe8f(%rip),%rdi
call   1050 <ksft_exit_skip@plt>
lea    0xeb6(%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    0x2e90(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1060 <sys_membarrier@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1185 <test_membarrier_query+0x45>
mov    0x2e6a(%rip),%rax
mov    (%rax),%rax
mov    0x2e78(%rip),%rcx
cmp    (%rcx),%rax
jne    1179 <test_membarrier_query+0x39>
lea    0xe8c(%rip),%rdi
call   1050 <ksft_exit_skip@plt>
lea    0xeb1(%rip),%rdi
call   1040 <ksft_exit_fail_msg@plt>
mov    0x2e64(%rip),%rax
test   %ebx,(%rax)
jne    119c <test_membarrier_query+0x5c>
lea    0xeb3(%rip),%rdi
call   1050 <ksft_exit_skip@plt>
lea    0xeda(%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 = [51 x i8] c"sys_membarrier unsupported: CMD_GLOBAL not found.\0A\00"
@.str.3 = [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 %23, label %21
21: ; preds = %16
%22 = call i32 @ksft_exit_skip(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %16
%24 = call i32 @ksft_test_result_pass(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, 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 = [51 x i8] c"sys_membarrier unsupported: CMD_GLOBAL not found.\0A\00"
@.str.3 = [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 %18
16: ; preds = %12
%17 = tail call i32 @ksft_exit_skip(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %16, %12
%19 = tail call i32 @ksft_test_result_pass(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
ret i32 0
}