#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 fileaccess {
  int dummy;
};

/* Variables and functions */
int file_read_buf(struct fileaccess *, char *, size_t);
int file_read_string(struct fileaccess *fa, char *buf, size_t size) {
  int count;
  if (!size)
    return 0;
  count = file_read_buf(fa, buf, size - 1);
  if (count < 0)
    return count;
  buf[count] = '\0';
  return count;
}


x86 assembly (clang, -O0):
<file_read_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    113b <file_read_string+0x2b>
movl   $0x0,-0x4(%rbp)
jmp    117a <file_read_string+0x6a>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    $0x1,%rdx
call   1030 <file_read_buf@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1168 <file_read_string+0x58>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    117a <file_read_string+0x6a>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_read_string>:
push   %rbx
test   %rdx,%rdx
je     112e <file_read_string+0x1e>
mov    %rsi,%rbx
add    $0xffffffffffffffff,%rdx
call   1030 <file_read_buf@plt>
test   %eax,%eax
js     112c <file_read_string+0x1c>
mov    %eax,%ecx
movb   $0x0,(%rbx,%rcx,1)
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.fileaccess = type { i32 }
define i32 @file_read_string(%struct.fileaccess* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca %struct.fileaccess*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store %struct.fileaccess* %0, %struct.fileaccess** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load i64, i64* %7
%10 = icmp ne i64 %9, 0
br i1 %10, label %12, label %11
11: ; preds = %3
store i32 0, i32* %4
br label %28
12: ; preds = %3
%13 = load %struct.fileaccess*, %struct.fileaccess** %5
%14 = load i8*, i8** %6
%15 = load i64, i64* %7
%16 = sub i64 %15, 1
%17 = call i32 @file_read_buf(%struct.fileaccess* %13, i8* %14, i64 %16)
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %12
%21 = load i32, i32* %8
store i32 %21, i32* %4
br label %28
22: ; preds = %12
%23 = load i8*, i8** %6
%24 = load i32, i32* %8
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 0, i8* %26
%27 = load i32, i32* %8
store i32 %27, i32* %4
br label %28
28: ; preds = %22, %20, %11
%29 = load i32, i32* %4
ret i32 %29
}

LLVM IR (-O2):
%struct.fileaccess = type { i32 }
define i32 @file_read_string(%struct.fileaccess* %0, i8* %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %12, label %5
5: ; preds = %3
%6 = add i64 %2, -1
%7 = tail call i32 @file_read_buf(%struct.fileaccess* %0, i8* %1, i64 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %5
%10 = zext i32 %7 to i64
%11 = getelementptr inbounds i8, i8* %1, i64 %10
store i8 0, i8* %11
br label %12
12: ; preds = %5, %3, %9
%13 = phi i32 [ %7, %9 ], [ 0, %3 ], [ %7, %5 ]
ret i32 %13
}