#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*/ assert(int);
int /*<<< orphan*/ memcpy(void *, void const *, int);
int valid(void const *, int);
int _Nub_fetch(int space, const void *address, void *buf, int nbytes) {
  if (nbytes <= 0)
    return 0;
  nbytes = valid(address, nbytes);
  assert(nbytes >= 0);
  if (nbytes > 0 && buf)
    memcpy(buf, address, nbytes);
  return nbytes;
}


x86 assembly (clang, -O0):
<_Nub_fetch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     115c <_Nub_fetch+0x2c>
movl   $0x0,-0x4(%rbp)
jmp    11a7 <_Nub_fetch+0x77>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1050 <valid@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
cmpl   $0x0,-0x1c(%rbp)
jle    11a1 <_Nub_fetch+0x71>
cmpq   $0x0,-0x18(%rbp)
je     11a1 <_Nub_fetch+0x71>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_Nub_fetch>:
push   %r15
push   %r14
push   %rbx
test   %ecx,%ecx
jle    116f <_Nub_fetch+0x3f>
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rsi,%rdi
mov    %ecx,%esi
call   1050 <valid@plt>
mov    %eax,%ebx
mov    %eax,%edi
not    %edi
shr    $0x1f,%edi
call   1030 <assert@plt>
test   %r15,%r15
je     1171 <_Nub_fetch+0x41>
test   %ebx,%ebx
jle    1171 <_Nub_fetch+0x41>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <memcpy@plt>
jmp    1171 <_Nub_fetch+0x41>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @_Nub_fetch(i32 %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%10 = load i32, i32* %9
%11 = icmp sle i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %4
store i32 0, i32* %5
br label %33
13: ; preds = %4
%14 = load i8*, i8** %7
%15 = load i32, i32* %9
%16 = call i32 @valid(i8* %14, i32 %15)
store i32 %16, i32* %9
%17 = load i32, i32* %9
%18 = icmp sge i32 %17, 0
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
%21 = load i32, i32* %9
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %31
23: ; preds = %13
%24 = load i8*, i8** %8
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %31
26: ; preds = %23
%27 = load i8*, i8** %8
%28 = load i8*, i8** %7
%29 = load i32, i32* %9
%30 = call i32 @memcpy(i8* %27, i8* %28, i32 %29)
br label %31
31: ; preds = %26, %23, %13
%32 = load i32, i32* %9
store i32 %32, i32* %5
br label %33
33: ; preds = %31, %12
%34 = load i32, i32* %5
ret i32 %34
}

LLVM IR (-O2):
define i32 @_Nub_fetch(i32 %0, i8* %1, i8* %2, i32 %3) {
%5 = icmp slt i32 %3, 1
br i1 %5, label %16, label %6
6: ; preds = %4
%7 = tail call i32 @valid(i8* %1, i32 %3)
%8 = xor i32 %7, -1
%9 = lshr i32 %8, 31
%10 = tail call i32 @assert(i32 %9)
%11 = icmp sgt i32 %7, 0
%12 = icmp ne i8* %2, null
%13 = and i1 %12, %11
br i1 %13, label %14, label %16
14: ; preds = %6
%15 = tail call i32 @memcpy(i8* %2, i8* %1, i32 %7)
br label %16
16: ; preds = %6, %14, %4
%17 = phi i32 [ 0, %4 ], [ %7, %14 ], [ %7, %6 ]
ret i32 %17
}