#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 */
int F_SEAL_SHRINK;
int F_SEAL_WRITE;
int MFD_ALLOW_SEALING;
int MFD_CLOEXEC;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ mfd_assert_add_seals(int, int);
int /*<<< orphan*/ mfd_assert_has_seals(int, int);
void *mfd_assert_mmap_private(int);
void *mfd_assert_mmap_shared(int);
int mfd_assert_new(char *, int, int);
int /*<<< orphan*/ mfd_fail_add_seals(int, int);
int /*<<< orphan*/ munmap(void *, int);
void test_share_mmap(void) {
  int fd;
  void *p;
  fd = mfd_assert_new("kern_memfd_share_mmap", 8192,
                      MFD_CLOEXEC | MFD_ALLOW_SEALING);
  mfd_assert_has_seals(fd, 0);
  p = mfd_assert_mmap_shared(fd);
  mfd_fail_add_seals(fd, F_SEAL_WRITE);
  mfd_assert_has_seals(fd, 0);
  mfd_assert_add_seals(fd, F_SEAL_SHRINK);
  mfd_assert_has_seals(fd, F_SEAL_SHRINK);
  munmap(p, 8192);
  p = mfd_assert_mmap_private(fd);
  mfd_assert_add_seals(fd, F_SEAL_WRITE);
  mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
  munmap(p, 8192);
  close(fd);
}


x86 assembly (clang, -O0):
<test_share_mmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
mov    0x2e50(%rip),%rax
or     (%rax),%edx
lea    0xe5f(%rip),%rdi
mov    $0x2000,%esi
call   1030 <mfd_assert_new@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%edi
xor    %esi,%esi
call   1040 <mfd_assert_has_seals@plt>
mov    -0x4(%rbp),%edi
call   1080 <mfd_assert_mmap_shared@plt>
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
call   1050 <mfd_fail_add_seals@plt>
mov    -0x4(%rbp),%edi
xor    %esi,%esi
call   1040 <mfd_assert_has_seals@plt>
mov    -0x4(%rbp),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
call   10a0 <mfd_assert_add_seals@plt>
mov    -0x4(%rbp),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
call   1040 <mfd_assert_has_seals@plt>
mov    -0x10(%rbp),%rdi
mov    $0x2000,%esi
call   1090 <munmap@plt>
mov    -0x4(%rbp),%edi
call   1060 <mfd_assert_mmap_private@plt>
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
call   10a0 <mfd_assert_add_seals@plt>
mov    -0x4(%rbp),%edi
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
mov    0x2da1(%rip),%rax
or     (%rax),%esi
call   1040 <mfd_assert_has_seals@plt>
mov    -0x10(%rbp),%rdi
mov    $0x2000,%esi
call   1090 <munmap@plt>
mov    -0x4(%rbp),%edi
call   1070 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_share_mmap>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2e39(%rip),%rax
mov    0x2e52(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe5f(%rip),%rdi
mov    $0x2000,%esi
call   1030 <mfd_assert_new@plt>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
call   1040 <mfd_assert_has_seals@plt>
mov    %ebx,%edi
call   1080 <mfd_assert_mmap_shared@plt>
mov    %rax,%r14
mov    0x2e09(%rip),%r15
mov    (%r15),%esi
mov    %ebx,%edi
call   1050 <mfd_fail_add_seals@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <mfd_assert_has_seals@plt>
mov    0x2dff(%rip),%r12
mov    (%r12),%esi
mov    %ebx,%edi
call   10a0 <mfd_assert_add_seals@plt>
mov    (%r12),%esi
mov    %ebx,%edi
call   1040 <mfd_assert_has_seals@plt>
mov    %r14,%rdi
mov    $0x2000,%esi
call   1090 <munmap@plt>
mov    %ebx,%edi
call   1060 <mfd_assert_mmap_private@plt>
mov    %rax,%r14
mov    (%r15),%esi
mov    %ebx,%edi
call   10a0 <mfd_assert_add_seals@plt>
mov    (%r12),%esi
or     (%r15),%esi
mov    %ebx,%edi
call   1040 <mfd_assert_has_seals@plt>
mov    %r14,%rdi
mov    $0x2000,%esi
call   1090 <munmap@plt>
mov    %ebx,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1070 <close@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"kern_memfd_share_mmap\00"
@MFD_CLOEXEC = global i32 0
@MFD_ALLOW_SEALING = global i32 0
@F_SEAL_WRITE = global i32 0
@F_SEAL_SHRINK = global i32 0
define void @test_share_mmap() {
%1 = alloca i32
%2 = alloca i8*
%3 = load i32, i32* @MFD_CLOEXEC
%4 = load i32, i32* @MFD_ALLOW_SEALING
%5 = or i32 %3, %4
%6 = call i32 @mfd_assert_new(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 8192, i32 %5)
store i32 %6, i32* %1
%7 = load i32, i32* %1
%8 = call i32 @mfd_assert_has_seals(i32 %7, i32 0)
%9 = load i32, i32* %1
%10 = call i8* @mfd_assert_mmap_shared(i32 %9)
store i8* %10, i8** %2
%11 = load i32, i32* %1
%12 = load i32, i32* @F_SEAL_WRITE
%13 = call i32 @mfd_fail_add_seals(i32 %11, i32 %12)
%14 = load i32, i32* %1
%15 = call i32 @mfd_assert_has_seals(i32 %14, i32 0)
%16 = load i32, i32* %1
%17 = load i32, i32* @F_SEAL_SHRINK
%18 = call i32 @mfd_assert_add_seals(i32 %16, i32 %17)
%19 = load i32, i32* %1
%20 = load i32, i32* @F_SEAL_SHRINK
%21 = call i32 @mfd_assert_has_seals(i32 %19, i32 %20)
%22 = load i8*, i8** %2
%23 = call i32 @munmap(i8* %22, i32 8192)
%24 = load i32, i32* %1
%25 = call i8* @mfd_assert_mmap_private(i32 %24)
store i8* %25, i8** %2
%26 = load i32, i32* %1
%27 = load i32, i32* @F_SEAL_WRITE
%28 = call i32 @mfd_assert_add_seals(i32 %26, i32 %27)
%29 = load i32, i32* %1
%30 = load i32, i32* @F_SEAL_WRITE
%31 = load i32, i32* @F_SEAL_SHRINK
%32 = or i32 %30, %31
%33 = call i32 @mfd_assert_has_seals(i32 %29, i32 %32)
%34 = load i8*, i8** %2
%35 = call i32 @munmap(i8* %34, i32 8192)
%36 = load i32, i32* %1
%37 = call i32 @close(i32 %36)
ret void
}

LLVM IR (-O2):
@.str = [22 x i8] c"kern_memfd_share_mmap\00"
@MFD_CLOEXEC = global i32 0
@MFD_ALLOW_SEALING = global i32 0
@F_SEAL_WRITE = global i32 0
@F_SEAL_SHRINK = global i32 0
define void @test_share_mmap() {
%1 = load i32, i32* @MFD_CLOEXEC
%2 = load i32, i32* @MFD_ALLOW_SEALING
%3 = or i32 %2, %1
%4 = tail call i32 @mfd_assert_new(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 8192, i32 %3)
%5 = tail call i32 @mfd_assert_has_seals(i32 %4, i32 0)
%6 = tail call i8* @mfd_assert_mmap_shared(i32 %4)
%7 = load i32, i32* @F_SEAL_WRITE
%8 = tail call i32 @mfd_fail_add_seals(i32 %4, i32 %7)
%9 = tail call i32 @mfd_assert_has_seals(i32 %4, i32 0)
%10 = load i32, i32* @F_SEAL_SHRINK
%11 = tail call i32 @mfd_assert_add_seals(i32 %4, i32 %10)
%12 = load i32, i32* @F_SEAL_SHRINK
%13 = tail call i32 @mfd_assert_has_seals(i32 %4, i32 %12)
%14 = tail call i32 @munmap(i8* %6, i32 8192)
%15 = tail call i8* @mfd_assert_mmap_private(i32 %4)
%16 = load i32, i32* @F_SEAL_WRITE
%17 = tail call i32 @mfd_assert_add_seals(i32 %4, i32 %16)
%18 = load i32, i32* @F_SEAL_WRITE
%19 = load i32, i32* @F_SEAL_SHRINK
%20 = or i32 %19, %18
%21 = tail call i32 @mfd_assert_has_seals(i32 %4, i32 %20)
%22 = tail call i32 @munmap(i8* %15, i32 8192)
%23 = tail call i32 @close(i32 %4)
ret void
}