#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_on(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_on>:
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_on+0x39>
movl   $0x0,-0x4(%rbp)
jmp    1190 <fcntl_fd_on+0x80>
mov    -0x10(%rbp),%eax
or     -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmp    -0x10(%rbp),%eax
je     1189 <fcntl_fd_on+0x79>
mov    -0x8(%rbp),%edi
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
mov    -0x14(%rbp),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%eax
je     1184 <fcntl_fd_on+0x74>
movl   $0x0,-0x4(%rbp)
jmp    1190 <fcntl_fd_on+0x80>
jmp    1189 <fcntl_fd_on+0x79>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fcntl_fd_on>:
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     1152 <fcntl_fd_on+0x42>
or     %eax,%ebp
cmp    %eax,%ebp
je     114c <fcntl_fd_on+0x3c>
mov    0x2ea3(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %ebp,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
jne    1152 <fcntl_fd_on+0x42>
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_on(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 %28
13: ; preds = %2
%14 = load i32, i32* %6
%15 = load i32, i32* %5
%16 = or i32 %14, %15
store i32 %16, i32* %7
%17 = load i32, i32* %6
%18 = icmp ne i32 %16, %17
br i1 %18, label %19, label %27
19: ; preds = %13
%20 = load i32, i32* %4
%21 = load i32, i32* @F_SETFD
%22 = load i32, i32* %7
%23 = call i32 @fcntl(i32 %20, i32 %21, i32 %22)
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %19
store i32 0, i32* %3
br label %28
26: ; preds = %19
br label %27
27: ; preds = %26, %13
store i32 1, i32* %3
br label %28
28: ; preds = %27, %25, %12
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
@F_GETFD = global i32 0
@F_SETFD = global i32 0
define i32 @fcntl_fd_on(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 %14, label %6
6: ; preds = %2
%7 = or i32 %4, %1
%8 = icmp eq i32 %7, %4
br i1 %8, label %13, label %9
9: ; preds = %6
%10 = load i32, i32* @F_SETFD
%11 = tail call i32 @fcntl(i32 %0, i32 %10, i32 %7)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %9, %6
br label %14
14: ; preds = %9, %2, %13
%15 = phi i32 [ 1, %13 ], [ 0, %2 ], [ 0, %9 ]
ret i32 %15
}