#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 fcntl(int, int /*<<< orphan*/, int);
int fcntl_fd_off(int fd, int flag) {
  int flags;
  int newflags;
  if ((flags = fcntl(fd, F_GETFD, 0)) == -1)
    return 0;
  if ((newflags = flags & ~flag) != flags)
    if (fcntl(fd, F_SETFD, newflags))
      return 0;
  return 1;
}


x86 assembly (clang, -O0):
<fcntl_fd_off>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2eb0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    1149 <fcntl_fd_off+0x39>
movl   $0x0,-0x4(%rbp)
jmp    1195 <fcntl_fd_off+0x85>
mov    -0x10(%rbp),%eax
mov    -0xc(%rbp),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    %eax,-0x14(%rbp)
cmp    -0x10(%rbp),%eax
je     118e <fcntl_fd_off+0x7e>
mov    -0x8(%rbp),%edi
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
mov    -0x14(%rbp),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%eax
je     1189 <fcntl_fd_off+0x79>
movl   $0x0,-0x4(%rbp)
jmp    1195 <fcntl_fd_off+0x85>
jmp    118e <fcntl_fd_off+0x7e>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fcntl_fd_off>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %edi,%ebx
mov    0x2eb9(%rip),%rax
mov    (%rax),%esi
xor    %r14d,%r14d
xor    %edx,%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1154 <fcntl_fd_off+0x44>
not    %ebp
and    %eax,%ebp
cmp    %eax,%ebp
je     114e <fcntl_fd_off+0x3e>
mov    0x2ea1(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %ebp,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
jne    1154 <fcntl_fd_off+0x44>
mov    $0x1,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@F_GETFD = global i32 0
@F_SETFD = global i32 0
define i32 @fcntl_fd_off(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %4
%9 = load i32, i32* @F_GETFD
%10 = call i32 @fcntl(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 0, i32* %3
br label %29
13: ; preds = %2
%14 = load i32, i32* %6
%15 = load i32, i32* %5
%16 = xor i32 %15, -1
%17 = and i32 %14, %16
store i32 %17, i32* %7
%18 = load i32, i32* %6
%19 = icmp ne i32 %17, %18
br i1 %19, label %20, label %28
20: ; preds = %13
%21 = load i32, i32* %4
%22 = load i32, i32* @F_SETFD
%23 = load i32, i32* %7
%24 = call i32 @fcntl(i32 %21, i32 %22, i32 %23)
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %20
store i32 0, i32* %3
br label %29
27: ; preds = %20
br label %28
28: ; preds = %27, %13
store i32 1, i32* %3
br label %29
29: ; preds = %28, %26, %12
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
@F_GETFD = global i32 0
@F_SETFD = global i32 0
define i32 @fcntl_fd_off(i32 %0, i32 %1) {
%3 = load i32, i32* @F_GETFD
%4 = tail call i32 @fcntl(i32 %0, i32 %3, i32 0)
%5 = icmp eq i32 %4, -1
br i1 %5, label %15, label %6
6: ; preds = %2
%7 = xor i32 %1, -1
%8 = and i32 %4, %7
%9 = icmp eq i32 %8, %4
br i1 %9, label %14, label %10
10: ; preds = %6
%11 = load i32, i32* @F_SETFD
%12 = tail call i32 @fcntl(i32 %0, i32 %11, i32 %8)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %10, %6
br label %15
15: ; preds = %10, %2, %14
%16 = phi i32 [ 1, %14 ], [ 0, %2 ], [ 0, %10 ]
ret i32 %16
}