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

/* Variables and functions */
char *_malloc_(int);
char readByte();
int readShort();
char *readName() {
  int count, length;
  char *name;
  length = readShort();
  if (length > 0) {
    name = _malloc_(length + 1);
    for (count = 0; count < length; count++) {
      name[count] = readByte();
    }
    name[count] = '\0';
    return name;
  } else {
    return "unknown";
  }
}


x86 assembly (clang, -O0):
<readName>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1050 <readShort@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    11a9 <readName+0x79>
mov    -0x10(%rbp),%edi
add    $0x1,%edi
call   1030 <_malloc_@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1190 <readName+0x60>
mov    $0x0,%al
call   1040 <readByte@plt>
mov    %al,%dl
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1162 <readName+0x32>
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b4 <readName+0x84>
lea    0xe50(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readName>:
push   %r15
push   %r14
push   %rbx
xor    %eax,%eax
call   1050 <readShort@plt>
test   %eax,%eax
jle    117b <readName+0x4b>
mov    %eax,%r14d
lea    0x1(%r14),%edi
call   1030 <_malloc_@plt>
mov    %rax,%r15
mov    %r14d,%r14d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %eax,%eax
call   1040 <readByte@plt>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1160 <readName+0x30>
movb   $0x0,(%r15,%r14,1)
jmp    1182 <readName+0x52>
lea    0xe7e(%rip),%r15
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [8 x i8] c"unknown\00"
define i8* @readName() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = call i32 (...) @readShort()
store i32 %5, i32* %3
%6 = load i32, i32* %3
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %31
8: ; preds = %0
%9 = load i32, i32* %3
%10 = add nsw i32 %9, 1
%11 = call i8* @_malloc_(i32 %10)
store i8* %11, i8** %4
store i32 0, i32* %2
br label %12
12: ; preds = %22, %8
%13 = load i32, i32* %2
%14 = load i32, i32* %3
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %25
16: ; preds = %12
%17 = call signext i8 (...) @readByte()
%18 = load i8*, i8** %4
%19 = load i32, i32* %2
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
store i8 %17, i8* %21
br label %22
22: ; preds = %16
%23 = load i32, i32* %2
%24 = add nsw i32 %23, 1
store i32 %24, i32* %2
br label %12
25: ; preds = %12
%26 = load i8*, i8** %4
%27 = load i32, i32* %2
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 0, i8* %29
%30 = load i8*, i8** %4
store i8* %30, i8** %1
br label %32
31: ; preds = %0
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %1
br label %32
32: ; preds = %31, %25
%33 = load i8*, i8** %1
ret i8* %33
}

LLVM IR (-O2):
@.str = [8 x i8] c"unknown\00"
define i8* @readName() {
%1 = tail call i32 (...) @readShort()
%2 = icmp sgt i32 %1, 0
br i1 %2, label %3, label %15
3: ; preds = %0
%4 = add nuw nsw i32 %1, 1
%5 = tail call i8* @_malloc_(i32 %4)
%6 = zext i32 %1 to i64
br label %7
7: ; preds = %3, %7
%8 = phi i64 [ 0, %3 ], [ %11, %7 ]
%9 = tail call signext i8 (...) @readByte()
%10 = getelementptr inbounds i8, i8* %5, i64 %8
store i8 %9, i8* %10
%11 = add nuw nsw i64 %8, 1
%12 = icmp eq i64 %11, %6
br i1 %12, label %13, label %7
13: ; preds = %7
%14 = getelementptr inbounds i8, i8* %5, i64 %6
store i8 0, i8* %14
br label %15
15: ; preds = %0, %13
%16 = phi i8* [ %5, %13 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %0 ]
ret i8* %16
}