#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*/ err_out(char *);
int /*<<< orphan*/ err_sys(char *);
int /*<<< orphan*/ feof(int /*<<< orphan*/);
scalar_t__ ferror(int /*<<< orphan*/);
scalar_t__ fread(char *, int, int, int /*<<< orphan*/);
scalar_t__ realloc(char *, size_t);
int /*<<< orphan*/ stdin;
size_t get_message(char **msg) {
  char *buf;
  size_t buflen, msglen;
  buf = NULL;
  buflen = 0;
  msglen = 0;
  while (!feof(stdin)) {
    buflen += 1024;
    if ((buf = (char *)realloc(buf, buflen)) == NULL)
      err_sys("realloc");
    msglen += fread(&buf[msglen], 1, 1024, stdin);
    if (ferror(stdin))
      err_sys("fread");
  }
  if (msglen < 4)
    err_out("Input too short. Expecting at least 4 characters");
  if ((*msg = (char *)realloc(buf, msglen * 4)) == NULL)
    err_sys("realloc");
  return msglen;
}


x86 assembly (clang, -O0):
<get_message>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
call   1070 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11a7 <get_message+0x47>
jmp    122d <get_message+0xcd>
mov    -0x18(%rbp),%rax
add    $0x400,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11dc <get_message+0x7c>
lea    0xe29(%rip),%rdi
call   1050 <err_sys@plt>
mov    -0x10(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    0x2dfd(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
mov    $0x400,%edx
call   1040 <fread@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1228 <get_message+0xc8>
lea    0xde5(%rip),%rdi
call   1050 <err_sys@plt>
jmp    1184 <get_message+0x24>
cmpq   $0x4,-0x20(%rbp)
jae    1244 <get_message+0xe4>
lea    0xdcf(%rip),%rdi
call   1060 <err_out@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
shl    $0x2,%rsi
call   1080 <realloc@plt>
mov    -0x8(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    1272 <get_message+0x112>
lea    0xd93(%rip),%rdi
call   1050 <err_sys@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_message>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e73(%rip),%r13
mov    0x0(%r13),%edi
call   1070 <feof@plt>
xor    %r12d,%r12d
mov    $0x0,%r15d
test   %eax,%eax
jne    1222 <get_message+0xc2>
mov    %rbx,(%rsp)
mov    $0x400,%ebx
xor    %r12d,%r12d
lea    0xe66(%rip),%rbp
xor    %r15d,%r15d
jmp    11c7 <get_message+0x67>
nopw   0x0(%rax,%rax,1)
add    %r14,%r12
mov    0x0(%r13),%edi
call   1070 <feof@plt>
add    $0x400,%rbx
test   %eax,%eax
jne    1218 <get_message+0xb8>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1080 <realloc@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11e6 <get_message+0x86>
lea    0xe1f(%rip),%rdi
call   1050 <err_sys@plt>
lea    (%r15,%r12,1),%rdi
mov    0x0(%r13),%ecx
mov    $0x1,%esi
mov    $0x400,%edx
call   1040 <fread@plt>
mov    %rax,%r14
mov    0x0(%r13),%edi
call   1030 <ferror@plt>
test   %rax,%rax
je     11b0 <get_message+0x50>
mov    %rbp,%rdi
call   1050 <err_sys@plt>
jmp    11b0 <get_message+0x50>
cmp    $0x3,%r12
mov    (%rsp),%rbx
ja     122e <get_message+0xce>
lea    0xde5(%rip),%rdi
call   1060 <err_out@plt>
lea    0x0(,%r12,4),%rsi
mov    %r15,%rdi
call   1080 <realloc@plt>
mov    %rax,(%rbx)
test   %rax,%rax
jne    1252 <get_message+0xf2>
lea    0xdb3(%rip),%rdi
call   1050 <err_sys@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
@.str = [8 x i8] c"realloc\00"
@.str.1 = [6 x i8] c"fread\00"
@.str.2 = [49 x i8] c"Input too short. Expecting at least 4 characters\00"
define i64 @get_message(i8** %0) {
%2 = alloca i8**
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
store i8** %0, i8*** %2
store i8* null, i8** %3
store i64 0, i64* %4
store i64 0, i64* %5
br label %6
6: ; preds = %34, %1
%7 = load i32, i32* @stdin
%8 = call i32 @feof(i32 %7)
%9 = icmp ne i32 %8, 0
%10 = xor i1 %9, true
br i1 %10, label %11, label %35
11: ; preds = %6
%12 = load i64, i64* %4
%13 = add i64 %12, 1024
store i64 %13, i64* %4
%14 = load i8*, i8** %3
%15 = load i64, i64* %4
%16 = call i64 @realloc(i8* %14, i64 %15)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %3
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %11
%20 = call i32 @err_sys(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %11
%22 = load i8*, i8** %3
%23 = load i64, i64* %5
%24 = getelementptr inbounds i8, i8* %22, i64 %23
%25 = load i32, i32* @stdin
%26 = call i64 @fread(i8* %24, i32 1, i32 1024, i32 %25)
%27 = load i64, i64* %5
%28 = add i64 %27, %26
store i64 %28, i64* %5
%29 = load i32, i32* @stdin
%30 = call i64 @ferror(i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %21
%33 = call i32 @err_sys(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %32, %21
br label %6
35: ; preds = %6
%36 = load i64, i64* %5
%37 = icmp ult i64 %36, 4
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i32 @err_out(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %38, %35
%41 = load i8*, i8** %3
%42 = load i64, i64* %5
%43 = mul i64 %42, 4
%44 = call i64 @realloc(i8* %41, i64 %43)
%45 = inttoptr i64 %44 to i8*
%46 = load i8**, i8*** %2
store i8* %45, i8** %46
%47 = icmp eq i8* %45, null
br i1 %47, label %48, label %50
48: ; preds = %40
%49 = call i32 @err_sys(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %50
50: ; preds = %48, %40
%51 = load i64, i64* %5
ret i64 %51
}

LLVM IR (-O2):
@stdin = global i32 0
@.str = [8 x i8] c"realloc\00"
@.str.1 = [6 x i8] c"fread\00"
@.str.2 = [49 x i8] c"Input too short. Expecting at least 4 characters\00"
define i64 @get_message(i8** nocapture writeonly %0) {
%2 = load i32, i32* @stdin
%3 = tail call i32 @feof(i32 %2)
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %32
5: ; preds = %1, %25
%6 = phi i64 [ %19, %25 ], [ 0, %1 ]
%7 = phi i64 [ %9, %25 ], [ 0, %1 ]
%8 = phi i8* [ %11, %25 ], [ null, %1 ]
%9 = add i64 %7, 1024
%10 = tail call i64 @realloc(i8* %8, i64 %9)
%11 = inttoptr i64 %10 to i8*
%12 = icmp eq i64 %10, 0
br i1 %12, label %13, label %15
13: ; preds = %5
%14 = tail call i32 @err_sys(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %5
%16 = getelementptr inbounds i8, i8* %11, i64 %6
%17 = load i32, i32* @stdin
%18 = tail call i64 @fread(i8* %16, i32 1, i32 1024, i32 %17)
%19 = add i64 %18, %6
%20 = load i32, i32* @stdin
%21 = tail call i64 @ferror(i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %15
%24 = tail call i32 @err_sys(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %15
%26 = load i32, i32* @stdin
%27 = tail call i32 @feof(i32 %26)
%28 = icmp eq i32 %27, 0
br i1 %28, label %5, label %29
29: ; preds = %25
%30 = inttoptr i64 %10 to i8*
%31 = icmp ult i64 %19, 4
br i1 %31, label %32, label %36
32: ; preds = %1, %29
%33 = phi i64 [ %19, %29 ], [ 0, %1 ]
%34 = phi i8* [ %30, %29 ], [ null, %1 ]
%35 = tail call i32 @err_out(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %32, %29
%37 = phi i64 [ %33, %32 ], [ %19, %29 ]
%38 = phi i8* [ %34, %32 ], [ %30, %29 ]
%39 = shl i64 %37, 2
%40 = tail call i64 @realloc(i8* %38, i64 %39)
%41 = inttoptr i64 %40 to i8*
store i8* %41, i8** %0
%42 = icmp eq i64 %40, 0
br i1 %42, label %43, label %45
43: ; preds = %36
%44 = tail call i32 @err_sys(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %45
45: ; preds = %43, %36
ret i64 %37
}