#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 /*<<< orphan*/ SEEK_END;
int S_IRUSR;
int S_IWUSR;
int atoi(char *);
int close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
char *strerror(int /*<<< orphan*/);
int write(int, char *, int);
int main(int argc, char **argv) {
  if (argc < 3 || argc > 4) {
    fprintf(stderr, "Usage: %s file bytes [x]\n", argv[0]);
    return 1;
  }
  int use_append = 1;
  if (argc == 4 && strcmp(argv[3], "x") == 0) {
    use_append = 0;
  }
  int flags = O_WRONLY | O_CREAT;
  if (use_append) {
    flags = flags | O_APPEND;
  }
  int fd = open(argv[1], flags, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    fprintf(stderr, "open: %s\n", strerror(errno));
    return 1;
  }
  char c = 'x';
  for (int i = 0; i < atoi(argv[2]); i++) {
    if (!use_append) {
      if (lseek(fd, 0, SEEK_END) == -1) {
        fprintf(stderr, "lseek: %s\n", strerror(errno));
        return 1;
      }
    }
    if (write(fd, &c, 1) != 1) {
      fprintf(stderr, "write: %s\n", strerror(errno));
      return 1;
    }
  }
  if (close(fd) == -1) {
    fprintf(stderr, "close: %s\n", strerror(errno));
    return 1;
  }
  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   $0x3,-0x8(%rbp)
jl     11aa <main+0x2a>
cmpl   $0x4,-0x8(%rbp)
jle    11d2 <main+0x52>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe3f(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13e0 <main+0x260>
movl   $0x1,-0x14(%rbp)
cmpl   $0x4,-0x8(%rbp)
jne    1208 <main+0x88>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    0xe28(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1208 <main+0x88>
movl   $0x0,-0x14(%rbp)
mov    0x2d99(%rip),%rax
mov    (%rax),%eax
mov    0x2de0(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1236 <main+0xb6>
mov    -0x18(%rbp),%eax
mov    0x2d6f(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x18(%rbp),%esi
mov    0x2d70(%rip),%rax
mov    (%rax),%edx
mov    0x2d87(%rip),%rax
or     (%rax),%edx
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    129d <main+0x11d>
mov    0x2d5c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xd90(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13e0 <main+0x260>
movb   $0x78,-0x1d(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
cmp    %ecx,%eax
jge    1390 <main+0x210>
cmpl   $0x0,-0x14(%rbp)
jne    132b <main+0x1ab>
mov    -0x1c(%rbp),%edi
mov    0x2d04(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
jne    1326 <main+0x1a6>
mov    0x2cd3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2cbf(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rdx
lea    0xd11(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13e0 <main+0x260>
jmp    132b <main+0x1ab>
mov    -0x1c(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     137d <main+0x1fd>
mov    0x2c7c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2c68(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xcc5(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13e0 <main+0x260>
jmp    1382 <main+0x202>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    12a8 <main+0x128>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    13d9 <main+0x259>
mov    0x2c20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2c0c(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x38(%rbp),%edi
mov    %rax,%rdx
lea    0xc74(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13e0 <main+0x260>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r13
lea    -0x5(%rdi),%eax
cmp    $0xfffffffd,%eax
ja     11af <main+0x2f>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
mov    0x0(%r13),%rdx
lea    0xe56(%rip),%rsi
jmp    12f4 <main+0x174>
cmp    $0x4,%edi
jne    11ee <main+0x6e>
mov    0x18(%r13),%rdi
lea    0xe5b(%rip),%rsi
call   1060 <strcmp@plt>
mov    0x2ddd(%rip),%rcx
mov    0x2e26(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
xor    %ecx,%ecx
test   %rax,%rax
sete   %r12b
je     11ea <main+0x6a>
mov    0x2db8(%rip),%rax
mov    (%rax),%ecx
or     %ecx,%esi
jmp    120c <main+0x8c>
mov    0x2db3(%rip),%rax
mov    0x2dfc(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d99(%rip),%rax
or     (%rax),%esi
xor    %r12d,%r12d
mov    0x8(%r13),%rdi
mov    0x2da1(%rip),%rax
mov    0x2dba(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     12a3 <main+0x123>
mov    %eax,%ebp
movb   $0x78,0x7(%rsp)
mov    0x10(%r13),%rdi
call   1090 <atoi@plt>
test   %eax,%eax
jle    128a <main+0x10a>
xor    %ebx,%ebx
mov    0x2d97(%rip),%r14
lea    0x7(%rsp),%r15
xchg   %ax,%ax
test   %r12b,%r12b
je     1266 <main+0xe6>
mov    (%r14),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
je     12ce <main+0x14e>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
jne    12c5 <main+0x145>
add    $0x1,%ebx
mov    0x10(%r13),%rdi
call   1090 <atoi@plt>
cmp    %eax,%ebx
jl     1250 <main+0xd0>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    12fe <main+0x17e>
lea    0xd9b(%rip),%rbp
jmp    12d5 <main+0x155>
mov    0x2d1e(%rip),%rax
mov    (%rax),%ebx
mov    0x2d0d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd5b(%rip),%rsi
mov    %ebx,%edi
jmp    12f1 <main+0x171>
lea    0xd65(%rip),%rbp
jmp    12d5 <main+0x155>
lea    0xd51(%rip),%rbp
mov    0x2cec(%rip),%rax
mov    (%rax),%ebx
mov    0x2cdb(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [26 x i8] c"Usage: %s file bytes [x]\0A\00"
@.str.1 = [2 x i8] c"x\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 = [10 x i8] c"open: %s\0A\00"
@errno = global i32 0
@SEEK_END = global i32 0
@.str.3 = [11 x i8] c"lseek: %s\0A\00"
@.str.4 = [11 x i8] c"write: %s\0A\00"
@.str.5 = [11 x i8] c"close: %s\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 i32
%9 = alloca i8
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 3
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = load i32, i32* %4
%15 = icmp sgt i32 %14, 4
br i1 %15, label %16, label %22
16: ; preds = %13, %2
%17 = load i32, i32* @stderr
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %20)
store i32 1, i32* %3
br label %103
22: ; preds = %13
store i32 1, i32* %6
%23 = load i32, i32* %4
%24 = icmp eq i32 %23, 4
br i1 %24, label %25, label %32
25: ; preds = %22
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 3
%28 = load i8*, i8** %27
%29 = call i64 @strcmp(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %25
store i32 0, i32* %6
br label %32
32: ; preds = %31, %25, %22
%33 = load i32, i32* @O_WRONLY
%34 = load i32, i32* @O_CREAT
%35 = or i32 %33, %34
store i32 %35, i32* %7
%36 = load i32, i32* %6
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %32
%39 = load i32, i32* %7
%40 = load i32, i32* @O_APPEND
%41 = or i32 %39, %40
store i32 %41, i32* %7
br label %42
42: ; preds = %38, %32
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 1
%45 = load i8*, i8** %44
%46 = load i32, i32* %7
%47 = load i32, i32* @S_IRUSR
%48 = load i32, i32* @S_IWUSR
%49 = or i32 %47, %48
%50 = call i32 @open(i8* %45, i32 %46, i32 %49)
store i32 %50, i32* %8
%51 = load i32, i32* %8
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %58
53: ; preds = %42
%54 = load i32, i32* @stderr
%55 = load i32, i32* @errno
%56 = call i8* @strerror(i32 %55)
%57 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %56)
store i32 1, i32* %3
br label %103
58: ; preds = %42
store i8 120, i8* %9
store i32 0, i32* %10
br label %59
59: ; preds = %90, %58
%60 = load i32, i32* %10
%61 = load i8**, i8*** %5
%62 = getelementptr inbounds i8*, i8** %61, i64 2
%63 = load i8*, i8** %62
%64 = call i32 @atoi(i8* %63)
%65 = icmp slt i32 %60, %64
br i1 %65, label %66, label %93
66: ; preds = %59
%67 = load i32, i32* %6
%68 = icmp ne i32 %67, 0
br i1 %68, label %80, label %69
69: ; preds = %66
%70 = load i32, i32* %8
%71 = load i32, i32* @SEEK_END
%72 = call i32 @lseek(i32 %70, i32 0, i32 %71)
%73 = icmp eq i32 %72, -1
br i1 %73, label %74, label %79
74: ; preds = %69
%75 = load i32, i32* @stderr
%76 = load i32, i32* @errno
%77 = call i8* @strerror(i32 %76)
%78 = call i32 @fprintf(i32 %75, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %77)
store i32 1, i32* %3
br label %103
79: ; preds = %69
br label %80
80: ; preds = %79, %66
%81 = load i32, i32* %8
%82 = call i32 @write(i32 %81, i8* %9, i32 1)
%83 = icmp ne i32 %82, 1
br i1 %83, label %84, label %89
84: ; preds = %80
%85 = load i32, i32* @stderr
%86 = load i32, i32* @errno
%87 = call i8* @strerror(i32 %86)
%88 = call i32 @fprintf(i32 %85, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* %87)
store i32 1, i32* %3
br label %103
89: ; preds = %80
br label %90
90: ; preds = %89
%91 = load i32, i32* %10
%92 = add nsw i32 %91, 1
store i32 %92, i32* %10
br label %59
93: ; preds = %59
%94 = load i32, i32* %8
%95 = call i32 @close(i32 %94)
%96 = icmp eq i32 %95, -1
br i1 %96, label %97, label %102
97: ; preds = %93
%98 = load i32, i32* @stderr
%99 = load i32, i32* @errno
%100 = call i8* @strerror(i32 %99)
%101 = call i32 @fprintf(i32 %98, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i8* %100)
store i32 1, i32* %3
br label %103
102: ; preds = %93
store i32 0, i32* %3
br label %103
103: ; preds = %102, %97, %84, %74, %53, %16
%104 = load i32, i32* %3
ret i32 %104
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [26 x i8] c"Usage: %s file bytes [x]\0A\00"
@.str.1 = [2 x i8] c"x\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 = [10 x i8] c"open: %s\0A\00"
@errno = global i32 0
@SEEK_END = global i32 0
@.str.3 = [11 x i8] c"lseek: %s\0A\00"
@.str.4 = [11 x i8] c"write: %s\0A\00"
@.str.5 = [11 x i8] c"close: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8
%4 = add i32 %0, -5
%5 = icmp ult i32 %4, -2
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %74
10: ; preds = %2
%11 = icmp eq i32 %0, 4
br i1 %11, label %18, label %12
12: ; preds = %10
%13 = load i32, i32* @O_WRONLY
%14 = load i32, i32* @O_CREAT
%15 = or i32 %14, %13
%16 = load i32, i32* @O_APPEND
%17 = or i32 %15, %16
br label %29
18: ; preds = %10
%19 = getelementptr inbounds i8*, i8** %1, i64 3
%20 = load i8*, i8** %19
%21 = tail call i64 @strcmp(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
%23 = load i32, i32* @O_WRONLY
%24 = load i32, i32* @O_CREAT
%25 = or i32 %24, %23
%26 = load i32, i32* @O_APPEND
%27 = select i1 %22, i32 0, i32 %26
%28 = or i32 %25, %27
br label %29
29: ; preds = %18, %12
%30 = phi i1 [ false, %12 ], [ %22, %18 ]
%31 = phi i32 [ %17, %12 ], [ %28, %18 ]
%32 = getelementptr inbounds i8*, i8** %1, i64 1
%33 = load i8*, i8** %32
%34 = load i32, i32* @S_IRUSR
%35 = load i32, i32* @S_IWUSR
%36 = or i32 %35, %34
%37 = tail call i32 @open(i8* %33, i32 %31, i32 %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %44
39: ; preds = %29
%40 = load i32, i32* @stderr
%41 = load i32, i32* @errno
%42 = tail call i8* @strerror(i32 %41)
%43 = tail call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %42)
br label %74
44: ; preds = %29
store i8 120, i8* %3
%45 = getelementptr inbounds i8*, i8** %1, i64 2
%46 = load i8*, i8** %45
%47 = tail call i32 @atoi(i8* %46)
%48 = icmp sgt i32 %47, 0
br i1 %48, label %53, label %63
49: ; preds = %59
%50 = load i8*, i8** %45
%51 = tail call i32 @atoi(i8* %50)
%52 = icmp slt i32 %62, %51
br i1 %52, label %53, label %63
53: ; preds = %44, %49
%54 = phi i32 [ %62, %49 ], [ 0, %44 ]
br i1 %30, label %55, label %59
55: ; preds = %53
%56 = load i32, i32* @SEEK_END
%57 = tail call i32 @lseek(i32 %37, i32 0, i32 %56)
%58 = icmp eq i32 %57, -1
br i1 %58, label %66, label %59
59: ; preds = %55, %53
%60 = call i32 @write(i32 %37, i8* %3, i32 1)
%61 = icmp eq i32 %60, 1
%62 = add nuw nsw i32 %54, 1
br i1 %61, label %49, label %66
63: ; preds = %49, %44
%64 = tail call i32 @close(i32 %37)
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %72
66: ; preds = %59, %55, %63
%67 = phi i8* [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), %63 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), %55 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), %59 ]
%68 = load i32, i32* @stderr
%69 = load i32, i32* @errno
%70 = tail call i8* @strerror(i32 %69)
%71 = tail call i32 @fprintf(i32 %68, i8* %67, i8* %70)
br label %72
72: ; preds = %66, %63
%73 = phi i32 [ 0, %63 ], [ 1, %66 ]
br label %74
74: ; preds = %39, %72, %6
%75 = phi i32 [ 1, %6 ], [ 1, %39 ], [ %73, %72 ]
ret i32 %75
}