#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 */
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ F_SEAL_SEAL;
int /*<<< orphan*/ F_SEAL_WRITE;
int MFD_ALLOW_SEALING;
int MFD_CLOEXEC;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ join_idle_thread(int /*<<< orphan*/);
int /*<<< orphan*/ mfd_assert_add_seals(int, int /*<<< orphan*/);
int /*<<< orphan*/ mfd_assert_has_seals(int, int /*<<< orphan*/);
int mfd_assert_new(char *, int, int);
int /*<<< orphan*/ mfd_fail_add_seals(int, int /*<<< orphan*/);
int /*<<< orphan*/ spawn_idle_thread(int /*<<< orphan*/);
void test_share_fork(void) {
  int fd;
  pid_t pid;
  fd = mfd_assert_new("kern_memfd_share_fork", 8192,
                      MFD_CLOEXEC | MFD_ALLOW_SEALING);
  mfd_assert_has_seals(fd, 0);
  pid = spawn_idle_thread(0);
  mfd_assert_add_seals(fd, F_SEAL_SEAL);
  mfd_assert_has_seals(fd, F_SEAL_SEAL);
  mfd_fail_add_seals(fd, F_SEAL_WRITE);
  mfd_assert_has_seals(fd, F_SEAL_SEAL);
  join_idle_thread(pid);
  mfd_fail_add_seals(fd, F_SEAL_WRITE);
  mfd_assert_has_seals(fd, F_SEAL_SEAL);
  close(fd);
}


x86 assembly (clang, -O0):
<test_share_fork>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e51(%rip),%rax
mov    (%rax),%edx
mov    0x2e60(%rip),%rax
or     (%rax),%edx
lea    0xe6f(%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>
xor    %edi,%edi
call   1090 <spawn_idle_thread@plt>
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
call   1080 <mfd_assert_add_seals@plt>
mov    -0x4(%rbp),%edi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
call   1040 <mfd_assert_has_seals@plt>
mov    -0x4(%rbp),%edi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
call   1050 <mfd_fail_add_seals@plt>
mov    -0x4(%rbp),%edi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
call   1040 <mfd_assert_has_seals@plt>
mov    -0x8(%rbp),%edi
call   1070 <join_idle_thread@plt>
mov    -0x4(%rbp),%edi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
call   1050 <mfd_fail_add_seals@plt>
mov    -0x4(%rbp),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
call   1040 <mfd_assert_has_seals@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_share_fork>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e52(%rip),%rax
mov    0x2e63(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe70(%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>
xor    %edi,%edi
call   1090 <spawn_idle_thread@plt>
mov    %eax,%r14d
mov    0x2e0a(%rip),%rbp
mov    0x0(%rbp),%esi
mov    %ebx,%edi
call   1080 <mfd_assert_add_seals@plt>
mov    0x0(%rbp),%esi
mov    %ebx,%edi
call   1040 <mfd_assert_has_seals@plt>
mov    0x2e07(%rip),%r15
mov    (%r15),%esi
mov    %ebx,%edi
call   1050 <mfd_fail_add_seals@plt>
mov    0x0(%rbp),%esi
mov    %ebx,%edi
call   1040 <mfd_assert_has_seals@plt>
mov    %r14d,%edi
call   1070 <join_idle_thread@plt>
mov    (%r15),%esi
mov    %ebx,%edi
call   1050 <mfd_fail_add_seals@plt>
mov    0x0(%rbp),%esi
mov    %ebx,%edi
call   1040 <mfd_assert_has_seals@plt>
mov    %ebx,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <close@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"kern_memfd_share_fork\00"
@MFD_CLOEXEC = global i32 0
@MFD_ALLOW_SEALING = global i32 0
@F_SEAL_SEAL = global i32 0
@F_SEAL_WRITE = global i32 0
define void @test_share_fork() {
%1 = alloca i32
%2 = alloca i32
%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 = call i32 @spawn_idle_thread(i32 0)
store i32 %9, i32* %2
%10 = load i32, i32* %1
%11 = load i32, i32* @F_SEAL_SEAL
%12 = call i32 @mfd_assert_add_seals(i32 %10, i32 %11)
%13 = load i32, i32* %1
%14 = load i32, i32* @F_SEAL_SEAL
%15 = call i32 @mfd_assert_has_seals(i32 %13, i32 %14)
%16 = load i32, i32* %1
%17 = load i32, i32* @F_SEAL_WRITE
%18 = call i32 @mfd_fail_add_seals(i32 %16, i32 %17)
%19 = load i32, i32* %1
%20 = load i32, i32* @F_SEAL_SEAL
%21 = call i32 @mfd_assert_has_seals(i32 %19, i32 %20)
%22 = load i32, i32* %2
%23 = call i32 @join_idle_thread(i32 %22)
%24 = load i32, i32* %1
%25 = load i32, i32* @F_SEAL_WRITE
%26 = call i32 @mfd_fail_add_seals(i32 %24, i32 %25)
%27 = load i32, i32* %1
%28 = load i32, i32* @F_SEAL_SEAL
%29 = call i32 @mfd_assert_has_seals(i32 %27, i32 %28)
%30 = load i32, i32* %1
%31 = call i32 @close(i32 %30)
ret void
}

LLVM IR (-O2):
@.str = [22 x i8] c"kern_memfd_share_fork\00"
@MFD_CLOEXEC = global i32 0
@MFD_ALLOW_SEALING = global i32 0
@F_SEAL_SEAL = global i32 0
@F_SEAL_WRITE = global i32 0
define void @test_share_fork() {
%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 i32 @spawn_idle_thread(i32 0)
%7 = load i32, i32* @F_SEAL_SEAL
%8 = tail call i32 @mfd_assert_add_seals(i32 %4, i32 %7)
%9 = load i32, i32* @F_SEAL_SEAL
%10 = tail call i32 @mfd_assert_has_seals(i32 %4, i32 %9)
%11 = load i32, i32* @F_SEAL_WRITE
%12 = tail call i32 @mfd_fail_add_seals(i32 %4, i32 %11)
%13 = load i32, i32* @F_SEAL_SEAL
%14 = tail call i32 @mfd_assert_has_seals(i32 %4, i32 %13)
%15 = tail call i32 @join_idle_thread(i32 %6)
%16 = load i32, i32* @F_SEAL_WRITE
%17 = tail call i32 @mfd_fail_add_seals(i32 %4, i32 %16)
%18 = load i32, i32* @F_SEAL_SEAL
%19 = tail call i32 @mfd_assert_has_seals(i32 %4, i32 %18)
%20 = tail call i32 @close(i32 %4)
ret void
}