#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*/ free(char *);
size_t getline(char **, size_t *, int /*<<< orphan*/ *);
char *malloc(int);
char *realloc(char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcmp(char *, char *);
size_t strlen(char *);
char *getNextMessage(FILE *f) {
  char *msg = NULL;
  char *line = NULL;
  size_t line_len, length, msg_len;
  while ((length = getline(&line, &line_len, f)) != -1) {
    if (!strcmp(line, "-END OF MESSAGE-\n"))
      break;
    if (msg == NULL) {
      msg = malloc(sizeof(char) * 512);
      msg[0] = '\0';
      msg_len = 512;
    }
    while (strlen(line) + strlen(msg) >= msg_len) {
      msg = realloc(msg, sizeof(char) * (2 * msg_len));
      msg_len *= 2;
    }
    strcat(msg, line);
  }
  free(line);
  return msg;
}


x86 assembly (clang, -O0):
<getNextMessage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdx
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <getline@plt>
mov    %rax,-0x28(%rbp)
cmp    $0xffffffffffffffff,%rax
je     125e <getNextMessage+0xee>
mov    -0x18(%rbp),%rdi
lea    0xe4a(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    11c9 <getNextMessage+0x59>
jmp    125e <getNextMessage+0xee>
cmpq   $0x0,-0x10(%rbp)
jne    11f1 <getNextMessage+0x81>
mov    $0x200,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movq   $0x200,-0x30(%rbp)
jmp    11f6 <getNextMessage+0x86>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
add    %rcx,%rax
cmp    -0x30(%rbp),%rax
jb     124c <getNextMessage+0xdc>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rax
shl    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%esi
call   1070 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x30(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    11f6 <getNextMessage+0x86>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <strcat@plt>
jmp    118c <getNextMessage+0x1c>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getNextMessage>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    %r14,%rdx
call   1080 <getline@plt>
xor    %r15d,%r15d
cmp    $0xffffffffffffffff,%rax
je     1265 <getNextMessage+0xf5>
jmp    11d7 <getNextMessage+0x67>
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r12,%rsi
call   1090 <strcat@plt>
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    %r14,%rdx
call   1080 <getline@plt>
cmp    $0xffffffffffffffff,%rax
je     1265 <getNextMessage+0xf5>
mov    0x8(%rsp),%r12
mov    %r12,%rdi
lea    0xe1a(%rip),%rsi
call   1050 <strcmp@plt>
test   %eax,%eax
je     1265 <getNextMessage+0xf5>
test   %r15,%r15
jne    1209 <getNextMessage+0x99>
mov    $0x200,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
movb   $0x0,(%rax)
mov    $0x200,%ebp
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %rax,%r13
mov    %r15,%rdi
call   1040 <strlen@plt>
add    %r13,%rax
cmp    %rbp,%rax
jb     11b0 <getNextMessage+0x40>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    %rbp,%rbp
mov    %r15,%rdi
mov    %ebp,%esi
call   1070 <realloc@plt>
mov    %rax,%r15
mov    0x8(%rsp),%r12
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1040 <strlen@plt>
add    %rbx,%rax
cmp    %rbp,%rax
jae    1230 <getNextMessage+0xc0>
jmp    11b0 <getNextMessage+0x40>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %r15,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"-END OF MESSAGE-\0A\00"
define i8* @getNextMessage(i32* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
store i32* %0, i32** %2
store i8* null, i8** %3
store i8* null, i8** %4
br label %8
8: ; preds = %42, %1
%9 = load i32*, i32** %2
%10 = call i64 @getline(i8** %4, i64* %5, i32* %9)
store i64 %10, i64* %6
%11 = icmp ne i64 %10, -1
br i1 %11, label %12, label %46
12: ; preds = %8
%13 = load i8*, i8** %4
%14 = call i32 @strcmp(i8* %13, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%15 = icmp ne i32 %14, 0
br i1 %15, label %17, label %16
16: ; preds = %12
br label %46
17: ; preds = %12
%18 = load i8*, i8** %3
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = call i8* @malloc(i32 512)
store i8* %21, i8** %3
%22 = load i8*, i8** %3
%23 = getelementptr inbounds i8, i8* %22, i64 0
store i8 0, i8* %23
store i64 512, i64* %7
br label %24
24: ; preds = %20, %17
br label %25
25: ; preds = %33, %24
%26 = load i8*, i8** %4
%27 = call i64 @strlen(i8* %26)
%28 = load i8*, i8** %3
%29 = call i64 @strlen(i8* %28)
%30 = add i64 %27, %29
%31 = load i64, i64* %7
%32 = icmp uge i64 %30, %31
br i1 %32, label %33, label %42
33: ; preds = %25
%34 = load i8*, i8** %3
%35 = load i64, i64* %7
%36 = mul i64 2, %35
%37 = mul i64 1, %36
%38 = trunc i64 %37 to i32
%39 = call i8* @realloc(i8* %34, i32 %38)
store i8* %39, i8** %3
%40 = load i64, i64* %7
%41 = mul i64 %40, 2
store i64 %41, i64* %7
br label %25
42: ; preds = %25
%43 = load i8*, i8** %3
%44 = load i8*, i8** %4
%45 = call i32 @strcat(i8* %43, i8* %44)
br label %8
46: ; preds = %16, %8
%47 = load i8*, i8** %4
%48 = call i32 @free(i8* %47)
%49 = load i8*, i8** %3
ret i8* %49
}

LLVM IR (-O2):
@.str = [18 x i8] c"-END OF MESSAGE-\0A\00"
define i8* @getNextMessage(i32* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%5 = bitcast i64* %3 to i8*
%6 = call i64 @getline(i8** %2, i64* %3, i32* %0)
%7 = icmp eq i64 %6, -1
br i1 %7, label %43, label %8
8: ; preds = %1, %36
%9 = phi i64 [ %38, %36 ], [ undef, %1 ]
%10 = phi i8* [ %37, %36 ], [ null, %1 ]
%11 = load i8*, i8** %2
%12 = call i32 @strcmp(i8* dereferenceable(1) %11, i8* dereferenceable(18) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i32 %12, 0
br i1 %13, label %43, label %14
14: ; preds = %8
%15 = icmp eq i8* %10, null
br i1 %15, label %16, label %18
16: ; preds = %14
%17 = call dereferenceable_or_null(512) i8* @malloc(i32 512)
store i8 0, i8* %17
br label %18
18: ; preds = %16, %14
%19 = phi i8* [ %17, %16 ], [ %10, %14 ]
%20 = phi i64 [ 512, %16 ], [ %9, %14 ]
%21 = call i64 @strlen(i8* dereferenceable(1) %11)
%22 = call i64 @strlen(i8* dereferenceable(1) %19)
%23 = add i64 %22, %21
%24 = icmp ult i64 %23, %20
br i1 %24, label %36, label %25
25: ; preds = %18, %25
%26 = phi i64 [ %28, %25 ], [ %20, %18 ]
%27 = phi i8* [ %30, %25 ], [ %19, %18 ]
%28 = shl i64 %26, 1
%29 = trunc i64 %28 to i32
%30 = call i8* @realloc(i8* %27, i32 %29)
%31 = load i8*, i8** %2
%32 = call i64 @strlen(i8* dereferenceable(1) %31)
%33 = call i64 @strlen(i8* dereferenceable(1) %30)
%34 = add i64 %33, %32
%35 = icmp ult i64 %34, %28
br i1 %35, label %36, label %25
36: ; preds = %25, %18
%37 = phi i8* [ %19, %18 ], [ %30, %25 ]
%38 = phi i64 [ %20, %18 ], [ %28, %25 ]
%39 = phi i8* [ %11, %18 ], [ %31, %25 ]
%40 = call i32 @strcat(i8* %37, i8* %39)
%41 = call i64 @getline(i8** %2, i64* %3, i32* %0)
%42 = icmp eq i64 %41, -1
br i1 %42, label %43, label %8
43: ; preds = %36, %8, %1
%44 = phi i8* [ null, %1 ], [ %10, %8 ], [ %37, %36 ]
%45 = load i8*, i8** %2
%46 = call i32 @free(i8* %45)
ret i8* %44
}