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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(char *, ...);
int /*<<< orphan*/ getpagesize();
char *malloc(size_t);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
int open(char const *, int /*<<< orphan*/);
scalar_t__ read(int, char *, size_t);
char *strerror(int /*<<< orphan*/);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
int read_attribute(const char *path, char *contents, size_t n) {
  static char *buf = 0;
  int fd;
  int retval = 1;
  ssize_t amt_read;
  size_t page_size = (size_t)getpagesize();
  if (!buf) {
    buf = malloc(page_size);
    if (!buf) {
      error("%s cannot allocate memory\n");
      goto out;
    }
  }
  fd = open(path, O_RDONLY);
  if (fd < 0) {
    error("cannot open %s -- %s\n", path, strerror(errno));
    goto out;
  }
  memset(buf, 0, page_size);
  amt_read = read(fd, buf, page_size);
  if (amt_read < 0) {
    error("cannot read attribute %s -- %s\n", path, strerror(errno));
    goto fd;
  }
  if (strlen(buf) > 0)
    buf[strlen(buf) - 1] = '\0';
  strncpy(contents, buf, n < page_size ? n : page_size);
  retval = 0;
fd:
  close(fd);
out:
  return retval;
}


x86 assembly (clang, -O0):
<read_attribute>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
mov    $0x0,%al
call   10b0 <getpagesize@plt>
cltq
mov    %rax,-0x30(%rbp)
cmpq   $0x0,0x2ea8(%rip)
jne    120c <read_attribute+0x6c>
mov    -0x30(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,0x2e92(%rip)
cmpq   $0x0,0x2e8a(%rip)
jne    1207 <read_attribute+0x67>
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1090 <error@plt>
jmp    1351 <read_attribute+0x1b1>
jmp    120c <read_attribute+0x6c>
mov    -0x8(%rbp),%rdi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    125b <read_attribute+0xbb>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1090 <error@plt>
jmp    1351 <read_attribute+0x1b1>
mov    0x2e16(%rip),%rdi
mov    -0x30(%rbp),%rdx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x1c(%rbp),%edi
mov    0x2e01(%rip),%rsi
mov    -0x30(%rbp),%rdx
call   1070 <read@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    12bf <read_attribute+0x11f>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    %rax,%rdx
lea    0xd7e(%rip),%rdi
mov    $0x0,%al
call   1090 <error@plt>
jmp    1349 <read_attribute+0x1a9>
mov    0x2db2(%rip),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%eax
jle    12fb <read_attribute+0x15b>
mov    0x2d9d(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d92(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2d6e(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    1329 <read_attribute+0x189>
mov    -0x18(%rbp),%rax
mov    %rax,-0x60(%rbp)
jmp    1331 <read_attribute+0x191>
mov    -0x30(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x50(%rbp),%rsi
mov    -0x58(%rbp),%rdi
mov    -0x60(%rbp),%rdx
call   1030 <strncpy@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_attribute>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
xor    %eax,%eax
call   10b0 <getpagesize@plt>
movslq %eax,%rbx
cmpq   $0x0,0x2eb5(%rip)
jne    11dd <read_attribute+0x3d>
mov    %rbx,%rdi
call   1080 <malloc@plt>
mov    %rax,0x2ea4(%rip)
test   %rax,%rax
je     12b3 <read_attribute+0x113>
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     125f <read_attribute+0xbf>
mov    %eax,%ebp
mov    0x2e7d(%rip),%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1050 <memset@plt>
mov    0x2e6c(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1070 <read@plt>
test   %rax,%rax
js     1283 <read_attribute+0xe3>
mov    0x2e56(%rip),%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    1242 <read_attribute+0xa2>
mov    0x2e46(%rip),%r12
mov    %r12,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%r12,1)
mov    0x2e2f(%rip),%rsi
cmp    %r15,%rbx
cmova  %r15,%rbx
mov    %r14,%rdi
mov    %rbx,%rdx
call   1030 <strncpy@plt>
xor    %ebx,%ebx
jmp    12aa <read_attribute+0x10a>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xda7(%rip),%rdi
mov    %r12,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1090 <error@plt>
jmp    12c1 <read_attribute+0x121>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd99(%rip),%rdi
mov    %r12,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1090 <error@plt>
mov    $0x1,%ebx
mov    %ebp,%edi
call   1060 <close@plt>
jmp    12c6 <read_attribute+0x126>
lea    0xd46(%rip),%rdi
xor    %eax,%eax
call   1090 <error@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@read_attribute.buf = internal global i8* null
@.str = [27 x i8] c"%s cannot allocate memory\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [22 x i8] c"cannot open %s -- %s\0A\00"
@errno = global i32 0
@.str.2 = [32 x i8] c"cannot read attribute %s -- %s\0A\00"
define i32 @read_attribute(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i32 1, i32* %8
%11 = call i32 (...) @getpagesize()
%12 = sext i32 %11 to i64
store i64 %12, i64* %10
%13 = load i8*, i8** @read_attribute.buf
%14 = icmp ne i8* %13, null
br i1 %14, label %23, label %15
15: ; preds = %3
%16 = load i64, i64* %10
%17 = call i8* @malloc(i64 %16)
store i8* %17, i8** @read_attribute.buf
%18 = load i8*, i8** @read_attribute.buf
%19 = icmp ne i8* %18, null
br i1 %19, label %22, label %20
20: ; preds = %15
%21 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %76
22: ; preds = %15
br label %23
23: ; preds = %22, %3
%24 = load i8*, i8** %4
%25 = load i32, i32* @O_RDONLY
%26 = call i32 @open(i8* %24, i32 %25)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %23
%30 = load i8*, i8** %4
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %30, i8* %32)
br label %76
34: ; preds = %23
%35 = load i8*, i8** @read_attribute.buf
%36 = load i64, i64* %10
%37 = call i32 @memset(i8* %35, i32 0, i64 %36)
%38 = load i32, i32* %7
%39 = load i8*, i8** @read_attribute.buf
%40 = load i64, i64* %10
%41 = call i64 @read(i32 %38, i8* %39, i64 %40)
store i64 %41, i64* %9
%42 = load i64, i64* %9
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %34
%45 = load i8*, i8** %4
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %45, i8* %47)
br label %73
49: ; preds = %34
%50 = load i8*, i8** @read_attribute.buf
%51 = call i32 @strlen(i8* %50)
%52 = icmp sgt i32 %51, 0
br i1 %52, label %53, label %60
53: ; preds = %49
%54 = load i8*, i8** @read_attribute.buf
%55 = load i8*, i8** @read_attribute.buf
%56 = call i32 @strlen(i8* %55)
%57 = sub nsw i32 %56, 1
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %54, i64 %58
store i8 0, i8* %59
br label %60
60: ; preds = %53, %49
%61 = load i8*, i8** %5
%62 = load i8*, i8** @read_attribute.buf
%63 = load i64, i64* %6
%64 = load i64, i64* %10
%65 = icmp ult i64 %63, %64
br i1 %65, label %66, label %68
66: ; preds = %60
%67 = load i64, i64* %6
br label %70
68: ; preds = %60
%69 = load i64, i64* %10
br label %70
70: ; preds = %68, %66
%71 = phi i64 [ %67, %66 ], [ %69, %68 ]
%72 = call i32 @strncpy(i8* %61, i8* %62, i64 %71)
store i32 0, i32* %8
br label %73
73: ; preds = %70, %44
%74 = load i32, i32* %7
%75 = call i32 @close(i32 %74)
br label %76
76: ; preds = %73, %29, %20
%77 = load i32, i32* %8
ret i32 %77
}

LLVM IR (-O2):
@read_attribute.buf = internal unnamed_addr global i8* null
@.str = [27 x i8] c"%s cannot allocate memory\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [22 x i8] c"cannot open %s -- %s\0A\00"
@errno = global i32 0
@.str.2 = [32 x i8] c"cannot read attribute %s -- %s\0A\00"
define i32 @read_attribute(i8* %0, i8* %1, i64 %2) {
%4 = tail call i32 (...) @getpagesize()
%5 = sext i32 %4 to i64
%6 = load i8*, i8** @read_attribute.buf
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %13
8: ; preds = %3
%9 = tail call i8* @malloc(i64 %5)
store i8* %9, i8** @read_attribute.buf
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %49
13: ; preds = %8, %3
%14 = load i32, i32* @O_RDONLY
%15 = tail call i32 @open(i8* %0, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %13
%18 = load i32, i32* @errno
%19 = tail call i8* @strerror(i32 %18)
%20 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %19)
br label %49
21: ; preds = %13
%22 = load i8*, i8** @read_attribute.buf
%23 = tail call i32 @memset(i8* %22, i32 0, i64 %5)
%24 = load i8*, i8** @read_attribute.buf
%25 = tail call i64 @read(i32 %15, i8* %24, i64 %5)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %21
%28 = load i32, i32* @errno
%29 = tail call i8* @strerror(i32 %28)
%30 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %29)
br label %46
31: ; preds = %21
%32 = load i8*, i8** @read_attribute.buf
%33 = tail call i32 @strlen(i8* %32)
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %31
%36 = load i8*, i8** @read_attribute.buf
%37 = tail call i32 @strlen(i8* %36)
%38 = add nsw i32 %37, -1
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %36, i64 %39
store i8 0, i8* %40
br label %41
41: ; preds = %35, %31
%42 = load i8*, i8** @read_attribute.buf
%43 = icmp ugt i64 %5, %2
%44 = select i1 %43, i64 %2, i64 %5
%45 = tail call i32 @strncpy(i8* %1, i8* %42, i64 %44)
br label %46
46: ; preds = %41, %27
%47 = phi i32 [ 1, %27 ], [ 0, %41 ]
%48 = tail call i32 @close(i32 %15)
br label %49
49: ; preds = %46, %17, %11
%50 = phi i32 [ 1, %17 ], [ %47, %46 ], [ 1, %11 ]
ret i32 %50
}