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

/* Variables and functions */
int /*<<< orphan*/ a3dsReadChar(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ printf(char *, char *, int);
int a3dsReadString(FILE *f, char *str, size_t maxlen) {
  char ch;
  int i = 0;
  do {
    if (!a3dsReadChar(f, &ch)) {
      return 0;
    }
    if (i < maxlen) {
      str[i++] = ch;
    }
  } while (ch != '\0');
  printf("3ds: "
         " Reading string: '%s' (len: %d)\n",
         str, i - 1);
  return 1;
}


x86 assembly (clang, -O0):
<a3dsReadString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x21(%rbp),%rsi
call   1040 <a3dsReadChar@plt>
cmp    $0x0,%eax
jne    115d <a3dsReadString+0x3d>
movl   $0x0,-0x4(%rbp)
jmp    11b2 <a3dsReadString+0x92>
movslq -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1183 <a3dsReadString+0x63>
mov    -0x21(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x28(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1188 <a3dsReadString+0x68>
movsbl -0x21(%rbp),%eax
cmp    $0x0,%eax
jne    113b <a3dsReadString+0x1b>
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%edx
sub    $0x1,%edx
lea    0xe5a(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<a3dsReadString>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%rbp
xor    %r14d,%r14d
lea    0x7(%rsp),%r13
xor    %ebx,%ebx
jmp    1144 <a3dsReadString+0x24>
test   %al,%al
je     1169 <a3dsReadString+0x49>
mov    %rbp,%rdi
mov    %r13,%rsi
call   1040 <a3dsReadChar@plt>
test   %eax,%eax
je     1183 <a3dsReadString+0x63>
movslq %ebx,%rcx
movzbl 0x7(%rsp),%eax
cmp    %r12,%rcx
jae    1140 <a3dsReadString+0x20>
lea    0x1(%rcx),%ebx
mov    %al,(%r15,%rcx,1)
jmp    1140 <a3dsReadString+0x20>
add    $0xffffffff,%ebx
lea    0xe8d(%rip),%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1030 <printf@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [38 x i8] c"3ds: Reading string: '%s' (len: %d)\0A\00"
define i32 @a3dsReadString(i32* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8
%9 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 0, i32* %9
br label %10
10: ; preds = %28, %3
%11 = load i32*, i32** %5
%12 = call i32 @a3dsReadChar(i32* %11, i8* %8)
%13 = icmp ne i32 %12, 0
br i1 %13, label %15, label %14
14: ; preds = %10
store i32 0, i32* %4
br label %37
15: ; preds = %10
%16 = load i32, i32* %9
%17 = sext i32 %16 to i64
%18 = load i64, i64* %7
%19 = icmp ult i64 %17, %18
br i1 %19, label %20, label %27
20: ; preds = %15
%21 = load i8, i8* %8
%22 = load i8*, i8** %6
%23 = load i32, i32* %9
%24 = add nsw i32 %23, 1
store i32 %24, i32* %9
%25 = sext i32 %23 to i64
%26 = getelementptr inbounds i8, i8* %22, i64 %25
store i8 %21, i8* %26
br label %27
27: ; preds = %20, %15
br label %28
28: ; preds = %27
%29 = load i8, i8* %8
%30 = sext i8 %29 to i32
%31 = icmp ne i32 %30, 0
br i1 %31, label %10, label %32
32: ; preds = %28
%33 = load i8*, i8** %6
%34 = load i32, i32* %9
%35 = sub nsw i32 %34, 1
%36 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %33, i32 %35)
store i32 1, i32* %4
br label %37
37: ; preds = %32, %14
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
@.str = [38 x i8] c"3ds: Reading string: '%s' (len: %d)\0A\00"
define i32 @a3dsReadString(i32* %0, i8* %1, i64 %2) {
%4 = alloca i8
br label %5
5: ; preds = %16, %3
%6 = phi i32 [ 0, %3 ], [ %17, %16 ]
%7 = call i32 @a3dsReadChar(i32* %0, i8* %4)
%8 = icmp eq i32 %7, 0
br i1 %8, label %22, label %9
9: ; preds = %5
%10 = sext i32 %6 to i64
%11 = icmp ult i64 %10, %2
%12 = load i8, i8* %4
br i1 %11, label %13, label %16
13: ; preds = %9
%14 = add nsw i32 %6, 1
%15 = getelementptr inbounds i8, i8* %1, i64 %10
store i8 %12, i8* %15
br label %16
16: ; preds = %9, %13
%17 = phi i32 [ %14, %13 ], [ %6, %9 ]
%18 = icmp eq i8 %12, 0
br i1 %18, label %19, label %5
19: ; preds = %16
%20 = add nsw i32 %17, -1
%21 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %1, i32 %20)
br label %22
22: ; preds = %5, %19
%23 = phi i32 [ 1, %19 ], [ 0, %5 ]
ret i32 %23
}