#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;
typedef void *off_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWGRP;
int S_IWOTH;
int S_IWUSR;
int close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fdatasync(int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ isprint(unsigned char);
int lseek(int, void *, int /*<<< orphan*/);
char *malloc(size_t);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, size_t);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strlen(char *);
void *strtol(char *, int /*<<< orphan*/ *, int);
int write(int, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  size_t len;
  off_t offset;
  int fd, i, j;
  char *buf;
  ssize_t numRead, numWritten;
  if ((argc < 3) || (strcmp(argv[1], "--help") == 0)) {
    printf("Usage %s file {r<length>|R<length>|w<string>|<soffset>}...\n",
           argv[0]);
    exit(EXIT_SUCCESS);
  }
  printf("Input file is %s\n", argv[1]);
  fd = open(argv[1], O_RDWR | O_CREAT,
            S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
  if (fd == -1) {
    printf("Error opening the file %s\n", argv[1]);
    exit(EXIT_FAILURE);
  }
  for (i = 2; i < argc; i++) {
    switch (argv[i][0]) {
    case 'r':
    case 'R':
      len = strtol(&argv[i][1], NULL, 10);
      buf = malloc(len);
      if (buf == NULL) {
        printf("Memory allocation failed\n");
        exit(EXIT_FAILURE);
      }
      if ((numRead = read(fd, buf, len)) == -1) {
        printf("Error reading from file %s\n", argv[1]);
        exit(EXIT_FAILURE);
      }
      if (numRead == 0) {
        printf("End of file %s\n", argv[1]);
      } else {
        for (j = 0; j < numRead; j++) {
          if (argv[i][0] == 'r') {
            printf("%c", isprint((unsigned char)buf[j]) ? buf[j] : '?');
          } else {
            printf("%02x ", buf[j]);
          }
        }
      }
      printf("\n");
      free(buf);
      break;
    case 'w':
      numWritten = write(fd, &argv[i][1], strlen(&argv[i][1]));
      if (numWritten == -1) {
        printf("Cannot write to %s\n", argv[1]);
        exit(EXIT_FAILURE);
      } else {
        if (fdatasync(fd) == -1) {
          printf("sync to file failed\n");
        }
        printf("Write succeeded %s\n", argv[i]);
        printf("Wrote %zd bytes to %s\n", numWritten, argv[1]);
      }
      break;
    case 's':
      offset = strtol(&argv[i][1], NULL, 10);
      printf("offset is %zd\n", offset);
      if (lseek(fd, offset, SEEK_SET) == -1) {
        printf("could not seek\n");
        exit(EXIT_FAILURE);
      }
      printf("Seek succeeded %s\n", argv[i]);
      break;
    default:
      printf("Arugment must start with rRws: %s \n", argv[i]);
      exit(EXIT_FAILURE);
    }
  }
  if (close(fd) == -1) {
    printf("Cannot close fd\n");
    exit(EXIT_FAILURE);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jl     121e <main+0x3e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe89(%rip),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%rax
jne    1241 <main+0x61>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe8b(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d7a(%rip),%rax
mov    (%rax),%esi
mov    0x2d89(%rip),%rax
or     (%rax),%esi
mov    0x2d28(%rip),%rax
mov    (%rax),%edx
mov    0x2d4f(%rip),%rax
or     (%rax),%edx
mov    0x2d2e(%rip),%rax
or     (%rax),%edx
mov    0x2d45(%rip),%rax
or     (%rax),%edx
mov    0x2d14(%rip),%rax
or     (%rax),%edx
mov    0x2ce3(%rip),%rax
or     (%rax),%edx
call   10f0 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    12dd <main+0xfd>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    0x2cd2(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
movl   $0x2,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1637 <main+0x457>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movsbl (%rax),%eax
add    $0xffffffae,%eax
mov    %eax,%ecx
mov    %rcx,-0x48(%rbp)
sub    $0x25,%eax
ja     15fc <main+0x41c>
mov    -0x48(%rbp),%rax
lea    0xce4(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
add    $0x1,%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   10c0 <strtol@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   10d0 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    137b <main+0x19b>
lea    0xda2(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    0x2c34(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
mov    -0x24(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   10a0 <read@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0xffffffff,%eax
jne    13bb <main+0x1db>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd7c(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    0x2bf4(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
cmpl   $0x0,-0x3c(%rbp)
jne    13e0 <main+0x200>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd6a(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    148e <main+0x2ae>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jge    1489 <main+0x2a9>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movsbl (%rax),%eax
cmp    $0x72,%eax
jne    145c <main+0x27c>
mov    -0x38(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movzbl (%rax,%rcx,1),%edi
call   10e0 <isprint@plt>
cmp    $0x0,%eax
je     1439 <main+0x259>
mov    -0x38(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    %eax,-0x4c(%rbp)
jmp    1446 <main+0x266>
mov    $0x3f,%eax
mov    %eax,-0x4c(%rbp)
jmp    1446 <main+0x266>
mov    -0x4c(%rbp),%esi
lea    0xcfe(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1476 <main+0x296>
mov    -0x38(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsbl (%rax,%rcx,1),%esi
lea    0xce2(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    147b <main+0x29b>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    13e7 <main+0x207>
jmp    148e <main+0x2ae>
lea    0xd6a(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
jmp    1624 <main+0x444>
mov    -0x24(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
add    $0x1,%rax
mov    %rax,-0x58(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
add    $0x1,%rdi
call   1060 <strlen@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0xffffffff,-0x40(%rbp)
jne    1518 <main+0x338>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xc54(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    0x2a97(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
mov    -0x24(%rbp),%edi
call   1040 <fdatasync@plt>
cmp    $0xffffffff,%eax
jne    1537 <main+0x357>
lea    0xc3b(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xc36(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x40(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xc31(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1624 <main+0x444>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
add    $0x1,%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   10c0 <strtol@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xc0f(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    0x2a0f(%rip),%rax
mov    (%rax),%edx
call   1080 <lseek@plt>
cmp    $0xffffffff,%eax
jne    15dd <main+0x3fd>
lea    0xbf2(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    0x29d2(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xbda(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1624 <main+0x444>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xbce(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    0x298b(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
jmp    1629 <main+0x449>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    12e4 <main+0x104>
mov    -0x24(%rbp),%edi
call   1090 <close@plt>
cmp    $0xffffffff,%eax
jne    1664 <main+0x484>
lea    0xbb2(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    0x294b(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
mov    0x292d(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jl     122d <main+0x2d>
mov    %edi,%ebp
mov    0x8(%rbx),%rdi
lea    0xe75(%rip),%rsi
call   10d0 <strcmp@plt>
test   %rax,%rax
jne    124c <main+0x4c>
mov    (%rbx),%rsi
lea    0xe68(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>
mov    0x8(%rbx),%rsi
lea    0xe84(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
mov    0x2d7b(%rip),%rax
mov    0x2d8c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d29(%rip),%rax
mov    0x2d52(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d2f(%rip),%rax
or     (%rax),%edx
mov    0x2d46(%rip),%rax
or     (%rax),%edx
mov    0x2d15(%rip),%rax
or     (%rax),%edx
mov    0x8(%rbx),%rdi
mov    0x2ce0(%rip),%rax
or     (%rax),%edx
call   1110 <open@plt>
cmp    $0xffffffff,%eax
jne    12cb <main+0xcb>
mov    0x8(%rbx),%rsi
lea    0xe2e(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
jmp    1516 <main+0x316>
mov    %eax,%r14d
mov    %ebp,%r15d
mov    $0x2,%r12d
lea    0xd22(%rip),%r13
mov    %eax,0xc(%rsp)
mov    %r15,0x10(%rsp)
jmp    1332 <main+0x132>
nopl   0x0(%rax)
mov    0x8(%rbx),%rsi
lea    0xe29(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    0xc(%rsp),%r14d
mov    0x10(%rsp),%r15
lea    0xcdb(%rip),%r13
add    $0x1,%r12
cmp    %r15,%r12
je     14c8 <main+0x2c8>
mov    (%rbx,%r12,8),%rbp
movsbl 0x0(%rbp),%eax
add    $0xffffffae,%eax
cmp    $0x25,%eax
ja     14eb <main+0x2eb>
movslq 0x0(%r13,%rax,4),%rax
add    %r13,%rax
jmp    *%rax
add    $0x1,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   10e0 <strtol@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   10f0 <malloc@plt>
test   %rax,%rax
je     14e2 <main+0x2e2>
mov    %rax,%r13
mov    %r14d,%edi
mov    %rax,%rsi
mov    %rbp,%rdx
call   10c0 <read@plt>
test   %eax,%eax
je     12f0 <main+0xf0>
cmp    $0xffffffff,%eax
je     14fa <main+0x2fa>
test   %eax,%eax
jle    1302 <main+0x102>
mov    %eax,%r15d
xor    %r14d,%r14d
jmp    13cd <main+0x1cd>
nopl   0x0(%rax)
lea    0xd7d(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1090 <printf@plt>
add    $0x1,%r14
cmp    %r14,%r15
je     1302 <main+0x102>
mov    (%rbx,%r12,8),%rax
cmpb   $0x72,(%rax)
movsbl 0x0(%r13,%r14,1),%ebp
jne    13b0 <main+0x1b0>
movzbl %bpl,%edi
call   1100 <isprint@plt>
test   %eax,%eax
mov    $0x3f,%eax
cmove  %eax,%ebp
mov    %ebp,%edi
call   1040 <putchar@plt>
jmp    13c0 <main+0x1c0>
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   10e0 <strtol@plt>
mov    %rax,%rbp
lea    0xd5c(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1090 <printf@plt>
mov    0x2b92(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
mov    %rbp,%rsi
call   10a0 <lseek@plt>
cmp    $0xffffffff,%eax
je     150a <main+0x30a>
mov    (%rbx,%r12,8),%rsi
lea    0xd39(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
jmp    1325 <main+0x125>
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbp
mov    %rbp,%rdi
call   1080 <strlen@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1070 <write@plt>
cmp    $0xffffffff,%eax
je     1524 <main+0x324>
mov    %eax,%ebp
mov    %r14d,%edi
call   1050 <fdatasync@plt>
cmp    $0xffffffff,%eax
jne    149d <main+0x29d>
lea    0xd46(%rip),%rdi
call   1060 <puts@plt>
mov    (%rbx,%r12,8),%rsi
lea    0xca6(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
mov    0x8(%rbx),%rdx
lea    0xca8(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1090 <printf@plt>
jmp    1325 <main+0x125>
mov    %r14d,%edi
call   10b0 <close@plt>
cmp    $0xffffffff,%eax
jne    123e <main+0x3e>
lea    0xcdf(%rip),%rdi
jmp    1511 <main+0x311>
lea    0xd09(%rip),%rdi
jmp    1511 <main+0x311>
lea    0xca9(%rip),%rdi
mov    %rbp,%rsi
jmp    12bf <main+0xbf>
mov    0x8(%rbx),%rsi
lea    0xc03(%rip),%rdi
jmp    12bf <main+0xbf>
lea    0xcbe(%rip),%rdi
call   1060 <puts@plt>
mov    0x2a8b(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>
mov    0x8(%rbx),%rsi
lea    0xc0b(%rip),%rdi
jmp    12bf <main+0xbf>

LLVM IR (-O0):
@.str = [7 x i8] c"--help\00"
@.str.1 = [60 x i8] c"Usage %s file {r<length>|R<length>|w<string>|<soffset>}...\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [18 x i8] c"Input file is %s\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@.str.3 = [27 x i8] c"Error opening the file %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [26 x i8] c"Memory allocation failed\0A\00"
@.str.5 = [28 x i8] c"Error reading from file %s\0A\00"
@.str.6 = [16 x i8] c"End of file %s\0A\00"
@.str.7 = [3 x i8] c"%c\00"
@.str.8 = [6 x i8] c"%02x \00"
@.str.9 = [2 x i8] c"\0A\00"
@.str.10 = [20 x i8] c"Cannot write to %s\0A\00"
@.str.11 = [21 x i8] c"sync to file failed\0A\00"
@.str.12 = [20 x i8] c"Write succeeded %s\0A\00"
@.str.13 = [23 x i8] c"Wrote %zd bytes to %s\0A\00"
@.str.14 = [15 x i8] c"offset is %zd\0A\00"
@SEEK_SET = global i32 0
@.str.15 = [16 x i8] c"could not seek\0A\00"
@.str.16 = [19 x i8] c"Seek succeeded %s\0A\00"
@.str.17 = [36 x i8] c"Arugment must start with rRws: %s \0A\00"
@.str.18 = [17 x i8] c"Cannot close fd\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 3
br i1 %15, label %22, label %16
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %16, %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 0
%25 = load i8*, i8** %24
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = load i32, i32* @EXIT_SUCCESS
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %16
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %32)
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
%37 = load i32, i32* @O_RDWR
%38 = load i32, i32* @O_CREAT
%39 = or i32 %37, %38
%40 = load i32, i32* @S_IRUSR
%41 = load i32, i32* @S_IWUSR
%42 = or i32 %40, %41
%43 = load i32, i32* @S_IRGRP
%44 = or i32 %42, %43
%45 = load i32, i32* @S_IWGRP
%46 = or i32 %44, %45
%47 = load i32, i32* @S_IROTH
%48 = or i32 %46, %47
%49 = load i32, i32* @S_IWOTH
%50 = or i32 %48, %49
%51 = call i32 @open(i8* %36, i32 %39, i32 %50)
store i32 %51, i32* %8
%52 = load i32, i32* %8
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %61
54: ; preds = %29
%55 = load i8**, i8*** %5
%56 = getelementptr inbounds i8*, i8** %55, i64 1
%57 = load i8*, i8** %56
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %57)
%59 = load i32, i32* @EXIT_FAILURE
%60 = call i32 @exit(i32 %59)
unreachable
61: ; preds = %29
store i32 2, i32* %9
br label %62
62: ; preds = %244, %61
%63 = load i32, i32* %9
%64 = load i32, i32* %4
%65 = icmp slt i32 %63, %64
br i1 %65, label %66, label %247
66: ; preds = %62
%67 = load i8**, i8*** %5
%68 = load i32, i32* %9
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds i8*, i8** %67, i64 %69
%71 = load i8*, i8** %70
%72 = getelementptr inbounds i8, i8* %71, i64 0
%73 = load i8, i8* %72
%74 = sext i8 %73 to i32
switch i32 %74, label %234 [
i32 114, label %75
i32 82, label %75
i32 119, label %164
i32 115, label %208
]
75: ; preds = %66, %66
%76 = load i8**, i8*** %5
%77 = load i32, i32* %9
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i8*, i8** %76, i64 %78
%80 = load i8*, i8** %79
%81 = getelementptr inbounds i8, i8* %80, i64 1
%82 = call i8* @strtol(i8* %81, i32* null, i32 10)
%83 = ptrtoint i8* %82 to i64
store i64 %83, i64* %6
%84 = load i64, i64* %6
%85 = call i8* @malloc(i64 %84)
store i8* %85, i8** %11
%86 = load i8*, i8** %11
%87 = icmp eq i8* %86, null
br i1 %87, label %88, label %92
88: ; preds = %75
%89 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%90 = load i32, i32* @EXIT_FAILURE
%91 = call i32 @exit(i32 %90)
unreachable
92: ; preds = %75
%93 = load i32, i32* %8
%94 = load i8*, i8** %11
%95 = load i64, i64* %6
%96 = call i32 @read(i32 %93, i8* %94, i64 %95)
store i32 %96, i32* %12
%97 = icmp eq i32 %96, -1
br i1 %97, label %98, label %105
98: ; preds = %92
%99 = load i8**, i8*** %5
%100 = getelementptr inbounds i8*, i8** %99, i64 1
%101 = load i8*, i8** %100
%102 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %101)
%103 = load i32, i32* @EXIT_FAILURE
%104 = call i32 @exit(i32 %103)
unreachable
105: ; preds = %92
%106 = load i32, i32* %12
%107 = icmp eq i32 %106, 0
br i1 %107, label %108, label %113
108: ; preds = %105
%109 = load i8**, i8*** %5
%110 = getelementptr inbounds i8*, i8** %109, i64 1
%111 = load i8*, i8** %110
%112 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i8* %111)
br label %160
113: ; preds = %105
store i32 0, i32* %10
br label %114
114: ; preds = %156, %113
%115 = load i32, i32* %10
%116 = load i32, i32* %12
%117 = icmp slt i32 %115, %116
br i1 %117, label %118, label %159
118: ; preds = %114
%119 = load i8**, i8*** %5
%120 = load i32, i32* %9
%121 = sext i32 %120 to i64
%122 = getelementptr inbounds i8*, i8** %119, i64 %121
%123 = load i8*, i8** %122
%124 = getelementptr inbounds i8, i8* %123, i64 0
%125 = load i8, i8* %124
%126 = sext i8 %125 to i32
%127 = icmp eq i32 %126, 114
br i1 %127, label %128, label %147
128: ; preds = %118
%129 = load i8*, i8** %11
%130 = load i32, i32* %10
%131 = sext i32 %130 to i64
%132 = getelementptr inbounds i8, i8* %129, i64 %131
%133 = load i8, i8* %132
%134 = call i32 @isprint(i8 zeroext %133)
%135 = icmp ne i32 %134, 0
br i1 %135, label %136, label %143
136: ; preds = %128
%137 = load i8*, i8** %11
%138 = load i32, i32* %10
%139 = sext i32 %138 to i64
%140 = getelementptr inbounds i8, i8* %137, i64 %139
%141 = load i8, i8* %140
%142 = sext i8 %141 to i32
br label %144
143: ; preds = %128
br label %144
144: ; preds = %143, %136
%145 = phi i32 [ %142, %136 ], [ 63, %143 ]
%146 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i32 %145)
br label %155
147: ; preds = %118
%148 = load i8*, i8** %11
%149 = load i32, i32* %10
%150 = sext i32 %149 to i64
%151 = getelementptr inbounds i8, i8* %148, i64 %150
%152 = load i8, i8* %151
%153 = sext i8 %152 to i32
%154 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0), i32 %153)
br label %155
155: ; preds = %147, %144
br label %156
156: ; preds = %155
%157 = load i32, i32* %10
%158 = add nsw i32 %157, 1
store i32 %158, i32* %10
br label %114
159: ; preds = %114
br label %160
160: ; preds = %159, %108
%161 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
%162 = load i8*, i8** %11
%163 = call i32 @free(i8* %162)
br label %243
164: ; preds = %66
%165 = load i32, i32* %8
%166 = load i8**, i8*** %5
%167 = load i32, i32* %9
%168 = sext i32 %167 to i64
%169 = getelementptr inbounds i8*, i8** %166, i64 %168
%170 = load i8*, i8** %169
%171 = getelementptr inbounds i8, i8* %170, i64 1
%172 = load i8**, i8*** %5
%173 = load i32, i32* %9
%174 = sext i32 %173 to i64
%175 = getelementptr inbounds i8*, i8** %172, i64 %174
%176 = load i8*, i8** %175
%177 = getelementptr inbounds i8, i8* %176, i64 1
%178 = call i32 @strlen(i8* %177)
%179 = call i32 @write(i32 %165, i8* %171, i32 %178)
store i32 %179, i32* %13
%180 = load i32, i32* %13
%181 = icmp eq i32 %180, -1
br i1 %181, label %182, label %189
182: ; preds = %164
%183 = load i8**, i8*** %5
%184 = getelementptr inbounds i8*, i8** %183, i64 1
%185 = load i8*, i8** %184
%186 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.10, i64 0, i64 0), i8* %185)
%187 = load i32, i32* @EXIT_FAILURE
%188 = call i32 @exit(i32 %187)
unreachable
189: ; preds = %164
%190 = load i32, i32* %8
%191 = call i32 @fdatasync(i32 %190)
%192 = icmp eq i32 %191, -1
br i1 %192, label %193, label %195
193: ; preds = %189
%194 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.11, i64 0, i64 0))
br label %195
195: ; preds = %193, %189
%196 = load i8**, i8*** %5
%197 = load i32, i32* %9
%198 = sext i32 %197 to i64
%199 = getelementptr inbounds i8*, i8** %196, i64 %198
%200 = load i8*, i8** %199
%201 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.12, i64 0, i64 0), i8* %200)
%202 = load i32, i32* %13
%203 = load i8**, i8*** %5
%204 = getelementptr inbounds i8*, i8** %203, i64 1
%205 = load i8*, i8** %204
%206 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.13, i64 0, i64 0), i32 %202, i8* %205)
br label %207
207: ; preds = %195
br label %243
208: ; preds = %66
%209 = load i8**, i8*** %5
%210 = load i32, i32* %9
%211 = sext i32 %210 to i64
%212 = getelementptr inbounds i8*, i8** %209, i64 %211
%213 = load i8*, i8** %212
%214 = getelementptr inbounds i8, i8* %213, i64 1
%215 = call i8* @strtol(i8* %214, i32* null, i32 10)
store i8* %215, i8** %7
%216 = load i8*, i8** %7
%217 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.14, i64 0, i64 0), i8* %216)
%218 = load i32, i32* %8
%219 = load i8*, i8** %7
%220 = load i32, i32* @SEEK_SET
%221 = call i32 @lseek(i32 %218, i8* %219, i32 %220)
%222 = icmp eq i32 %221, -1
br i1 %222, label %223, label %227
223: ; preds = %208
%224 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.15, i64 0, i64 0))
%225 = load i32, i32* @EXIT_FAILURE
%226 = call i32 @exit(i32 %225)
unreachable
227: ; preds = %208
%228 = load i8**, i8*** %5
%229 = load i32, i32* %9
%230 = sext i32 %229 to i64
%231 = getelementptr inbounds i8*, i8** %228, i64 %230
%232 = load i8*, i8** %231
%233 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.16, i64 0, i64 0), i8* %232)
br label %243
234: ; preds = %66
%235 = load i8**, i8*** %5
%236 = load i32, i32* %9
%237 = sext i32 %236 to i64
%238 = getelementptr inbounds i8*, i8** %235, i64 %237
%239 = load i8*, i8** %238
%240 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.17, i64 0, i64 0), i8* %239)
%241 = load i32, i32* @EXIT_FAILURE
%242 = call i32 @exit(i32 %241)
unreachable
243: ; preds = %227, %207, %160
br label %244
244: ; preds = %243
%245 = load i32, i32* %9
%246 = add nsw i32 %245, 1
store i32 %246, i32* %9
br label %62
247: ; preds = %62
%248 = load i32, i32* %8
%249 = call i32 @close(i32 %248)
%250 = icmp eq i32 %249, -1
br i1 %250, label %251, label %255
251: ; preds = %247
%252 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.18, i64 0, i64 0))
%253 = load i32, i32* @EXIT_FAILURE
%254 = call i32 @exit(i32 %253)
unreachable
255: ; preds = %247
%256 = load i32, i32* @EXIT_SUCCESS
%257 = call i32 @exit(i32 %256)
unreachable
}

LLVM IR (-O2):
@.str = [7 x i8] c"--help\00"
@.str.1 = [60 x i8] c"Usage %s file {r<length>|R<length>|w<string>|<soffset>}...\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [18 x i8] c"Input file is %s\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@.str.3 = [27 x i8] c"Error opening the file %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.5 = [28 x i8] c"Error reading from file %s\0A\00"
@.str.6 = [16 x i8] c"End of file %s\0A\00"
@.str.8 = [6 x i8] c"%02x \00"
@.str.10 = [20 x i8] c"Cannot write to %s\0A\00"
@.str.12 = [20 x i8] c"Write succeeded %s\0A\00"
@.str.13 = [23 x i8] c"Wrote %zd bytes to %s\0A\00"
@.str.14 = [15 x i8] c"offset is %zd\0A\00"
@SEEK_SET = global i32 0
@.str.16 = [19 x i8] c"Seek succeeded %s\0A\00"
@.str.17 = [36 x i8] c"Arugment must start with rRws: %s \0A\00"
@str = [16 x i8] c"Cannot close fd\00"
@str.19 = [15 x i8] c"could not seek\00"
@str.20 = [20 x i8] c"sync to file failed\00"
@str.21 = [25 x i8] c"Memory allocation failed\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %9, label %4
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i64 @strcmp(i8* %6, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %4, %2
%10 = load i8*, i8** %1
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([60 x i8], [60 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @EXIT_SUCCESS
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %4
%15 = load i8*, i8** %5
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %15)
%17 = load i8*, i8** %5
%18 = load i32, i32* @O_RDWR
%19 = load i32, i32* @O_CREAT
%20 = or i32 %19, %18
%21 = load i32, i32* @S_IRUSR
%22 = load i32, i32* @S_IWUSR
%23 = or i32 %22, %21
%24 = load i32, i32* @S_IRGRP
%25 = or i32 %23, %24
%26 = load i32, i32* @S_IWGRP
%27 = or i32 %25, %26
%28 = load i32, i32* @S_IROTH
%29 = or i32 %27, %28
%30 = load i32, i32* @S_IWOTH
%31 = or i32 %29, %30
%32 = tail call i32 @open(i8* %17, i32 %20, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %36, label %34
34: ; preds = %14
%35 = zext i32 %0 to i64
br label %41
36: ; preds = %14
%37 = load i8*, i8** %5
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %37)
%39 = load i32, i32* @EXIT_FAILURE
%40 = tail call i32 @exit(i32 %39)
unreachable
41: ; preds = %34, %131
%42 = phi i64 [ 2, %34 ], [ %132, %131 ]
%43 = getelementptr inbounds i8*, i8** %1, i64 %42
%44 = load i8*, i8** %43
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
switch i32 %46, label %127 [
i32 114, label %47
i32 82, label %47
i32 119, label %93
i32 115, label %113
]
47: ; preds = %41, %41
%48 = getelementptr inbounds i8, i8* %44, i64 1
%49 = tail call i8* @strtol(i8* nocapture %48, i32* null, i32 10)
%50 = ptrtoint i8* %49 to i64
%51 = tail call i8* @malloc(i64 %50)
%52 = icmp eq i8* %51, null
br i1 %52, label %53, label %57
53: ; preds = %47
%54 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.21, i64 0, i64 0))
%55 = load i32, i32* @EXIT_FAILURE
%56 = tail call i32 @exit(i32 %55)
unreachable
57: ; preds = %47
%58 = tail call i32 @read(i32 %32, i8* %51, i64 %50)
switch i32 %58, label %59 [
i32 -1, label %63
i32 0, label %68
]
59: ; preds = %57
%60 = icmp sgt i32 %58, 0
br i1 %60, label %61, label %90
61: ; preds = %59
%62 = zext i32 %58 to i64
br label %71
63: ; preds = %57
%64 = load i8*, i8** %5
%65 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %64)
%66 = load i32, i32* @EXIT_FAILURE
%67 = tail call i32 @exit(i32 %66)
unreachable
68: ; preds = %57
%69 = load i8*, i8** %5
%70 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i8* %69)
br label %90
71: ; preds = %61, %87
%72 = phi i64 [ 0, %61 ], [ %88, %87 ]
%73 = load i8*, i8** %43
%74 = load i8, i8* %73
%75 = icmp eq i8 %74, 114
%76 = getelementptr inbounds i8, i8* %51, i64 %72
%77 = load i8, i8* %76
br i1 %75, label %78, label %84
78: ; preds = %71
%79 = tail call i32 @isprint(i8 zeroext %77)
%80 = icmp eq i32 %79, 0
%81 = sext i8 %77 to i32
%82 = select i1 %80, i32 63, i32 %81
%83 = tail call i32 @putchar(i32 %82)
br label %87
84: ; preds = %71
%85 = sext i8 %77 to i32
%86 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0), i32 %85)
br label %87
87: ; preds = %78, %84
%88 = add nuw nsw i64 %72, 1
%89 = icmp eq i64 %88, %62
br i1 %89, label %90, label %71
90: ; preds = %87, %59, %68
%91 = tail call i32 @putchar(i32 10)
%92 = tail call i32 @free(i8* %51)
br label %131
93: ; preds = %41
%94 = getelementptr inbounds i8, i8* %44, i64 1
%95 = tail call i32 @strlen(i8* %94)
%96 = tail call i32 @write(i32 %32, i8* %94, i32 %95)
%97 = icmp eq i32 %96, -1
br i1 %97, label %98, label %103
98: ; preds = %93
%99 = load i8*, i8** %5
%100 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.10, i64 0, i64 0), i8* %99)
%101 = load i32, i32* @EXIT_FAILURE
%102 = tail call i32 @exit(i32 %101)
unreachable
103: ; preds = %93
%104 = tail call i32 @fdatasync(i32 %32)
%105 = icmp eq i32 %104, -1
br i1 %105, label %106, label %108
106: ; preds = %103
%107 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.20, i64 0, i64 0))
br label %108
108: ; preds = %106, %103
%109 = load i8*, i8** %43
%110 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.12, i64 0, i64 0), i8* %109)
%111 = load i8*, i8** %5
%112 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.13, i64 0, i64 0), i32 %96, i8* %111)
br label %131
113: ; preds = %41
%114 = getelementptr inbounds i8, i8* %44, i64 1
%115 = tail call i8* @strtol(i8* nocapture %114, i32* null, i32 10)
%116 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.14, i64 0, i64 0), i8* %115)
%117 = load i32, i32* @SEEK_SET
%118 = tail call i32 @lseek(i32 %32, i8* %115, i32 %117)
%119 = icmp eq i32 %118, -1
br i1 %119, label %120, label %124
120: ; preds = %113
%121 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.19, i64 0, i64 0))
%122 = load i32, i32* @EXIT_FAILURE
%123 = tail call i32 @exit(i32 %122)
unreachable
124: ; preds = %113
%125 = load i8*, i8** %43
%126 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.16, i64 0, i64 0), i8* %125)
br label %131
127: ; preds = %41
%128 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.17, i64 0, i64 0), i8* %44)
%129 = load i32, i32* @EXIT_FAILURE
%130 = tail call i32 @exit(i32 %129)
unreachable
131: ; preds = %90, %108, %124
%132 = add nuw nsw i64 %42, 1
%133 = icmp eq i64 %132, %35
br i1 %133, label %134, label %41
134: ; preds = %131
%135 = tail call i32 @close(i32 %32)
%136 = icmp eq i32 %135, -1
br i1 %136, label %137, label %141
137: ; preds = %134
%138 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%139 = load i32, i32* @EXIT_FAILURE
%140 = tail call i32 @exit(i32 %139)
unreachable
141: ; preds = %134
%142 = load i32, i32* @EXIT_SUCCESS
%143 = tail call i32 @exit(i32 %142)
unreachable
}