#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__ chk_malloc(int);
int close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fe(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*/ strncat(char *, char *, int);
int write(int, ...);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("Appends string to file\n");
    printf("USAGE: %s [file] [string]\n", argv[0]);
    exit(0);
  }
  int fd, userid;
  char *buf, *file;
  buf = (char *)chk_malloc(256);
  file = (char *)chk_malloc(40);
  strcpy(file, "/var/notes");
  strcpy(buf, argv[1]);
  strncat(buf, "\n", 1);
  printf("BUF %p %s\n", buf, buf);
  printf("FILE %p %s\n", file, file);
  fd = open(file, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    fe("in main() on file load");
  }
  printf("fd is %d\n", fd);
  userid = getuid();
  printf("%d", userid);
  if (write(fd, &userid, 4) == -1) {
    fe("in main() on file write of user ID");
  } else {
    write(fd, "\n", 1);
  }
  if (write(fd, buf, strlen(buf)) == -1) {
    fe("in main() on file write of string");
  } else {
    write(fd, "\n", 1);
  }
  if (close(fd) == -1) {
    fe("in main() on file close");
  }
  free(file);
  free(buf);
  return (0);
}


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)
cmpl   $0x2,-0x8(%rbp)
jge    120a <main+0x4a>
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
mov    $0x100,%edi
call   10c0 <chk_malloc@plt>
mov    %rax,-0x20(%rbp)
mov    $0x28,%edi
call   10c0 <chk_malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe02(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
lea    0xddf(%rip),%rsi
mov    $0x1,%edx
call   1090 <strncat@plt>
mov    -0x20(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xdd3(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x28(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    0x2d2d(%rip),%rax
mov    (%rax),%esi
mov    0x2d5c(%rip),%rax
or     (%rax),%esi
mov    0x2d13(%rip),%rax
or     (%rax),%esi
mov    0x2d22(%rip),%rax
mov    (%rax),%edx
mov    0x2d29(%rip),%rax
or     (%rax),%edx
call   10d0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    12d7 <main+0x117>
lea    0xd83(%rip),%rdi
call   10b0 <fe@plt>
mov    -0x14(%rbp),%esi
lea    0xd8b(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    $0x0,%al
call   1070 <getuid@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xd7a(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1050 <write@plt>
cmp    $0xffffffff,%eax
jne    1330 <main+0x170>
lea    0xd53(%rip),%rdi
call   10b0 <fe@plt>
jmp    1346 <main+0x186>
mov    -0x14(%rbp),%edi
lea    0xcf7(%rip),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1050 <write@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
mov    $0x0,%al
call   1050 <write@plt>
cmp    $0xffffffff,%eax
jne    1387 <main+0x1c7>
lea    0xd1f(%rip),%rdi
call   10b0 <fe@plt>
jmp    139d <main+0x1dd>
mov    -0x14(%rbp),%edi
lea    0xca0(%rip),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1050 <write@plt>
mov    -0x14(%rbp),%edi
call   10a0 <close@plt>
cmp    $0xffffffff,%eax
jne    13ba <main+0x1fa>
lea    0xd09(%rip),%rdi
call   10b0 <fe@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbp
cmp    $0x1,%edi
jle    1374 <main+0x1a4>
mov    $0x100,%edi
call   10d0 <chk_malloc@plt>
mov    %rax,%rbx
mov    $0x28,%edi
call   10d0 <chk_malloc@plt>
mov    %rax,%r14
lea    0xe16(%rip),%rsi
mov    %rax,%rdi
call   1040 <strcpy@plt>
mov    0x8(%rbp),%rsi
mov    %rbx,%rdi
call   1040 <strcpy@plt>
lea    0xdf9(%rip),%rsi
mov    %rbx,%rdi
mov    $0x1,%edx
call   10a0 <strncat@plt>
lea    0xdf2(%rip),%rdi
mov    %rbx,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1090 <printf@plt>
lea    0xde9(%rip),%rdi
mov    %r14,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1090 <printf@plt>
mov    0x2d64(%rip),%rax
mov    0x2d95(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d4a(%rip),%rax
or     (%rax),%esi
mov    0x2d59(%rip),%rax
mov    0x2d62(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %r14,%rdi
call   10e0 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    129d <main+0xcd>
lea    0xda5(%rip),%rdi
call   10c0 <fe@plt>
lea    0xdb0(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1090 <printf@plt>
xor    %eax,%eax
call   1080 <getuid@plt>
mov    %eax,0xc(%rsp)
lea    0xd9f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1090 <printf@plt>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
xor    %eax,%eax
call   1060 <write@plt>
cmp    $0xffffffff,%eax
je     12f7 <main+0x127>
lea    0xd32(%rip),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
xor    %eax,%eax
call   1060 <write@plt>
jmp    1303 <main+0x133>
lea    0xd63(%rip),%rdi
call   10c0 <fe@plt>
mov    %rbx,%rdi
call   1070 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <write@plt>
cmp    $0xffffffff,%eax
je     1335 <main+0x165>
lea    0xcf4(%rip),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
xor    %eax,%eax
call   1060 <write@plt>
jmp    1341 <main+0x171>
lea    0xd48(%rip),%rdi
call   10c0 <fe@plt>
mov    %ebp,%edi
call   10b0 <close@plt>
cmp    $0xffffffff,%eax
jne    1359 <main+0x189>
lea    0xd52(%rip),%rdi
call   10c0 <fe@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd43(%rip),%rdi
call   1050 <puts@plt>
mov    0x0(%rbp),%rsi
lea    0xc75(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
xor    %edi,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"Appends string to file\0A\00"
@.str.1 = [27 x i8] c"USAGE: %s [file] [string]\0A\00"
@.str.2 = [11 x i8] c"/var/notes\00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [11 x i8] c"BUF %p %s\0A\00"
@.str.5 = [12 x i8] c"FILE %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.6 = [23 x i8] c"in main() on file load\00"
@.str.7 = [10 x i8] c"fd is %d\0A\00"
@.str.8 = [3 x i8] c"%d\00"
@.str.9 = [35 x i8] c"in main() on file write of user ID\00"
@.str.10 = [34 x i8] c"in main() on file write of string\00"
@.str.11 = [24 x i8] c"in main() on file close\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 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %19
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 0)
unreachable
19: ; preds = %2
%20 = call i64 @chk_malloc(i32 256)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %8
%22 = call i64 @chk_malloc(i32 40)
%23 = inttoptr i64 %22 to i8*
store i8* %23, i8** %9
%24 = load i8*, i8** %9
%25 = call i32 @strcpy(i8* %24, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%26 = load i8*, i8** %8
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32 @strcpy(i8* %26, i8* %29)
%31 = load i8*, i8** %8
%32 = call i32 @strncat(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%33 = load i8*, i8** %8
%34 = load i8*, i8** %8
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* %33, i8* %34)
%36 = load i8*, i8** %9
%37 = load i8*, i8** %9
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* %36, i8* %37)
%39 = load i8*, i8** %9
%40 = load i32, i32* @O_WRONLY
%41 = load i32, i32* @O_CREAT
%42 = or i32 %40, %41
%43 = load i32, i32* @O_APPEND
%44 = or i32 %42, %43
%45 = load i32, i32* @S_IRUSR
%46 = load i32, i32* @S_IWUSR
%47 = or i32 %45, %46
%48 = call i32 @open(i8* %39, i32 %44, i32 %47)
store i32 %48, i32* %6
%49 = load i32, i32* %6
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %19
%52 = call i32 @fe(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
br label %53
53: ; preds = %51, %19
%54 = load i32, i32* %6
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i32 %54)
%56 = call i32 (...) @getuid()
store i32 %56, i32* %7
%57 = load i32, i32* %7
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i32 %57)
%59 = load i32, i32* %6
%60 = call i32 (i32, ...) @write(i32 %59, i32* %7, i32 4)
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %64
62: ; preds = %53
%63 = call i32 @fe(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.9, i64 0, i64 0))
br label %67
64: ; preds = %53
%65 = load i32, i32* %6
%66 = call i32 (i32, ...) @write(i32 %65, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
br label %67
67: ; preds = %64, %62
%68 = load i32, i32* %6
%69 = load i8*, i8** %8
%70 = load i8*, i8** %8
%71 = call i32 @strlen(i8* %70)
%72 = call i32 (i32, ...) @write(i32 %68, i8* %69, i32 %71)
%73 = icmp eq i32 %72, -1
br i1 %73, label %74, label %76
74: ; preds = %67
%75 = call i32 @fe(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.10, i64 0, i64 0))
br label %79
76: ; preds = %67
%77 = load i32, i32* %6
%78 = call i32 (i32, ...) @write(i32 %77, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
br label %79
79: ; preds = %76, %74
%80 = load i32, i32* %6
%81 = call i32 @close(i32 %80)
%82 = icmp eq i32 %81, -1
br i1 %82, label %83, label %85
83: ; preds = %79
%84 = call i32 @fe(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.11, i64 0, i64 0))
br label %85
85: ; preds = %83, %79
%86 = load i8*, i8** %9
%87 = call i32 @free(i8* %86)
%88 = load i8*, i8** %8
%89 = call i32 @free(i8* %88)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [27 x i8] c"USAGE: %s [file] [string]\0A\00"
@.str.2 = [11 x i8] c"/var/notes\00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [11 x i8] c"BUF %p %s\0A\00"
@.str.5 = [12 x i8] c"FILE %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.6 = [23 x i8] c"in main() on file load\00"
@.str.7 = [10 x i8] c"fd is %d\0A\00"
@.str.8 = [3 x i8] c"%d\00"
@.str.9 = [35 x i8] c"in main() on file write of user ID\00"
@.str.10 = [34 x i8] c"in main() on file write of string\00"
@.str.11 = [24 x i8] c"in main() on file close\00"
@str = [23 x i8] c"Appends string to file\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %2
%11 = bitcast i32* %3 to i8*
%12 = tail call i64 @chk_malloc(i32 256)
%13 = inttoptr i64 %12 to i8*
%14 = tail call i64 @chk_malloc(i32 40)
%15 = inttoptr i64 %14 to i8*
%16 = tail call i32 @strcpy(i8* %15, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = load i8*, i8** %17
%19 = tail call i32 @strcpy(i8* %13, i8* %18)
%20 = tail call i32 @strncat(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* %13, i8* %13)
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* %15, i8* %15)
%23 = load i32, i32* @O_WRONLY
%24 = load i32, i32* @O_CREAT
%25 = or i32 %24, %23
%26 = load i32, i32* @O_APPEND
%27 = or i32 %25, %26
%28 = load i32, i32* @S_IRUSR
%29 = load i32, i32* @S_IWUSR
%30 = or i32 %29, %28
%31 = tail call i32 @open(i8* %15, i32 %27, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %10
%34 = tail call i32 @fe(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
br label %35
35: ; preds = %33, %10
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i32 %31)
%37 = tail call i32 (...) @getuid()
store i32 %37, i32* %3
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i32 %37)
%39 = call i32 (i32, ...) @write(i32 %31, i32* %3, i32 4)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %35
%42 = call i32 @fe(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.9, i64 0, i64 0))
br label %45
43: ; preds = %35
%44 = call i32 (i32, ...) @write(i32 %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
br label %45
45: ; preds = %43, %41
%46 = call i32 @strlen(i8* %13)
%47 = call i32 (i32, ...) @write(i32 %31, i8* %13, i32 %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = call i32 @fe(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.10, i64 0, i64 0))
br label %53
51: ; preds = %45
%52 = call i32 (i32, ...) @write(i32 %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
br label %53
53: ; preds = %51, %49
%54 = call i32 @close(i32 %31)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %58
56: ; preds = %53
%57 = call i32 @fe(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.11, i64 0, i64 0))
br label %58
58: ; preds = %56, %53
%59 = call i32 @free(i8* %15)
%60 = call i32 @free(i8* %13)
ret i32 0
}