#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*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(void *);
size_t lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
void *malloc(size_t);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char const *, size_t, size_t);
size_t read_all(int, void *, size_t);
void *read_strings(const char *name, size_t *size) {
  int f = open(name, O_RDONLY);
  size_t sz = *size;
  size_t r;
  void *buf;
  if (f < 0)
    return NULL;
  if (!sz) {
    sz = lseek(f, 0, SEEK_END);
    lseek(f, 0, SEEK_SET);
  }
  buf = malloc(sz);
  if (!buf) {
    close(f);
    return NULL;
  }
  r = read_all(f, buf, sz);
  close(f);
  if (r != sz) {
    printf("Error: from %s read %zd bytes out of %zd\n", name, r, sz);
    free(buf);
    return NULL;
  }
  *size = sz;
  return buf;
}


x86 assembly (clang, -O0):
<read_strings>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11b7 <read_strings+0x47>
movq   $0x0,-0x8(%rbp)
jmp    1284 <read_strings+0x114>
cmpq   $0x0,-0x28(%rbp)
jne    11ec <read_strings+0x7c>
mov    -0x1c(%rbp),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2ded(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    -0x28(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1219 <read_strings+0xa9>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1284 <read_strings+0x114>
mov    -0x1c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1090 <read_all@plt>
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     1271 <read_strings+0x101>
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    0xdaa(%rip),%rdi
call   1040 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1284 <read_strings+0x114>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_strings>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     121f <read_strings+0xaf>
mov    %eax,%ebp
mov    (%r14),%r12
test   %r12,%r12
jne    11c8 <read_strings+0x58>
mov    0x2e40(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %rax,%r12
mov    0x2e13(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %r12,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     11f9 <read_strings+0x89>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %rax,%rsi
mov    %r12,%rdx
call   1090 <read_all@plt>
mov    %rax,%r13
mov    %ebp,%edi
call   1060 <close@plt>
cmp    %r12,%r13
jne    1202 <read_strings+0x92>
mov    %r12,(%r14)
jmp    1221 <read_strings+0xb1>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    121f <read_strings+0xaf>
lea    0xdf7(%rip),%rdi
mov    %r15,%rsi
mov    %r13,%rdx
mov    %r12,%rcx
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str = [42 x i8] c"Error: from %s read %zd bytes out of %zd\0A\00"
define i8* @read_strings(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
store i8* %0, i8** %4
store i64* %1, i64** %5
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %6
%13 = load i64*, i64** %5
%14 = load i64, i64* %13
store i64 %14, i64* %7
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %2
store i8* null, i8** %3
br label %57
18: ; preds = %2
%19 = load i64, i64* %7
%20 = icmp ne i64 %19, 0
br i1 %20, label %28, label %21
21: ; preds = %18
%22 = load i32, i32* %6
%23 = load i32, i32* @SEEK_END
%24 = call i64 @lseek(i32 %22, i32 0, i32 %23)
store i64 %24, i64* %7
%25 = load i32, i32* %6
%26 = load i32, i32* @SEEK_SET
%27 = call i64 @lseek(i32 %25, i32 0, i32 %26)
br label %28
28: ; preds = %21, %18
%29 = load i64, i64* %7
%30 = call i8* @malloc(i64 %29)
store i8* %30, i8** %9
%31 = load i8*, i8** %9
%32 = icmp ne i8* %31, null
br i1 %32, label %36, label %33
33: ; preds = %28
%34 = load i32, i32* %6
%35 = call i32 @close(i32 %34)
store i8* null, i8** %3
br label %57
36: ; preds = %28
%37 = load i32, i32* %6
%38 = load i8*, i8** %9
%39 = load i64, i64* %7
%40 = call i64 @read_all(i32 %37, i8* %38, i64 %39)
store i64 %40, i64* %8
%41 = load i32, i32* %6
%42 = call i32 @close(i32 %41)
%43 = load i64, i64* %8
%44 = load i64, i64* %7
%45 = icmp ne i64 %43, %44
br i1 %45, label %46, label %53
46: ; preds = %36
%47 = load i8*, i8** %4
%48 = load i64, i64* %8
%49 = load i64, i64* %7
%50 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %47, i64 %48, i64 %49)
%51 = load i8*, i8** %9
%52 = call i32 @free(i8* %51)
store i8* null, i8** %3
br label %57
53: ; preds = %36
%54 = load i64, i64* %7
%55 = load i64*, i64** %5
store i64 %54, i64* %55
%56 = load i8*, i8** %9
store i8* %56, i8** %3
br label %57
57: ; preds = %53, %46, %33, %17
%58 = load i8*, i8** %3
ret i8* %58
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str = [42 x i8] c"Error: from %s read %zd bytes out of %zd\0A\00"
define i8* @read_strings(i8* %0, i64* nocapture %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = load i64, i64* %1
%6 = icmp slt i32 %4, 0
br i1 %6, label %28, label %7
7: ; preds = %2
%8 = icmp eq i64 %5, 0
br i1 %8, label %9, label %14
9: ; preds = %7
%10 = load i32, i32* @SEEK_END
%11 = tail call i64 @lseek(i32 %4, i32 0, i32 %10)
%12 = load i32, i32* @SEEK_SET
%13 = tail call i64 @lseek(i32 %4, i32 0, i32 %12)
br label %14
14: ; preds = %9, %7
%15 = phi i64 [ %5, %7 ], [ %11, %9 ]
%16 = tail call i8* @malloc(i64 %15)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = tail call i32 @close(i32 %4)
br label %28
20: ; preds = %14
%21 = tail call i64 @read_all(i32 %4, i8* %16, i64 %15)
%22 = tail call i32 @close(i32 %4)
%23 = icmp eq i64 %21, %15
br i1 %23, label %27, label %24
24: ; preds = %20
%25 = tail call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %0, i64 %21, i64 %15)
%26 = tail call i32 @free(i8* %16)
br label %28
27: ; preds = %20
store i64 %15, i64* %1
br label %28
28: ; preds = %2, %27, %24, %18
%29 = phi i8* [ null, %24 ], [ %16, %27 ], [ null, %18 ], [ null, %2 ]
ret i8* %29
}