#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, ...);
scalar_t__ 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;
  if (i > n) {
    fprintf(stderr,
            "# Error: tms_get_string: index 0x%04X out of range 0x%04X\n", i,
            n);
  }
  size = 2 * (tms_get_int(msg, &i, 2) - 1);
  if (i + size > n || size <= 0) {
    fprintf(stderr,
            "# Error: tms_get_string: index 0x%04X out of range 0x%04X "
            "(length:%d)\n",
            i + size, n, size);
  } else {
    string = (char *)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    -0x18(%rbp),%eax
cmp    -0xc(%rbp),%eax
jle    1189 <tms_get_string+0x49>
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
mov    -0xc(%rbp),%ecx
lea    0xe7e(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
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
jg     11bd <tms_get_string+0x7d>
cmpl   $0x0,-0x14(%rbp)
jg     11e6 <tms_get_string+0xa6>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
add    -0x14(%rbp),%edx
mov    -0xc(%rbp),%ecx
mov    -0x14(%rbp),%r8d
lea    0xe61(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    120d <tms_get_string+0xcd>
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,%r15d
mov    %rdi,%r14
mov    %edx,0x4(%rsp)
cmp    %esi,%edx
jle    116f <tms_get_string+0x2f>
mov    0x2e84(%rip),%rax
mov    (%rax),%edi
lea    0xe9b(%rip),%rsi
mov    %r15d,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0x4(%rsp),%rsi
mov    %r14,%rdi
mov    $0x2,%edx
call   1040 <tms_get_int@plt>
lea    (%rax,%rax,1),%ebp
add    $0xfffffffe,%ebp
mov    0x4(%rsp),%edx
add    %ebp,%edx
test   %ebp,%ebp
jle    11b5 <tms_get_string+0x75>
cmp    %r15d,%edx
jg     11b5 <tms_get_string+0x75>
mov    %ebp,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
movslq 0x4(%rsp),%rax
lea    (%r14,%rax,4),%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
call   1030 <strncpy@plt>
jmp    11d4 <tms_get_string+0x94>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    0xe76(%rip),%rsi
xor    %ebx,%ebx
mov    %r15d,%ecx
mov    %ebp,%r8d
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbx,%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"
@.str.1 = [71 x i8] c"# Error: tms_get_string: index 0x%04X out of range 0x%04X (length:%d)\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* %8
%12 = load i32, i32* %5
%13 = icmp sgt i32 %11, %12
br i1 %13, label %14, label %19
14: ; preds = %3
%15 = load i32, i32* @stderr
%16 = load i32, i32* %8
%17 = load i32, i32* %5
%18 = call i32 (i32, i8*, i32, i32, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0), i32 %16, i32 %17)
br label %19
19: ; preds = %14, %3
%20 = load i32*, i32** %4
%21 = call i32 @tms_get_int(i32* %20, i32* %8, i32 2)
%22 = sub nsw i32 %21, 1
%23 = mul nsw i32 2, %22
store i32 %23, i32* %7
%24 = load i32, i32* %8
%25 = load i32, i32* %7
%26 = add nsw i32 %24, %25
%27 = load i32, i32* %5
%28 = icmp sgt i32 %26, %27
br i1 %28, label %32, label %29
29: ; preds = %19
%30 = load i32, i32* %7
%31 = icmp sle i32 %30, 0
br i1 %31, label %32, label %40
32: ; preds = %29, %19
%33 = load i32, i32* @stderr
%34 = load i32, i32* %8
%35 = load i32, i32* %7
%36 = add nsw i32 %34, %35
%37 = load i32, i32* %5
%38 = load i32, i32* %7
%39 = call i32 (i32, i8*, i32, i32, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.1, i64 0, i64 0), i32 %36, i32 %37, i32 %38)
br label %52
40: ; preds = %29
%41 = load i32, i32* %7
%42 = call i64 @malloc(i32 %41)
%43 = inttoptr i64 %42 to i8*
store i8* %43, i8** %9
%44 = load i8*, i8** %9
%45 = load i32*, i32** %4
%46 = load i32, i32* %8
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i32, i32* %45, i64 %47
%49 = bitcast i32* %48 to i8*
%50 = load i32, i32* %7
%51 = call i32 @strncpy(i8* %44, i8* %49, i32 %50)
br label %52
52: ; preds = %40, %32
%53 = load i8*, i8** %9
ret i8* %53
}

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"
@.str.1 = [71 x i8] c"# Error: tms_get_string: index 0x%04X out of range 0x%04X (length:%d)\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 = icmp sgt i32 %2, %1
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, i32, i32, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0), i32 %2, i32 %1)
br label %10
10: ; preds = %7, %3
%11 = call i32 @tms_get_int(i32* %0, i32* %4, i32 2)
%12 = shl i32 %11, 1
%13 = add i32 %12, -2
%14 = load i32, i32* %4
%15 = add nsw i32 %13, %14
%16 = icmp sgt i32 %15, %1
%17 = icmp slt i32 %13, 1
%18 = or i1 %17, %16
br i1 %18, label %19, label %22
19: ; preds = %10
%20 = load i32, i32* @stderr
%21 = call i32 (i32, i8*, i32, i32, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 %1, i32 %13)
br label %30
22: ; preds = %10
%23 = call i64 @malloc(i32 %13)
%24 = inttoptr i64 %23 to i8*
%25 = load i32, i32* %4
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i32, i32* %0, i64 %26
%28 = bitcast i32* %27 to i8*
%29 = call i32 @strncpy(i8* %24, i8* %28, i32 %13)
br label %30
30: ; preds = %22, %19
%31 = phi i8* [ null, %19 ], [ %24, %22 ]
ret i8* %31
}