#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 int /*<<< orphan*/ uint8_t;
typedef int int32_t;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int);
char *malloc(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(char *, char *, int);
int tms_get_int(int /*<<< orphan*/ *, int *, int);
char *tms_get_string(uint8_t *msg, int32_t n, int32_t start) {
  int32_t size;
  int32_t i = start;
  char *string = NULL;
  size = 2 * (tms_get_int(msg, &i, 2) - 1);
  if (i + size > n) {
    fprintf(stderr,
            "# Error: tms_get_string: index 0x%04X out of range 0x%04X\n",
            i + size, n);
  } else {
    string = malloc(size);
    strncpy(string, (char *)&msg[i], size);
  }
  return (string);
}


x86 assembly (clang, -O0):
<tms_get_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x2,%edx
call   1040 <tms_get_int@plt>
sub    $0x1,%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jle    11ad <tms_get_string+0x6d>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
add    -0x14(%rbp),%edx
mov    -0xc(%rbp),%ecx
lea    0xe5d(%rip),%rsi
call   1050 <fprintf@plt>
jmp    11d4 <tms_get_string+0x94>
mov    -0x14(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movslq -0x18(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tms_get_string>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%r15
mov    %edx,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    $0x2,%edx
call   1040 <tms_get_int@plt>
movslq 0x4(%rsp),%rbx
lea    (%rbx,%rax,2),%edx
add    $0xfffffffe,%edx
cmp    %ebp,%edx
jle    1189 <tms_get_string+0x49>
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
lea    0xe82(%rip),%rsi
mov    %ebp,%ecx
call   1050 <fprintf@plt>
xor    %ebp,%ebp
jmp    11ab <tms_get_string+0x6b>
lea    (%rax,%rax,1),%r14d
add    $0xfffffffe,%r14d
mov    %r14d,%edi
call   1060 <malloc@plt>
mov    %rax,%rbp
lea    (%r15,%rbx,4),%rsi
mov    %rax,%rdi
mov    %r14d,%edx
call   1030 <strncpy@plt>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [59 x i8] c"# Error: tms_get_string: index 0x%04X out of range 0x%04X\0A\00"
define i8* @tms_get_string(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%10 = load i32, i32* %6
store i32 %10, i32* %8
store i8* null, i8** %9
%11 = load i32*, i32** %4
%12 = call i32 @tms_get_int(i32* %11, i32* %8, i32 2)
%13 = sub nsw i32 %12, 1
%14 = mul nsw i32 2, %13
store i32 %14, i32* %7
%15 = load i32, i32* %8
%16 = load i32, i32* %7
%17 = add nsw i32 %15, %16
%18 = load i32, i32* %5
%19 = icmp sgt i32 %17, %18
br i1 %19, label %20, label %27
20: ; preds = %3
%21 = load i32, i32* @stderr
%22 = load i32, i32* %8
%23 = load i32, i32* %7
%24 = add nsw i32 %22, %23
%25 = load i32, i32* %5
%26 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0), i32 %24, i32 %25)
br label %38
27: ; preds = %3
%28 = load i32, i32* %7
%29 = call i8* @malloc(i32 %28)
store i8* %29, i8** %9
%30 = load i8*, i8** %9
%31 = load i32*, i32** %4
%32 = load i32, i32* %8
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i32, i32* %31, i64 %33
%35 = bitcast i32* %34 to i8*
%36 = load i32, i32* %7
%37 = call i32 @strncpy(i8* %30, i8* %35, i32 %36)
br label %38
38: ; preds = %27, %20
%39 = load i8*, i8** %9
ret i8* %39
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [59 x i8] c"# Error: tms_get_string: index 0x%04X out of range 0x%04X\0A\00"
define i8* @tms_get_string(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
store i32 %2, i32* %4
%6 = call i32 @tms_get_int(i32* %0, i32* %4, i32 2)
%7 = shl i32 %6, 1
%8 = add i32 %7, -2
%9 = load i32, i32* %4
%10 = add nsw i32 %8, %9
%11 = icmp sgt i32 %10, %1
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %1)
br label %21
15: ; preds = %3
%16 = call i8* @malloc(i32 %8)
%17 = sext i32 %9 to i64
%18 = getelementptr inbounds i32, i32* %0, i64 %17
%19 = bitcast i32* %18 to i8*
%20 = call i32 @strncpy(i8* %16, i8* %19, i32 %8)
br label %21
21: ; preds = %15, %12
%22 = phi i8* [ null, %12 ], [ %16, %15 ]
ret i8* %22
}