#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 FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
scalar_t__ fcntl(int, int /*<<< orphan*/, int);
int sukat_util_flagit(int fd) {
  int flags;
  flags = fcntl(fd, F_GETFL, 0);
  if (flags >= 0) {
    flags |= O_NONBLOCK;
    if (fcntl(fd, F_SETFL, flags) == 0) {
      flags = fcntl(fd, F_GETFD, 0);
      if (flags >= 0) {
        flags |= FD_CLOEXEC;
        if (fcntl(fd, F_SETFD, flags) == 0) {
          return 0;
        }
      }
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<sukat_util_flagit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e93(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     11d0 <sukat_util_flagit+0xc0>
mov    0x2ea6(%rip),%rax
mov    (%rax),%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jne    11cb <sukat_util_flagit+0xbb>
mov    -0x8(%rbp),%edi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     11c6 <sukat_util_flagit+0xb6>
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jne    11c1 <sukat_util_flagit+0xb1>
movl   $0x0,-0x4(%rbp)
jmp    11d7 <sukat_util_flagit+0xc7>
jmp    11c6 <sukat_util_flagit+0xb6>
jmp    11cb <sukat_util_flagit+0xbb>
jmp    11d0 <sukat_util_flagit+0xc0>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sukat_util_flagit>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2e9c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
js     1149 <sukat_util_flagit+0x39>
mov    0x2eb8(%rip),%rcx
or     (%rcx),%eax
mov    0x2e97(%rip),%rcx
mov    (%rcx),%esi
mov    %ebx,%edi
mov    %eax,%edx
call   1030 <fcntl@plt>
test   %rax,%rax
je     1157 <sukat_util_flagit+0x47>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
mov    %ebx,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
js     1149 <sukat_util_flagit+0x39>
mov    0x2e52(%rip),%rcx
or     (%rcx),%eax
mov    0x2e59(%rip),%rcx
mov    (%rcx),%esi
mov    %ebx,%edi
mov    %eax,%edx
call   1030 <fcntl@plt>
test   %rax,%rax
jne    1149 <sukat_util_flagit+0x39>
jmp    114e <sukat_util_flagit+0x3e>

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

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