#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 /*<<< orphan*/ F_SEAL_WRITE;
int MFD_ALLOW_SEALING;
int MFD_CLOEXEC;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ mfd_assert_add_seals(int, int /*<<< orphan*/);
int /*<<< orphan*/ mfd_assert_grow(int);
int /*<<< orphan*/ mfd_assert_has_seals(int, int /*<<< orphan*/);
int mfd_assert_new(char *, int, int);
int /*<<< orphan*/ mfd_assert_read(int);
int /*<<< orphan*/ mfd_assert_shrink(int);
int /*<<< orphan*/ mfd_fail_grow_write(int);
int /*<<< orphan*/ mfd_fail_write(int);
void test_seal_write(void) {
  int fd;
  fd = mfd_assert_new("kern_memfd_seal_write", 8192,
                      MFD_CLOEXEC | MFD_ALLOW_SEALING);
  mfd_assert_has_seals(fd, 0);
  mfd_assert_add_seals(fd, F_SEAL_WRITE);
  mfd_assert_has_seals(fd, F_SEAL_WRITE);
  mfd_assert_read(fd);
  mfd_fail_write(fd);
  mfd_assert_shrink(fd);
  mfd_assert_grow(fd);
  mfd_fail_grow_write(fd);
  close(fd);
}


x86 assembly (clang, -O0):
<test_seal_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
mov    0x2e40(%rip),%rax
or     (%rax),%edx
lea    0xe4f(%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
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
call   10b0 <mfd_assert_add_seals@plt>
mov    -0x4(%rbp),%edi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
call   1040 <mfd_assert_has_seals@plt>
mov    -0x4(%rbp),%edi
call   1080 <mfd_assert_read@plt>
mov    -0x4(%rbp),%edi
call   10a0 <mfd_fail_write@plt>
mov    -0x4(%rbp),%edi
call   1090 <mfd_assert_shrink@plt>
mov    -0x4(%rbp),%edi
call   1060 <mfd_assert_grow@plt>
mov    -0x4(%rbp),%edi
call   1070 <mfd_fail_grow_write@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_seal_write>:
push   %r14
push   %rbx
push   %rax
mov    0x2e35(%rip),%rax
mov    0x2e46(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe53(%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    0x2e0f(%rip),%r14
mov    (%r14),%esi
mov    %ebx,%edi
call   10b0 <mfd_assert_add_seals@plt>
mov    (%r14),%esi
mov    %ebx,%edi
call   1040 <mfd_assert_has_seals@plt>
mov    %ebx,%edi
call   1080 <mfd_assert_read@plt>
mov    %ebx,%edi
call   10a0 <mfd_fail_write@plt>
mov    %ebx,%edi
call   1090 <mfd_assert_shrink@plt>
mov    %ebx,%edi
call   1060 <mfd_assert_grow@plt>
mov    %ebx,%edi
call   1070 <mfd_fail_grow_write@plt>
mov    %ebx,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <close@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"kern_memfd_seal_write\00"
@MFD_CLOEXEC = global i32 0
@MFD_ALLOW_SEALING = global i32 0
@F_SEAL_WRITE = global i32 0
define void @test_seal_write() {
%1 = alloca i32
%2 = load i32, i32* @MFD_CLOEXEC
%3 = load i32, i32* @MFD_ALLOW_SEALING
%4 = or i32 %2, %3
%5 = call i32 @mfd_assert_new(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 8192, i32 %4)
store i32 %5, i32* %1
%6 = load i32, i32* %1
%7 = call i32 @mfd_assert_has_seals(i32 %6, i32 0)
%8 = load i32, i32* %1
%9 = load i32, i32* @F_SEAL_WRITE
%10 = call i32 @mfd_assert_add_seals(i32 %8, i32 %9)
%11 = load i32, i32* %1
%12 = load i32, i32* @F_SEAL_WRITE
%13 = call i32 @mfd_assert_has_seals(i32 %11, i32 %12)
%14 = load i32, i32* %1
%15 = call i32 @mfd_assert_read(i32 %14)
%16 = load i32, i32* %1
%17 = call i32 @mfd_fail_write(i32 %16)
%18 = load i32, i32* %1
%19 = call i32 @mfd_assert_shrink(i32 %18)
%20 = load i32, i32* %1
%21 = call i32 @mfd_assert_grow(i32 %20)
%22 = load i32, i32* %1
%23 = call i32 @mfd_fail_grow_write(i32 %22)
%24 = load i32, i32* %1
%25 = call i32 @close(i32 %24)
ret void
}

LLVM IR (-O2):
@.str = [22 x i8] c"kern_memfd_seal_write\00"
@MFD_CLOEXEC = global i32 0
@MFD_ALLOW_SEALING = global i32 0
@F_SEAL_WRITE = global i32 0
define void @test_seal_write() {
%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 = load i32, i32* @F_SEAL_WRITE
%7 = tail call i32 @mfd_assert_add_seals(i32 %4, i32 %6)
%8 = load i32, i32* @F_SEAL_WRITE
%9 = tail call i32 @mfd_assert_has_seals(i32 %4, i32 %8)
%10 = tail call i32 @mfd_assert_read(i32 %4)
%11 = tail call i32 @mfd_fail_write(i32 %4)
%12 = tail call i32 @mfd_assert_shrink(i32 %4)
%13 = tail call i32 @mfd_assert_grow(i32 %4)
%14 = tail call i32 @mfd_fail_grow_write(i32 %4)
%15 = tail call i32 @close(i32 %4)
ret void
}