#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 */
struct editor {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ copy(struct editor *, unsigned char *, int, int);
int /*<<< orphan*/ get_selection(struct editor *, int *, int *);
int get_selected_text(struct editor *ed, char *buffer, int size) {
  int selstart, selend, len;
  if (!get_selection(ed, &selstart, &selend))
    return 0;
  len = selend - selstart;
  if (len >= size)
    return 0;
  copy(ed, (unsigned char *)buffer, selstart, len);
  buffer[len] = 0;
  return len;
}


x86 assembly (clang, -O0):
<get_selected_text>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1030 <get_selection@plt>
cmp    $0x0,%eax
jne    1159 <get_selected_text+0x39>
movl   $0x0,-0x4(%rbp)
jmp    119f <get_selected_text+0x7f>
mov    -0x24(%rbp),%eax
sub    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jl     117a <get_selected_text+0x5a>
movl   $0x0,-0x4(%rbp)
jmp    119f <get_selected_text+0x7f>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
mov    -0x28(%rbp),%ecx
call   1040 <copy@plt>
mov    -0x18(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_selected_text>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x4(%rsp),%rsi
mov    %rsp,%rdx
call   1030 <get_selection@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
je     116a <get_selected_text+0x4a>
movslq (%rsp),%rbp
movslq 0x4(%rsp),%rdx
sub    %rdx,%rbp
cmp    %r15d,%ebp
jge    116a <get_selected_text+0x4a>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%ecx
call   1040 <copy@plt>
movb   $0x0,(%r14,%rbp,1)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.editor = type { i32 }
define i32 @get_selected_text(%struct.editor* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.editor*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store %struct.editor* %0, %struct.editor** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load %struct.editor*, %struct.editor** %5
%12 = call i32 @get_selection(%struct.editor* %11, i32* %8, i32* %9)
%13 = icmp ne i32 %12, 0
br i1 %13, label %15, label %14
14: ; preds = %3
store i32 0, i32* %4
br label %34
15: ; preds = %3
%16 = load i32, i32* %9
%17 = load i32, i32* %8
%18 = sub nsw i32 %16, %17
store i32 %18, i32* %10
%19 = load i32, i32* %10
%20 = load i32, i32* %7
%21 = icmp sge i32 %19, %20
br i1 %21, label %22, label %23
22: ; preds = %15
store i32 0, i32* %4
br label %34
23: ; preds = %15
%24 = load %struct.editor*, %struct.editor** %5
%25 = load i8*, i8** %6
%26 = load i32, i32* %8
%27 = load i32, i32* %10
%28 = call i32 @copy(%struct.editor* %24, i8* %25, i32 %26, i32 %27)
%29 = load i8*, i8** %6
%30 = load i32, i32* %10
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 0, i8* %32
%33 = load i32, i32* %10
store i32 %33, i32* %4
br label %34
34: ; preds = %23, %22, %14
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
%struct.editor = type { i32 }
define i32 @get_selected_text(%struct.editor* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = call i32 @get_selection(%struct.editor* %0, i32* %4, i32* %5)
%9 = icmp eq i32 %8, 0
br i1 %9, label %19, label %10
10: ; preds = %3
%11 = load i32, i32* %5
%12 = load i32, i32* %4
%13 = sub nsw i32 %11, %12
%14 = icmp slt i32 %13, %2
br i1 %14, label %15, label %19
15: ; preds = %10
%16 = call i32 @copy(%struct.editor* %0, i8* %1, i32 %12, i32 %13)
%17 = sext i32 %13 to i64
%18 = getelementptr inbounds i8, i8* %1, i64 %17
store i8 0, i8* %18
br label %19
19: ; preds = %10, %3, %15
%20 = phi i32 [ %13, %15 ], [ 0, %3 ], [ 0, %10 ]
ret i32 %20
}