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

/* Variables and functions */
size_t UINT_MAX;
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ readBuffer(int, char *, size_t);
int /*<<< orphan*/ readCrlf(int);
int readLine(int, char *, int);
scalar_t__ realloc(char *, size_t);
int /*<<< orphan*/ sscanf(char *, char *, size_t *);
char *readChunks(int fd, size_t *len) {
  size_t total = 0;
  char *data = NULL;
  *len = 0;
  int i;
  for (;;) {
    char chunkline[4096];
    int n;
    size_t chunksize;
    n = readLine(fd, chunkline, sizeof(chunkline) - 1);
    if (n < 0) {
      if (data)
        free(data);
      return NULL;
    }
    if (n == 0)
      break;
    chunkline[n] = '\0';
    sscanf(chunkline, "%x", &chunksize);
    if (chunksize == 0)
      break;
    if (data == NULL) {
      data = (char *)malloc(chunksize);
    } else {
      if (UINT_MAX - chunksize < total)
        return NULL;
      data = (char *)realloc(data, chunksize + total);
    }
    if (data == NULL)
      return NULL;
    readBuffer(fd, data + total, chunksize);
    total += chunksize;
    readCrlf(fd);
  }
  *len = total;
  return data;
}


x86 assembly (clang, -O0):
<readChunks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0xc(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0xfff,%edx
call   1060 <readLine@plt>
mov    %eax,-0x1034(%rbp)
cmpl   $0x0,-0x1034(%rbp)
jge    11e5 <readChunks+0x75>
cmpq   $0x0,-0x28(%rbp)
je     11d8 <readChunks+0x68>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12f1 <readChunks+0x181>
cmpl   $0x0,-0x1034(%rbp)
jne    11f7 <readChunks+0x87>
jmp    12de <readChunks+0x16e>
movslq -0x1034(%rbp),%rax
movb   $0x0,-0x1030(%rbp,%rax,1)
lea    -0x1030(%rbp),%rdi
lea    0xdec(%rip),%rsi
lea    -0x1040(%rbp),%rdx
call   1080 <sscanf@plt>
cmpq   $0x0,-0x1040(%rbp)
jne    1233 <readChunks+0xc3>
jmp    12de <readChunks+0x16e>
cmpq   $0x0,-0x28(%rbp)
jne    1253 <readChunks+0xe3>
mov    -0x1040(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
jmp    1293 <readChunks+0x123>
mov    0x2d8e(%rip),%rax
mov    (%rax),%rax
sub    -0x1040(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    127b <readChunks+0x10b>
movq   $0x0,-0x8(%rbp)
jmp    12f1 <readChunks+0x181>
mov    -0x28(%rbp),%rdi
mov    -0x1040(%rbp),%rsi
add    -0x20(%rbp),%rsi
call   1090 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12ab <readChunks+0x13b>
movq   $0x0,-0x8(%rbp)
jmp    12f1 <readChunks+0x181>
mov    -0xc(%rbp),%edi
mov    -0x28(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x1040(%rbp),%rdx
call   1050 <readBuffer@plt>
mov    -0x1040(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <readCrlf@plt>
jmp    119d <readChunks+0x2d>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readChunks>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %edi,%ebp
mov    %rsi,0x8(%rsp)
movq   $0x0,(%rsi)
lea    0x10(%rsp),%rsi
mov    $0xfff,%edx
call   1060 <readLine@plt>
test   %eax,%eax
js     1259 <readChunks+0xe9>
xor    %r14d,%r14d
lea    0xe50(%rip),%r15
lea    0x10(%rsp),%r13
mov    %rsp,%r12
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
test   %eax,%eax
je     125d <readChunks+0xed>
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1080 <sscanf@plt>
mov    (%rsp),%rsi
test   %rsi,%rsi
je     125d <readChunks+0xed>
test   %rbx,%rbx
je     1210 <readChunks+0xa0>
mov    0x2df6(%rip),%rax
mov    (%rax),%rax
sub    %rsi,%rax
cmp    %r14,%rax
jb     1259 <readChunks+0xe9>
add    %r14,%rsi
mov    %rbx,%rdi
call   1090 <realloc@plt>
jmp    1218 <readChunks+0xa8>
nopw   0x0(%rax,%rax,1)
mov    %rsi,%rdi
call   1070 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1259 <readChunks+0xe9>
lea    (%rbx,%r14,1),%rsi
mov    (%rsp),%rdx
mov    %ebp,%edi
call   1050 <readBuffer@plt>
add    (%rsp),%r14
mov    %ebp,%edi
call   1040 <readCrlf@plt>
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0xfff,%edx
call   1060 <readLine@plt>
test   %eax,%eax
jns    11c0 <readChunks+0x50>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
jmp    1265 <readChunks+0xf5>
mov    0x8(%rsp),%rax
mov    %r14,(%rax)
mov    %rbx,%rax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%x\00"
@UINT_MAX = global i64 0
define i8* @readChunks(i32 %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
%9 = alloca [4096 x i8]
%10 = alloca i32
%11 = alloca i64
store i32 %0, i32* %4
store i64* %1, i64** %5
store i64 0, i64* %6
store i8* null, i8** %7
%12 = load i64*, i64** %5
store i64 0, i64* %12
br label %13
13: ; preds = %64, %2
%14 = load i32, i32* %4
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%16 = call i32 @readLine(i32 %14, i8* %15, i32 4095)
store i32 %16, i32* %10
%17 = load i32, i32* %10
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %13
%20 = load i8*, i8** %7
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i8*, i8** %7
%24 = call i32 @free(i8* %23)
br label %25
25: ; preds = %22, %19
store i8* null, i8** %3
br label %80
26: ; preds = %13
%27 = load i32, i32* %10
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %26
br label %76
30: ; preds = %26
%31 = load i32, i32* %10
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 %32
store i8 0, i8* %33
%34 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%35 = call i32 @sscanf(i8* %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i64* %11)
%36 = load i64, i64* %11
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %30
br label %76
39: ; preds = %30
%40 = load i8*, i8** %7
%41 = icmp eq i8* %40, null
br i1 %41, label %42, label %46
42: ; preds = %39
%43 = load i64, i64* %11
%44 = call i64 @malloc(i64 %43)
%45 = inttoptr i64 %44 to i8*
store i8* %45, i8** %7
br label %60
46: ; preds = %39
%47 = load i64, i64* @UINT_MAX
%48 = load i64, i64* %11
%49 = sub i64 %47, %48
%50 = load i64, i64* %6
%51 = icmp ult i64 %49, %50
br i1 %51, label %52, label %53
52: ; preds = %46
store i8* null, i8** %3
br label %80
53: ; preds = %46
%54 = load i8*, i8** %7
%55 = load i64, i64* %11
%56 = load i64, i64* %6
%57 = add i64 %55, %56
%58 = call i64 @realloc(i8* %54, i64 %57)
%59 = inttoptr i64 %58 to i8*
store i8* %59, i8** %7
br label %60
60: ; preds = %53, %42
%61 = load i8*, i8** %7
%62 = icmp eq i8* %61, null
br i1 %62, label %63, label %64
63: ; preds = %60
store i8* null, i8** %3
br label %80
64: ; preds = %60
%65 = load i32, i32* %4
%66 = load i8*, i8** %7
%67 = load i64, i64* %6
%68 = getelementptr inbounds i8, i8* %66, i64 %67
%69 = load i64, i64* %11
%70 = call i32 @readBuffer(i32 %65, i8* %68, i64 %69)
%71 = load i64, i64* %11
%72 = load i64, i64* %6
%73 = add i64 %72, %71
store i64 %73, i64* %6
%74 = load i32, i32* %4
%75 = call i32 @readCrlf(i32 %74)
br label %13
76: ; preds = %38, %29
%77 = load i64, i64* %6
%78 = load i64*, i64** %5
store i64 %77, i64* %78
%79 = load i8*, i8** %7
store i8* %79, i8** %3
br label %80
80: ; preds = %76, %63, %52, %25
%81 = load i8*, i8** %3
ret i8* %81
}

LLVM IR (-O2):
@.str = [3 x i8] c"%x\00"
@UINT_MAX = global i64 0
define i8* @readChunks(i32 %0, i64* nocapture writeonly %1) {
%3 = alloca [4096 x i8]
%4 = alloca i64
store i64 0, i64* %1
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%6 = bitcast i64* %4 to i8*
%7 = call i32 @readLine(i32 %0, i8* %5, i32 4095)
%8 = icmp slt i32 %7, 0
br i1 %8, label %38, label %12
9: ; preds = %39
%10 = inttoptr i64 %35 to i8*
%11 = call i32 @free(i8* %10)
br label %38
12: ; preds = %2, %39
%13 = phi i32 [ %46, %39 ], [ %7, %2 ]
%14 = phi i64 [ %44, %39 ], [ 0, %2 ]
%15 = phi i8* [ %36, %39 ], [ null, %2 ]
%16 = icmp eq i32 %13, 0
br i1 %16, label %48, label %17
17: ; preds = %12
%18 = zext i32 %13 to i64
%19 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %18
store i8 0, i8* %19
%20 = call i32 @sscanf(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i64* %4)
%21 = load i64, i64* %4
%22 = icmp eq i64 %21, 0
br i1 %22, label %48, label %23
23: ; preds = %17
%24 = icmp eq i8* %15, null
br i1 %24, label %25, label %27
25: ; preds = %23
%26 = call i64 @malloc(i64 %21)
br label %34
27: ; preds = %23
%28 = load i64, i64* @UINT_MAX
%29 = sub i64 %28, %21
%30 = icmp ult i64 %29, %14
br i1 %30, label %38, label %31
31: ; preds = %27
%32 = add i64 %21, %14
%33 = call i64 @realloc(i8* %15, i64 %32)
br label %34
34: ; preds = %31, %25
%35 = phi i64 [ %26, %25 ], [ %33, %31 ]
%36 = inttoptr i64 %35 to i8*
%37 = icmp eq i64 %35, 0
br i1 %37, label %38, label %39
38: ; preds = %27, %34, %2, %9
br label %49
39: ; preds = %34
%40 = getelementptr inbounds i8, i8* %36, i64 %14
%41 = load i64, i64* %4
%42 = call i32 @readBuffer(i32 %0, i8* %40, i64 %41)
%43 = load i64, i64* %4
%44 = add i64 %43, %14
%45 = call i32 @readCrlf(i32 %0)
%46 = call i32 @readLine(i32 %0, i8* %5, i32 4095)
%47 = icmp slt i32 %46, 0
br i1 %47, label %9, label %12
48: ; preds = %12, %17
store i64 %14, i64* %1
br label %49
49: ; preds = %38, %48
%50 = phi i8* [ %15, %48 ], [ null, %38 ]
ret i8* %50
}