#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_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int fcntl(int, int /*<<< orphan*/, int);
int block_save_flags(int fd) {
  int flags;
  flags = fcntl(fd, F_GETFL, 0);
  if (flags & O_NONBLOCK) {
    flags &= ~O_NONBLOCK;
    fcntl(fd, F_SETFL, flags);
    flags |= O_NONBLOCK;
  }
  return flags;
}


x86 assembly (clang, -O0):
<block_save_flags>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2eab(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2ead(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     117b <block_save_flags+0x6b>
mov    0x2e9b(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
call   1030 <fcntl@plt>
mov    0x2e75(%rip),%rax
mov    (%rax),%eax
or     -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<block_save_flags>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebp
mov    0x2eb3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,%ebx
mov    0x2eb9(%rip),%r14
mov    (%r14),%eax
test   %ebx,%eax
je     114f <block_save_flags+0x3f>
not    %eax
and    %eax,%ebx
mov    0x2e97(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %ebx,%edx
call   1030 <fcntl@plt>
or     (%r14),%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @block_save_flags(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = load i32, i32* @F_GETFL
%6 = call i32 @fcntl(i32 %4, i32 %5, i32 0)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = load i32, i32* @O_NONBLOCK
%9 = and i32 %7, %8
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %23
11: ; preds = %1
%12 = load i32, i32* @O_NONBLOCK
%13 = xor i32 %12, -1
%14 = load i32, i32* %3
%15 = and i32 %14, %13
store i32 %15, i32* %3
%16 = load i32, i32* %2
%17 = load i32, i32* @F_SETFL
%18 = load i32, i32* %3
%19 = call i32 @fcntl(i32 %16, i32 %17, i32 %18)
%20 = load i32, i32* @O_NONBLOCK
%21 = load i32, i32* %3
%22 = or i32 %21, %20
store i32 %22, i32* %3
br label %23
23: ; preds = %11, %1
%24 = load i32, i32* %3
ret i32 %24
}

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