#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 set_sock_flags(int sock, int set_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 | set_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):
<set_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 <set_sock_flags+0x7d>
jmp    1166 <set_sock_flags+0x36>
cmpq   $0x0,-0x18(%rbp)
je     119c <set_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 <set_sock_flags+0x71>
movl   $0x0,-0x4(%rbp)
jmp    1221 <set_sock_flags+0xf1>
mov    -0x8(%rbp),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
or     -0xc(%rbp),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    121a <set_sock_flags+0xea>
jmp    11d3 <set_sock_flags+0xa3>
cmpq   $0x0,-0x18(%rbp)
je     1209 <set_sock_flags+0xd9>
mov    -0x18(%rbp),%rdi
lea    0xe21(%rip),%rsi
mov    $0x9,%edx
call   1040 <memcpy@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
add    $0x9,%rsi
call   1050 <STRERROR@plt>
jmp    120e <set_sock_flags+0xde>
movl   $0x0,-0x4(%rbp)
jmp    1221 <set_sock_flags+0xf1>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_sock_flags>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%r15d
mov    %edi,%ebx
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
xor    %edx,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
js     1182 <set_sock_flags+0x52>
mov    0x2e84(%rip),%rcx
mov    (%rcx),%esi
or     %r15d,%eax
mov    %ebx,%edi
mov    %eax,%edx
call   1030 <fcntl@plt>
mov    $0x1,%ebp
test   %rax,%rax
jns    11b2 <set_sock_flags+0x82>
test   %r14,%r14
je     11b0 <set_sock_flags+0x80>
lea    0xe8a(%rip),%rsi
jmp    118e <set_sock_flags+0x5e>
test   %r14,%r14
je     11b2 <set_sock_flags+0x82>
lea    0xe72(%rip),%rsi
mov    %r14,%rdi
mov    $0x9,%edx
call   1040 <memcpy@plt>
mov    0x2e36(%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 @set_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 %49
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 = or i32 %30, %31
%33 = call i64 @fcntl(i32 %28, i32 %29, i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %48
35: ; preds = %27
br label %36
36: ; preds = %35
%37 = load i8*, i8** %7
%38 = icmp ne i8* %37, null
br i1 %38, label %39, label %46
39: ; preds = %36
%40 = load i8*, i8** %7
%41 = call i32 @memcpy(i8* %40, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 9)
%42 = load i32, i32* @errno
%43 = load i8*, i8** %7
%44 = getelementptr inbounds i8, i8* %43, i64 9
%45 = call i32 @STRERROR(i32 %42, i8* %44)
br label %46
46: ; preds = %39, %36
br label %47
47: ; preds = %46
store i32 0, i32* %4
br label %49
48: ; preds = %27
store i32 1, i32* %4
br label %49
49: ; preds = %48, %47, %26
%50 = load i32, i32* %4
ret i32 %50
}

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 @set_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 %23, label %17
10: ; preds = %3
%11 = load i32, i32* @F_SETFL
%12 = or i32 %6, %1
%13 = tail call i64 @fcntl(i32 %0, i32 %11, i32 %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %10
%16 = icmp eq i8* %2, null
br i1 %16, label %23, label %17
17: ; preds = %15, %8
%18 = 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), %15 ]
%19 = tail call i32 @memcpy(i8* %2, i8* %18, i32 9)
%20 = load i32, i32* @errno
%21 = getelementptr inbounds i8, i8* %2, i64 9
%22 = tail call i32 @STRERROR(i32 %20, i8* %21)
br label %23
23: ; preds = %17, %10, %15, %8
%24 = phi i32 [ 0, %8 ], [ 0, %15 ], [ 1, %10 ], [ 0, %17 ]
ret i32 %24
}