#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_GROW;
int F_SEAL_SEAL;
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 mfd_assert_dup(int);
int /*<<< orphan*/ mfd_assert_has_seals(int, int);
int mfd_assert_new(char *, int, int);
int /*<<< orphan*/ mfd_fail_add_seals(int, int);
void test_share_dup(void) {
  int fd, fd2;
  fd = mfd_assert_new("kern_memfd_share_dup", 8192,
                      MFD_CLOEXEC | MFD_ALLOW_SEALING);
  mfd_assert_has_seals(fd, 0);
  fd2 = mfd_assert_dup(fd);
  mfd_assert_has_seals(fd2, 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);
  mfd_assert_add_seals(fd, 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);
  mfd_fail_add_seals(fd, F_SEAL_GROW);
  mfd_fail_add_seals(fd2, F_SEAL_GROW);
  mfd_fail_add_seals(fd, F_SEAL_SEAL);
  mfd_fail_add_seals(fd2, F_SEAL_SEAL);
  close(fd2);
  mfd_fail_add_seals(fd, F_SEAL_GROW);
  close(fd);
}


x86 assembly (clang, -O0):
<test_share_dup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e51(%rip),%rax
mov    (%rax),%edx
mov    0x2e70(%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
call   1060 <mfd_assert_dup@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1040 <mfd_assert_has_seals@plt>
mov    -0x4(%rbp),%edi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
call   1080 <mfd_assert_add_seals@plt>
mov    -0x4(%rbp),%edi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
call   1040 <mfd_assert_has_seals@plt>
mov    -0x8(%rbp),%edi
mov    0x2def(%rip),%rax
mov    (%rax),%esi
call   1040 <mfd_assert_has_seals@plt>
mov    -0x8(%rbp),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
call   1080 <mfd_assert_add_seals@plt>
mov    -0x4(%rbp),%edi
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
mov    0x2dd4(%rip),%rax
or     (%rax),%esi
call   1040 <mfd_assert_has_seals@plt>
mov    -0x8(%rbp),%edi
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
mov    0x2dba(%rip),%rax
or     (%rax),%esi
call   1040 <mfd_assert_has_seals@plt>
mov    -0x4(%rbp),%edi
mov    0x2d81(%rip),%rax
mov    (%rax),%esi
call   1080 <mfd_assert_add_seals@plt>
mov    -0x4(%rbp),%edi
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
mov    0x2d8f(%rip),%rax
or     (%rax),%esi
mov    0x2d5e(%rip),%rax
or     (%rax),%esi
call   1040 <mfd_assert_has_seals@plt>
mov    -0x8(%rbp),%edi
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
mov    0x2d6c(%rip),%rax
or     (%rax),%esi
mov    0x2d3b(%rip),%rax
or     (%rax),%esi
call   1040 <mfd_assert_has_seals@plt>
mov    -0x4(%rbp),%edi
mov    0x2d5a(%rip),%rax
mov    (%rax),%esi
call   1050 <mfd_fail_add_seals@plt>
mov    -0x8(%rbp),%edi
mov    0x2d49(%rip),%rax
mov    (%rax),%esi
call   1050 <mfd_fail_add_seals@plt>
mov    -0x4(%rbp),%edi
mov    0x2d08(%rip),%rax
mov    (%rax),%esi
call   1050 <mfd_fail_add_seals@plt>
mov    -0x8(%rbp),%edi
mov    0x2cf7(%rip),%rax
mov    (%rax),%esi
call   1050 <mfd_fail_add_seals@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    -0x4(%rbp),%edi
mov    0x2d0e(%rip),%rax
mov    (%rax),%esi
call   1050 <mfd_fail_add_seals@plt>
mov    -0x4(%rbp),%edi
call   1070 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_share_dup>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e51(%rip),%rax
mov    0x2e72(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe7f(%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   1060 <mfd_assert_dup@plt>
mov    %eax,%ebp
mov    %eax,%edi
xor    %esi,%esi
call   1040 <mfd_assert_has_seals@plt>
mov    0x2e19(%rip),%r14
mov    (%r14),%esi
mov    %ebx,%edi
call   1080 <mfd_assert_add_seals@plt>
mov    (%r14),%esi
mov    %ebx,%edi
call   1040 <mfd_assert_has_seals@plt>
mov    (%r14),%esi
mov    %ebp,%edi
call   1040 <mfd_assert_has_seals@plt>
mov    0x2e04(%rip),%r12
mov    (%r12),%esi
mov    %ebp,%edi
call   1080 <mfd_assert_add_seals@plt>
mov    (%r12),%esi
or     (%r14),%esi
mov    %ebx,%edi
call   1040 <mfd_assert_has_seals@plt>
mov    (%r12),%esi
or     (%r14),%esi
mov    %ebp,%edi
call   1040 <mfd_assert_has_seals@plt>
mov    0x2dae(%rip),%r15
mov    (%r15),%esi
mov    %ebx,%edi
call   1080 <mfd_assert_add_seals@plt>
mov    (%r12),%esi
or     (%r14),%esi
or     (%r15),%esi
mov    %ebx,%edi
call   1040 <mfd_assert_has_seals@plt>
mov    (%r12),%esi
or     (%r14),%esi
or     (%r15),%esi
mov    %ebp,%edi
call   1040 <mfd_assert_has_seals@plt>
mov    0x2dab(%rip),%r14
mov    (%r14),%esi
mov    %ebx,%edi
call   1050 <mfd_fail_add_seals@plt>
mov    (%r14),%esi
mov    %ebp,%edi
call   1050 <mfd_fail_add_seals@plt>
mov    (%r15),%esi
mov    %ebx,%edi
call   1050 <mfd_fail_add_seals@plt>
mov    (%r15),%esi
mov    %ebp,%edi
call   1050 <mfd_fail_add_seals@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    (%r14),%esi
mov    %ebx,%edi
call   1050 <mfd_fail_add_seals@plt>
mov    %ebx,%edi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <close@plt>

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

LLVM IR (-O2):
@.str = [21 x i8] c"kern_memfd_share_dup\00"
@MFD_CLOEXEC = global i32 0
@MFD_ALLOW_SEALING = global i32 0
@F_SEAL_WRITE = global i32 0
@F_SEAL_SHRINK = global i32 0
@F_SEAL_SEAL = global i32 0
@F_SEAL_GROW = global i32 0
define void @test_share_dup() {
%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 ([21 x i8], [21 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 @mfd_assert_dup(i32 %4)
%7 = tail call i32 @mfd_assert_has_seals(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 %6, i32 %12)
%14 = load i32, i32* @F_SEAL_SHRINK
%15 = tail call i32 @mfd_assert_add_seals(i32 %6, 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 %6, i32 %22)
%24 = load i32, i32* @F_SEAL_SEAL
%25 = tail call i32 @mfd_assert_add_seals(i32 %4, i32 %24)
%26 = load i32, i32* @F_SEAL_WRITE
%27 = load i32, i32* @F_SEAL_SHRINK
%28 = or i32 %27, %26
%29 = load i32, i32* @F_SEAL_SEAL
%30 = or i32 %28, %29
%31 = tail call i32 @mfd_assert_has_seals(i32 %4, i32 %30)
%32 = load i32, i32* @F_SEAL_WRITE
%33 = load i32, i32* @F_SEAL_SHRINK
%34 = or i32 %33, %32
%35 = load i32, i32* @F_SEAL_SEAL
%36 = or i32 %34, %35
%37 = tail call i32 @mfd_assert_has_seals(i32 %6, i32 %36)
%38 = load i32, i32* @F_SEAL_GROW
%39 = tail call i32 @mfd_fail_add_seals(i32 %4, i32 %38)
%40 = load i32, i32* @F_SEAL_GROW
%41 = tail call i32 @mfd_fail_add_seals(i32 %6, i32 %40)
%42 = load i32, i32* @F_SEAL_SEAL
%43 = tail call i32 @mfd_fail_add_seals(i32 %4, i32 %42)
%44 = load i32, i32* @F_SEAL_SEAL
%45 = tail call i32 @mfd_fail_add_seals(i32 %6, i32 %44)
%46 = tail call i32 @close(i32 %6)
%47 = load i32, i32* @F_SEAL_GROW
%48 = tail call i32 @mfd_fail_add_seals(i32 %4, i32 %47)
%49 = tail call i32 @close(i32 %4)
ret void
}