#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);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, size_t);
scalar_t__ realloc(char *, int);
char *strdup(char *);
char *proc_get_info(const char *path) {
  int fd = open(path, O_RDONLY);
  if (fd == -1) {
    perror("open");
    return strdup("error");
  }
  size_t offset = 0;
  size_t size = 0;
  char *info = NULL;
  while (1) {
    if (offset >= size) {
      size += 100;
      info = (char *)realloc(info, sizeof(char) * size);
      if (info == NULL) {
        perror("malloc");
        return strdup("error");
      }
    }
    int bytes = read(fd, &info[offset], size - offset);
    if (bytes < 0) {
      perror("read");
      break;
    } else if (bytes == 0)
      break;
    offset += bytes;
  }
  close(fd);
  info[offset - 1] = '\0';
  return info;
}


x86 assembly (clang, -O0):
<proc_get_info>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ac <proc_get_info+0x4c>
lea    0xe6e(%rip),%rdi
call   1070 <perror@plt>
lea    0xe67(%rip),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    12a1 <proc_get_info+0x141>
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jb     1226 <proc_get_info+0xc6>
mov    -0x28(%rbp),%rax
add    $0x64,%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%esi
call   1050 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1221 <proc_get_info+0xc1>
lea    0xe04(%rip),%rdi
call   1070 <perror@plt>
lea    0xdf2(%rip),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    12a1 <proc_get_info+0x141>
jmp    1226 <proc_get_info+0xc6>
mov    -0x14(%rbp),%edi
mov    -0x30(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    125c <proc_get_info+0xfc>
lea    0xdc0(%rip),%rdi
call   1070 <perror@plt>
jmp    1281 <proc_get_info+0x121>
cmpl   $0x0,-0x34(%rbp)
jne    126b <proc_get_info+0x10b>
jmp    1281 <proc_get_info+0x121>
jmp    1270 <proc_get_info+0x110>
movslq -0x34(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    11c4 <proc_get_info+0x64>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<proc_get_info>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11e9 <proc_get_info+0x89>
mov    %eax,%r14d
xor    %ecx,%ecx
xor    %ebp,%ebp
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rcx,%rbx
cmp    %rbp,%rcx
jb     11ae <proc_get_info+0x4e>
add    $0x64,%rbp
mov    %r15,%rdi
mov    %ebp,%esi
call   1050 <realloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     1200 <proc_get_info+0xa0>
lea    (%r15,%rbx,1),%rsi
mov    %rbp,%rdx
sub    %rbx,%rdx
mov    %r14d,%edi
call   1040 <read@plt>
test   %eax,%eax
js     11f2 <proc_get_info+0x92>
mov    %eax,%ecx
add    %rbx,%rcx
test   %eax,%eax
jne    1190 <proc_get_info+0x30>
mov    %r14d,%edi
call   1030 <close@plt>
movb   $0x0,-0x1(%rbx,%r15,1)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe10(%rip),%rdi
jmp    1207 <proc_get_info+0xa7>
lea    0xe19(%rip),%rdi
call   1070 <perror@plt>
jmp    11cd <proc_get_info+0x6d>
lea    0xe04(%rip),%rdi
call   1070 <perror@plt>
lea    0xdf2(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <strdup@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@.str.1 = [6 x i8] c"error\00"
@.str.2 = [7 x i8] c"malloc\00"
@.str.3 = [5 x i8] c"read\00"
define i8* @proc_get_info(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%16 = call i8* @strdup(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i8* %16, i8** %2
br label %68
17: ; preds = %1
store i64 0, i64* %5
store i64 0, i64* %6
store i8* null, i8** %7
br label %18
18: ; preds = %17, %55
%19 = load i64, i64* %5
%20 = load i64, i64* %6
%21 = icmp uge i64 %19, %20
br i1 %21, label %22, label %37
22: ; preds = %18
%23 = load i64, i64* %6
%24 = add i64 %23, 100
store i64 %24, i64* %6
%25 = load i8*, i8** %7
%26 = load i64, i64* %6
%27 = mul i64 1, %26
%28 = trunc i64 %27 to i32
%29 = call i64 @realloc(i8* %25, i32 %28)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %7
%31 = load i8*, i8** %7
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %36
33: ; preds = %22
%34 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%35 = call i8* @strdup(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i8* %35, i8** %2
br label %68
36: ; preds = %22
br label %37
37: ; preds = %36, %18
%38 = load i32, i32* %4
%39 = load i8*, i8** %7
%40 = load i64, i64* %5
%41 = getelementptr inbounds i8, i8* %39, i64 %40
%42 = load i64, i64* %6
%43 = load i64, i64* %5
%44 = sub i64 %42, %43
%45 = call i32 @read(i32 %38, i8* %41, i64 %44)
store i32 %45, i32* %8
%46 = load i32, i32* %8
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %37
%49 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %60
50: ; preds = %37
%51 = load i32, i32* %8
%52 = icmp eq i32 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %50
br label %60
54: ; preds = %50
br label %55
55: ; preds = %54
%56 = load i32, i32* %8
%57 = sext i32 %56 to i64
%58 = load i64, i64* %5
%59 = add i64 %58, %57
store i64 %59, i64* %5
br label %18
60: ; preds = %53, %48
%61 = load i32, i32* %4
%62 = call i32 @close(i32 %61)
%63 = load i8*, i8** %7
%64 = load i64, i64* %5
%65 = sub i64 %64, 1
%66 = getelementptr inbounds i8, i8* %63, i64 %65
store i8 0, i8* %66
%67 = load i8*, i8** %7
store i8* %67, i8** %2
br label %68
68: ; preds = %60, %33, %14
%69 = load i8*, i8** %2
ret i8* %69
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@.str.1 = [6 x i8] c"error\00"
@.str.2 = [7 x i8] c"malloc\00"
@.str.3 = [5 x i8] c"read\00"
define i8* @proc_get_info(i8* nocapture %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%7 = tail call dereferenceable_or_null(6) i8* @strdup(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %39
8: ; preds = %1, %31
%9 = phi i64 [ %34, %31 ], [ 0, %1 ]
%10 = phi i64 [ %23, %31 ], [ 0, %1 ]
%11 = phi i8* [ %24, %31 ], [ null, %1 ]
%12 = icmp ult i64 %9, %10
br i1 %12, label %22, label %13
13: ; preds = %8
%14 = add i64 %10, 100
%15 = trunc i64 %14 to i32
%16 = tail call i64 @realloc(i8* %11, i32 %15)
%17 = inttoptr i64 %16 to i8*
%18 = icmp eq i64 %16, 0
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%21 = tail call dereferenceable_or_null(6) i8* @strdup(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %39
22: ; preds = %13, %8
%23 = phi i64 [ %14, %13 ], [ %10, %8 ]
%24 = phi i8* [ %17, %13 ], [ %11, %8 ]
%25 = getelementptr inbounds i8, i8* %24, i64 %9
%26 = sub i64 %23, %9
%27 = tail call i32 @read(i32 %3, i8* %25, i64 %26)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %22
%30 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %35
31: ; preds = %22
%32 = icmp eq i32 %27, 0
%33 = zext i32 %27 to i64
%34 = add i64 %9, %33
br i1 %32, label %35, label %8
35: ; preds = %31, %29
%36 = tail call i32 @close(i32 %3)
%37 = add i64 %9, -1
%38 = getelementptr inbounds i8, i8* %24, i64 %37
store i8 0, i8* %38
br label %39
39: ; preds = %19, %35, %5
%40 = phi i8* [ %7, %5 ], [ %21, %19 ], [ %24, %35 ]
ret i8* %40
}