#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_SEAL;
int F_SEAL_SHRINK;
int F_SEAL_WRITE;
int MFD_ALLOW_SEALING;
int MFD_CLOEXEC;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ mfd_assert_add_seals(int, int);
int /*<<< orphan*/ mfd_assert_has_seals(int, int);
int mfd_assert_new(char *, int, int);
int mfd_assert_open(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mfd_fail_add_seals(int, int);
void test_share_open(void) {
  int fd, fd2;
  fd = mfd_assert_new("kern_memfd_share_open", 8192,
                      MFD_CLOEXEC | MFD_ALLOW_SEALING);
  mfd_assert_has_seals(fd, 0);
  fd2 = mfd_assert_open(fd, O_RDWR, 0);
  mfd_assert_add_seals(fd, F_SEAL_WRITE);
  mfd_assert_has_seals(fd, F_SEAL_WRITE);
  mfd_assert_has_seals(fd2, F_SEAL_WRITE);
  mfd_assert_add_seals(fd2, F_SEAL_SHRINK);
  mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
  mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK);
  close(fd);
  fd = mfd_assert_open(fd2, O_RDONLY, 0);
  mfd_fail_add_seals(fd, F_SEAL_SEAL);
  mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
  mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK);
  close(fd2);
  fd2 = mfd_assert_open(fd, O_RDWR, 0);
  mfd_assert_add_seals(fd2, F_SEAL_SEAL);
  mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
  mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
  close(fd2);
  close(fd);
}


x86 assembly (clang, -O0):
<test_share_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e49(%rip),%rax
mov    (%rax),%edx
mov    0x2e68(%rip),%rax
or     (%rax),%edx
lea    0xe7f(%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    0x2e46(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <mfd_assert_open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
call   1080 <mfd_assert_add_seals@plt>
mov    -0x4(%rbp),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
call   1040 <mfd_assert_has_seals@plt>
mov    -0x8(%rbp),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
call   1040 <mfd_assert_has_seals@plt>
mov    -0x8(%rbp),%edi
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
call   1080 <mfd_assert_add_seals@plt>
mov    -0x4(%rbp),%edi
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
mov    0x2dcb(%rip),%rax
or     (%rax),%esi
call   1040 <mfd_assert_has_seals@plt>
mov    -0x8(%rbp),%edi
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
mov    0x2db1(%rip),%rax
or     (%rax),%esi
call   1040 <mfd_assert_has_seals@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%edi
mov    0x2da0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <mfd_assert_open@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2d5a(%rip),%rax
mov    (%rax),%esi
call   1050 <mfd_fail_add_seals@plt>
mov    -0x4(%rbp),%edi
mov    0x2d61(%rip),%rax
mov    (%rax),%esi
mov    0x2d68(%rip),%rax
or     (%rax),%esi
call   1040 <mfd_assert_has_seals@plt>
mov    -0x8(%rbp),%edi
mov    0x2d47(%rip),%rax
mov    (%rax),%esi
mov    0x2d4e(%rip),%rax
or     (%rax),%esi
call   1040 <mfd_assert_has_seals@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0x4(%rbp),%edi
mov    0x2d4d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <mfd_assert_open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2cf7(%rip),%rax
mov    (%rax),%esi
call   1080 <mfd_assert_add_seals@plt>
mov    -0x4(%rbp),%edi
mov    0x2cfe(%rip),%rax
mov    (%rax),%esi
mov    0x2d05(%rip),%rax
or     (%rax),%esi
mov    0x2cd4(%rip),%rax
or     (%rax),%esi
call   1040 <mfd_assert_has_seals@plt>
mov    -0x8(%rbp),%edi
mov    0x2cdb(%rip),%rax
mov    (%rax),%esi
mov    0x2ce2(%rip),%rax
or     (%rax),%esi
mov    0x2cb1(%rip),%rax
or     (%rax),%esi
call   1040 <mfd_assert_has_seals@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_share_open>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e46(%rip),%rax
mov    0x2e67(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe7c(%rip),%rdi
mov    $0x2000,%esi
call   1030 <mfd_assert_new@plt>
mov    %eax,%ebp
mov    %eax,%edi
xor    %esi,%esi
call   1040 <mfd_assert_has_seals@plt>
mov    0x2e48(%rip),%r15
mov    (%r15),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1070 <mfd_assert_open@plt>
mov    %eax,%ebx
mov    0x2e0b(%rip),%r14
mov    (%r14),%esi
mov    %ebp,%edi
call   1080 <mfd_assert_add_seals@plt>
mov    (%r14),%esi
mov    %ebp,%edi
call   1040 <mfd_assert_has_seals@plt>
mov    (%r14),%esi
mov    %ebx,%edi
call   1040 <mfd_assert_has_seals@plt>
mov    0x2df6(%rip),%r12
mov    (%r12),%esi
mov    %ebx,%edi
call   1080 <mfd_assert_add_seals@plt>
mov    (%r12),%esi
or     (%r14),%esi
mov    %ebp,%edi
call   1040 <mfd_assert_has_seals@plt>
mov    (%r12),%esi
or     (%r14),%esi
mov    %ebx,%edi
call   1040 <mfd_assert_has_seals@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1070 <mfd_assert_open@plt>
mov    %eax,%ebp
mov    0x2d85(%rip),%r13
mov    0x0(%r13),%esi
mov    %eax,%edi
call   1050 <mfd_fail_add_seals@plt>
mov    (%r12),%esi
or     (%r14),%esi
mov    %ebp,%edi
call   1040 <mfd_assert_has_seals@plt>
mov    (%r12),%esi
or     (%r14),%esi
mov    %ebx,%edi
call   1040 <mfd_assert_has_seals@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    (%r15),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1070 <mfd_assert_open@plt>
mov    %eax,%ebx
mov    0x0(%r13),%esi
mov    %eax,%edi
call   1080 <mfd_assert_add_seals@plt>
mov    (%r12),%esi
or     (%r14),%esi
or     0x0(%r13),%esi
mov    %ebp,%edi
call   1040 <mfd_assert_has_seals@plt>
mov    (%r12),%esi
or     (%r14),%esi
or     0x0(%r13),%esi
mov    %ebx,%edi
call   1040 <mfd_assert_has_seals@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <close@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"kern_memfd_share_open\00"
@MFD_CLOEXEC = global i32 0
@MFD_ALLOW_SEALING = global i32 0
@O_RDWR = global i32 0
@F_SEAL_WRITE = global i32 0
@F_SEAL_SHRINK = global i32 0
@O_RDONLY = global i32 0
@F_SEAL_SEAL = global i32 0
define void @test_share_open() {
%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 = load i32, i32* %1
%10 = load i32, i32* @O_RDWR
%11 = call i32 @mfd_assert_open(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %2
%12 = load i32, i32* %1
%13 = load i32, i32* @F_SEAL_WRITE
%14 = call i32 @mfd_assert_add_seals(i32 %12, i32 %13)
%15 = load i32, i32* %1
%16 = load i32, i32* @F_SEAL_WRITE
%17 = call i32 @mfd_assert_has_seals(i32 %15, i32 %16)
%18 = load i32, i32* %2
%19 = load i32, i32* @F_SEAL_WRITE
%20 = call i32 @mfd_assert_has_seals(i32 %18, i32 %19)
%21 = load i32, i32* %2
%22 = load i32, i32* @F_SEAL_SHRINK
%23 = call i32 @mfd_assert_add_seals(i32 %21, i32 %22)
%24 = load i32, i32* %1
%25 = load i32, i32* @F_SEAL_WRITE
%26 = load i32, i32* @F_SEAL_SHRINK
%27 = or i32 %25, %26
%28 = call i32 @mfd_assert_has_seals(i32 %24, i32 %27)
%29 = load i32, i32* %2
%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 i32, i32* %1
%35 = call i32 @close(i32 %34)
%36 = load i32, i32* %2
%37 = load i32, i32* @O_RDONLY
%38 = call i32 @mfd_assert_open(i32 %36, i32 %37, i32 0)
store i32 %38, i32* %1
%39 = load i32, i32* %1
%40 = load i32, i32* @F_SEAL_SEAL
%41 = call i32 @mfd_fail_add_seals(i32 %39, i32 %40)
%42 = load i32, i32* %1
%43 = load i32, i32* @F_SEAL_WRITE
%44 = load i32, i32* @F_SEAL_SHRINK
%45 = or i32 %43, %44
%46 = call i32 @mfd_assert_has_seals(i32 %42, i32 %45)
%47 = load i32, i32* %2
%48 = load i32, i32* @F_SEAL_WRITE
%49 = load i32, i32* @F_SEAL_SHRINK
%50 = or i32 %48, %49
%51 = call i32 @mfd_assert_has_seals(i32 %47, i32 %50)
%52 = load i32, i32* %2
%53 = call i32 @close(i32 %52)
%54 = load i32, i32* %1
%55 = load i32, i32* @O_RDWR
%56 = call i32 @mfd_assert_open(i32 %54, i32 %55, i32 0)
store i32 %56, i32* %2
%57 = load i32, i32* %2
%58 = load i32, i32* @F_SEAL_SEAL
%59 = call i32 @mfd_assert_add_seals(i32 %57, i32 %58)
%60 = load i32, i32* %1
%61 = load i32, i32* @F_SEAL_WRITE
%62 = load i32, i32* @F_SEAL_SHRINK
%63 = or i32 %61, %62
%64 = load i32, i32* @F_SEAL_SEAL
%65 = or i32 %63, %64
%66 = call i32 @mfd_assert_has_seals(i32 %60, i32 %65)
%67 = load i32, i32* %2
%68 = load i32, i32* @F_SEAL_WRITE
%69 = load i32, i32* @F_SEAL_SHRINK
%70 = or i32 %68, %69
%71 = load i32, i32* @F_SEAL_SEAL
%72 = or i32 %70, %71
%73 = call i32 @mfd_assert_has_seals(i32 %67, i32 %72)
%74 = load i32, i32* %2
%75 = call i32 @close(i32 %74)
%76 = load i32, i32* %1
%77 = call i32 @close(i32 %76)
ret void
}

LLVM IR (-O2):
@.str = [22 x i8] c"kern_memfd_share_open\00"
@MFD_CLOEXEC = global i32 0
@MFD_ALLOW_SEALING = global i32 0
@O_RDWR = global i32 0
@F_SEAL_WRITE = global i32 0
@F_SEAL_SHRINK = global i32 0
@O_RDONLY = global i32 0
@F_SEAL_SEAL = global i32 0
define void @test_share_open() {
%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* @O_RDWR
%7 = tail call i32 @mfd_assert_open(i32 %4, i32 %6, i32 0)
%8 = load i32, i32* @F_SEAL_WRITE
%9 = tail call i32 @mfd_assert_add_seals(i32 %4, i32 %8)
%10 = load i32, i32* @F_SEAL_WRITE
%11 = tail call i32 @mfd_assert_has_seals(i32 %4, i32 %10)
%12 = load i32, i32* @F_SEAL_WRITE
%13 = tail call i32 @mfd_assert_has_seals(i32 %7, i32 %12)
%14 = load i32, i32* @F_SEAL_SHRINK
%15 = tail call i32 @mfd_assert_add_seals(i32 %7, i32 %14)
%16 = load i32, i32* @F_SEAL_WRITE
%17 = load i32, i32* @F_SEAL_SHRINK
%18 = or i32 %17, %16
%19 = tail call i32 @mfd_assert_has_seals(i32 %4, i32 %18)
%20 = load i32, i32* @F_SEAL_WRITE
%21 = load i32, i32* @F_SEAL_SHRINK
%22 = or i32 %21, %20
%23 = tail call i32 @mfd_assert_has_seals(i32 %7, i32 %22)
%24 = tail call i32 @close(i32 %4)
%25 = load i32, i32* @O_RDONLY
%26 = tail call i32 @mfd_assert_open(i32 %7, i32 %25, i32 0)
%27 = load i32, i32* @F_SEAL_SEAL
%28 = tail call i32 @mfd_fail_add_seals(i32 %26, i32 %27)
%29 = load i32, i32* @F_SEAL_WRITE
%30 = load i32, i32* @F_SEAL_SHRINK
%31 = or i32 %30, %29
%32 = tail call i32 @mfd_assert_has_seals(i32 %26, i32 %31)
%33 = load i32, i32* @F_SEAL_WRITE
%34 = load i32, i32* @F_SEAL_SHRINK
%35 = or i32 %34, %33
%36 = tail call i32 @mfd_assert_has_seals(i32 %7, i32 %35)
%37 = tail call i32 @close(i32 %7)
%38 = load i32, i32* @O_RDWR
%39 = tail call i32 @mfd_assert_open(i32 %26, i32 %38, i32 0)
%40 = load i32, i32* @F_SEAL_SEAL
%41 = tail call i32 @mfd_assert_add_seals(i32 %39, i32 %40)
%42 = load i32, i32* @F_SEAL_WRITE
%43 = load i32, i32* @F_SEAL_SHRINK
%44 = or i32 %43, %42
%45 = load i32, i32* @F_SEAL_SEAL
%46 = or i32 %44, %45
%47 = tail call i32 @mfd_assert_has_seals(i32 %26, i32 %46)
%48 = load i32, i32* @F_SEAL_WRITE
%49 = load i32, i32* @F_SEAL_SHRINK
%50 = or i32 %49, %48
%51 = load i32, i32* @F_SEAL_SEAL
%52 = or i32 %50, %51
%53 = tail call i32 @mfd_assert_has_seals(i32 %39, i32 %52)
%54 = tail call i32 @close(i32 %39)
%55 = tail call i32 @close(i32 %26)
ret void
}