#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 long __u64;

/* Variables and functions */
scalar_t__ EBUSY;
int /*<<< orphan*/ F_ADD_SEALS;
int /*<<< orphan*/ F_GET_SEALS;
int /*<<< orphan*/ abort();
scalar_t__ errno;
long fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ printf(char *, int, unsigned long long, unsigned long long);
int mfd_busy_add_seals(int fd, __u64 seals) {
  long r;
  __u64 s;
  r = fcntl(fd, F_GET_SEALS);
  if (r < 0)
    s = 0;
  else
    s = r;
  r = fcntl(fd, F_ADD_SEALS, seals);
  if (r < 0 && errno != EBUSY) {
    printf(
        "ADD_SEALS(%d, %llu -> %llu) didn't fail as expected with EBUSY: %m\n",
        fd, (unsigned long long)s, (unsigned long long)seals);
    abort();
  }
  return r;
}


x86 assembly (clang, -O0):
<mfd_busy_add_seals>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e87(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    116e <mfd_busy_add_seals+0x3e>
movq   $0x0,-0x20(%rbp)
jmp    1176 <mfd_busy_add_seals+0x46>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11d4 <mfd_busy_add_seals+0xa4>
mov    0x2e35(%rip),%rax
mov    (%rax),%rax
mov    0x2e1b(%rip),%rcx
cmp    (%rcx),%rax
je     11d4 <mfd_busy_add_seals+0xa4>
mov    -0x4(%rbp),%esi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe38(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mfd_busy_add_seals>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1040 <fcntl@plt>
mov    %rax,%r14
mov    0x2e9d(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <fcntl@plt>
test   %rax,%rax
jns    117c <mfd_busy_add_seals+0x4c>
mov    0x2e6b(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e51(%rip),%rdx
cmp    (%rdx),%rcx
jne    1181 <mfd_busy_add_seals+0x51>
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
test   %r14,%r14
cmovs  %rax,%r14
lea    0xe6f(%rip),%rdi
mov    %ebp,%esi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1050 <printf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@F_GET_SEALS = global i32 0
@F_ADD_SEALS = global i32 0
@errno = global i64 0
@EBUSY = global i64 0
@.str = [68 x i8] c"ADD_SEALS(%d, %llu -> %llu) didn't fail as expected with EBUSY: %m\0A\00"
define i32 @mfd_busy_add_seals(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
store i32 %0, i32* %3
store i64 %1, i64* %4
%7 = load i32, i32* %3
%8 = load i32, i32* @F_GET_SEALS
%9 = call i64 (i32, i32, ...) @fcntl(i32 %7, i32 %8)
store i64 %9, i64* %5
%10 = load i64, i64* %5
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %2
store i64 0, i64* %6
br label %15
13: ; preds = %2
%14 = load i64, i64* %5
store i64 %14, i64* %6
br label %15
15: ; preds = %13, %12
%16 = load i32, i32* %3
%17 = load i32, i32* @F_ADD_SEALS
%18 = load i64, i64* %4
%19 = call i64 (i32, i32, ...) @fcntl(i32 %16, i32 %17, i64 %18)
store i64 %19, i64* %5
%20 = load i64, i64* %5
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %32
22: ; preds = %15
%23 = load i64, i64* @errno
%24 = load i64, i64* @EBUSY
%25 = icmp ne i64 %23, %24
br i1 %25, label %26, label %32
26: ; preds = %22
%27 = load i32, i32* %3
%28 = load i64, i64* %6
%29 = load i64, i64* %4
%30 = call i32 @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str, i64 0, i64 0), i32 %27, i64 %28, i64 %29)
%31 = call i32 (...) @abort()
unreachable
32: ; preds = %22, %15
%33 = load i64, i64* %5
%34 = trunc i64 %33 to i32
ret i32 %34
}

LLVM IR (-O2):
@F_GET_SEALS = global i32 0
@F_ADD_SEALS = global i32 0
@errno = global i64 0
@EBUSY = global i64 0
@.str = [68 x i8] c"ADD_SEALS(%d, %llu -> %llu) didn't fail as expected with EBUSY: %m\0A\00"
define i32 @mfd_busy_add_seals(i32 %0, i64 %1) {
%3 = load i32, i32* @F_GET_SEALS
%4 = tail call i64 (i32, i32, ...) @fcntl(i32 %0, i32 %3)
%5 = load i32, i32* @F_ADD_SEALS
%6 = tail call i64 (i32, i32, ...) @fcntl(i32 %0, i32 %5, i64 %1)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %17
8: ; preds = %2
%9 = load i64, i64* @errno
%10 = load i64, i64* @EBUSY
%11 = icmp eq i64 %9, %10
br i1 %11, label %17, label %12
12: ; preds = %8
%13 = icmp sgt i64 %4, 0
%14 = select i1 %13, i64 %4, i64 0
%15 = tail call i32 @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str, i64 0, i64 0), i32 %0, i64 %14, i64 %1)
%16 = tail call i32 (...) @abort()
unreachable
17: ; preds = %8, %2
%18 = trunc i64 %6 to i32
ret i32 %18
}