#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 ssize_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
int open(char *, int /*<<< orphan*/);
int read(int, char *, size_t);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int read_string(char *filename, char *buf, size_t buflen) {
  int fd;
  ssize_t r = -1;
  memset(buf, 0, buflen);
  fd = open(filename, O_RDONLY);
  if (-1 < fd) {
    r = read(fd, buf, buflen - 1);
    (void)close(fd);
    if (-1 == r) {
      fprintf(stderr, "Error while reading \"%s\": %s\n", filename,
              strerror(errno));
      exit(EXIT_FAILURE);
    }
  }
  return (int)r;
}


x86 assembly (clang, -O0):
<read_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0xffffffff,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    $0xffffffff,%eax
cmp    -0x1c(%rbp),%eax
jge    1235 <read_string+0xc5>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
call   1050 <read@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    $0xffffffff,%eax
cmp    -0x20(%rbp),%eax
jne    1230 <read_string+0xc0>
mov    0x2de7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
lea    0xde3(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    1235 <read_string+0xc5>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_string>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
xor    %esi,%esi
call   1030 <memset@plt>
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11f9 <read_string+0x89>
mov    %eax,%ebp
add    $0xffffffffffffffff,%rbx
mov    %eax,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1050 <read@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%ebx
jne    11fe <read_string+0x8e>
mov    0x2e11(%rip),%rax
mov    (%rax),%ebx
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe22(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1060 <fprintf@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@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
@stderr = global i32 0
@.str = [30 x i8] c"Error while reading \22%s\22: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @read_string(i8* %0, i8* %1, i64 %2) {
%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
store i64 %2, i64* %6
store i32 -1, i32* %8
%9 = load i8*, i8** %5
%10 = load i64, i64* %6
%11 = call i32 @memset(i8* %9, i32 0, i64 %10)
%12 = load i8*, i8** %4
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp slt i32 -1, %15
br i1 %16, label %17, label %36
17: ; preds = %3
%18 = load i32, i32* %7
%19 = load i8*, i8** %5
%20 = load i64, i64* %6
%21 = sub i64 %20, 1
%22 = call i32 @read(i32 %18, i8* %19, i64 %21)
store i32 %22, i32* %8
%23 = load i32, i32* %7
%24 = call i32 @close(i32 %23)
%25 = load i32, i32* %8
%26 = icmp eq i32 -1, %25
br i1 %26, label %27, label %35
27: ; preds = %17
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %4
%30 = load i32, i32* @errno
%31 = call i8* @strerror(i32 %30)
%32 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %29, i8* %31)
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %17
br label %36
36: ; preds = %35, %3
%37 = load i32, i32* %8
ret i32 %37
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [30 x i8] c"Error while reading \22%s\22: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @read_string(i8* %0, i8* %1, i64 %2) {
%4 = tail call i32 @memset(i8* %1, i32 0, i64 %2)
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp sgt i32 %6, -1
br i1 %7, label %8, label %20
8: ; preds = %3
%9 = add i64 %2, -1
%10 = tail call i32 @read(i32 %6, i8* %1, i64 %9)
%11 = tail call i32 @close(i32 %6)
%12 = icmp eq i32 %10, -1
br i1 %12, label %13, label %20
13: ; preds = %8
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %16)
%18 = load i32, i32* @EXIT_FAILURE
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %8, %3
%21 = phi i32 [ %10, %8 ], [ -1, %3 ]
ret i32 %21
}