#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_GETFD;
int /*<<< orphan*/ F_SETFD;
int errno;
scalar_t__ fcntl(int const, int /*<<< orphan*/, ...);
int glb_fd_setfd(int const fd, int const flag, bool const on) {
  int const old_flags = fcntl(fd, F_GETFD);
  if (old_flags >= 0) {
    int const new_flags = on ? (old_flags | flag) : (old_flags & ~flag);
    if (new_flags == old_flags || fcntl(fd, F_SETFD, new_flags) >= 0)
      return 0;
  }
  return -errno;
}


x86 assembly (clang, -O0):
<glb_fd_setfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ea5(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     11aa <glb_fd_setfd+0x9a>
cmpl   $0x0,-0x10(%rbp)
je     1159 <glb_fd_setfd+0x49>
mov    -0x14(%rbp),%eax
or     -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1167 <glb_fd_setfd+0x57>
mov    -0x14(%rbp),%eax
mov    -0xc(%rbp),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     1199 <glb_fd_setfd+0x89>
mov    -0x8(%rbp),%edi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jl     11a5 <glb_fd_setfd+0x95>
movl   $0x0,-0x4(%rbp)
jmp    11b8 <glb_fd_setfd+0xa8>
jmp    11aa <glb_fd_setfd+0x9a>
xor    %eax,%eax
mov    0x2e25(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<glb_fd_setfd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %esi,%ebx
mov    %edi,%r15d
mov    0x2ea9(%rip),%rax
mov    (%rax),%esi
xor    %r12d,%r12d
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
js     1169 <glb_fd_setfd+0x59>
mov    %eax,%ecx
or     %ebx,%ecx
not    %ebx
and    %eax,%ebx
test   %r14d,%r14d
cmovne %ecx,%ebx
cmp    %eax,%ebx
jne    114d <glb_fd_setfd+0x3d>
xor    %ebp,%ebp
jmp    1176 <glb_fd_setfd+0x66>
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
mov    %r15d,%edi
mov    %ebx,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %rax,%rax
jns    1176 <glb_fd_setfd+0x66>
mov    0x2e68(%rip),%rax
sub    (%rax),%r12d
mov    %r12d,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@F_GETFD = global i32 0
@F_SETFD = global i32 0
@errno = global i32 0
define i32 @glb_fd_setfd(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i32, i32* %5
%11 = load i32, i32* @F_GETFD
%12 = call i64 (i32, i32, ...) @fcntl(i32 %10, i32 %11)
%13 = trunc i64 %12 to i32
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp sge i32 %14, 0
br i1 %15, label %16, label %41
16: ; preds = %3
%17 = load i32, i32* %7
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load i32, i32* %8
%21 = load i32, i32* %6
%22 = or i32 %20, %21
br label %28
23: ; preds = %16
%24 = load i32, i32* %8
%25 = load i32, i32* %6
%26 = xor i32 %25, -1
%27 = and i32 %24, %26
br label %28
28: ; preds = %23, %19
%29 = phi i32 [ %22, %19 ], [ %27, %23 ]
store i32 %29, i32* %9
%30 = load i32, i32* %9
%31 = load i32, i32* %8
%32 = icmp eq i32 %30, %31
br i1 %32, label %39, label %33
33: ; preds = %28
%34 = load i32, i32* %5
%35 = load i32, i32* @F_SETFD
%36 = load i32, i32* %9
%37 = call i64 (i32, i32, ...) @fcntl(i32 %34, i32 %35, i32 %36)
%38 = icmp sge i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %33, %28
store i32 0, i32* %4
br label %44
40: ; preds = %33
br label %41
41: ; preds = %40, %3
%42 = load i32, i32* @errno
%43 = sub nsw i32 0, %42
store i32 %43, i32* %4
br label %44
44: ; preds = %41, %39
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
@F_GETFD = global i32 0
@F_SETFD = global i32 0
@errno = global i32 0
define i32 @glb_fd_setfd(i32 %0, i32 %1, i32 %2) {
%4 = load i32, i32* @F_GETFD
%5 = tail call i64 (i32, i32, ...) @fcntl(i32 %0, i32 %4)
%6 = trunc i64 %5 to i32
%7 = icmp sgt i32 %6, -1
br i1 %7, label %8, label %19
8: ; preds = %3
%9 = icmp eq i32 %2, 0
%10 = or i32 %6, %1
%11 = xor i32 %1, -1
%12 = and i32 %6, %11
%13 = select i1 %9, i32 %12, i32 %10
%14 = icmp eq i32 %13, %6
br i1 %14, label %22, label %15
15: ; preds = %8
%16 = load i32, i32* @F_SETFD
%17 = tail call i64 (i32, i32, ...) @fcntl(i32 %0, i32 %16, i32 %13)
%18 = icmp sgt i64 %17, -1
br i1 %18, label %22, label %19
19: ; preds = %15, %3
%20 = load i32, i32* @errno
%21 = sub nsw i32 0, %20
br label %22
22: ; preds = %15, %8, %19
%23 = phi i32 [ %21, %19 ], [ 0, %8 ], [ 0, %15 ]
ret i32 %23
}