#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 size_t off_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t string_read_with_length(const char *str, char *buf, size_t size,
                               const off_t offset, size_t length) {
  if ((str == NULL) || (length == 0) || (offset >= length) || (offset < 0)) {
    return 0;
  }
  assert(offset >= 0);
  if ((offset + size) > length) {
    size = length - offset;
  }
  memcpy(buf, str + offset, size);
  return size;
}


x86 assembly (clang, -O0):
<string_read_with_length>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     116b <string_read_with_length+0x4b>
cmpq   $0x0,-0x30(%rbp)
je     116b <string_read_with_length+0x4b>
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    116b <string_read_with_length+0x4b>
cmpq   $0x0,-0x28(%rbp)
jae    1178 <string_read_with_length+0x58>
movq   $0x0,-0x8(%rbp)
jmp    11c5 <string_read_with_length+0xa5>
cmpq   $0x0,-0x28(%rbp)
setae  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
cmp    -0x30(%rbp),%rax
jbe    11a8 <string_read_with_length+0x88>
mov    -0x30(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_read_with_length>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
test   %rdi,%rdi
je     1172 <string_read_with_length+0x52>
mov    %r8,%rbp
mov    %rcx,%r15
mov    %r8,%rbx
sub    %rcx,%rbx
jbe    1172 <string_read_with_length+0x52>
mov    %rdx,%r13
mov    %rsi,%r14
mov    %rdi,%r12
mov    $0x1,%edi
call   1030 <assert@plt>
lea    (%r15,%r13,1),%rax
cmp    %rbp,%rax
cmovbe %r13,%rbx
add    %r15,%r12
mov    %r14,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @string_read_with_length(i8* %0, i8* %1, i64 %2, i64 %3, i64 %4) {
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
store i8* %0, i8** %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store i64 %3, i64* %10
store i64 %4, i64* %11
%12 = load i8*, i8** %7
%13 = icmp eq i8* %12, null
br i1 %13, label %24, label %14
14: ; preds = %5
%15 = load i64, i64* %11
%16 = icmp eq i64 %15, 0
br i1 %16, label %24, label %17
17: ; preds = %14
%18 = load i64, i64* %10
%19 = load i64, i64* %11
%20 = icmp uge i64 %18, %19
br i1 %20, label %24, label %21
21: ; preds = %17
%22 = load i64, i64* %10
%23 = icmp ult i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %21, %17, %14, %5
store i64 0, i64* %6
br label %47
25: ; preds = %21
%26 = load i64, i64* %10
%27 = icmp uge i64 %26, 0
%28 = zext i1 %27 to i32
%29 = call i32 @assert(i32 %28)
%30 = load i64, i64* %10
%31 = load i64, i64* %9
%32 = add i64 %30, %31
%33 = load i64, i64* %11
%34 = icmp ugt i64 %32, %33
br i1 %34, label %35, label %39
35: ; preds = %25
%36 = load i64, i64* %11
%37 = load i64, i64* %10
%38 = sub i64 %36, %37
store i64 %38, i64* %9
br label %39
39: ; preds = %35, %25
%40 = load i8*, i8** %8
%41 = load i8*, i8** %7
%42 = load i64, i64* %10
%43 = getelementptr inbounds i8, i8* %41, i64 %42
%44 = load i64, i64* %9
%45 = call i32 @memcpy(i8* %40, i8* %43, i64 %44)
%46 = load i64, i64* %9
store i64 %46, i64* %6
br label %47
47: ; preds = %39, %24
%48 = load i64, i64* %6
ret i64 %48
}

LLVM IR (-O2):
define i64 @string_read_with_length(i8* %0, i8* %1, i64 %2, i64 %3, i64 %4) {
%6 = icmp ne i8* %0, null
%7 = icmp ugt i64 %4, %3
%8 = and i1 %6, %7
br i1 %8, label %9, label %17
9: ; preds = %5
%10 = tail call i32 @assert(i32 1)
%11 = add i64 %3, %2
%12 = icmp ugt i64 %11, %4
%13 = sub i64 %4, %3
%14 = select i1 %12, i64 %13, i64 %2
%15 = getelementptr inbounds i8, i8* %0, i64 %3
%16 = tail call i32 @memcpy(i8* %1, i8* %15, i64 %14)
br label %17
17: ; preds = %5, %9
%18 = phi i64 [ %14, %9 ], [ 0, %5 ]
ret i64 %18
}