#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 superblock_type {
  int dummy;
};
typedef int int32_t;

/* Variables and functions */
int /*<<< orphan*/ romfs_read_noinc(struct superblock_type *, char *, int, int);
int /*<<< orphan*/ strncat(char *, char *, int);
int32_t romfs_read_string(struct superblock_type *sb, int32_t offset,
                          char *buffer, int32_t size) {
  char temp_buffer[16];
  int32_t our_offset = offset;
  int32_t max_stringsize = size;
  int32_t max_length;
  buffer[0] = 0;
  while (1) {
    romfs_read_noinc(sb, temp_buffer, our_offset, 16);
    our_offset += 16;
    if (max_stringsize > 0) {
      if (max_stringsize > 16) {
        max_length = 16;
      } else {
        max_length = max_stringsize;
      }
      strncat(buffer, temp_buffer, max_length);
    }
    if (temp_buffer[15] == 0)
      break;
    max_stringsize -= 16;
  }
  return our_offset - offset;
}


x86 assembly (clang, -O0):
<romfs_read_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edx
mov    $0x10,%ecx
call   1040 <romfs_read_noinc@plt>
mov    -0x34(%rbp),%eax
add    $0x10,%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x38(%rbp)
jle    119d <romfs_read_string+0x7d>
cmpl   $0x10,-0x38(%rbp)
jle    1187 <romfs_read_string+0x67>
movl   $0x10,-0x3c(%rbp)
jmp    118d <romfs_read_string+0x6d>
mov    -0x38(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1030 <strncat@plt>
movsbl -0x21(%rbp),%eax
cmp    $0x0,%eax
jne    11af <romfs_read_string+0x8f>
jmp    11bd <romfs_read_string+0x9d>
mov    -0x38(%rbp),%eax
sub    $0x10,%eax
mov    %eax,-0x38(%rbp)
jmp    1149 <romfs_read_string+0x29>
mov    -0x34(%rbp),%eax
sub    -0xc(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<romfs_read_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%ebx
mov    %rdx,%r14
mov    %esi,%r15d
mov    %rdi,%r12
movb   $0x0,(%rdx)
xor    %r13d,%r13d
mov    %rsp,%rbp
jmp    115e <romfs_read_string+0x3e>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0xfffffff0,%ebx
add    $0x10,%r13d
cmpb   $0x0,0xf(%rsp)
je     118e <romfs_read_string+0x6e>
lea    (%r15,%r13,1),%edx
mov    %r12,%rdi
mov    %rbp,%rsi
mov    $0x10,%ecx
call   1040 <romfs_read_noinc@plt>
test   %ebx,%ebx
jle    1150 <romfs_read_string+0x30>
cmp    $0x10,%ebx
mov    $0x10,%edx
cmovb  %ebx,%edx
mov    %r14,%rdi
mov    %rbp,%rsi
call   1030 <strncat@plt>
jmp    1150 <romfs_read_string+0x30>
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.superblock_type = type { i32 }
define i32 @romfs_read_string(%struct.superblock_type* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca %struct.superblock_type*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca [16 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store %struct.superblock_type* %0, %struct.superblock_type** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%13 = load i32, i32* %6
store i32 %13, i32* %10
%14 = load i32, i32* %8
store i32 %14, i32* %11
%15 = load i8*, i8** %7
%16 = getelementptr inbounds i8, i8* %15, i64 0
store i8 0, i8* %16
br label %17
17: ; preds = %4, %43
%18 = load %struct.superblock_type*, %struct.superblock_type** %5
%19 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%20 = load i32, i32* %10
%21 = call i32 @romfs_read_noinc(%struct.superblock_type* %18, i8* %19, i32 %20, i32 16)
%22 = load i32, i32* %10
%23 = add nsw i32 %22, 16
store i32 %23, i32* %10
%24 = load i32, i32* %11
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %37
26: ; preds = %17
%27 = load i32, i32* %11
%28 = icmp sgt i32 %27, 16
br i1 %28, label %29, label %30
29: ; preds = %26
store i32 16, i32* %12
br label %32
30: ; preds = %26
%31 = load i32, i32* %11
store i32 %31, i32* %12
br label %32
32: ; preds = %30, %29
%33 = load i8*, i8** %7
%34 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%35 = load i32, i32* %12
%36 = call i32 @strncat(i8* %33, i8* %34, i32 %35)
br label %37
37: ; preds = %32, %17
%38 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 15
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %37
br label %46
43: ; preds = %37
%44 = load i32, i32* %11
%45 = sub nsw i32 %44, 16
store i32 %45, i32* %11
br label %17
46: ; preds = %42
%47 = load i32, i32* %10
%48 = load i32, i32* %6
%49 = sub nsw i32 %47, %48
ret i32 %49
}

LLVM IR (-O2):
%struct.superblock_type = type { i32 }
define i32 @romfs_read_string(%struct.superblock_type* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca [16 x i8]
%6 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
store i8 0, i8* %2
%7 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 15
br label %8
8: ; preds = %18, %4
%9 = phi i32 [ %1, %4 ], [ %12, %18 ]
%10 = phi i32 [ %3, %4 ], [ %21, %18 ]
%11 = call i32 @romfs_read_noinc(%struct.superblock_type* %0, i8* %6, i32 %9, i32 16)
%12 = add nsw i32 %9, 16
%13 = icmp sgt i32 %10, 0
br i1 %13, label %14, label %18
14: ; preds = %8
%15 = icmp ult i32 %10, 16
%16 = select i1 %15, i32 %10, i32 16
%17 = call i32 @strncat(i8* %2, i8* %6, i32 %16)
br label %18
18: ; preds = %14, %8
%19 = load i8, i8* %7
%20 = icmp eq i8 %19, 0
%21 = add nsw i32 %10, -16
br i1 %20, label %22, label %8
22: ; preds = %18
%23 = sub nsw i32 %12, %1
ret i32 %23
}