#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 /*<<< orphan*/ STRERROR(int /*<<< orphan*/, char *);
int /*<<< orphan*/ errno;
scalar_t__ fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int unset_sock_flags(int sock, int unset_flags, char *errorstr) {
  int flags;
  if ((flags = fcntl(sock, F_GETFL, NULL)) < 0) {
    do {
      if (errorstr != NULL) {
        memcpy(errorstr,
               "F_GETFL"
               ": ",
               (sizeof("F_GETFL"
                       ": ") /
                    sizeof(char) -
                1));
        STRERROR(errno, errorstr + (sizeof("F_GETFL"
                                           ": ") /
                                        sizeof(char) -
                                    1));
      }
    } while (0);
    return 0;
  }
  if (fcntl(sock, F_SETFL, flags & ~unset_flags) < 0) {
    do {
      if (errorstr != NULL) {
        memcpy(errorstr,
               "F_SETFL"
               ": ",
               (sizeof("F_SETFL"
                       ": ") /
                    sizeof(char) -
                1));
        STRERROR(errno, errorstr + (sizeof("F_SETFL"
                                           ": ") /
                                        sizeof(char) -
                                    1));
      }
    } while (0);
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<unset_sock_flags>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11ad <unset_sock_flags+0x7d>
jmp    1166 <unset_sock_flags+0x36>
cmpq   $0x0,-0x18(%rbp)
je     119c <unset_sock_flags+0x6c>
mov    -0x18(%rbp),%rdi
lea    0xe84(%rip),%rsi
mov    $0x9,%edx
call   1040 <memcpy@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
add    $0x9,%rsi
call   1050 <STRERROR@plt>
jmp    11a1 <unset_sock_flags+0x71>
movl   $0x0,-0x4(%rbp)
jmp    1226 <unset_sock_flags+0xf6>
mov    -0x8(%rbp),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
mov    -0xc(%rbp),%eax
xor    $0xffffffff,%eax
and    %eax,%edx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    121f <unset_sock_flags+0xef>
jmp    11d8 <unset_sock_flags+0xa8>
cmpq   $0x0,-0x18(%rbp)
je     120e <unset_sock_flags+0xde>
mov    -0x18(%rbp),%rdi
lea    0xe1c(%rip),%rsi
mov    $0x9,%edx
call   1040 <memcpy@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
add    $0x9,%rsi
call   1050 <STRERROR@plt>
jmp    1213 <unset_sock_flags+0xe3>
movl   $0x0,-0x4(%rbp)
jmp    1226 <unset_sock_flags+0xf6>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unset_sock_flags>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%ebx
mov    %edi,%r15d
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
xor    %edx,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
js     1184 <unset_sock_flags+0x54>
mov    0x2e84(%rip),%rcx
mov    (%rcx),%esi
not    %ebx
and    %ebx,%eax
mov    %r15d,%edi
mov    %eax,%edx
call   1030 <fcntl@plt>
mov    $0x1,%ebp
test   %rax,%rax
jns    11b4 <unset_sock_flags+0x84>
test   %r14,%r14
je     11b2 <unset_sock_flags+0x82>
lea    0xe88(%rip),%rsi
jmp    1190 <unset_sock_flags+0x60>
test   %r14,%r14
je     11b4 <unset_sock_flags+0x84>
lea    0xe70(%rip),%rsi
mov    %r14,%rdi
mov    $0x9,%edx
call   1040 <memcpy@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
add    $0x9,%r14
mov    %r14,%rsi
call   1050 <STRERROR@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@F_GETFL = global i32 0
@.str = [10 x i8] c"F_GETFL: \00"
@errno = global i32 0
@F_SETFL = global i32 0
@.str.1 = [10 x i8] c"F_SETFL: \00"
define i32 @unset_sock_flags(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%9 = load i32, i32* %5
%10 = load i32, i32* @F_GETFL
%11 = call i64 @fcntl(i32 %9, i32 %10, i32 0)
%12 = trunc i64 %11 to i32
store i32 %12, i32* %8
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %27
14: ; preds = %3
br label %15
15: ; preds = %14
%16 = load i8*, i8** %7
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %25
18: ; preds = %15
%19 = load i8*, i8** %7
%20 = call i32 @memcpy(i8* %19, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 9)
%21 = load i32, i32* @errno
%22 = load i8*, i8** %7
%23 = getelementptr inbounds i8, i8* %22, i64 9
%24 = call i32 @STRERROR(i32 %21, i8* %23)
br label %25
25: ; preds = %18, %15
br label %26
26: ; preds = %25
store i32 0, i32* %4
br label %50
27: ; preds = %3
%28 = load i32, i32* %5
%29 = load i32, i32* @F_SETFL
%30 = load i32, i32* %8
%31 = load i32, i32* %6
%32 = xor i32 %31, -1
%33 = and i32 %30, %32
%34 = call i64 @fcntl(i32 %28, i32 %29, i32 %33)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %49
36: ; preds = %27
br label %37
37: ; preds = %36
%38 = load i8*, i8** %7
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %47
40: ; preds = %37
%41 = load i8*, i8** %7
%42 = call i32 @memcpy(i8* %41, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 9)
%43 = load i32, i32* @errno
%44 = load i8*, i8** %7
%45 = getelementptr inbounds i8, i8* %44, i64 9
%46 = call i32 @STRERROR(i32 %43, i8* %45)
br label %47
47: ; preds = %40, %37
br label %48
48: ; preds = %47
store i32 0, i32* %4
br label %50
49: ; preds = %27
store i32 1, i32* %4
br label %50
50: ; preds = %49, %48, %26
%51 = load i32, i32* %4
ret i32 %51
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@.str = [10 x i8] c"F_GETFL: \00"
@errno = global i32 0
@F_SETFL = global i32 0
@.str.1 = [10 x i8] c"F_SETFL: \00"
define i32 @unset_sock_flags(i32 %0, i32 %1, i8* %2) {
%4 = load i32, i32* @F_GETFL
%5 = tail call i64 @fcntl(i32 %0, i32 %4, i32 0)
%6 = trunc i64 %5 to i32
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = icmp eq i8* %2, null
br i1 %9, label %24, label %18
10: ; preds = %3
%11 = load i32, i32* @F_SETFL
%12 = xor i32 %1, -1
%13 = and i32 %6, %12
%14 = tail call i64 @fcntl(i32 %0, i32 %11, i32 %13)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %10
%17 = icmp eq i8* %2, null
br i1 %17, label %24, label %18
18: ; preds = %16, %8
%19 = phi i8* [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %8 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %16 ]
%20 = tail call i32 @memcpy(i8* %2, i8* %19, i32 9)
%21 = load i32, i32* @errno
%22 = getelementptr inbounds i8, i8* %2, i64 9
%23 = tail call i32 @STRERROR(i32 %21, i8* %22)
br label %24
24: ; preds = %18, %10, %16, %8
%25 = phi i32 [ 0, %8 ], [ 0, %16 ], [ 1, %10 ], [ 0, %18 ]
ret i32 %25
}