#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 */
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int msx_readstr(char *fn, char *buff, int *size) {
  int fd = open(fn, 0);
  int r;
  if (fd == -1)
    return (0);
  r = read(fd, buff, *size - 1);
  close(fd);
  if (r == -1)
    return 0;
  buff[*size - 1] = '\0';
  *size = r;
  return 1;
}


x86 assembly (clang, -O0):
<msx_readstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1168 <msx_readstr+0x38>
movl   $0x0,-0x4(%rbp)
jmp    11c2 <msx_readstr+0x92>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
sub    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
cmpl   $0xffffffff,-0x28(%rbp)
jne    119e <msx_readstr+0x6e>
movl   $0x0,-0x4(%rbp)
jmp    11c2 <msx_readstr+0x92>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<msx_readstr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
xor    %r12d,%r12d
xor    %esi,%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     117f <msx_readstr+0x4f>
mov    %eax,%ebx
mov    (%r15),%edx
add    $0xffffffff,%edx
mov    %eax,%edi
mov    %r14,%rsi
call   1040 <read@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1030 <close@plt>
cmp    $0xffffffff,%ebp
je     117f <msx_readstr+0x4f>
movslq (%r15),%rax
movb   $0x0,-0x1(%rax,%r14,1)
mov    %ebp,(%r15)
mov    $0x1,%r12d
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @msx_readstr(i8* %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
%10 = load i8*, i8** %5
%11 = call i32 @open(i8* %10, i32 0)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 0, i32* %4
br label %36
15: ; preds = %3
%16 = load i32, i32* %8
%17 = load i8*, i8** %6
%18 = load i32*, i32** %7
%19 = load i32, i32* %18
%20 = sub nsw i32 %19, 1
%21 = call i32 @read(i32 %16, i8* %17, i32 %20)
store i32 %21, i32* %9
%22 = load i32, i32* %8
%23 = call i32 @close(i32 %22)
%24 = load i32, i32* %9
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %27
26: ; preds = %15
store i32 0, i32* %4
br label %36
27: ; preds = %15
%28 = load i8*, i8** %6
%29 = load i32*, i32** %7
%30 = load i32, i32* %29
%31 = sub nsw i32 %30, 1
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %28, i64 %32
store i8 0, i8* %33
%34 = load i32, i32* %9
%35 = load i32*, i32** %7
store i32 %34, i32* %35
store i32 1, i32* %4
br label %36
36: ; preds = %27, %26, %14
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
define i32 @msx_readstr(i8* nocapture %0, i8* nocapture %1, i32* nocapture %2) {
%4 = tail call i32 @open(i8* %0, i32 0)
%5 = icmp eq i32 %4, -1
br i1 %5, label %17, label %6
6: ; preds = %3
%7 = load i32, i32* %2
%8 = add nsw i32 %7, -1
%9 = tail call i32 @read(i32 %4, i8* %1, i32 %8)
%10 = tail call i32 @close(i32 %4)
%11 = icmp eq i32 %9, -1
br i1 %11, label %17, label %12
12: ; preds = %6
%13 = load i32, i32* %2
%14 = add nsw i32 %13, -1
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %1, i64 %15
store i8 0, i8* %16
store i32 %9, i32* %2
br label %17
17: ; preds = %6, %3, %12
%18 = phi i32 [ 1, %12 ], [ 0, %3 ], [ 0, %6 ]
ret i32 %18
}