#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  int _flags;
};
typedef TYPE_1__ FILE;

/* Variables and functions */
int FSETLOCKING_BYCALLER;
int FSETLOCKING_INTERNAL;
int FSETLOCKING_QUERY;
int _IO_USER_LOCK;
int __fsetlocking(FILE *fp, int type) {
  int result = ((fp->_flags & _IO_USER_LOCK) ? FSETLOCKING_BYCALLER
                                             : FSETLOCKING_INTERNAL);
  if (type != FSETLOCKING_QUERY) {
    fp->_flags &= ~_IO_USER_LOCK;
    if (type == FSETLOCKING_BYCALLER)
      fp->_flags |= _IO_USER_LOCK;
  }
  return result;
}


x86 assembly (clang, -O0):
<__fsetlocking>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    0x2eb8(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1134 <__fsetlocking+0x34>
mov    0x2e9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    1140 <__fsetlocking+0x40>
mov    0x2ead(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2e88(%rip),%rcx
cmp    (%rcx),%eax
je     1194 <__fsetlocking+0x94>
mov    0x2e71(%rip),%rax
mov    (%rax),%ecx
xor    $0xffffffff,%ecx
mov    -0x8(%rbp),%rax
and    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0xc(%rbp),%eax
mov    0x2e52(%rip),%rcx
cmp    (%rcx),%eax
jne    118f <__fsetlocking+0x8f>
mov    0x2e4b(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%rax
or     (%rax),%ecx
mov    %ecx,(%rax)
jmp    1194 <__fsetlocking+0x94>
mov    -0x10(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<__fsetlocking>:
mov    (%rdi),%r11d
mov    0x2ec6(%rip),%r9
mov    (%r9),%ecx
mov    0x2eb4(%rip),%r10
mov    (%r10),%eax
mov    0x2eca(%rip),%rdx
mov    (%rdx),%r8d
mov    0x2eb0(%rip),%rdx
cmp    %esi,(%rdx)
je     113f <__fsetlocking+0x3f>
mov    %ecx,%edx
not    %edx
and    %r11d,%edx
mov    %edx,(%rdi)
cmp    %esi,(%r10)
jne    113f <__fsetlocking+0x3f>
or     (%r9),%edx
mov    %edx,(%rdi)
test   %r11d,%ecx
cmove  %r8d,%eax
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32 }
@_IO_USER_LOCK = global i32 0
@FSETLOCKING_BYCALLER = global i32 0
@FSETLOCKING_INTERNAL = global i32 0
@FSETLOCKING_QUERY = global i32 0
define i32 @__fsetlocking(%struct.TYPE_3__* %0, i32 %1) {
%3 = alloca %struct.TYPE_3__*
%4 = alloca i32
%5 = alloca i32
store %struct.TYPE_3__* %0, %struct.TYPE_3__** %3
store i32 %1, i32* %4
%6 = load %struct.TYPE_3__*, %struct.TYPE_3__** %3
%7 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %6, i32 0, i32 0
%8 = load i32, i32* %7
%9 = load i32, i32* @_IO_USER_LOCK
%10 = and i32 %8, %9
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = load i32, i32* @FSETLOCKING_BYCALLER
br label %16
14: ; preds = %2
%15 = load i32, i32* @FSETLOCKING_INTERNAL
br label %16
16: ; preds = %14, %12
%17 = phi i32 [ %13, %12 ], [ %15, %14 ]
store i32 %17, i32* %5
%18 = load i32, i32* %4
%19 = load i32, i32* @FSETLOCKING_QUERY
%20 = icmp ne i32 %18, %19
br i1 %20, label %21, label %38
21: ; preds = %16
%22 = load i32, i32* @_IO_USER_LOCK
%23 = xor i32 %22, -1
%24 = load %struct.TYPE_3__*, %struct.TYPE_3__** %3
%25 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %24, i32 0, i32 0
%26 = load i32, i32* %25
%27 = and i32 %26, %23
store i32 %27, i32* %25
%28 = load i32, i32* %4
%29 = load i32, i32* @FSETLOCKING_BYCALLER
%30 = icmp eq i32 %28, %29
br i1 %30, label %31, label %37
31: ; preds = %21
%32 = load i32, i32* @_IO_USER_LOCK
%33 = load %struct.TYPE_3__*, %struct.TYPE_3__** %3
%34 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = or i32 %35, %32
store i32 %36, i32* %34
br label %37
37: ; preds = %31, %21
br label %38
38: ; preds = %37, %16
%39 = load i32, i32* %5
ret i32 %39
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32 }
@_IO_USER_LOCK = global i32 0
@FSETLOCKING_BYCALLER = global i32 0
@FSETLOCKING_INTERNAL = global i32 0
@FSETLOCKING_QUERY = global i32 0
define i32 @__fsetlocking(%struct.TYPE_3__* nocapture %0, i32 %1) {
%3 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %0, i64 0, i32 0
%4 = load i32, i32* %3
%5 = load i32, i32* @_IO_USER_LOCK
%6 = load i32, i32* @FSETLOCKING_BYCALLER
%7 = load i32, i32* @FSETLOCKING_INTERNAL
%8 = load i32, i32* @FSETLOCKING_QUERY
%9 = icmp eq i32 %8, %1
br i1 %9, label %18, label %10
10: ; preds = %2
%11 = xor i32 %5, -1
%12 = and i32 %4, %11
store i32 %12, i32* %3
%13 = load i32, i32* @FSETLOCKING_BYCALLER
%14 = icmp eq i32 %13, %1
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = load i32, i32* @_IO_USER_LOCK
%17 = or i32 %16, %12
store i32 %17, i32* %3
br label %18
18: ; preds = %10, %15, %2
%19 = and i32 %5, %4
%20 = icmp eq i32 %19, 0
%21 = select i1 %20, i32 %7, i32 %6
ret i32 %21
}