#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 value {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ collect(struct value *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *get_str(struct value *);
char *malloc(int);
int /*<<< orphan*/ malloc_error();
int /*<<< orphan*/ new_str(char *);
struct value *pop();
int pop_int();
int /*<<< orphan*/ push(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
void builtin_cut() {
  int len0 = pop_int();
  struct value *v = pop();
  char *s = get_str(v);
  int len = strlen(s);
  int len1 = len - len0;
  if (len0 < 0 || len1 < 0) {
    fprintf(stderr, "cut: index out of bounds\n");
    exit(0);
  }
  char *a = malloc(len0 * sizeof(char));
  if (a == NULL) {
    malloc_error();
  }
  char *b = malloc(len1 * sizeof(char));
  if (b == NULL) {
    malloc_error();
  }
  strncpy(a, s, len0);
  a[len0] = '\0';
  strncpy(b, s + len0, len1);
  b[len1] = '\0';
  push(new_str(a));
  push(new_str(b));
  collect(v);
}


x86 assembly (clang, -O0):
<builtin_cut>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    $0x0,%al
call   1090 <pop_int@plt>
mov    %eax,-0x4(%rbp)
mov    $0x0,%al
call   1050 <pop@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <get_str@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
sub    -0x4(%rbp),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x4(%rbp)
jl     1213 <builtin_cut+0x53>
cmpl   $0x0,-0x20(%rbp)
jge    122f <builtin_cut+0x6f>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
call   1060 <fprintf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
movslq -0x4(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1254 <builtin_cut+0x94>
mov    $0x0,%al
call   10a0 <malloc_error@plt>
movslq -0x20(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1279 <builtin_cut+0xb9>
mov    $0x0,%al
call   10a0 <malloc_error@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x4(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
movslq -0x4(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
movslq -0x4(%rbp),%rax
add    %rax,%rsi
mov    -0x20(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x30(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   10b0 <new_str@plt>
mov    %eax,%edi
call   10c0 <push@plt>
mov    -0x30(%rbp),%rdi
call   10b0 <new_str@plt>
mov    %eax,%edi
call   10c0 <push@plt>
mov    -0x10(%rbp),%rdi
call   10e0 <collect@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<builtin_cut>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
xor    %eax,%eax
call   1090 <pop_int@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   1050 <pop@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <get_str@plt>
mov    %rax,%r13
mov    %rax,%rdi
call   1040 <strlen@plt>
test   %r15d,%r15d
js     12a9 <builtin_cut+0xe9>
mov    %eax,%ebp
sub    %r15d,%ebp
js     12a9 <builtin_cut+0xe9>
mov    %rbx,0x10(%rsp)
mov    %r15d,%eax
mov    %rax,0x8(%rsp)
mov    %r15d,%edi
call   1070 <malloc@plt>
mov    %rax,%r12
test   %rax,%rax
jne    122d <builtin_cut+0x6d>
xor    %eax,%eax
call   10a0 <malloc_error@plt>
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1243 <builtin_cut+0x83>
xor    %eax,%eax
call   10a0 <malloc_error@plt>
mov    %ebp,%r14d
mov    %r12,%rdi
mov    %r13,%rsi
mov    %r15d,%edx
call   1030 <strncpy@plt>
mov    0x8(%rsp),%rax
movb   $0x0,(%r12,%rax,1)
add    %rax,%r13
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
movb   $0x0,(%rbx,%r14,1)
mov    %r12,%rdi
call   10b0 <new_str@plt>
mov    %eax,%edi
call   10c0 <push@plt>
mov    %rbx,%rdi
call   10b0 <new_str@plt>
mov    %eax,%edi
call   10c0 <push@plt>
mov    0x10(%rsp),%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10e0 <collect@plt>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
lea    0xd47(%rip),%rsi
call   1060 <fprintf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.value = type { i32 }
@stderr = global i32 0
@.str = [26 x i8] c"cut: index out of bounds\0A\00"
define void @builtin_cut() {
%1 = alloca i32
%2 = alloca %struct.value*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = call i32 (...) @pop_int()
store i32 %8, i32* %1
%9 = call %struct.value* (...) @pop()
store %struct.value* %9, %struct.value** %2
%10 = load %struct.value*, %struct.value** %2
%11 = call i8* @get_str(%struct.value* %10)
store i8* %11, i8** %3
%12 = load i8*, i8** %3
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = load i32, i32* %1
%16 = sub nsw i32 %14, %15
store i32 %16, i32* %5
%17 = load i32, i32* %1
%18 = icmp slt i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %0
%20 = load i32, i32* %5
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %19, %0
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 0)
unreachable
26: ; preds = %19
%27 = load i32, i32* %1
%28 = sext i32 %27 to i64
%29 = mul i64 %28, 1
%30 = trunc i64 %29 to i32
%31 = call i8* @malloc(i32 %30)
store i8* %31, i8** %6
%32 = load i8*, i8** %6
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %36
34: ; preds = %26
%35 = call i32 (...) @malloc_error()
br label %36
36: ; preds = %34, %26
%37 = load i32, i32* %5
%38 = sext i32 %37 to i64
%39 = mul i64 %38, 1
%40 = trunc i64 %39 to i32
%41 = call i8* @malloc(i32 %40)
store i8* %41, i8** %7
%42 = load i8*, i8** %7
%43 = icmp eq i8* %42, null
br i1 %43, label %44, label %46
44: ; preds = %36
%45 = call i32 (...) @malloc_error()
br label %46
46: ; preds = %44, %36
%47 = load i8*, i8** %6
%48 = load i8*, i8** %3
%49 = load i32, i32* %1
%50 = call i32 @strncpy(i8* %47, i8* %48, i32 %49)
%51 = load i8*, i8** %6
%52 = load i32, i32* %1
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
store i8 0, i8* %54
%55 = load i8*, i8** %7
%56 = load i8*, i8** %3
%57 = load i32, i32* %1
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %56, i64 %58
%60 = load i32, i32* %5
%61 = call i32 @strncpy(i8* %55, i8* %59, i32 %60)
%62 = load i8*, i8** %7
%63 = load i32, i32* %5
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8, i8* %62, i64 %64
store i8 0, i8* %65
%66 = load i8*, i8** %6
%67 = call i32 @new_str(i8* %66)
%68 = call i32 @push(i32 %67)
%69 = load i8*, i8** %7
%70 = call i32 @new_str(i8* %69)
%71 = call i32 @push(i32 %70)
%72 = load %struct.value*, %struct.value** %2
%73 = call i32 @collect(%struct.value* %72)
ret void
}

LLVM IR (-O2):
%struct.value = type { i32 }
@stderr = global i32 0
@.str = [26 x i8] c"cut: index out of bounds\0A\00"
define void @builtin_cut() {
%1 = tail call i32 (...) @pop_int()
%2 = tail call %struct.value* (...) @pop()
%3 = tail call i8* @get_str(%struct.value* %2)
%4 = tail call i32 @strlen(i8* %3)
%5 = sub nsw i32 %4, %1
%6 = icmp slt i32 %1, 0
%7 = icmp slt i32 %5, 0
%8 = select i1 %6, i1 true, i1 %7
br i1 %8, label %9, label %13
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 0)
unreachable
13: ; preds = %0
%14 = zext i32 %1 to i64
%15 = tail call i8* @malloc(i32 %1)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = tail call i32 (...) @malloc_error()
br label %19
19: ; preds = %17, %13
%20 = tail call i8* @malloc(i32 %5)
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = tail call i32 (...) @malloc_error()
br label %24
24: ; preds = %22, %19
%25 = zext i32 %5 to i64
%26 = tail call i32 @strncpy(i8* %15, i8* %3, i32 %1)
%27 = getelementptr inbounds i8, i8* %15, i64 %14
store i8 0, i8* %27
%28 = getelementptr inbounds i8, i8* %3, i64 %14
%29 = tail call i32 @strncpy(i8* %20, i8* %28, i32 %5)
%30 = getelementptr inbounds i8, i8* %20, i64 %25
store i8 0, i8* %30
%31 = tail call i32 @new_str(i8* %15)
%32 = tail call i32 @push(i32 %31)
%33 = tail call i32 @new_str(i8* %20)
%34 = tail call i32 @push(i32 %33)
%35 = tail call i32 @collect(%struct.value* %2)
ret void
}