#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 ssize_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int O_APPEND;
int O_CREAT;
int O_WRONLY;
int atoi(char *);
char *check_file(char const *);
int close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char const *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char const *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ seek_end(int);
int write(int, char const *, int);
int main(int argc, char **argv) {
  int fd, num_bytes, skip_append, count;
  ssize_t ret;
  const char *file;
  const char *byte;
  if (argc < 3 || argc > 4) {
    printf("Exercise 5-3: Atomic append\nUsage: %s <file> <num_bytes> "
           "[noappend]\n",
           argv[0]);
    puts("Test1: $ atomic_append f1 1000000 & atomic_append f1 1000000");
    puts("Test2: $ atomic_append f2 1000000 noappend & atomic_append f2 "
         "1000000 noappend");
    puts("Verify: $ ls -l f1 f2");
    exit(EXIT_FAILURE);
  }
  file = argv[1];
  num_bytes = atoi(argv[2]);
  if (argv[3]) {
    skip_append = 1;
  }
  if (skip_append == 1) {
    byte = check_file(file);
    if ((fd = open(file, O_WRONLY | O_CREAT, 0744)) == -1) {
      perror("open");
      exit(EXIT_FAILURE);
    }
    printf("File %s opened.\n", file);
  } else {
    byte = check_file(file);
    if ((fd = open(file, O_WRONLY | O_CREAT | O_APPEND, 0744)) == -1) {
      perror("open");
      exit(EXIT_FAILURE);
    }
    printf("File %s opened with O_APPEND.\n", file);
  }
  for (count = 0; count <= num_bytes; count++) {
    if (skip_append == 1) {
      seek_end(fd);
      if (count == 0) {
        puts("Seeking to end of file for each one byte write");
      }
    }
    if ((ret = write(fd, byte, 1)) == -1) {
      perror("write");
      exit(EXIT_FAILURE);
    }
  }
  if (close(fd) == -1) {
    perror("close");
  }
}


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   $0x3,-0x8(%rbp)
jl     11ca <main+0x2a>
cmpl   $0x4,-0x8(%rbp)
jle    120f <main+0x6f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe28(%rip),%rdi
call   1060 <printf@plt>
lea    0xe61(%rip),%rdi
call   1030 <puts@plt>
lea    0xe92(%rip),%rdi
call   1030 <puts@plt>
lea    0xed5(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x18(%rax)
je     1241 <main+0xa1>
movl   $0x1,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jne    12b3 <main+0x113>
mov    -0x30(%rbp),%rdi
call   1080 <check_file@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
mov    0x2d8c(%rip),%rax
or     (%rax),%esi
mov    $0x1e4,%edx
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    129e <main+0xfe>
lea    0xe5c(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x30(%rbp),%rsi
lea    0xe43(%rip),%rdi
call   1060 <printf@plt>
jmp    131f <main+0x17f>
mov    -0x30(%rbp),%rdi
call   1080 <check_file@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2cfd(%rip),%rax
mov    (%rax),%esi
mov    0x2d24(%rip),%rax
or     (%rax),%esi
mov    0x2ce3(%rip),%rax
or     (%rax),%esi
mov    $0x1e4,%edx
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    130f <main+0x16f>
lea    0xdeb(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cd0(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x30(%rbp),%rsi
lea    0xde3(%rip),%rdi
call   1060 <printf@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x18(%rbp),%eax
jg     13a9 <main+0x209>
cmpl   $0x1,-0x1c(%rbp)
jne    135f <main+0x1bf>
mov    -0x14(%rbp),%edi
call   1050 <seek_end@plt>
cmpl   $0x0,-0x20(%rbp)
jne    135a <main+0x1ba>
lea    0xdc7(%rip),%rdi
call   1030 <puts@plt>
jmp    135f <main+0x1bf>
mov    -0x14(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    1396 <main+0x1f6>
lea    0xdc2(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c49(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
jmp    139b <main+0x1fb>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1326 <main+0x186>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
cmp    $0xffffffff,%eax
jne    13c6 <main+0x226>
lea    0xd8a(%rip),%rdi
call   10a0 <perror@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
add    $0xfffffffb,%edi
cmp    $0xfffffffd,%edi
jbe    12a1 <main+0x101>
mov    0x8(%rsi),%r15
mov    0x10(%rsi),%rdi
call   10b0 <atoi@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1080 <check_file@plt>
mov    %rax,%r14
mov    0x2df4(%rip),%rax
mov    0x2e1d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r15,%rdi
mov    $0x1e4,%edx
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12d6 <main+0x136>
mov    %eax,%ebp
lea    0xeee(%rip),%rdi
mov    %r15,%rsi
call   1060 <printf@plt>
test   %ebx,%ebx
js     1260 <main+0xc0>
mov    %ebp,%edi
call   1050 <seek_end@plt>
lea    0xee5(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     1287 <main+0xe7>
test   %ebx,%ebx
je     1260 <main+0xc0>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edi
call   1050 <seek_end@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     1287 <main+0xe7>
add    $0xffffffff,%ebx
jne    1240 <main+0xa0>
mov    %ebp,%edi
call   1070 <close@plt>
cmp    $0xffffffff,%eax
je     1279 <main+0xd9>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xeac(%rip),%rdi
call   10a0 <perror@plt>
jmp    126c <main+0xcc>
lea    0xe98(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    (%rsi),%rsi
lea    0xd55(%rip),%rdi
call   1060 <printf@plt>
lea    0xd8e(%rip),%rdi
call   1030 <puts@plt>
lea    0xdbf(%rip),%rdi
call   1030 <puts@plt>
lea    0xe02(%rip),%rdi
call   1030 <puts@plt>
jmp    1293 <main+0xf3>
lea    0xe0a(%rip),%rdi
jmp    128e <main+0xee>

LLVM IR (-O0):
@.str = [69 x i8] c"Exercise 5-3: Atomic append\0AUsage: %s <file> <num_bytes> [noappend]\0A\00"
@.str.1 = [61 x i8] c"Test1: $ atomic_append f1 1000000 & atomic_append f1 1000000\00"
@.str.2 = [79 x i8] c"Test2: $ atomic_append f2 1000000 noappend & atomic_append f2 1000000 noappend\00"
@.str.3 = [22 x i8] c"Verify: $ ls -l f1 f2\00"
@EXIT_FAILURE = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.4 = [5 x i8] c"open\00"
@.str.5 = [17 x i8] c"File %s opened.\0A\00"
@O_APPEND = global i32 0
@.str.6 = [31 x i8] c"File %s opened with O_APPEND.\0A\00"
@.str.7 = [47 x i8] c"Seeking to end of file for each one byte write\00"
@.str.8 = [6 x i8] c"write\00"
@.str.9 = [6 x i8] c"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 i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 3
br i1 %14, label %18, label %15
15: ; preds = %2
%16 = load i32, i32* %4
%17 = icmp sgt i32 %16, 4
br i1 %17, label %18, label %28
18: ; preds = %15, %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = call i32 @puts(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @puts(i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @puts(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %15
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
store i8* %31, i8** %11
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 2
%34 = load i8*, i8** %33
%35 = call i32 @atoi(i8* %34)
store i32 %35, i32* %7
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 3
%38 = load i8*, i8** %37
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %41
40: ; preds = %28
store i32 1, i32* %8
br label %41
41: ; preds = %40, %28
%42 = load i32, i32* %8
%43 = icmp eq i32 %42, 1
br i1 %43, label %44, label %60
44: ; preds = %41
%45 = load i8*, i8** %11
%46 = call i8* @check_file(i8* %45)
store i8* %46, i8** %12
%47 = load i8*, i8** %11
%48 = load i32, i32* @O_WRONLY
%49 = load i32, i32* @O_CREAT
%50 = or i32 %48, %49
%51 = call i32 @open(i8* %47, i32 %50, i32 484)
store i32 %51, i32* %6
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %57
53: ; preds = %44
%54 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%55 = load i32, i32* @EXIT_FAILURE
%56 = call i32 @exit(i32 %55)
unreachable
57: ; preds = %44
%58 = load i8*, i8** %11
%59 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* %58)
br label %78
60: ; preds = %41
%61 = load i8*, i8** %11
%62 = call i8* @check_file(i8* %61)
store i8* %62, i8** %12
%63 = load i8*, i8** %11
%64 = load i32, i32* @O_WRONLY
%65 = load i32, i32* @O_CREAT
%66 = or i32 %64, %65
%67 = load i32, i32* @O_APPEND
%68 = or i32 %66, %67
%69 = call i32 @open(i8* %63, i32 %68, i32 484)
store i32 %69, i32* %6
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %75
71: ; preds = %60
%72 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%73 = load i32, i32* @EXIT_FAILURE
%74 = call i32 @exit(i32 %73)
unreachable
75: ; preds = %60
%76 = load i8*, i8** %11
%77 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i8* %76)
br label %78
78: ; preds = %75, %57
store i32 0, i32* %9
br label %79
79: ; preds = %104, %78
%80 = load i32, i32* %9
%81 = load i32, i32* %7
%82 = icmp sle i32 %80, %81
br i1 %82, label %83, label %107
83: ; preds = %79
%84 = load i32, i32* %8
%85 = icmp eq i32 %84, 1
br i1 %85, label %86, label %94
86: ; preds = %83
%87 = load i32, i32* %6
%88 = call i32 @seek_end(i32 %87)
%89 = load i32, i32* %9
%90 = icmp eq i32 %89, 0
br i1 %90, label %91, label %93
91: ; preds = %86
%92 = call i32 @puts(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.7, i64 0, i64 0))
br label %93
93: ; preds = %91, %86
br label %94
94: ; preds = %93, %83
%95 = load i32, i32* %6
%96 = load i8*, i8** %12
%97 = call i32 @write(i32 %95, i8* %96, i32 1)
store i32 %97, i32* %10
%98 = icmp eq i32 %97, -1
br i1 %98, label %99, label %103
99: ; preds = %94
%100 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
%101 = load i32, i32* @EXIT_FAILURE
%102 = call i32 @exit(i32 %101)
unreachable
103: ; preds = %94
br label %104
104: ; preds = %103
%105 = load i32, i32* %9
%106 = add nsw i32 %105, 1
store i32 %106, i32* %9
br label %79
107: ; preds = %79
%108 = load i32, i32* %6
%109 = call i32 @close(i32 %108)
%110 = icmp eq i32 %109, -1
br i1 %110, label %111, label %113
111: ; preds = %107
%112 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0))
br label %113
113: ; preds = %111, %107
%114 = load i32, i32* %3
ret i32 %114
}

LLVM IR (-O2):
@.str = [69 x i8] c"Exercise 5-3: Atomic append\0AUsage: %s <file> <num_bytes> [noappend]\0A\00"
@.str.1 = [61 x i8] c"Test1: $ atomic_append f1 1000000 & atomic_append f1 1000000\00"
@.str.2 = [79 x i8] c"Test2: $ atomic_append f2 1000000 noappend & atomic_append f2 1000000 noappend\00"
@.str.3 = [22 x i8] c"Verify: $ ls -l f1 f2\00"
@EXIT_FAILURE = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.4 = [5 x i8] c"open\00"
@.str.5 = [17 x i8] c"File %s opened.\0A\00"
@O_APPEND = global i32 0
@.str.7 = [47 x i8] c"Seeking to end of file for each one byte write\00"
@.str.8 = [6 x i8] c"write\00"
@.str.9 = [6 x i8] c"close\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = add i32 %0, -5
%4 = icmp ult i32 %3, -2
br i1 %4, label %5, label %13
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([79 x i8], [79 x i8]* @.str.2, i64 0, i64 0))
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = tail call i32 @atoi(i8* %17)
%19 = tail call i8* @check_file(i8* %15)
%20 = load i32, i32* @O_WRONLY
%21 = load i32, i32* @O_CREAT
%22 = or i32 %21, %20
%23 = tail call i32 @open(i8* %15, i32 %22, i32 484)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %13
%26 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = tail call i32 @exit(i32 %27)
unreachable
29: ; preds = %13
%30 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* %15)
%31 = icmp slt i32 %18, 0
br i1 %31, label %51, label %32
32: ; preds = %29
%33 = tail call i32 @seek_end(i32 %23)
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.7, i64 0, i64 0))
%35 = tail call i32 @write(i32 %23, i8* %19, i32 1)
%36 = icmp eq i32 %35, -1
br i1 %36, label %47, label %37
37: ; preds = %32
%38 = icmp eq i32 %18, 0
br i1 %38, label %51, label %42
39: ; preds = %42
%40 = add nuw i32 %43, 1
%41 = icmp eq i32 %43, %18
br i1 %41, label %51, label %42
42: ; preds = %37, %39
%43 = phi i32 [ %40, %39 ], [ 1, %37 ]
%44 = tail call i32 @seek_end(i32 %23)
%45 = tail call i32 @write(i32 %23, i8* %19, i32 1)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %39
47: ; preds = %42, %32
%48 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
%49 = load i32, i32* @EXIT_FAILURE
%50 = tail call i32 @exit(i32 %49)
unreachable
51: ; preds = %39, %37, %29
%52 = tail call i32 @close(i32 %23)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0))
br label %56
56: ; preds = %54, %51
ret i32 0
}