#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(struct freelist *fl, void *_p) {
  struct node *q;
  struct node **p = _p;
  for (;;) {
    q = *p;
    if (!q)
      return NULL;
    mem_incr_ref(q);
    if (q == *p)
      return q;
    else
      mem_release(fl, q);
  }
}


x86 assembly (clang, -O0):
<mem_safe_read>:
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)
jne    115b <mem_safe_read+0x3b>
movq   $0x0,-0x8(%rbp)
jmp    1194 <mem_safe_read+0x74>
mov    -0x20(%rbp),%rdi
call   1030 <mem_incr_ref@plt>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
cmp    (%rcx),%rax
jne    1182 <mem_safe_read+0x62>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1194 <mem_safe_read+0x74>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <mem_release@plt>
jmp    1138 <mem_safe_read+0x18>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mem_safe_read>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    (%rsi),%rbx
test   %rbx,%rbx
je     1164 <mem_safe_read+0x44>
mov    %rsi,%r12
mov    %rdi,%r15
xor    %r14d,%r14d
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1030 <mem_incr_ref@plt>
cmp    (%r12),%rbx
je     1169 <mem_safe_read+0x49>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1040 <mem_release@plt>
mov    (%r12),%rbx
test   %rbx,%rbx
jne    1140 <mem_safe_read+0x20>
jmp    116c <mem_safe_read+0x4c>
xor    %r14d,%r14d
jmp    116c <mem_safe_read+0x4c>
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(%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 = %32, %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 %16, label %15
15: ; preds = %10
store i8* null, i8** %3
br label %33
16: ; preds = %10
%17 = load %struct.node*, %struct.node** %6
%18 = bitcast %struct.node* %17 to i8*
%19 = call i32 @mem_incr_ref(i8* %18)
%20 = load %struct.node*, %struct.node** %6
%21 = load %struct.node**, %struct.node*** %7
%22 = load %struct.node*, %struct.node** %21
%23 = icmp eq %struct.node* %20, %22
br i1 %23, label %24, label %27
24: ; preds = %16
%25 = load %struct.node*, %struct.node** %6
%26 = bitcast %struct.node* %25 to i8*
store i8* %26, i8** %3
br label %33
27: ; preds = %16
%28 = load %struct.freelist*, %struct.freelist** %4
%29 = load %struct.node*, %struct.node** %6
%30 = bitcast %struct.node* %29 to i8*
%31 = call i32 @mem_release(%struct.freelist* %28, i8* %30)
br label %32
32: ; preds = %27
br label %10
33: ; preds = %24, %15
%34 = load i8*, i8** %3
ret i8* %34
}

LLVM IR (-O2):
%struct.freelist = type { i32 }
%struct.node = type opaque
define i8* @mem_safe_read(%struct.freelist* %0, i8* nocapture %1) {
%3 = bitcast i8* %1 to %struct.node**
%4 = load %struct.node*, %struct.node** %3
%5 = icmp eq %struct.node* %4, null
br i1 %5, label %18, label %6
6: ; preds = %2, %12
%7 = phi %struct.node* [ %14, %12 ], [ %4, %2 ]
%8 = bitcast %struct.node* %7 to i8*
%9 = tail call i32 @mem_incr_ref(i8* %8)
%10 = load %struct.node*, %struct.node** %3
%11 = icmp eq %struct.node* %7, %10
br i1 %11, label %16, label %12
12: ; preds = %6
%13 = tail call i32 @mem_release(%struct.freelist* %0, i8* %8)
%14 = load %struct.node*, %struct.node** %3
%15 = icmp eq %struct.node* %14, null
br i1 %15, label %18, label %6
16: ; preds = %6
%17 = bitcast %struct.node* %7 to i8*
br label %18
18: ; preds = %12, %16, %2
%19 = phi i8* [ null, %2 ], [ %17, %16 ], [ null, %12 ]
ret i8* %19
}