#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 */
size_t safe_strlen(char const *);
size_t string_read_with_length(char const *, char *, size_t, size_t const,
                               size_t);
size_t string_read(const char *str, char *buf, size_t size,
                   const off_t offset) {
  size_t length = safe_strlen(str);
  if ((length == 0) || (offset >= length) || (offset < 0)) {
    return 0;
  }
  return string_read_with_length(str, buf, size, offset, length);
}


x86 assembly (clang, -O0):
<string_read>:
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    -0x10(%rbp),%rdi
call   1040 <safe_strlen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1169 <string_read+0x49>
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    1169 <string_read+0x49>
cmpq   $0x0,-0x28(%rbp)
jae    1176 <string_read+0x56>
movq   $0x0,-0x8(%rbp)
jmp    1193 <string_read+0x73>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%r8
call   1030 <string_read_with_length@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_read>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r12
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
call   1040 <safe_strlen@plt>
cmp    %r12,%rax
jbe    115d <string_read+0x3d>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    %r12,%rcx
mov    %rax,%r8
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <string_read_with_length@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @string_read(i8* %0, i8* %1, i64 %2, i64 %3) {
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i64 %3, i64* %9
%11 = load i8*, i8** %6
%12 = call i64 @safe_strlen(i8* %11)
store i64 %12, i64* %10
%13 = load i64, i64* %10
%14 = icmp eq i64 %13, 0
br i1 %14, label %22, label %15
15: ; preds = %4
%16 = load i64, i64* %9
%17 = load i64, i64* %10
%18 = icmp uge i64 %16, %17
br i1 %18, label %22, label %19
19: ; preds = %15
%20 = load i64, i64* %9
%21 = icmp ult i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %19, %15, %4
store i64 0, i64* %5
br label %30
23: ; preds = %19
%24 = load i8*, i8** %6
%25 = load i8*, i8** %7
%26 = load i64, i64* %8
%27 = load i64, i64* %9
%28 = load i64, i64* %10
%29 = call i64 @string_read_with_length(i8* %24, i8* %25, i64 %26, i64 %27, i64 %28)
store i64 %29, i64* %5
br label %30
30: ; preds = %23, %22
%31 = load i64, i64* %5
ret i64 %31
}

LLVM IR (-O2):
define i64 @string_read(i8* %0, i8* %1, i64 %2, i64 %3) {
%5 = tail call i64 @safe_strlen(i8* %0)
%6 = icmp ugt i64 %5, %3
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i64 @string_read_with_length(i8* %0, i8* %1, i64 %2, i64 %3, i64 %5)
br label %9
9: ; preds = %4, %7
%10 = phi i64 [ %8, %7 ], [ 0, %4 ]
ret i64 %10
}