#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*/ title;
typedef int /*<<< orphan*/ msg;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ get_filename(char *, int /*<<< orphan*/ *);
scalar_t__ isspace(int /*<<< orphan*/);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int strlen(char *);
char *timestamp(char *);
void append_note(char *file, char *s) {
  FILE *fp;
  int i, n = 0;
  char title[100], msg[100];
  char *tp = title, *lp = s;
  get_filename(file, NULL);
  fp = fopen(file, "a+");
  if (!fp)
    die("Couldn't open file.");
  fprintf(fp, "\n\n%s \n", timestamp("%T"));
  fprintf(fp, "%s", s);
  for (i = strlen(s); n < 3 && i--; *tp++ = *lp++)
    if (isspace((*s++)))
      n++;
  tp[(n == 3) ? -1 : 0] = '\0';
  if (strlen(title) > sizeof(title))
    puts("Title bonked, but we recorded your note!");
  else {
    snprintf(msg, sizeof(msg), "> Note \"%s ...\" written to file %s", title,
             file);
    puts(msg);
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<append_note>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
lea    -0x90(%rbp),%rax
mov    %rax,-0x108(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x110(%rbp)
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <get_filename@plt>
mov    -0x8(%rbp),%rdi
lea    0xe15(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    120b <append_note+0x6b>
lea    0xdfd(%rip),%rdi
call   10c0 <die@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x118(%rbp)
lea    0xe01(%rip),%rdi
call   1070 <timestamp@plt>
mov    -0x118(%rbp),%rdi
mov    %rax,%rdx
lea    0xde4(%rip),%rsi
call   10a0 <fprintf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe23(%rip),%rsi
call   10a0 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0x3,-0x20(%rbp)
mov    %al,-0x119(%rbp)
jge    1281 <append_note+0xe1>
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x1c(%rbp)
cmp    $0x0,%eax
setne  %al
mov    %al,-0x119(%rbp)
mov    -0x119(%rbp),%al
test   $0x1,%al
jne    1294 <append_note+0xf4>
jmp    12f6 <append_note+0x156>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movsbl (%rax),%edi
call   1090 <isspace@plt>
cmp    $0x0,%rax
je     12be <append_note+0x11e>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    12c3 <append_note+0x123>
mov    -0x110(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x110(%rbp)
mov    (%rax),%cl
mov    -0x108(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x108(%rbp)
mov    %cl,(%rax)
jmp    1258 <append_note+0xb8>
mov    -0x108(%rbp),%rax
mov    -0x20(%rbp),%esi
xor    %ecx,%ecx
mov    $0xffffffff,%edx
cmp    $0x3,%esi
cmove  %edx,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
lea    -0x90(%rbp),%rdi
call   1060 <strlen@plt>
cltq
cmp    $0x64,%rax
jbe    133d <append_note+0x19d>
lea    0xcee(%rip),%rdi
call   1040 <puts@plt>
jmp    136c <append_note+0x1cc>
lea    -0x100(%rbp),%rdi
lea    -0x90(%rbp),%rcx
mov    -0x8(%rbp),%r8
mov    $0x64,%esi
lea    0xcef(%rip),%rdx
call   1080 <snprintf@plt>
lea    -0x100(%rbp),%rdi
call   1040 <puts@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_note>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xd8,%rsp
mov    %rsi,%r13
mov    %rdi,%r14
xor    %esi,%esi
call   1030 <get_filename@plt>
lea    0xe2b(%rip),%rsi
mov    %r14,%rdi
call   10c0 <fopen@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11f1 <append_note+0x41>
lea    0xe17(%rip),%rdi
call   10d0 <die@plt>
lea    0xe26(%rip),%rdi
call   1070 <timestamp@plt>
lea    0xe13(%rip),%rsi
mov    %r15,%rdi
mov    %rax,%rdx
call   10b0 <fprintf@plt>
mov    %r13,%rdi
mov    %r15,%rsi
call   1090 <fputs@plt>
mov    %r13,%rdi
call   1060 <strlen@plt>
xor    %ebx,%ebx
test   %eax,%eax
je     127c <append_note+0xcc>
mov    %eax,%r12d
add    $0xffffffff,%r12d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsbl 0x0(%r13,%rbp,1),%edi
call   10a0 <isspace@plt>
movzbl 0x0(%r13,%rbp,1),%ecx
mov    %cl,(%rsp,%rbp,1)
cmp    $0x1,%rax
sbb    $0xffffffff,%ebx
lea    0x1(%rbp),%rax
cmp    $0x2,%ebx
ja     126c <append_note+0xbc>
cmp    %ebp,%r12d
mov    %rax,%rbp
jne    1240 <append_note+0x90>
xor    %ecx,%ecx
cmp    $0x3,%ebx
sete   %cl
neg    %rcx
add    %rsp,%rax
jmp    1281 <append_note+0xd1>
mov    %rsp,%rax
xor    %ecx,%ecx
movb   $0x0,(%rax,%rcx,1)
mov    %rsp,%rdi
call   1060 <strlen@plt>
cmp    $0x65,%eax
jb     129b <append_note+0xeb>
lea    0xd88(%rip),%rdi
jmp    12bd <append_note+0x10d>
lea    0xda8(%rip),%rdx
lea    0x70(%rsp),%rbx
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    $0x64,%esi
mov    %r14,%r8
call   1080 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
add    $0xd8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"a+\00"
@.str.1 = [20 x i8] c"Couldn't open file.\00"
@.str.2 = [7 x i8] c"\0A\0A%s \0A\00"
@.str.3 = [3 x i8] c"%T\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [41 x i8] c"Title bonked, but we recorded your note!\00"
@.str.6 = [35 x i8] c"> Note \22%s ...\22 written to file %s\00"
define void @append_note(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [100 x i8]
%9 = alloca [100 x i8]
%10 = alloca i8*
%11 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %7
%12 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
store i8* %12, i8** %10
%13 = load i8*, i8** %4
store i8* %13, i8** %11
%14 = load i8*, i8** %3
%15 = call i32 @get_filename(i8* %14, i32* null)
%16 = load i8*, i8** %3
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %17, i32** %5
%18 = load i32*, i32** %5
%19 = icmp ne i32* %18, null
br i1 %19, label %22, label %20
20: ; preds = %2
%21 = call i32 @die(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %2
%23 = load i32*, i32** %5
%24 = call i8* @timestamp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%25 = call i32 @fprintf(i32* %23, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %24)
%26 = load i32*, i32** %5
%27 = load i8*, i8** %4
%28 = call i32 @fprintf(i32* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %27)
%29 = load i8*, i8** %4
%30 = call i32 @strlen(i8* %29)
store i32 %30, i32* %6
br label %31
31: ; preds = %51, %22
%32 = load i32, i32* %7
%33 = icmp slt i32 %32, 3
br i1 %33, label %34, label %38
34: ; preds = %31
%35 = load i32, i32* %6
%36 = add nsw i32 %35, -1
store i32 %36, i32* %6
%37 = icmp ne i32 %35, 0
br label %38
38: ; preds = %34, %31
%39 = phi i1 [ false, %31 ], [ %37, %34 ]
br i1 %39, label %40, label %57
40: ; preds = %38
%41 = load i8*, i8** %4
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %4
%43 = load i8, i8* %41
%44 = sext i8 %43 to i32
%45 = call i64 @isspace(i32 %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %40
%48 = load i32, i32* %7
%49 = add nsw i32 %48, 1
store i32 %49, i32* %7
br label %50
50: ; preds = %47, %40
br label %51
51: ; preds = %50
%52 = load i8*, i8** %11
%53 = getelementptr inbounds i8, i8* %52, i32 1
store i8* %53, i8** %11
%54 = load i8, i8* %52
%55 = load i8*, i8** %10
%56 = getelementptr inbounds i8, i8* %55, i32 1
store i8* %56, i8** %10
store i8 %54, i8* %55
br label %31
57: ; preds = %38
%58 = load i8*, i8** %10
%59 = load i32, i32* %7
%60 = icmp eq i32 %59, 3
%61 = zext i1 %60 to i64
%62 = select i1 %60, i32 -1, i32 0
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %58, i64 %63
store i8 0, i8* %64
%65 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%66 = call i32 @strlen(i8* %65)
%67 = sext i32 %66 to i64
%68 = icmp ugt i64 %67, 100
br i1 %68, label %69, label %71
69: ; preds = %57
%70 = call i32 @puts(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0))
br label %78
71: ; preds = %57
%72 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%73 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%74 = load i8*, i8** %3
%75 = call i32 @snprintf(i8* %72, i32 100, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i8* %73, i8* %74)
%76 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%77 = call i32 @puts(i8* %76)
br label %78
78: ; preds = %71, %69
%79 = load i32*, i32** %5
%80 = call i32 @fclose(i32* %79)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"a+\00"
@.str.1 = [20 x i8] c"Couldn't open file.\00"
@.str.2 = [7 x i8] c"\0A\0A%s \0A\00"
@.str.3 = [3 x i8] c"%T\00"
@.str.5 = [41 x i8] c"Title bonked, but we recorded your note!\00"
@.str.6 = [35 x i8] c"> Note \22%s ...\22 written to file %s\00"
define void @append_note(i8* %0, i8* %1) {
%3 = alloca [100 x i8]
%4 = alloca [100 x i8]
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%7 = tail call i32 @get_filename(i8* %0, i32* null)
%8 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @die(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %2
%13 = tail call i8* @timestamp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%14 = tail call i32 @fprintf(i32* %8, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %13)
%15 = tail call i32 @fputs(i8* %1, i32* %8)
%16 = tail call i32 @strlen(i8* %1)
%17 = icmp eq i32 %16, 0
br i1 %17, label %39, label %18
18: ; preds = %12, %18
%19 = phi i8* [ %24, %18 ], [ %1, %12 ]
%20 = phi i8* [ %30, %18 ], [ %5, %12 ]
%21 = phi i32 [ %32, %18 ], [ 0, %12 ]
%22 = phi i32 [ %23, %18 ], [ %16, %12 ]
%23 = add nsw i32 %22, -1
%24 = getelementptr inbounds i8, i8* %19, i64 1
%25 = load i8, i8* %19
%26 = sext i8 %25 to i32
%27 = tail call i64 @isspace(i32 %26)
%28 = icmp ne i64 %27, 0
%29 = load i8, i8* %19
%30 = getelementptr inbounds i8, i8* %20, i64 1
store i8 %29, i8* %20
%31 = zext i1 %28 to i32
%32 = add nuw nsw i32 %21, %31
%33 = icmp ugt i32 %32, 2
%34 = icmp eq i32 %23, 0
%35 = select i1 %33, i1 true, i1 %34
br i1 %35, label %36, label %18
36: ; preds = %18
%37 = icmp eq i32 %32, 3
%38 = sext i1 %37 to i64
br label %39
39: ; preds = %36, %12
%40 = phi i64 [ 0, %12 ], [ %38, %36 ]
%41 = phi i8* [ %5, %12 ], [ %30, %36 ]
%42 = getelementptr inbounds i8, i8* %41, i64 %40
store i8 0, i8* %42
%43 = call i32 @strlen(i8* %5)
%44 = icmp ugt i32 %43, 100
br i1 %44, label %45, label %47
45: ; preds = %39
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0))
br label %50
47: ; preds = %39
%48 = call i32 @snprintf(i8* %6, i32 100, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i8* %5, i8* %0)
%49 = call i32 @puts(i8* %6)
br label %50
50: ; preds = %47, %45
%51 = call i32 @fclose(i32* %8)
ret void
}