#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*/ writeStr;
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int O_APPEND;
int O_WRONLY;
int /*<<< orphan*/ SEEK_SET;
int close(int);
int /*<<< orphan*/ helpAndLeave(char *, int /*<<< orphan*/);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ pexit(char *);
int /*<<< orphan*/ printf(char *, char *, long);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  char *filename;
  int flags, fd;
  char writeStr[] = "Writing to the file\n";
  int numBytes = sizeof(writeStr);
  ssize_t numWritten;
  if (argc != 2) {
    helpAndLeave(argv[0], EXIT_FAILURE);
  }
  filename = argv[1];
  flags = O_WRONLY | O_APPEND;
  fd = open(filename, flags);
  if (fd == -1) {
    pexit("open");
  }
  lseek(fd, 0, SEEK_SET);
  numWritten = write(fd, writeStr, numBytes);
  if (numWritten == -1) {
    pexit("write");
  }
  if (close(fd) == -1) {
    pexit("close");
  }
  printf("Done. Check file %s now. Number of bytes written: %ld\n", filename,
         (long)numWritten);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe73(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe70(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xe6e(%rip),%eax
mov    %eax,-0x30(%rbp)
mov    0xe69(%rip),%al
mov    %al,-0x2c(%rbp)
movl   $0x15,-0x44(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11d4 <main+0x64>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
call   1080 <helpAndLeave@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    0x2dd9(%rip),%rax
mov    (%rax),%eax
mov    0x2dc8(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    121a <main+0xaa>
lea    0xe00(%rip),%rdi
call   1090 <pexit@plt>
mov    -0x20(%rbp),%edi
mov    0x2dbc(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    -0x20(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    -0x44(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0xffffffff,-0x48(%rbp)
jne    1255 <main+0xe5>
lea    0xdca(%rip),%rdi
call   1090 <pexit@plt>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    1272 <main+0x102>
lea    0xdb3(%rip),%rdi
call   1090 <pexit@plt>
mov    -0x18(%rbp),%rsi
movslq -0x48(%rbp),%rdx
lea    0xda5(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d43(%rip),%rax
mov    (%rax),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbx
movaps 0xe7e(%rip),%xmm0
movaps %xmm0,(%rsp)
movabs $0xa656c69662065,%rax
mov    %rax,0xd(%rsp)
cmp    $0x2,%edi
je     11ab <main+0x3b>
mov    (%rbx),%rdi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
call   1080 <helpAndLeave@plt>
mov    0x8(%rbx),%r14
mov    0x2e0a(%rip),%rax
mov    0x2dfb(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r14,%rdi
call   1070 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11dc <main+0x6c>
lea    0xe3e(%rip),%rdi
call   1090 <pexit@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x15,%edx
call   1030 <write@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1210 <main+0xa0>
lea    0xe0f(%rip),%rdi
call   1090 <pexit@plt>
mov    %ebp,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    1228 <main+0xb8>
lea    0xdfd(%rip),%rdi
call   1090 <pexit@plt>
movslq %ebx,%rdx
lea    0xdf4(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    0x2d8f(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.writeStr = [21 x i8] c"Writing to the file\0A\00"
@EXIT_FAILURE = global i32 0
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@.str = [5 x i8] c"open\00"
@SEEK_SET = global i32 0
@.str.1 = [6 x i8] c"write\00"
@.str.2 = [6 x i8] c"close\00"
@.str.3 = [55 x i8] c"Done. Check file %s now. Number of bytes written: %ld\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [21 x i8]
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = bitcast [21 x i8]* %9 to i8*
store i32 21, i32* %10
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @helpAndLeave(i8* %18, i32 %19)
br label %21
21: ; preds = %15, %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
store i8* %24, i8** %6
%25 = load i32, i32* @O_WRONLY
%26 = load i32, i32* @O_APPEND
%27 = or i32 %25, %26
store i32 %27, i32* %7
%28 = load i8*, i8** %6
%29 = load i32, i32* %7
%30 = call i32 @open(i8* %28, i32 %29)
store i32 %30, i32* %8
%31 = load i32, i32* %8
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %21
%34 = call i32 @pexit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %35
35: ; preds = %33, %21
%36 = load i32, i32* %8
%37 = load i32, i32* @SEEK_SET
%38 = call i32 @lseek(i32 %36, i32 0, i32 %37)
%39 = load i32, i32* %8
%40 = getelementptr inbounds [21 x i8], [21 x i8]* %9, i64 0, i64 0
%41 = load i32, i32* %10
%42 = call i32 @write(i32 %39, i8* %40, i32 %41)
store i32 %42, i32* %11
%43 = load i32, i32* %11
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %47
45: ; preds = %35
%46 = call i32 @pexit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %47
47: ; preds = %45, %35
%48 = load i32, i32* %8
%49 = call i32 @close(i32 %48)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = call i32 @pexit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %53
53: ; preds = %51, %47
%54 = load i8*, i8** %6
%55 = load i32, i32* %11
%56 = sext i32 %55 to i64
%57 = call i32 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.3, i64 0, i64 0), i8* %54, i64 %56)
%58 = load i32, i32* @EXIT_SUCCESS
ret i32 %58
}

LLVM IR (-O2):
@__const.main.writeStr = [21 x i8] c"Writing to the file\0A\00"
@EXIT_FAILURE = global i32 0
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@.str = [5 x i8] c"open\00"
@SEEK_SET = global i32 0
@.str.1 = [6 x i8] c"write\00"
@.str.2 = [6 x i8] c"close\00"
@.str.3 = [55 x i8] c"Done. Check file %s now. Number of bytes written: %ld\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [21 x i8]
%4 = getelementptr inbounds [21 x i8], [21 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @helpAndLeave(i8* %7, i32 %8)
br label %10
10: ; preds = %6, %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_WRONLY
%14 = load i32, i32* @O_APPEND
%15 = or i32 %14, %13
%16 = tail call i32 @open(i8* %12, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %10
%19 = tail call i32 @pexit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %10
%21 = load i32, i32* @SEEK_SET
%22 = tail call i32 @lseek(i32 %16, i32 0, i32 %21)
%23 = call i32 @write(i32 %16, i8* %4, i32 21)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = tail call i32 @pexit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %20
%28 = tail call i32 @close(i32 %16)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = tail call i32 @pexit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %30, %27
%33 = sext i32 %23 to i64
%34 = tail call i32 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.3, i64 0, i64 0), i8* %12, i64 %33)
%35 = load i32, i32* @EXIT_SUCCESS
ret i32 %35
}