#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;
scalar_t__ ec_malloc(int);
int /*<<< orphan*/ fatal(char *);
int /*<<< orphan*/ free(char *);
int getuid();
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ usage(char *, char *);
int write(int, ...);
int main(int argc, char *argv[]) {
  int userid, fd;
  char *buffer, *datafile;
  buffer = (char *)ec_malloc(100);
  datafile = (char *)ec_malloc(20);
  strcpy(datafile, "bin/notes");
  if (argc < 2)
    usage(argv[0], datafile);
  strcpy(buffer, argv[1]);
  printf("[DEBUG] buffer  @ %p: \'%s\'\n", buffer, buffer);
  printf("[DEBUG] buffer  @ %p: \'%s\'\n", datafile, datafile);
  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);
  userid = getuid();
  if (write(fd, &userid, 4) == -1)
    fatal("in main() while writing userid to file");
  write(fd, "\n", 1);
  if (write(fd, buffer, strlen(buffer)) == -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    0xe23(%rip),%rsi
call   1040 <strcpy@plt>
cmpl   $0x2,-0x8(%rbp)
jge    11fc <main+0x5c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
call   10a0 <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    0xdee(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x28(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    0x2d7c(%rip),%rax
mov    (%rax),%esi
mov    0x2dab(%rip),%rax
or     (%rax),%esi
mov    0x2d62(%rip),%rax
or     (%rax),%esi
mov    0x2d71(%rip),%rax
mov    (%rax),%edx
mov    0x2d78(%rip),%rax
or     (%rax),%edx
call   10b0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1288 <main+0xe8>
lea    0xda3(%rip),%rdi
call   10c0 <fatal@plt>
mov    -0x18(%rbp),%esi
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    $0x0,%al
call   1080 <getuid@plt>
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1060 <write@plt>
cmp    $0xffffffff,%eax
jne    12cb <main+0x12b>
lea    0xd9c(%rip),%rdi
call   10c0 <fatal@plt>
mov    -0x18(%rbp),%edi
lea    0xd4f(%rip),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1060 <write@plt>
mov    -0x18(%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
mov    $0x0,%al
call   1060 <write@plt>
cmp    $0xffffffff,%eax
jne    131d <main+0x17d>
lea    0xd71(%rip),%rdi
call   10c0 <fatal@plt>
lea    0xd82(%rip),%rdi
mov    $0x0,%al
call   1090 <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,%rbp
mov    %edi,%ebx
mov    $0x64,%edi
call   1060 <ec_malloc@plt>
mov    %rax,%r15
mov    $0x14,%edi
call   1060 <ec_malloc@plt>
mov    %rax,%r14
lea    0xe23(%rip),%rsi
mov    %rax,%rdi
call   1040 <strcpy@plt>
cmp    $0x1,%ebx
jg     11f6 <main+0x46>
mov    0x0(%rbp),%rdi
mov    %r14,%rsi
call   10b0 <usage@plt>
mov    0x8(%rbp),%rsi
mov    %r15,%rdi
call   1040 <strcpy@plt>
lea    0xe01(%rip),%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   10a0 <printf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   10a0 <printf@plt>
mov    0x2d90(%rip),%rax
mov    0x2dc1(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d76(%rip),%rax
or     (%rax),%esi
mov    0x2d85(%rip),%rax
mov    0x2d8e(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %r14,%rdi
call   10c0 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1271 <main+0xc1>
lea    0xdba(%rip),%rdi
call   10d0 <fatal@plt>
lea    0xdcb(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   10a0 <printf@plt>
xor    %eax,%eax
call   1090 <getuid@plt>
mov    %eax,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
xor    %eax,%eax
call   1070 <write@plt>
cmp    $0xffffffff,%eax
jne    12b0 <main+0x100>
lea    0xdb7(%rip),%rdi
call   10d0 <fatal@plt>
lea    0xd6d(%rip),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
xor    %eax,%eax
call   1070 <write@plt>
mov    %r15,%rdi
call   1080 <strlen@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <write@plt>
cmp    $0xffffffff,%eax
jne    12ec <main+0x13c>
lea    0xda2(%rip),%rdi
call   10d0 <fatal@plt>
lea    0xdb3(%rip),%rdi
call   1050 <puts@plt>
mov    %r15,%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 = [10 x i8] c"bin/notes\00"
@.str.1 = [28 x i8] c"[DEBUG] buffer @ %p: '%s'\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.2 = [29 x i8] c"in main() while opening file\00"
@.str.3 = [31 x i8] c"[DEBUG] file descriptor is %d\0A\00"
@.str.4 = [39 x i8] c"in main() while writing userid to file\00"
@.str.5 = [2 x i8] c"\0A\00"
@.str.6 = [29 x i8] c"in main() while closing file\00"
@.str.7 = [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 i32
%8 = alloca i8*
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i64 @ec_malloc(i32 100)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %8
%12 = call i64 @ec_malloc(i32 20)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %9
%14 = load i8*, i8** %9
%15 = call i32 @strcpy(i8* %14, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 2
br i1 %17, label %18, label %24
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = load i8*, i8** %9
%23 = call i32 @usage(i8* %21, i8* %22)
br label %24
24: ; preds = %18, %2
%25 = load i8*, i8** %8
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @strcpy(i8* %25, i8* %28)
%30 = load i8*, i8** %8
%31 = load i8*, i8** %8
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %30, i8* %31)
%33 = load i8*, i8** %9
%34 = load i8*, i8** %9
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %33, i8* %34)
%36 = load i8*, i8** %9
%37 = load i32, i32* @O_WRONLY
%38 = load i32, i32* @O_CREAT
%39 = or i32 %37, %38
%40 = load i32, i32* @O_APPEND
%41 = or i32 %39, %40
%42 = load i32, i32* @S_IRUSR
%43 = load i32, i32* @S_IWUSR
%44 = or i32 %42, %43
%45 = call i32 @open(i8* %36, i32 %41, i32 %44)
store i32 %45, i32* %7
%46 = load i32, i32* %7
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %24
%49 = call i32 @fatal(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %50
50: ; preds = %48, %24
%51 = load i32, i32* %7
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %51)
%53 = call i32 (...) @getuid()
store i32 %53, i32* %6
%54 = load i32, i32* %7
%55 = call i32 (i32, ...) @write(i32 %54, i32* %6, i32 4)
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %59
57: ; preds = %50
%58 = call i32 @fatal(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0))
br label %59
59: ; preds = %57, %50
%60 = load i32, i32* %7
%61 = call i32 (i32, ...) @write(i32 %60, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 1)
%62 = load i32, i32* %7
%63 = load i8*, i8** %8
%64 = load i8*, i8** %8
%65 = call i32 @strlen(i8* %64)
%66 = call i32 (i32, ...) @write(i32 %62, i8* %63, i32 %65)
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %70
68: ; preds = %59
%69 = call i32 @fatal(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
br label %70
70: ; preds = %68, %59
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0))
%72 = load i8*, i8** %8
%73 = call i32 @free(i8* %72)
%74 = load i8*, i8** %9
%75 = call i32 @free(i8* %74)
%76 = load i32, i32* %3
ret i32 %76
}

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