#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 O_APPEND;
int O_CREAT;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int close(int);
scalar_t__ ec_malloc(int);
int /*<<< orphan*/ fatal(char *);
int /*<<< orphan*/ free(char *);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
int /*<<< orphan*/ usage(char *, char *);
int write(int, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fd;
  char *buffer, *datafile;
  buffer = (char *)ec_malloc(100);
  datafile = (char *)ec_malloc(20);
  strcpy(datafile, "/tmp/notes");
  if (argc < 2)
    usage(argv[0], datafile);
  strcpy(buffer, argv[1]);
  printf("[DEBUG] buffer @ %p: \'%s\'\n", buffer, buffer);
  printf("[DEBUG] datafile @ %p: \'%s\'\n", datafile, datafile);
  strncat(buffer, "\n", 1);
  fd = open(datafile, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
  if (fd == -1)
    fatal("in main() while opening file");
  printf("[DEBUG] file descriptor is %d\n", fd);
  if (write(fd, buffer, strlen(buffer)) == -1)
    fatal("in main() while writing buffer to file");
  if (close(fd) == -1)
    fatal("in main() while closing file");
  printf("Note has been saved.\n");
  free(buffer);
  free(datafile);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x64,%edi
call   1050 <ec_malloc@plt>
mov    %rax,-0x20(%rbp)
mov    $0x14,%edi
call   1050 <ec_malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe13(%rip),%rsi
call   1040 <strcpy@plt>
cmpl   $0x2,-0x8(%rbp)
jge    120c <main+0x5c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
call   10b0 <usage@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x28(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x20(%rbp),%rdi
lea    0xded(%rip),%rsi
mov    $0x1,%edx
call   1090 <strncat@plt>
mov    -0x28(%rbp),%rdi
mov    0x2d57(%rip),%rax
mov    (%rax),%esi
mov    0x2d86(%rip),%rax
or     (%rax),%esi
mov    0x2d3d(%rip),%rax
or     (%rax),%esi
mov    0x2d4c(%rip),%rax
mov    (%rax),%edx
mov    0x2d53(%rip),%rax
or     (%rax),%edx
call   10c0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    12ad <main+0xfd>
lea    0xd9b(%rip),%rdi
call   10d0 <fatal@plt>
mov    -0x14(%rbp),%esi
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1060 <write@plt>
cmp    $0xffffffff,%eax
jne    12f8 <main+0x148>
lea    0xd8c(%rip),%rdi
call   10d0 <fatal@plt>
mov    -0x14(%rbp),%edi
call   10a0 <close@plt>
cmp    $0xffffffff,%eax
jne    1315 <main+0x165>
lea    0xd96(%rip),%rdi
call   10d0 <fatal@plt>
lea    0xda7(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%ebp
mov    $0x64,%edi
call   1060 <ec_malloc@plt>
mov    %rax,%rbx
mov    $0x14,%edi
call   1060 <ec_malloc@plt>
mov    %rax,%r14
lea    0xe13(%rip),%rsi
mov    %rax,%rdi
call   1040 <strcpy@plt>
cmp    $0x1,%ebp
jg     1205 <main+0x45>
mov    (%r15),%rdi
mov    %r14,%rsi
call   10c0 <usage@plt>
mov    0x8(%r15),%rsi
mov    %rbx,%rdi
call   1040 <strcpy@plt>
lea    0xdf3(%rip),%rdi
mov    %rbx,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1090 <printf@plt>
lea    0xdfa(%rip),%rdi
mov    %r14,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1090 <printf@plt>
lea    0xe01(%rip),%rsi
mov    %rbx,%rdi
mov    $0x1,%edx
call   10a0 <strncat@plt>
mov    0x2d6c(%rip),%rax
mov    0x2d9d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d52(%rip),%rax
or     (%rax),%esi
mov    0x2d61(%rip),%rax
mov    0x2d6a(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %r14,%rdi
call   10d0 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1295 <main+0xd5>
lea    0xdb3(%rip),%rdi
call   10e0 <fatal@plt>
lea    0xdc4(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1090 <printf@plt>
mov    %rbx,%rdi
call   1080 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1070 <write@plt>
cmp    $0xffffffff,%eax
jne    12ca <main+0x10a>
lea    0xdba(%rip),%rdi
call   10e0 <fatal@plt>
mov    %ebp,%edi
call   10b0 <close@plt>
cmp    $0xffffffff,%eax
jne    12e2 <main+0x122>
lea    0xdc9(%rip),%rdi
call   10e0 <fatal@plt>
lea    0xdda(%rip),%rdi
call   1050 <puts@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/tmp/notes\00"
@.str.1 = [27 x i8] c"[DEBUG] buffer @ %p: '%s'\0A\00"
@.str.2 = [29 x i8] c"[DEBUG] datafile @ %p: '%s'\0A\00"
@.str.3 = [2 x i8] c"\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.4 = [29 x i8] c"in main() while opening file\00"
@.str.5 = [31 x i8] c"[DEBUG] file descriptor is %d\0A\00"
@.str.6 = [39 x i8] c"in main() while writing buffer to file\00"
@.str.7 = [29 x i8] c"in main() while closing file\00"
@.str.8 = [22 x i8] c"Note has been saved.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i64 @ec_malloc(i32 100)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %7
%11 = call i64 @ec_malloc(i32 20)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %8
%13 = load i8*, i8** %8
%14 = call i32 @strcpy(i8* %13, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 2
br i1 %16, label %17, label %23
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = load i8*, i8** %8
%22 = call i32 @usage(i8* %20, i8* %21)
br label %23
23: ; preds = %17, %2
%24 = load i8*, i8** %7
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32 @strcpy(i8* %24, i8* %27)
%29 = load i8*, i8** %7
%30 = load i8*, i8** %7
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %29, i8* %30)
%32 = load i8*, i8** %8
%33 = load i8*, i8** %8
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %32, i8* %33)
%35 = load i8*, i8** %7
%36 = call i32 @strncat(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%37 = load i8*, i8** %8
%38 = load i32, i32* @O_WRONLY
%39 = load i32, i32* @O_CREAT
%40 = or i32 %38, %39
%41 = load i32, i32* @O_APPEND
%42 = or i32 %40, %41
%43 = load i32, i32* @S_IRUSR
%44 = load i32, i32* @S_IWUSR
%45 = or i32 %43, %44
%46 = call i32 @open(i8* %37, i32 %42, i32 %45)
store i32 %46, i32* %6
%47 = load i32, i32* %6
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %51
49: ; preds = %23
%50 = call i32 @fatal(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
br label %51
51: ; preds = %49, %23
%52 = load i32, i32* %6
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %52)
%54 = load i32, i32* %6
%55 = load i8*, i8** %7
%56 = load i8*, i8** %7
%57 = call i32 @strlen(i8* %56)
%58 = call i32 @write(i32 %54, i8* %55, i32 %57)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %62
60: ; preds = %51
%61 = call i32 @fatal(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0))
br label %62
62: ; preds = %60, %51
%63 = load i32, i32* %6
%64 = call i32 @close(i32 %63)
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %68
66: ; preds = %62
%67 = call i32 @fatal(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0))
br label %68
68: ; preds = %66, %62
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.8, i64 0, i64 0))
%70 = load i8*, i8** %7
%71 = call i32 @free(i8* %70)
%72 = load i8*, i8** %8
%73 = call i32 @free(i8* %72)
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
@.str = [11 x i8] c"/tmp/notes\00"
@.str.1 = [27 x i8] c"[DEBUG] buffer @ %p: '%s'\0A\00"
@.str.2 = [29 x i8] c"[DEBUG] datafile @ %p: '%s'\0A\00"
@.str.3 = [2 x i8] c"\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.4 = [29 x i8] c"in main() while opening file\00"
@.str.5 = [31 x i8] c"[DEBUG] file descriptor is %d\0A\00"
@.str.6 = [39 x i8] c"in main() while writing buffer to file\00"
@.str.7 = [29 x i8] c"in main() while closing file\00"
@str = [21 x i8] c"Note has been saved.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i64 @ec_malloc(i32 100)
%4 = inttoptr i64 %3 to i8*
%5 = tail call i64 @ec_malloc(i32 20)
%6 = inttoptr i64 %5 to i8*
%7 = tail call i32 @strcpy(i8* %6, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%8 = icmp slt i32 %0, 2
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i8*, i8** %1
%11 = tail call i32 @usage(i8* %10, i8* %6)
br label %12
12: ; preds = %9, %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i32 @strcpy(i8* %4, i8* %14)
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %4, i8* %4)
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %6, i8* %6)
%18 = tail call i32 @strncat(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%19 = load i32, i32* @O_WRONLY
%20 = load i32, i32* @O_CREAT
%21 = or i32 %20, %19
%22 = load i32, i32* @O_APPEND
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IRUSR
%25 = load i32, i32* @S_IWUSR
%26 = or i32 %25, %24
%27 = tail call i32 @open(i8* %6, i32 %23, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %12
%30 = tail call i32 @fatal(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
br label %31
31: ; preds = %29, %12
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %27)
%33 = tail call i32 @strlen(i8* %4)
%34 = tail call i32 @write(i32 %27, i8* %4, i32 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = tail call i32 @fatal(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0))
br label %38
38: ; preds = %36, %31
%39 = tail call i32 @close(i32 %27)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = tail call i32 @fatal(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0))
br label %43
43: ; preds = %41, %38
%44 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%45 = tail call i32 @free(i8* %4)
%46 = tail call i32 @free(i8* %6)
ret i32 0
}