#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*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ memcpy(char *, char const *, int);
scalar_t__ realloc(char *, int);
int /*<<< orphan*/ stderr;
int strlen(char const *);
char *append_to_line(char *buffer, const char *s1, int *len) {
  char *temp;
  int slen = 0, alen;
  if (s1 == NULL || s1[0] == 0) {
    fprintf(stderr, "\n#BUG: appending 0 to a line makes no sense!");
    return buffer;
  }
  if (*len > 0)
    slen = strlen(buffer);
  alen = strlen(s1);
  if (slen + alen + 1 >= *len) {
    *len += 512;
    temp = (char *)realloc(buffer, *len);
    if (!temp) {
      fprintf(stderr, "realloc failed!\n");
      *len -= 512;
      return buffer;
    } else
      buffer = temp;
  }
  temp = buffer + slen;
  memcpy(temp, s1, alen + 1);
  return buffer;
}


x86 assembly (clang, -O0):
<append_to_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1176 <append_to_line+0x36>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1198 <append_to_line+0x58>
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
lea    0xe7a(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1265 <append_to_line+0x125>
mov    -0x20(%rbp),%rax
cmpl   $0x0,(%rax)
jle    11b1 <append_to_line+0x71>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%eax
add    -0x30(%rbp),%eax
add    $0x1,%eax
mov    -0x20(%rbp),%rcx
cmp    (%rcx),%eax
jl     123b <append_to_line+0xfb>
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0x200,%ecx
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
call   1060 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    122e <append_to_line+0xee>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
sub    $0x200,%ecx
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1265 <append_to_line+0x125>
mov    -0x28(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    123b <append_to_line+0xfb>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%edx
add    $0x1,%edx
call   1050 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_to_line>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r14
test   %rsi,%rsi
je     116e <append_to_line+0x2e>
mov    %rsi,%rbx
cmpb   $0x0,(%rsi)
je     116e <append_to_line+0x2e>
mov    %rdx,%r15
cmpl   $0x0,(%rdx)
jle    1185 <append_to_line+0x45>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%r12d
jmp    1188 <append_to_line+0x48>
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
lea    0xe82(%rip),%rsi
call   1040 <fprintf@plt>
jmp    11ed <append_to_line+0xad>
xor    %r12d,%r12d
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%r13d
lea    (%r12,%r13,1),%eax
add    $0x1,%eax
mov    (%r15),%esi
cmp    %esi,%eax
jl     11ba <append_to_line+0x7a>
add    $0x200,%esi
mov    %esi,(%r15)
mov    %r14,%rdi
call   1060 <realloc@plt>
test   %rax,%rax
je     11d1 <append_to_line+0x91>
mov    %rax,%r14
add    $0x1,%r13d
movslq %r12d,%rdi
add    %r14,%rdi
mov    %rbx,%rsi
mov    %r13d,%edx
call   1050 <memcpy@plt>
jmp    11ed <append_to_line+0xad>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
lea    0xe4c(%rip),%rsi
call   1040 <fprintf@plt>
addl   $0xfffffe00,(%r15)
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [45 x i8] c"\0A#BUG: appending 0 to a line makes no sense!\00"
@.str.1 = [17 x i8] c"realloc failed!\0A\00"
define i8* @append_to_line(i8* %0, i8* %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
store i32 0, i32* %9
%11 = load i8*, i8** %6
%12 = icmp eq i8* %11, null
br i1 %12, label %19, label %13
13: ; preds = %3
%14 = load i8*, i8** %6
%15 = getelementptr inbounds i8, i8* %14, i64 0
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %13, %3
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%22 = load i8*, i8** %5
store i8* %22, i8** %4
br label %72
23: ; preds = %13
%24 = load i32*, i32** %7
%25 = load i32, i32* %24
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i8*, i8** %5
%29 = call i32 @strlen(i8* %28)
store i32 %29, i32* %9
br label %30
30: ; preds = %27, %23
%31 = load i8*, i8** %6
%32 = call i32 @strlen(i8* %31)
store i32 %32, i32* %10
%33 = load i32, i32* %9
%34 = load i32, i32* %10
%35 = add nsw i32 %33, %34
%36 = add nsw i32 %35, 1
%37 = load i32*, i32** %7
%38 = load i32, i32* %37
%39 = icmp sge i32 %36, %38
br i1 %39, label %40, label %61
40: ; preds = %30
%41 = load i32*, i32** %7
%42 = load i32, i32* %41
%43 = add nsw i32 %42, 512
store i32 %43, i32* %41
%44 = load i8*, i8** %5
%45 = load i32*, i32** %7
%46 = load i32, i32* %45
%47 = call i64 @realloc(i8* %44, i32 %46)
%48 = inttoptr i64 %47 to i8*
store i8* %48, i8** %8
%49 = load i8*, i8** %8
%50 = icmp ne i8* %49, null
br i1 %50, label %58, label %51
51: ; preds = %40
%52 = load i32, i32* @stderr
%53 = call i32 @fprintf(i32 %52, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%54 = load i32*, i32** %7
%55 = load i32, i32* %54
%56 = sub nsw i32 %55, 512
store i32 %56, i32* %54
%57 = load i8*, i8** %5
store i8* %57, i8** %4
br label %72
58: ; preds = %40
%59 = load i8*, i8** %8
store i8* %59, i8** %5
br label %60
60: ; preds = %58
br label %61
61: ; preds = %60, %30
%62 = load i8*, i8** %5
%63 = load i32, i32* %9
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8, i8* %62, i64 %64
store i8* %65, i8** %8
%66 = load i8*, i8** %8
%67 = load i8*, i8** %6
%68 = load i32, i32* %10
%69 = add nsw i32 %68, 1
%70 = call i32 @memcpy(i8* %66, i8* %67, i32 %69)
%71 = load i8*, i8** %5
store i8* %71, i8** %4
br label %72
72: ; preds = %61, %51, %19
%73 = load i8*, i8** %4
ret i8* %73
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [45 x i8] c"\0A#BUG: appending 0 to a line makes no sense!\00"
@.str.1 = [17 x i8] c"realloc failed!\0A\00"
define i8* @append_to_line(i8* %0, i8* %1, i32* nocapture %2) {
%4 = icmp eq i8* %1, null
br i1 %4, label %8, label %5
5: ; preds = %3
%6 = load i8, i8* %1
%7 = icmp eq i8 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %5, %3
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
br label %39
11: ; preds = %5
%12 = load i32, i32* %2
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = tail call i32 @strlen(i8* %0)
br label %16
16: ; preds = %14, %11
%17 = phi i32 [ %15, %14 ], [ 0, %11 ]
%18 = tail call i32 @strlen(i8* %1)
%19 = add i32 %18, 1
%20 = add i32 %19, %17
%21 = load i32, i32* %2
%22 = icmp slt i32 %20, %21
br i1 %22, label %34, label %23
23: ; preds = %16
%24 = add nsw i32 %21, 512
store i32 %24, i32* %2
%25 = tail call i64 @realloc(i8* %0, i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = tail call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* %2
%31 = add nsw i32 %30, -512
store i32 %31, i32* %2
br label %39
32: ; preds = %23
%33 = inttoptr i64 %25 to i8*
br label %34
34: ; preds = %32, %16
%35 = phi i8* [ %33, %32 ], [ %0, %16 ]
%36 = sext i32 %17 to i64
%37 = getelementptr inbounds i8, i8* %35, i64 %36
%38 = tail call i32 @memcpy(i8* %37, i8* %1, i32 %19)
br label %39
39: ; preds = %34, %27, %8
%40 = phi i8* [ %0, %8 ], [ %35, %34 ], [ %0, %27 ]
ret i8* %40
}