#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*/ isprint(char);
int /*<<< orphan*/ printf(char *);
scalar_t__ xread_offset(int, char *, unsigned int, unsigned int);
unsigned int findbackwardstring(int ifd, unsigned int end, unsigned int max) {
  unsigned int start, base;
  char ibuf[max + 1];
  if (max > end)
    max = end;
  base = end - max;
  if (xread_offset(ifd, ibuf, base, max + 1) < 0) {
    printf("ERROR: failed to read string buffer: %m\n");
    return 0;
  }
  for (start = end; (start > base); start--) {
    if (ibuf[start - base] == 0)
      return start;
    if (!isprint(ibuf[start - base]))
      return 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<findbackwardstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jbe    117c <findbackwardstring+0x4c>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x38(%rbp),%rsi
mov    -0xc(%rbp),%eax
sub    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%edx
mov    -0x10(%rbp),%ecx
add    $0x1,%ecx
call   1050 <xread_offset@plt>
cmp    $0x0,%rax
jge    11c3 <findbackwardstring+0x93>
lea    0xe55(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1252 <findbackwardstring+0x122>
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jbe    1244 <findbackwardstring+0x114>
mov    -0x38(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    -0x18(%rbp),%ecx
mov    %ecx,%ecx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    1200 <findbackwardstring+0xd0>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1252 <findbackwardstring+0x122>
mov    -0x38(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    -0x18(%rbp),%ecx
mov    %ecx,%ecx
movsbl (%rax,%rcx,1),%edi
call   1040 <isprint@plt>
cmp    $0x0,%eax
jne    1231 <findbackwardstring+0x101>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1252 <findbackwardstring+0x122>
jmp    1236 <findbackwardstring+0x106>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    11c9 <findbackwardstring+0x99>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findbackwardstring>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %esi,%r14d
lea    0x1(%rbx),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
cmp    %esi,%edx
cmova  %esi,%ebx
mov    %esi,%r12d
sub    %ebx,%r12d
lea    0x1(%rbx),%ecx
mov    %r15,%rsi
mov    %r12d,%edx
call   1050 <xread_offset@plt>
test   %rax,%rax
js     11a5 <findbackwardstring+0x75>
xor    %r13d,%r13d
cmp    %r14d,%r12d
jae    11b9 <findbackwardstring+0x89>
nopl   (%rax)
mov    %ebx,%eax
movzbl (%r15,%rax,1),%eax
test   %al,%al
je     11b6 <findbackwardstring+0x86>
movsbl %al,%edi
call   1040 <isprint@plt>
test   %eax,%eax
je     11b9 <findbackwardstring+0x89>
add    $0xffffffff,%r14d
add    $0xffffffff,%ebx
cmp    %r14d,%r12d
jb     1180 <findbackwardstring+0x50>
jmp    11b9 <findbackwardstring+0x89>
lea    0xe54(%rip),%rdi
call   1030 <printf@plt>
xor    %r13d,%r13d
jmp    11b9 <findbackwardstring+0x89>
mov    %r14d,%r13d
mov    %r13d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [41 x i8] c"ERROR: failed to read string buffer: %m\0A\00"
define i32 @findbackwardstring(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%13 = load i32, i32* %7
%14 = add i32 %13, 1
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %10
%17 = alloca i8, i64 %15
store i64 %15, i64* %11
%18 = load i32, i32* %7
%19 = load i32, i32* %6
%20 = icmp ugt i32 %18, %19
br i1 %20, label %21, label %23
21: ; preds = %3
%22 = load i32, i32* %6
store i32 %22, i32* %7
br label %23
23: ; preds = %21, %3
%24 = load i32, i32* %6
%25 = load i32, i32* %7
%26 = sub i32 %24, %25
store i32 %26, i32* %9
%27 = load i32, i32* %5
%28 = load i32, i32* %9
%29 = load i32, i32* %7
%30 = add i32 %29, 1
%31 = call i64 @xread_offset(i32 %27, i8* %17, i32 %28, i32 %30)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %23
%34 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
store i32 1, i32* %12
br label %67
35: ; preds = %23
%36 = load i32, i32* %6
store i32 %36, i32* %8
br label %37
37: ; preds = %63, %35
%38 = load i32, i32* %8
%39 = load i32, i32* %9
%40 = icmp ugt i32 %38, %39
br i1 %40, label %41, label %66
41: ; preds = %37
%42 = load i32, i32* %8
%43 = load i32, i32* %9
%44 = sub i32 %42, %43
%45 = zext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %17, i64 %45
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %41
%51 = load i32, i32* %8
store i32 %51, i32* %4
store i32 1, i32* %12
br label %67
52: ; preds = %41
%53 = load i32, i32* %8
%54 = load i32, i32* %9
%55 = sub i32 %53, %54
%56 = zext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %17, i64 %56
%58 = load i8, i8* %57
%59 = call i32 @isprint(i8 signext %58)
%60 = icmp ne i32 %59, 0
br i1 %60, label %62, label %61
61: ; preds = %52
store i32 0, i32* %4
store i32 1, i32* %12
br label %67
62: ; preds = %52
br label %63
63: ; preds = %62
%64 = load i32, i32* %8
%65 = add i32 %64, -1
store i32 %65, i32* %8
br label %37
66: ; preds = %37
store i32 0, i32* %4
store i32 1, i32* %12
br label %67
67: ; preds = %66, %61, %50, %33
%68 = load i8*, i8** %10
%69 = load i32, i32* %4
ret i32 %69
}

LLVM IR (-O2):
@.str = [41 x i8] c"ERROR: failed to read string buffer: %m\0A\00"
define i32 @findbackwardstring(i32 %0, i32 %1, i32 %2) {
%4 = add i32 %2, 1
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = icmp ugt i32 %2, %1
%8 = select i1 %7, i32 %1, i32 %2
%9 = sub i32 %1, %8
%10 = add i32 %8, 1
%11 = call i64 @xread_offset(i32 %0, i8* %6, i32 %9, i32 %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %3
%14 = icmp ult i32 %9, %1
br i1 %14, label %15, label %36
15: ; preds = %13
%16 = zext i32 %1 to i64
br label %19
17: ; preds = %3
%18 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
br label %36
19: ; preds = %15, %27
%20 = phi i64 [ %16, %15 ], [ %30, %27 ]
%21 = trunc i64 %20 to i32
%22 = sub i32 %21, %9
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %6, i64 %23
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 0
br i1 %26, label %34, label %27
27: ; preds = %19
%28 = call i32 @isprint(i8 signext %25)
%29 = icmp ne i32 %28, 0
%30 = add nsw i64 %20, -1
%31 = trunc i64 %30 to i32
%32 = icmp ult i32 %9, %31
%33 = select i1 %29, i1 %32, i1 false
br i1 %33, label %19, label %36
34: ; preds = %19
%35 = trunc i64 %20 to i32
br label %36
36: ; preds = %27, %34, %13, %17
%37 = phi i32 [ 0, %17 ], [ 0, %13 ], [ %35, %34 ], [ 0, %27 ]
ret i32 %37
}