#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*/ O_RDONLY;
int /*<<< orphan*/ close(int);
long getfilesize_fd(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, long);
int getbufffromtxt(char *filename, char *buffer) {
  long txtbuffsize;
  int fd = open(filename, O_RDONLY);
  if (fd < 0) {
    printf("*** ZVM Error open %s file\n", filename);
    return -1;
  }
  txtbuffsize = getfilesize_fd(fd, NULL, 0);
  if (txtbuffsize < 0) {
    close(fd);
    return -1;
  }
  int bread = read(fd, buffer, txtbuffsize);
  if (bread < 0 || bread != txtbuffsize)
    printf("***ZVM Error read data fron txt file %s\n", filename);
  close(fd);
  return txtbuffsize;
}


x86 assembly (clang, -O0):
<getbufffromtxt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    119b <getbufffromtxt+0x4b>
mov    -0x10(%rbp),%rsi
lea    0xe76(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1216 <getbufffromtxt+0xc6>
mov    -0x24(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1030 <getfilesize_fd@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11cc <getbufffromtxt+0x7c>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1216 <getbufffromtxt+0xc6>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1060 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jl     11f7 <getbufffromtxt+0xa7>
movslq -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     1207 <getbufffromtxt+0xb7>
mov    -0x10(%rbp),%rsi
lea    0xe1a(%rip),%rdi
call   1040 <printf@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getbufffromtxt>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     11b4 <getbufffromtxt+0x64>
mov    %eax,%ebp
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1030 <getfilesize_fd@plt>
test   %rax,%rax
js     11c5 <getbufffromtxt+0x75>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %r15,%rsi
mov    %rax,%rdx
call   1060 <read@plt>
test   %eax,%eax
js     119c <getbufffromtxt+0x4c>
cltq
cmp    %rax,%rbx
je     11ab <getbufffromtxt+0x5b>
lea    0xe79(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    11d1 <getbufffromtxt+0x81>
lea    0xe45(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
jmp    11cc <getbufffromtxt+0x7c>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [28 x i8] c"*** ZVM Error open %s file\0A\00"
@.str.1 = [41 x i8] c"***ZVM Error read data fron txt file %s\0A\00"
define i32 @getbufffromtxt(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 -1, i32* %3
br label %45
17: ; preds = %2
%18 = load i32, i32* %7
%19 = call i64 @getfilesize_fd(i32 %18, i32* null, i32 0)
store i64 %19, i64* %6
%20 = load i64, i64* %6
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i32, i32* %7
%24 = call i32 @close(i32 %23)
store i32 -1, i32* %3
br label %45
25: ; preds = %17
%26 = load i32, i32* %7
%27 = load i8*, i8** %5
%28 = load i64, i64* %6
%29 = call i32 @read(i32 %26, i8* %27, i64 %28)
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = icmp slt i32 %30, 0
br i1 %31, label %37, label %32
32: ; preds = %25
%33 = load i32, i32* %8
%34 = sext i32 %33 to i64
%35 = load i64, i64* %6
%36 = icmp ne i64 %34, %35
br i1 %36, label %37, label %40
37: ; preds = %32, %25
%38 = load i8*, i8** %4
%39 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %38)
br label %40
40: ; preds = %37, %32
%41 = load i32, i32* %7
%42 = call i32 @close(i32 %41)
%43 = load i64, i64* %6
%44 = trunc i64 %43 to i32
store i32 %44, i32* %3
br label %45
45: ; preds = %40, %22, %14
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [28 x i8] c"*** ZVM Error open %s file\0A\00"
@.str.1 = [41 x i8] c"***ZVM Error read data fron txt file %s\0A\00"
define i32 @getbufffromtxt(i8* %0, i8* nocapture %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %24
8: ; preds = %2
%9 = tail call i64 @getfilesize_fd(i32 %4, i32* null, i32 0)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 @close(i32 %4)
br label %24
13: ; preds = %8
%14 = tail call i32 @read(i32 %4, i8* %1, i64 %9)
%15 = icmp sgt i32 %14, -1
%16 = sext i32 %14 to i64
%17 = icmp eq i64 %9, %16
%18 = select i1 %15, i1 %17, i1 false
br i1 %18, label %21, label %19
19: ; preds = %13
%20 = tail call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %21
21: ; preds = %13, %19
%22 = tail call i32 @close(i32 %4)
%23 = trunc i64 %9 to i32
br label %24
24: ; preds = %21, %11, %6
%25 = phi i32 [ -1, %6 ], [ -1, %11 ], [ %23, %21 ]
ret i32 %25
}