#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*/ uint16_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
void *getbyte(int /*<<< orphan*/, int);
char *sbrk(int);
int /*<<< orphan*/ write(int, char *, int);
char *cstr(uint16_t s) {
  char *st;
  int n, i;
  n = getbyte(s, 0);
  st = sbrk(n + 1);
  if (st == (char *)-1) {
    write(2, "OOM\n", 4);
    exit(1);
  }
  for (i = 1; i <= n; i++)
    st[i - 1] = getbyte(s, i);
  st[n] = 0;
  return st;
}


x86 assembly (clang, -O0):
<cstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
xor    %esi,%esi
call   1030 <getbyte@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1050 <sbrk@plt>
mov    %rax,-0x10(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x10(%rbp)
jne    1198 <cstr+0x58>
mov    $0x2,%edi
lea    0xe7c(%rip),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
movl   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     11d6 <cstr+0x96>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1030 <getbyte@plt>
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    119f <cstr+0x5f>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cstr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r15d
xor    %esi,%esi
call   1030 <getbyte@plt>
mov    %rax,%r14
lea    0x1(%rax),%ebp
mov    %ebp,%edi
call   1050 <sbrk@plt>
cmp    $0xffffffffffffffff,%rax
je     11ac <cstr+0x6c>
mov    %rax,%r12
test   %r14d,%r14d
jle    1198 <cstr+0x58>
mov    %ebp,%ebx
mov    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15d,%edi
mov    %ebp,%esi
call   1030 <getbyte@plt>
mov    %al,-0x1(%r12,%rbp,1)
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1180 <cstr+0x40>
movslq %r14d,%rax
movb   $0x0,(%r12,%rax,1)
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe4d(%rip),%rsi
mov    $0x2,%edi
mov    $0x4,%edx
call   1040 <write@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"OOM\0A\00"
define i8* @cstr(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = call i8* @getbyte(i32 %6, i32 0)
%8 = ptrtoint i8* %7 to i32
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = add nsw i32 %9, 1
%11 = call i8* @sbrk(i32 %10)
store i8* %11, i8** %3
%12 = load i8*, i8** %3
%13 = icmp eq i8* %12, inttoptr (i64 -1 to i8*)
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = call i32 @write(i32 2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %1
store i32 1, i32* %5
br label %18
18: ; preds = %32, %17
%19 = load i32, i32* %5
%20 = load i32, i32* %4
%21 = icmp sle i32 %19, %20
br i1 %21, label %22, label %35
22: ; preds = %18
%23 = load i32, i32* %2
%24 = load i32, i32* %5
%25 = call i8* @getbyte(i32 %23, i32 %24)
%26 = ptrtoint i8* %25 to i8
%27 = load i8*, i8** %3
%28 = load i32, i32* %5
%29 = sub nsw i32 %28, 1
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %27, i64 %30
store i8 %26, i8* %31
br label %32
32: ; preds = %22
%33 = load i32, i32* %5
%34 = add nsw i32 %33, 1
store i32 %34, i32* %5
br label %18
35: ; preds = %18
%36 = load i8*, i8** %3
%37 = load i32, i32* %4
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
store i8 0, i8* %39
%40 = load i8*, i8** %3
ret i8* %40
}

LLVM IR (-O2):
@.str = [5 x i8] c"OOM\0A\00"
define i8* @cstr(i32 %0) {
%2 = tail call i8* @getbyte(i32 %0, i32 0)
%3 = ptrtoint i8* %2 to i64
%4 = trunc i64 %3 to i32
%5 = add i32 %4, 1
%6 = tail call i8* @sbrk(i32 %5)
%7 = icmp eq i8* %6, inttoptr (i64 -1 to i8*)
br i1 %7, label %12, label %8
8: ; preds = %1
%9 = icmp slt i32 %4, 1
br i1 %9, label %25, label %10
10: ; preds = %8
%11 = zext i32 %5 to i64
br label %15
12: ; preds = %1
%13 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %10, %15
%16 = phi i64 [ 1, %10 ], [ %23, %15 ]
%17 = trunc i64 %16 to i32
%18 = tail call i8* @getbyte(i32 %0, i32 %17)
%19 = ptrtoint i8* %18 to i64
%20 = trunc i64 %19 to i8
%21 = add nsw i64 %16, -1
%22 = getelementptr inbounds i8, i8* %6, i64 %21
store i8 %20, i8* %22
%23 = add nuw nsw i64 %16, 1
%24 = icmp eq i64 %23, %11
br i1 %24, label %25, label %15
25: ; preds = %15, %8
%26 = shl i64 %3, 32
%27 = ashr exact i64 %26, 32
%28 = getelementptr inbounds i8, i8* %6, i64 %27
store i8 0, i8* %28
ret i8* %6
}