#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 */
scalar_t__ EINTR;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ assert(int);
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, int const);
void append_flag(int fd, int flag) {
  int err;
  do {
    err = fcntl(fd, F_GETFL, 0);
  } while (err == -1 && errno == EINTR);
  assert(err >= 0);
  const int flags = err | flag;
  do {
    err = fcntl(fd, F_SETFL, flags);
  } while (err == -1 && errno == EINTR);
  assert(err >= 0);
}


x86 assembly (clang, -O0):
<append_flag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e98(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0xc(%rbp)
mov    %al,-0x11(%rbp)
jne    116d <append_flag+0x4d>
mov    0x2e7e(%rip),%rax
mov    (%rax),%rax
mov    0x2e64(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    112e <append_flag+0xe>
cmpl   $0x0,-0xc(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0xc(%rbp),%eax
or     -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0xc(%rbp)
mov    %al,-0x12(%rbp)
jne    11d2 <append_flag+0xb2>
mov    0x2e19(%rip),%rax
mov    (%rax),%rax
mov    0x2dff(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x12(%rbp)
mov    -0x12(%rbp),%al
test   $0x1,%al
jne    1192 <append_flag+0x72>
cmpl   $0x0,-0xc(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_flag>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%ebp
mov    0x2e99(%rip),%r13
mov    0x2e9a(%rip),%r15
mov    0x2e83(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x0(%r13),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    116d <append_flag+0x4d>
mov    (%r12),%rax
cmp    %rax,(%r15)
je     1150 <append_flag+0x30>
mov    %ebx,%edi
not    %edi
shr    $0x1f,%edi
call   1040 <assert@plt>
or     %ebx,%r14d
mov    0x2e5d(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%rbx),%esi
mov    %ebp,%edi
mov    %r14d,%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11aa <append_flag+0x8a>
mov    (%r12),%rcx
cmp    %rcx,(%r15)
je     1190 <append_flag+0x70>
not    %eax
shr    $0x1f,%eax
mov    %eax,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <assert@plt>

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

LLVM IR (-O2):
@F_GETFL = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@F_SETFL = global i32 0
define void @append_flag(i32 %0, i32 %1) {
br label %3
3: ; preds = %3, %2
%4 = load i32, i32* @F_GETFL
%5 = tail call i32 @fcntl(i32 %0, i32 %4, i32 0)
%6 = icmp eq i32 %5, -1
%7 = load i64, i64* @errno
%8 = load i64, i64* @EINTR
%9 = icmp eq i64 %7, %8
%10 = select i1 %6, i1 %9, i1 false
br i1 %10, label %3, label %11
11: ; preds = %3
%12 = xor i32 %5, -1
%13 = lshr i32 %12, 31
%14 = tail call i32 @assert(i32 %13)
%15 = or i32 %5, %1
br label %16
16: ; preds = %16, %11
%17 = load i32, i32* @F_SETFL
%18 = tail call i32 @fcntl(i32 %0, i32 %17, i32 %15)
%19 = icmp eq i32 %18, -1
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
%23 = select i1 %19, i1 %22, i1 false
br i1 %23, label %16, label %24
24: ; preds = %16
%25 = xor i32 %18, -1
%26 = lshr i32 %25, 31
%27 = tail call i32 @assert(i32 %26)
ret void
}