#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int O_APPEND;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ SEEK_SET;
int S_IRGRP;
int S_IRUSR;
int S_IWGRP;
int S_IWUSR;
int close(int);
int /*<<< orphan*/ exit(int);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
size_t strlen(char *);
int /*<<< orphan*/ usage();
size_t write(int, char *, size_t);
int main(int argc, char *argv[]) {
  char output[] = "Appended text\n";
  size_t output_len = strlen(output);
  char *filename;
  int fd = -1;
  if (argc != 2) {
    usage();
    exit(EXIT_FAILURE);
  }
  filename = argv[1];
  if ((fd = open(filename, O_WRONLY | O_CREAT | O_APPEND,
                 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP))) == -1) {
    perror("Opening file");
    goto fail;
  }
  if (lseek(fd, 0, SEEK_SET) == -1) {
    perror("Seeking to start of file");
    goto fail;
  }
  if (write(fd, output, output_len) != output_len) {
    perror("Appending output");
    goto fail;
  }
  if (close(fd) == -1) {
    perror("Closing file");
    goto fail;
  }
  return EXIT_SUCCESS;
fail:
  if (fd != -1) {
    close(fd);
  }
  return EXIT_FAILURE;
}


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    0xe63(%rip),%rax
mov    %rax,-0x1f(%rbp)
mov    0xe61(%rip),%eax
mov    %eax,-0x17(%rbp)
mov    0xe5b(%rip),%ax
mov    %ax,-0x13(%rbp)
mov    0xe53(%rip),%al
mov    %al,-0x11(%rbp)
lea    -0x1f(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
movl   $0xffffffff,-0x34(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11f1 <main+0x71>
mov    $0x0,%al
call   1070 <usage@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
mov    0x2de7(%rip),%rax
or     (%rax),%esi
mov    0x2d76(%rip),%rax
or     (%rax),%esi
mov    0x2d8d(%rip),%rax
mov    (%rax),%edx
mov    0x2dac(%rip),%rax
or     (%rax),%edx
mov    0x2d8b(%rip),%rax
or     (%rax),%edx
mov    0x2da2(%rip),%rax
or     (%rax),%edx
call   1080 <open@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    1262 <main+0xe2>
lea    0xdb7(%rip),%rdi
call   1090 <perror@plt>
jmp    12ed <main+0x16d>
mov    -0x34(%rbp),%edi
mov    0x2d5c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
jne    128f <main+0x10f>
lea    0xd97(%rip),%rdi
call   1090 <perror@plt>
jmp    12ed <main+0x16d>
mov    -0x34(%rbp),%edi
lea    -0x1f(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <write@plt>
cmp    -0x28(%rbp),%rax
je     12ba <main+0x13a>
lea    0xd85(%rip),%rdi
call   1090 <perror@plt>
jmp    12ed <main+0x16d>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    12dc <main+0x15c>
lea    0xd74(%rip),%rdi
call   1090 <perror@plt>
jmp    12ed <main+0x16d>
mov    0x2cc5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    130b <main+0x18b>
cmpl   $0xffffffff,-0x34(%rbp)
je     12ff <main+0x17f>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
mov    0x2cb2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbp
mov    %edi,%ebx
movabs $0xa747865742064,%rax
mov    %rax,0x7(%rsp)
movabs $0x6465646e65707041,%rax
64 65 64
mov    %rax,(%rsp)
mov    %rsp,%rdi
call   1040 <strlen@plt>
cmp    $0x2,%ebx
jne    1290 <main+0x110>
mov    %rax,%r14
mov    0x2dd3(%rip),%rax
mov    0x2e2c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2db9(%rip),%rax
or     (%rax),%esi
mov    0x2dd0(%rip),%rax
mov    0x2df1(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dce(%rip),%rax
or     (%rax),%edx
mov    0x8(%rbp),%rdi
mov    0x2de1(%rip),%rax
or     (%rax),%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     124b <main+0xcb>
mov    %eax,%ebp
mov    0x2db4(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
je     1259 <main+0xd9>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    %r14,%rdx
call   1030 <write@plt>
cmp    %r14,%rax
jne    1262 <main+0xe2>
mov    %ebp,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
je     126b <main+0xeb>
mov    0x2d5f(%rip),%rax
jmp    1285 <main+0x105>
lea    0xdbd(%rip),%rdi
call   1090 <perror@plt>
jmp    127e <main+0xfe>
lea    0xdbc(%rip),%rdi
jmp    1272 <main+0xf2>
lea    0xdcc(%rip),%rdi
jmp    1272 <main+0xf2>
lea    0xdd4(%rip),%rdi
call   1090 <perror@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x2d33(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
call   1070 <usage@plt>
mov    0x2d1a(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@__const.main.output = [15 x i8] c"Appended text\0A\00"
@EXIT_FAILURE = global i32 0
@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
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@.str = [13 x i8] c"Opening file\00"
@SEEK_SET = global i32 0
@.str.1 = [25 x i8] c"Seeking to start of file\00"
@.str.2 = [17 x i8] c"Appending output\00"
@.str.3 = [13 x i8] c"Closing file\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [15 x i8]
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = bitcast [15 x i8]* %6 to i8*
%11 = getelementptr inbounds [15 x i8], [15 x i8]* %6, i64 0, i64 0
%12 = call i64 @strlen(i8* %11)
store i64 %12, i64* %7
store i32 -1, i32* %9
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = call i32 (...) @usage()
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
store i8* %22, i8** %8
%23 = load i8*, i8** %8
%24 = load i32, i32* @O_WRONLY
%25 = load i32, i32* @O_CREAT
%26 = or i32 %24, %25
%27 = load i32, i32* @O_APPEND
%28 = or i32 %26, %27
%29 = load i32, i32* @S_IRUSR
%30 = load i32, i32* @S_IWUSR
%31 = or i32 %29, %30
%32 = load i32, i32* @S_IRGRP
%33 = or i32 %31, %32
%34 = load i32, i32* @S_IWGRP
%35 = or i32 %33, %34
%36 = call i32 @open(i8* %23, i32 %28, i32 %35)
store i32 %36, i32* %9
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %19
%39 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %64
40: ; preds = %19
%41 = load i32, i32* %9
%42 = load i32, i32* @SEEK_SET
%43 = call i32 @lseek(i32 %41, i32 0, i32 %42)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %47
45: ; preds = %40
%46 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %64
47: ; preds = %40
%48 = load i32, i32* %9
%49 = getelementptr inbounds [15 x i8], [15 x i8]* %6, i64 0, i64 0
%50 = load i64, i64* %7
%51 = call i64 @write(i32 %48, i8* %49, i64 %50)
%52 = load i64, i64* %7
%53 = icmp ne i64 %51, %52
br i1 %53, label %54, label %56
54: ; preds = %47
%55 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %64
56: ; preds = %47
%57 = load i32, i32* %9
%58 = call i32 @close(i32 %57)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %62
60: ; preds = %56
%61 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %64
62: ; preds = %56
%63 = load i32, i32* @EXIT_SUCCESS
store i32 %63, i32* %3
br label %72
64: ; preds = %60, %54, %45, %38
%65 = load i32, i32* %9
%66 = icmp ne i32 %65, -1
br i1 %66, label %67, label %70
67: ; preds = %64
%68 = load i32, i32* %9
%69 = call i32 @close(i32 %68)
br label %70
70: ; preds = %67, %64
%71 = load i32, i32* @EXIT_FAILURE
store i32 %71, i32* %3
br label %72
72: ; preds = %70, %62
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
@__const.main.output = [15 x i8] c"Appended text\0A\00"
@EXIT_FAILURE = global i32 0
@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
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@.str = [13 x i8] c"Opening file\00"
@SEEK_SET = global i32 0
@.str.1 = [25 x i8] c"Seeking to start of file\00"
@.str.2 = [17 x i8] c"Appending output\00"
@.str.3 = [13 x i8] c"Closing file\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [15 x i8]
%4 = getelementptr inbounds [15 x i8], [15 x i8]* %3, i64 0, i64 0
%5 = call i64 @strlen(i8* %4)
%6 = icmp eq i32 %0, 2
br i1 %6, label %11, label %7
7: ; preds = %2
%8 = tail call i32 (...) @usage()
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = load i32, i32* @O_WRONLY
%15 = load i32, i32* @O_CREAT
%16 = or i32 %15, %14
%17 = load i32, i32* @O_APPEND
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IRUSR
%20 = load i32, i32* @S_IWUSR
%21 = or i32 %20, %19
%22 = load i32, i32* @S_IRGRP
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IWGRP
%25 = or i32 %23, %24
%26 = tail call i32 @open(i8* %13, i32 %18, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %38, label %28
28: ; preds = %11
%29 = load i32, i32* @SEEK_SET
%30 = tail call i32 @lseek(i32 %26, i32 0, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %40, label %32
32: ; preds = %28
%33 = call i64 @write(i32 %26, i8* %4, i64 %5)
%34 = icmp eq i64 %33, %5
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = tail call i32 @close(i32 %26)
%37 = icmp eq i32 %36, -1
br i1 %37, label %40, label %44
38: ; preds = %11
%39 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %44
40: ; preds = %35, %32, %28
%41 = phi i8* [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), %28 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), %32 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), %35 ]
%42 = tail call i32 @perror(i8* %41)
%43 = tail call i32 @close(i32 %26)
br label %44
44: ; preds = %38, %40, %35
%45 = phi i32* [ @EXIT_SUCCESS, %35 ], [ @EXIT_FAILURE, %40 ], [ @EXIT_FAILURE, %38 ]
%46 = load i32, i32* %45
ret i32 %46
}