#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 */
typedef void node;
struct freelist {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ mem_incr_ref(void *);
int /*<<< orphan*/ mem_release(struct freelist *, void *);
void *mem_safe_read_null_on_mark(struct freelist *fl, void *_p) {
  struct node *q;
  struct node **p = _p;
  for (;;) {
    q = *p;
    if (!q || (((intptr_t)(q)) & 0x1))
      return NULL;
    mem_incr_ref(q);
    if (q == *p)
      return q;
    else
      mem_release(fl, q);
  }
}


x86 assembly (clang, -O0):
<mem_safe_read_null_on_mark>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1160 <mem_safe_read_null_on_mark+0x40>
mov    -0x20(%rbp),%rax
and    $0x1,%rax
cmp    $0x0,%rax
je     116d <mem_safe_read_null_on_mark+0x4d>
movq   $0x0,-0x8(%rbp)
jmp    11a6 <mem_safe_read_null_on_mark+0x86>
mov    -0x20(%rbp),%rdi
call   1030 <mem_incr_ref@plt>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
cmp    (%rcx),%rax
jne    1194 <mem_safe_read_null_on_mark+0x74>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11a6 <mem_safe_read_null_on_mark+0x86>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <mem_release@plt>
jmp    1138 <mem_safe_read_null_on_mark+0x18>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mem_safe_read_null_on_mark>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    (%rsi),%rbx
xor    %r14d,%r14d
test   %rbx,%rbx
je     1181 <mem_safe_read_null_on_mark+0x61>
mov    %ebx,%eax
and    $0x1,%eax
test   %rax,%rax
jne    1181 <mem_safe_read_null_on_mark+0x61>
mov    %rsi,%r12
mov    %rdi,%r15
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <mem_incr_ref@plt>
cmp    (%r12),%rbx
je     117e <mem_safe_read_null_on_mark+0x5e>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1040 <mem_release@plt>
mov    (%r12),%rbx
test   %rbx,%rbx
je     1181 <mem_safe_read_null_on_mark+0x61>
mov    %ebx,%eax
and    $0x1,%eax
test   %rax,%rax
je     1150 <mem_safe_read_null_on_mark+0x30>
jmp    1181 <mem_safe_read_null_on_mark+0x61>
mov    %rbx,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.freelist = type { i32 }
%struct.node = type opaque
define i8* @mem_safe_read_null_on_mark(%struct.freelist* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca %struct.freelist*
%5 = alloca i8*
%6 = alloca %struct.node*
%7 = alloca %struct.node**
store %struct.freelist* %0, %struct.freelist** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = bitcast i8* %8 to %struct.node**
store %struct.node** %9, %struct.node*** %7
br label %10
10: ; preds = %37, %2
%11 = load %struct.node**, %struct.node*** %7
%12 = load %struct.node*, %struct.node** %11
store %struct.node* %12, %struct.node** %6
%13 = load %struct.node*, %struct.node** %6
%14 = icmp ne %struct.node* %13, null
br i1 %14, label %15, label %20
15: ; preds = %10
%16 = load %struct.node*, %struct.node** %6
%17 = ptrtoint %struct.node* %16 to i64
%18 = and i64 %17, 1
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %15, %10
store i8* null, i8** %3
br label %38
21: ; preds = %15
%22 = load %struct.node*, %struct.node** %6
%23 = bitcast %struct.node* %22 to i8*
%24 = call i32 @mem_incr_ref(i8* %23)
%25 = load %struct.node*, %struct.node** %6
%26 = load %struct.node**, %struct.node*** %7
%27 = load %struct.node*, %struct.node** %26
%28 = icmp eq %struct.node* %25, %27
br i1 %28, label %29, label %32
29: ; preds = %21
%30 = load %struct.node*, %struct.node** %6
%31 = bitcast %struct.node* %30 to i8*
store i8* %31, i8** %3
br label %38
32: ; preds = %21
%33 = load %struct.freelist*, %struct.freelist** %4
%34 = load %struct.node*, %struct.node** %6
%35 = bitcast %struct.node* %34 to i8*
%36 = call i32 @mem_release(%struct.freelist* %33, i8* %35)
br label %37
37: ; preds = %32
br label %10
38: ; preds = %29, %20
%39 = load i8*, i8** %3
ret i8* %39
}

LLVM IR (-O2):
%struct.freelist = type { i32 }
%struct.node = type opaque
define i8* @mem_safe_read_null_on_mark(%struct.freelist* %0, i8* nocapture %1) {
%3 = bitcast i8* %1 to %struct.node**
%4 = load %struct.node*, %struct.node** %3
%5 = icmp ne %struct.node* %4, null
%6 = ptrtoint %struct.node* %4 to i64
%7 = and i64 %6, 1
%8 = icmp eq i64 %7, 0
%9 = select i1 %5, i1 %8, i1 false
br i1 %9, label %10, label %26
10: ; preds = %2, %16
%11 = phi %struct.node* [ %18, %16 ], [ %4, %2 ]
%12 = bitcast %struct.node* %11 to i8*
%13 = tail call i32 @mem_incr_ref(i8* %12)
%14 = load %struct.node*, %struct.node** %3
%15 = icmp eq %struct.node* %11, %14
br i1 %15, label %24, label %16
16: ; preds = %10
%17 = tail call i32 @mem_release(%struct.freelist* %0, i8* %12)
%18 = load %struct.node*, %struct.node** %3
%19 = icmp ne %struct.node* %18, null
%20 = ptrtoint %struct.node* %18 to i64
%21 = and i64 %20, 1
%22 = icmp eq i64 %21, 0
%23 = select i1 %19, i1 %22, i1 false
br i1 %23, label %10, label %26
24: ; preds = %10
%25 = bitcast %struct.node* %11 to i8*
br label %26
26: ; preds = %16, %24, %2
%27 = phi i8* [ null, %2 ], [ %25, %24 ], [ null, %16 ]
ret i8* %27
}