#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*/ exit(int);
int /*<<< orphan*/ printf(char *, ...);
int const strlen(char const *);
void buf_check(const char *str, const int len) {
  if (strlen(str) >= len) {
    printf("error: Buffer overflow caught\n");
    printf("string length: %lu\n", strlen(str));
    int pos = 0;
    int chars_to_print = 0;
    chars_to_print = (len >= 80) ? 80 : 10;
    for (pos = 0; pos < chars_to_print; pos++)
      printf("%c", str[pos]);
    printf("\n");
    exit(1);
  }
  return;
}


x86 assembly (clang, -O0):
<buf_check>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
cmp    -0xc(%rbp),%eax
jl     11ef <buf_check+0xbf>
lea    0xea8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%esi
lea    0xeae(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0xc(%rbp),%edx
mov    $0xa,%eax
mov    $0x50,%ecx
cmp    $0x50,%edx
cmovge %ecx,%eax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11d7 <buf_check+0xa7>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%esi
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11a3 <buf_check+0x73>
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buf_check>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
call   1050 <strlen@plt>
cmp    %r14d,%eax
jge    1169 <buf_check+0x19>
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xea4(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0xe7c(%rip),%rdi
xor    %ebp,%ebp
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
cmp    $0x50,%r14d
mov    $0xa,%eax
mov    $0x50,%r14d
cmovl  %rax,%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsbl (%rbx,%rbp,1),%edi
call   1030 <putchar@plt>
add    $0x1,%rbp
cmp    %rbp,%r14
jne    11b0 <buf_check+0x60>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [31 x i8] c"error: Buffer overflow caught\0A\00"
@.str.1 = [20 x i8] c"string length: %lu\0A\00"
@.str.2 = [3 x i8] c"%c\00"
@.str.3 = [2 x i8] c"\0A\00"
define void @buf_check(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i8*, i8** %3
%8 = call i32 @strlen(i8* %7)
%9 = load i32, i32* %4
%10 = icmp sge i32 %8, %9
br i1 %10, label %11, label %38
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%13 = load i8*, i8** %3
%14 = call i32 @strlen(i8* %13)
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %14)
store i32 0, i32* %5
store i32 0, i32* %6
%16 = load i32, i32* %4
%17 = icmp sge i32 %16, 80
%18 = zext i1 %17 to i64
%19 = select i1 %17, i32 80, i32 10
store i32 %19, i32* %6
store i32 0, i32* %5
br label %20
20: ; preds = %32, %11
%21 = load i32, i32* %5
%22 = load i32, i32* %6
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %35
24: ; preds = %20
%25 = load i8*, i8** %3
%26 = load i32, i32* %5
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %30)
br label %32
32: ; preds = %24
%33 = load i32, i32* %5
%34 = add nsw i32 %33, 1
store i32 %34, i32* %5
br label %20
35: ; preds = %20
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %2
ret void
}

LLVM IR (-O2):
@.str.1 = [20 x i8] c"string length: %lu\0A\00"
@str = [30 x i8] c"error: Buffer overflow caught\00"
define void @buf_check(i8* %0, i32 %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = icmp slt i32 %3, %1
br i1 %4, label %22, label %5
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%7 = tail call i32 @strlen(i8* %0)
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = icmp slt i32 %1, 80
%10 = select i1 %9, i64 10, i64 80
br label %11
11: ; preds = %5, %11
%12 = phi i64 [ 0, %5 ], [ %17, %11 ]
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = tail call i32 @putchar(i32 %15)
%17 = add nuw nsw i64 %12, 1
%18 = icmp eq i64 %17, %10
br i1 %18, label %19, label %11
19: ; preds = %11
%20 = tail call i32 @putchar(i32 10)
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %2
ret void
}