#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int fread(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, int, int, int);
int /*<<< orphan*/ puts(char *);
char *realloc(char *, int);
int /*<<< orphan*/ stdout;
int main() {
  char *buf = malloc(10 + 1);
  int chunkcounter, totalcounter = 0, limit = 10;
  FILE *mypipe = popen("cat", "r");
  while (1) {
    chunkcounter = fread(&buf[totalcounter], 1, 10, mypipe);
    if (chunkcounter == -1) {
      perror("read error");
      exit(0);
    } else {
      totalcounter += chunkcounter;
      if (chunkcounter == 10) {
        limit += 10;
        char *newbuf = realloc(buf, limit + 1);
        if (newbuf == NULL) {
          puts("realloc break");
          break;
        } else
          buf = newbuf;
      } else {
        puts("counter break");
        break;
      }
    }
  }
  pclose(mypipe);
  buf[totalcounter] = '\0';
  printf("total length %d, allocated %d, last chunk %d\n", totalcounter, limit,
         chunkcounter);
  fputs(buf, stdout);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0xb,%edi
call   1080 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0xa,-0x1c(%rbp)
lea    0xe2e(%rip),%rdi
lea    0xe34(%rip),%rsi
call   10a0 <popen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0xa,%edx
call   1040 <fread@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1220 <main+0x80>
lea    0xdf0(%rip),%rdi
call   10b0 <perror@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0xa,-0x14(%rbp)
jne    1278 <main+0xd8>
mov    -0x1c(%rbp),%eax
add    $0xa,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
add    $0x1,%esi
call   1090 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    126b <main+0xcb>
lea    0xdae(%rip),%rdi
call   1030 <puts@plt>
jmp    1293 <main+0xf3>
mov    -0x30(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    1289 <main+0xe9>
lea    0xd9e(%rip),%rdi
call   1030 <puts@plt>
jmp    1293 <main+0xf3>
jmp    128e <main+0xee>
jmp    11e2 <main+0x42>
mov    -0x28(%rbp),%rdi
call   1060 <pclose@plt>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xd73(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2d18(%rip),%rax
mov    (%rax),%esi
call   1070 <fputs@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xb,%edi
call   1080 <malloc@plt>
mov    %rax,%rbp
lea    0xe41(%rip),%rdi
lea    0xe47(%rip),%rsi
call   10a0 <popen@plt>
mov    %rax,(%rsp)
xor    %r12d,%r12d
mov    $0xa,%ebx
nopw   0x0(%rax,%rax,1)
mov    %rbp,%r15
movslq %r12d,%rdi
add    %rbp,%rdi
mov    $0x1,%esi
mov    $0xa,%edx
mov    (%rsp),%rcx
call   1040 <fread@plt>
cmp    $0xffffffff,%eax
je     1288 <main+0xe8>
mov    %eax,%r13d
add    %eax,%r12d
cmp    $0xa,%eax
jne    1238 <main+0x98>
lea    0xa(%rbx),%r14d
add    $0xb,%ebx
mov    %r15,%rdi
mov    %ebx,%esi
call   1090 <realloc@plt>
mov    %rax,%rbp
mov    %r14d,%ebx
test   %rax,%rax
jne    11e0 <main+0x40>
lea    0xddc(%rip),%rdi
mov    %r14d,%ebx
jmp    123f <main+0x9f>
lea    0xdde(%rip),%rdi
call   1030 <puts@plt>
mov    (%rsp),%rdi
call   1060 <pclose@plt>
movslq %r12d,%rsi
movb   $0x0,(%r15,%rsi,1)
lea    0xdcf(%rip),%rdi
mov    %ebx,%edx
mov    %r13d,%ecx
call   1050 <printf@plt>
mov    0x2d73(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1070 <fputs@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd75(%rip),%rdi
call   10b0 <perror@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"cat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [11 x i8] c"read error\00"
@.str.3 = [14 x i8] c"realloc break\00"
@.str.4 = [14 x i8] c"counter break\00"
@.str.5 = [46 x i8] c"total length %d, allocated %d, last chunk %d\0A\00"
@stdout = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8*
store i32 0, i32* %1
%8 = call i8* @malloc(i32 11)
store i8* %8, i8** %2
store i32 0, i32* %4
store i32 10, i32* %5
%9 = call i32* @popen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %6
br label %10
10: ; preds = %0, %45
%11 = load i8*, i8** %2
%12 = load i32, i32* %4
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %11, i64 %13
%15 = load i32*, i32** %6
%16 = call i32 @fread(i8* %14, i32 1, i32 10, i32* %15)
store i32 %16, i32* %3
%17 = load i32, i32* %3
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %10
%20 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @exit(i32 0)
unreachable
22: ; preds = %10
%23 = load i32, i32* %3
%24 = load i32, i32* %4
%25 = add nsw i32 %24, %23
store i32 %25, i32* %4
%26 = load i32, i32* %3
%27 = icmp eq i32 %26, 10
br i1 %27, label %28, label %42
28: ; preds = %22
%29 = load i32, i32* %5
%30 = add nsw i32 %29, 10
store i32 %30, i32* %5
%31 = load i8*, i8** %2
%32 = load i32, i32* %5
%33 = add nsw i32 %32, 1
%34 = call i8* @realloc(i8* %31, i32 %33)
store i8* %34, i8** %7
%35 = load i8*, i8** %7
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %39
37: ; preds = %28
%38 = call i32 @puts(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %46
39: ; preds = %28
%40 = load i8*, i8** %7
store i8* %40, i8** %2
br label %41
41: ; preds = %39
br label %44
42: ; preds = %22
%43 = call i32 @puts(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %46
44: ; preds = %41
br label %45
45: ; preds = %44
br label %10
46: ; preds = %42, %37
%47 = load i32*, i32** %6
%48 = call i32 @pclose(i32* %47)
%49 = load i8*, i8** %2
%50 = load i32, i32* %4
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
store i8 0, i8* %52
%53 = load i32, i32* %4
%54 = load i32, i32* %5
%55 = load i32, i32* %3
%56 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.5, i64 0, i64 0), i32 %53, i32 %54, i32 %55)
%57 = load i8*, i8** %2
%58 = load i32, i32* @stdout
%59 = call i32 @fputs(i8* %57, i32 %58)
%60 = load i32, i32* %1
ret i32 %60
}

LLVM IR (-O2):
@.str = [4 x i8] c"cat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [11 x i8] c"read error\00"
@.str.3 = [14 x i8] c"realloc break\00"
@.str.4 = [14 x i8] c"counter break\00"
@.str.5 = [46 x i8] c"total length %d, allocated %d, last chunk %d\0A\00"
@stdout = global i32 0
define i32 @main() {
%1 = tail call dereferenceable_or_null(11) i8* @malloc(i32 11)
%2 = tail call i32* @popen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %3
3: ; preds = %17, %0
%4 = phi i32 [ 10, %0 ], [ %18, %17 ]
%5 = phi i32 [ 0, %0 ], [ %15, %17 ]
%6 = phi i8* [ %1, %0 ], [ %20, %17 ]
%7 = sext i32 %5 to i64
%8 = getelementptr inbounds i8, i8* %6, i64 %7
%9 = tail call i32 @fread(i8* %8, i32 1, i32 10, i32* %2)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%13 = tail call i32 @exit(i32 0)
unreachable
14: ; preds = %3
%15 = add nsw i32 %9, %5
%16 = icmp eq i32 %9, 10
br i1 %16, label %17, label %22
17: ; preds = %14
%18 = add nuw nsw i32 %4, 10
%19 = add nuw nsw i32 %4, 11
%20 = tail call i8* @realloc(i8* %6, i32 %19)
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %3
22: ; preds = %14, %17
%23 = phi i8* [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), %17 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), %14 ]
%24 = phi i32 [ %18, %17 ], [ %4, %14 ]
%25 = tail call i32 @puts(i8* dereferenceable(1) %23)
%26 = tail call i32 @pclose(i32* %2)
%27 = sext i32 %15 to i64
%28 = getelementptr inbounds i8, i8* %6, i64 %27
store i8 0, i8* %28
%29 = tail call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.5, i64 0, i64 0), i32 %15, i32 %24, i32 %9)
%30 = load i32, i32* @stdout
%31 = tail call i32 @fputs(i8* %6, i32 %30)
ret i32 0
}