#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 struct string const string;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ string_append_char(struct string const *, char const);
char *string_get(struct string const *);
scalar_t__ string_length(struct string const *);
int /*<<< orphan*/ string_set(struct string const *, char *);
void string_substr(struct string *dst, struct string const *src, int first_idx,
                   int last_idx) {
  assert(src != dst);
  int src_len = (int)string_length(src);
  string_set(dst, "");
  if (!src_len)
    return;
  if (first_idx < 0)
    first_idx = 0;
  if (last_idx < 0)
    return;
  if (first_idx >= src_len)
    return;
  if (last_idx >= src_len)
    last_idx = src_len - 1;
  char const *src_c_str = string_get(src);
  string_set(dst, "");
  int idx = first_idx;
  while (idx <= last_idx) {
    string_append_char(dst, src_c_str[idx]);
    idx++;
  }
}


x86 assembly (clang, -O0):
<string_substr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x8(%rbp),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1070 <string_length@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe6e(%rip),%rsi
call   1030 <string_set@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    11a6 <string_substr+0x56>
jmp    123e <string_substr+0xee>
cmpl   $0x0,-0x14(%rbp)
jge    11b7 <string_substr+0x67>
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11c6 <string_substr+0x76>
jmp    123e <string_substr+0xee>
mov    -0x14(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jl     11d7 <string_substr+0x87>
jmp    123e <string_substr+0xee>
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jl     11ec <string_substr+0x9c>
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <string_get@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xdfc(%rip),%rsi
call   1030 <string_set@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jg     123e <string_substr+0xee>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsbl (%rax,%rcx,1),%esi
call   1040 <string_append_char@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    120f <string_substr+0xbf>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_substr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%ebx
mov    %edx,%r12d
mov    %rsi,%r14
mov    %rdi,%r13
xor    %edi,%edi
cmp    %r13,%rsi
setne  %dil
call   1050 <assert@plt>
mov    %r14,%rdi
call   1070 <string_length@plt>
mov    %rax,%rbp
lea    0xe7a(%rip),%rsi
mov    %r13,%rdi
call   1030 <string_set@plt>
test   %ebp,%ebp
je     11f6 <string_substr+0xa6>
xor    %r15d,%r15d
test   %r12d,%r12d
cmovns %r12d,%r15d
test   %ebx,%ebx
js     11f6 <string_substr+0xa6>
cmp    %ebp,%r15d
jge    11f6 <string_substr+0xa6>
mov    %ebx,%eax
lea    -0x1(%rbp),%ebx
cmp    %eax,%ebp
cmovg  %eax,%ebx
mov    %r14,%rdi
call   1060 <string_get@plt>
mov    %rax,%r14
lea    0xe3f(%rip),%rsi
mov    %r13,%rdi
call   1030 <string_set@plt>
cmp    %ebx,%r15d
jg     11f6 <string_substr+0xa6>
mov    %r15d,%ebp
add    $0x1,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsbl (%r14,%rbp,1),%esi
mov    %r13,%rdi
call   1040 <string_append_char@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    11e0 <string_substr+0x90>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.string = type opaque
@.str = [1 x i8] zeroinitializer
define void @string_substr(%struct.string* %0, %struct.string* %1, i32 %2, i32 %3) {
%5 = alloca %struct.string*
%6 = alloca %struct.string*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
store %struct.string* %0, %struct.string** %5
store %struct.string* %1, %struct.string** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%12 = load %struct.string*, %struct.string** %6
%13 = load %struct.string*, %struct.string** %5
%14 = icmp ne %struct.string* %12, %13
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = load %struct.string*, %struct.string** %6
%18 = call i64 @string_length(%struct.string* %17)
%19 = trunc i64 %18 to i32
store i32 %19, i32* %9
%20 = load %struct.string*, %struct.string** %5
%21 = call i32 @string_set(%struct.string* %20, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* %9
%23 = icmp ne i32 %22, 0
br i1 %23, label %25, label %24
24: ; preds = %4
br label %65
25: ; preds = %4
%26 = load i32, i32* %7
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %25
store i32 0, i32* %7
br label %29
29: ; preds = %28, %25
%30 = load i32, i32* %8
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %29
br label %65
33: ; preds = %29
%34 = load i32, i32* %7
%35 = load i32, i32* %9
%36 = icmp sge i32 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %33
br label %65
38: ; preds = %33
%39 = load i32, i32* %8
%40 = load i32, i32* %9
%41 = icmp sge i32 %39, %40
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = load i32, i32* %9
%44 = sub nsw i32 %43, 1
store i32 %44, i32* %8
br label %45
45: ; preds = %42, %38
%46 = load %struct.string*, %struct.string** %6
%47 = call i8* @string_get(%struct.string* %46)
store i8* %47, i8** %10
%48 = load %struct.string*, %struct.string** %5
%49 = call i32 @string_set(%struct.string* %48, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%50 = load i32, i32* %7
store i32 %50, i32* %11
br label %51
51: ; preds = %55, %45
%52 = load i32, i32* %11
%53 = load i32, i32* %8
%54 = icmp sle i32 %52, %53
br i1 %54, label %55, label %65
55: ; preds = %51
%56 = load %struct.string*, %struct.string** %5
%57 = load i8*, i8** %10
%58 = load i32, i32* %11
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %57, i64 %59
%61 = load i8, i8* %60
%62 = call i32 @string_append_char(%struct.string* %56, i8 signext %61)
%63 = load i32, i32* %11
%64 = add nsw i32 %63, 1
store i32 %64, i32* %11
br label %51
65: ; preds = %24, %32, %37, %51
ret void
}

LLVM IR (-O2):
%struct.string = type opaque
@.str = [1 x i8] zeroinitializer
define void @string_substr(%struct.string* %0, %struct.string* %1, i32 %2, i32 %3) {
%5 = icmp ne %struct.string* %1, %0
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = tail call i64 @string_length(%struct.string* %1)
%9 = trunc i64 %8 to i32
%10 = tail call i32 @string_set(%struct.string* %0, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i32 %9, 0
br i1 %11, label %36, label %12
12: ; preds = %4
%13 = icmp sgt i32 %2, 0
%14 = select i1 %13, i32 %2, i32 0
%15 = icmp sgt i32 %3, -1
%16 = icmp slt i32 %14, %9
%17 = select i1 %15, i1 %16, i1 false
br i1 %17, label %18, label %36
18: ; preds = %12
%19 = icmp sgt i32 %9, %3
%20 = add nsw i32 %9, -1
%21 = select i1 %19, i32 %3, i32 %20
%22 = tail call i8* @string_get(%struct.string* %1)
%23 = tail call i32 @string_set(%struct.string* %0, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%24 = icmp sgt i32 %14, %21
br i1 %24, label %36, label %25
25: ; preds = %18
%26 = zext i32 %14 to i64
%27 = add nuw i32 %21, 1
%28 = zext i32 %27 to i64
br label %29
29: ; preds = %25, %29
%30 = phi i64 [ %26, %25 ], [ %34, %29 ]
%31 = getelementptr inbounds i8, i8* %22, i64 %30
%32 = load i8, i8* %31
%33 = tail call i32 @string_append_char(%struct.string* %0, i8 signext %32)
%34 = add nuw nsw i64 %30, 1
%35 = icmp eq i64 %34, %28
br i1 %35, label %36, label %29
36: ; preds = %29, %18, %12, %4
ret void
}