#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*/ oldname;
typedef int off_t;
typedef int /*<<< orphan*/ newname;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int BUFSIZ;
int O_APPEND;
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ SEEK_END;
int STDIN_FILENO;
int STDOUT_FILENO;
void *atoi(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fpritnf(int /*<<< orphan*/, char *, size_t, int);
int /*<<< orphan*/ fsync(int);
int getopt(int, char **, char *);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
void *malloc(size_t);
int /*<<< orphan*/ memmove(char *, char *, size_t);
int open(char *, int, int);
int /*<<< orphan*/ optarg;
int optind;
size_t read(int, char *, int);
int /*<<< orphan*/ rename(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ xwrite(int *, char *, size_t);
int main(int argc, char *argv[]) {
  int infd = STDIN_FILENO;
  int stdfd = STDOUT_FILENO;
  int outfd = -1;
  void *rotbuf = NULL;
  size_t logsz = 0;
  int c;
  int rotate = 2;
  size_t maxlogsz = 1024;
  char *logname = NULL;
  while ((c = getopt(argc, argv, "r:n:")) != -1) {
    switch (c) {
    case 'n':
      maxlogsz = atoi(optarg);
      break;
    case 'r':
      rotate = atoi(optarg);
      break;
    }
  }
  if (optind != argc - 1) {
    fprintf(stderr, "log name expected\n");
    return -1;
  }
  logname = argv[optind];
  rotbuf = malloc(maxlogsz);
  if (rotbuf == NULL) {
    fprintf(stderr, "malloc() failed\n");
    return -1;
  }
  outfd = open(logname, O_CREAT | O_RDWR | O_APPEND, 0644);
  if (outfd == -1) {
    fprintf(stderr, "open() failed: file=%s, errno=%d\n", logname, errno);
  }
  off_t offset = lseek(outfd, 0, SEEK_END);
  if (offset == -1) {
    fprintf(stderr, "lseek() failed: errno=%d\n", errno);
    close(outfd);
    outfd = -1;
  }
  logsz = (size_t)offset;
  for (;;) {
    char buf[BUFSIZ];
    size_t sz;
    sz = read(infd, buf, sizeof(buf));
    if (sz <= 0) {
      if (sz < 0) {
        fpritnf(stderr, "read failed: %d, errno=%d\n", sz, errno);
      }
      break;
    }
    while (logsz + sz > maxlogsz) {
      int i;
      size_t tailsz;
      char oldname[BUFSIZ];
      char newname[BUFSIZ];
      tailsz = logsz + sz - maxlogsz;
      xwrite(&outfd, buf, sz - tailsz);
      memmove(buf, buf + sz - tailsz, tailsz);
      sz = tailsz;
      close(outfd);
      for (i = rotate - 1; i >= 0; i--) {
        snprintf(newname, sizeof(newname) - 1, "%s.%u", logname, i + 1);
        if (i > 0) {
          snprintf(oldname, sizeof(oldname) - 1, "%s.%u", logname, i);
        } else {
          snprintf(oldname, sizeof(oldname) - 1, "%s", logname);
        }
        unlink(newname);
        rename(oldname, newname);
      }
      outfd = open(logname, O_CREAT | O_RDWR | O_TRUNC, 0644);
      if (outfd == -1) {
        fprintf(stderr, "open() failed: file=%s, errno=%d\n", logname, errno);
      }
      logsz = 0;
    }
    xwrite(&stdfd, buf, sz);
    xwrite(&outfd, buf, sz);
    fsync(outfd);
  }
  close(infd);
  close(outfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2d80(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x2,-0x38(%rbp)
movq   $0x400,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xda3(%rip),%rdx
call   10f0 <getopt@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
je     12c6 <main+0xd6>
mov    -0x34(%rbp),%eax
mov    %eax,-0x94(%rbp)
sub    $0x6e,%eax
je     1299 <main+0xa9>
jmp    1285 <main+0x95>
mov    -0x94(%rbp),%eax
sub    $0x72,%eax
je     12b0 <main+0xc0>
jmp    12c1 <main+0xd1>
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
call   1100 <atoi@plt>
mov    %rax,-0x40(%rbp)
jmp    12c1 <main+0xd1>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
call   1100 <atoi@plt>
mov    %eax,-0x38(%rbp)
jmp    124f <main+0x5f>
mov    0x2ceb(%rip),%rax
mov    (%rax),%eax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
je     1300 <main+0x110>
mov    0x2cc4(%rip),%rax
mov    (%rax),%edi
lea    0xd18(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1700 <main+0x510>
mov    -0x10(%rbp),%rax
mov    0x2cad(%rip),%rcx
movslq (%rcx),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1351 <main+0x161>
mov    0x2c73(%rip),%rax
mov    (%rax),%edi
lea    0xcda(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1700 <main+0x510>
mov    -0x48(%rbp),%rdi
mov    0x2c9c(%rip),%rax
mov    (%rax),%esi
mov    0x2c63(%rip),%rax
or     (%rax),%esi
mov    0x2c12(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   10d0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    13ab <main+0x1bb>
mov    0x2c1a(%rip),%rax
mov    (%rax),%edi
mov    -0x48(%rbp),%rdx
mov    0x2bfd(%rip),%rax
mov    (%rax),%ecx
lea    0xc85(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x1c(%rbp),%edi
mov    0x2c0b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0xffffffff,-0x4c(%rbp)
jne    13fa <main+0x20a>
mov    0x2bd6(%rip),%rax
mov    (%rax),%edi
mov    0x2bbd(%rip),%rax
mov    (%rax),%edx
lea    0xc67(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x1c(%rbp)
movslq -0x4c(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2b97(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0xa0(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x60(%rbp)
mov    -0x14(%rbp),%edi
mov    %eax,%edx
call   1080 <read@plt>
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
ja     1483 <main+0x293>
cmpq   $0x0,-0x68(%rbp)
jae    1477 <main+0x287>
mov    0x2b4c(%rip),%rax
mov    (%rax),%edi
mov    -0x68(%rbp),%rdx
mov    0x2b2f(%rip),%rax
mov    (%rax),%ecx
lea    0xbf3(%rip),%rsi
call   1110 <fpritnf@plt>
movl   $0x5,-0x6c(%rbp)
jmp    16c8 <main+0x4d8>
jmp    1488 <main+0x298>
mov    -0x30(%rbp),%rax
add    -0x68(%rbp),%rax
cmp    -0x40(%rbp),%rax
jbe    1691 <main+0x4a1>
mov    -0xa0(%rbp),%rsi
mov    0x2af8(%rip),%rax
mov    (%rax),%ecx
mov    %rcx,-0xc0(%rbp)
mov    %rsp,%rdx
mov    %rdx,-0x80(%rbp)
mov    %rcx,%rdi
add    $0xf,%rdi
and    $0xfffffffffffffff0,%rdi
mov    %rsp,%rdx
sub    %rdi,%rdx
mov    %rdx,-0xb8(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x88(%rbp)
mov    (%rax),%eax
mov    %rax,-0xb0(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0xa8(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x90(%rbp)
mov    -0x30(%rbp),%rax
add    -0x68(%rbp),%rax
sub    -0x40(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x68(%rbp),%rdx
sub    -0x78(%rbp),%rdx
lea    -0x1c(%rbp),%rdi
call   1040 <xwrite@plt>
mov    -0xa0(%rbp),%rdi
mov    %rdi,%rsi
add    -0x68(%rbp),%rsi
xor    %eax,%eax
sub    -0x78(%rbp),%rax
add    %rax,%rsi
mov    -0x78(%rbp),%rdx
call   10b0 <memmove@plt>
mov    -0x78(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x38(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x70(%rbp)
cmpl   $0x0,-0x70(%rbp)
jl     1623 <main+0x433>
mov    -0xa8(%rbp),%rdi
mov    -0xb0(%rbp),%rax
sub    $0x1,%rax
mov    %eax,%esi
mov    -0x48(%rbp),%rcx
mov    -0x70(%rbp),%r8d
add    $0x1,%r8d
lea    0xaf0(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
cmpl   $0x0,-0x70(%rbp)
jle    15d0 <main+0x3e0>
mov    -0xb8(%rbp),%rdi
mov    -0xc0(%rbp),%rax
sub    $0x1,%rax
mov    %eax,%esi
mov    -0x48(%rbp),%rcx
mov    -0x70(%rbp),%r8d
lea    0xabc(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
jmp    15f6 <main+0x406>
mov    -0xb8(%rbp),%rdi
mov    -0xc0(%rbp),%rax
sub    $0x1,%rax
mov    %eax,%esi
mov    -0x48(%rbp),%rcx
lea    0xa97(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    -0xa8(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0xb8(%rbp),%rdi
mov    -0xa8(%rbp),%rsi
call   10e0 <rename@plt>
mov    -0x70(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x70(%rbp)
jmp    155f <main+0x36f>
mov    -0x48(%rbp),%rdi
mov    0x29ca(%rip),%rax
mov    (%rax),%esi
mov    0x2991(%rip),%rax
or     (%rax),%esi
mov    0x29a8(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   10d0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    167d <main+0x48d>
mov    0x2948(%rip),%rax
mov    (%rax),%edi
mov    -0x48(%rbp),%rdx
mov    0x292b(%rip),%rax
mov    (%rax),%ecx
lea    0x9b3(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movq   $0x0,-0x30(%rbp)
mov    -0x80(%rbp),%rax
mov    %rax,%rsp
jmp    1488 <main+0x298>
mov    -0xa0(%rbp),%rsi
mov    -0x68(%rbp),%rdx
lea    -0x18(%rbp),%rdi
call   1040 <xwrite@plt>
mov    -0xa0(%rbp),%rsi
mov    -0x68(%rbp),%rdx
lea    -0x1c(%rbp),%rdi
call   1040 <xwrite@plt>
mov    -0x1c(%rbp),%edi
call   10c0 <fsync@plt>
movl   $0x0,-0x6c(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,%rsp
mov    -0x6c(%rbp),%eax
test   %eax,%eax
je     16e4 <main+0x4f4>
jmp    16df <main+0x4ef>
jmp    16e9 <main+0x4f9>
jmp    1402 <main+0x212>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
mov    0x2da3(%rip),%rax
mov    (%rax),%r13d
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    $0x2,%eax
mov    %rax,-0x58(%rbp)
mov    $0x400,%eax
mov    %rax,-0x50(%rbp)
lea    0xdeb(%rip),%r15
mov    0x2dd4(%rip),%r12
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10e0 <getopt@plt>
cmp    $0xffffffff,%eax
je     125a <main+0x8a>
cmp    $0x72,%eax
je     124b <main+0x7b>
cmp    $0x6e,%eax
jne    1220 <main+0x50>
mov    (%r12),%edi
call   10f0 <atoi@plt>
mov    %rax,-0x50(%rbp)
jmp    1220 <main+0x50>
mov    (%r12),%edi
call   10f0 <atoi@plt>
mov    %rax,-0x58(%rbp)
jmp    1220 <main+0x50>
mov    0x2d57(%rip),%rax
movslq (%rax),%rax
add    $0xffffffff,%ebx
cmp    %ebx,%eax
jne    15d8 <main+0x408>
mov    0x2d82(%rip),%rcx
mov    0x2d4b(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    (%r14,%rax,8),%r12
mov    0x2cf4(%rip),%rax
or     (%rax),%esi
mov    %r12,%rdi
mov    $0x1a4,%edx
call   10c0 <open@plt>
mov    %eax,%r14d
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    12c9 <main+0xf9>
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
mov    0x2ce2(%rip),%rax
mov    (%rax),%ecx
lea    0xd59(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    0x2cf0(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    1313 <main+0x143>
mov    0x2cbd(%rip),%rax
mov    (%rax),%edi
mov    0x2ca4(%rip),%rax
mov    (%rax),%edx
lea    0xd3d(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    %r14d,%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x2c(%rbp)
mov    %rsp,-0x40(%rbp)
mov    0x2c82(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rsi
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rsi
mov    %rsi,%rsp
mov    %r13d,%edi
mov    %rsi,-0x60(%rbp)
call   1080 <read@plt>
test   %rax,%rax
je     15f6 <main+0x426>
mov    %rax,%rcx
movslq %r15d,%rax
mov    %rax,-0x38(%rbp)
mov    -0x58(%rbp),%rax
add    $0xffffffff,%eax
mov    %eax,-0x68(%rbp)
mov    -0x50(%rbp),%r15
mov    -0x40(%rbp),%r14
mov    %rcx,%rax
mov    %r13d,-0x64(%rbp)
mov    %r12,-0x80(%rbp)
jmp    13f0 <main+0x220>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
mov    %rax,-0x38(%rbp)
mov    -0x40(%rbp),%r14
lea    -0x6c(%rbp),%rdi
mov    -0x60(%rbp),%r13
mov    %r13,%rsi
mov    %rbx,%rdx
call   1040 <xwrite@plt>
lea    -0x2c(%rbp),%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1040 <xwrite@plt>
mov    -0x2c(%rbp),%edi
call   10b0 <fsync@plt>
mov    %r14,%rsp
mov    %rsp,%r14
mov    0x2bdf(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rsi
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rsi
mov    %rsi,%rsp
mov    -0x64(%rbp),%r13d
mov    %r13d,%edi
mov    %rsi,-0x60(%rbp)
call   1080 <read@plt>
test   %rax,%rax
je     15fa <main+0x42a>
mov    -0x38(%rbp),%rcx
add    %rax,%rcx
cmp    %r15,%rcx
jbe    15d0 <main+0x400>
mov    %rcx,%rbx
mov    %r14,-0x40(%rbp)
mov    %rax,%r13
jmp    1466 <main+0x296>
nopl   0x0(%rax)
mov    0x2bb1(%rip),%rax
mov    (%rax),%esi
mov    0x2bd8(%rip),%rax
or     (%rax),%esi
mov    0x2bbf(%rip),%rax
or     (%rax),%esi
mov    -0x80(%rbp),%r12
mov    %r12,%rdi
mov    $0x1a4,%edx
call   10c0 <open@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
je     15a0 <main+0x3d0>
mov    -0x88(%rbp),%rsp
mov    -0x90(%rbp),%rbx
mov    %rbx,%r13
mov    -0x50(%rbp),%r15
cmp    %r15,%rbx
jbe    1380 <main+0x1b0>
mov    %rsp,-0x88(%rbp)
mov    0x2b2c(%rip),%rcx
mov    (%rcx),%eax
mov    %rsp,%rdx
mov    %rax,-0x48(%rbp)
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rdx
mov    %rdx,-0x78(%rbp)
mov    %rdx,%rsp
mov    (%rcx),%eax
mov    %rsp,%r14
mov    %rax,-0x38(%rbp)
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
sub    %r15,%rbx
mov    %r13,%rdx
sub    %rbx,%rdx
lea    -0x2c(%rbp),%rdi
mov    -0x60(%rbp),%r15
mov    %r15,%rsi
call   1040 <xwrite@plt>
add    %r15,%r13
sub    %rbx,%r13
mov    %r15,%rdi
mov    %r13,%rsi
mov    %rbx,-0x90(%rbp)
mov    %rbx,%rdx
call   10a0 <memmove@plt>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
cmpl   $0x0,-0x58(%rbp)
lea    0xb67(%rip),%r13
jle    1410 <main+0x240>
mov    -0x38(%rbp),%rax
add    $0xffffffff,%eax
mov    %rax,-0x38(%rbp)
mov    -0x48(%rbp),%rax
add    $0xffffffff,%eax
mov    %rax,-0x48(%rbp)
mov    -0x68(%rbp),%eax
mov    %eax,%ebx
mov    %eax,%r15d
jmp    155f <main+0x38f>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    -0x78(%rbp),%rbx
mov    %rbx,%rdi
mov    -0x48(%rbp),%rsi
lea    0xb28(%rip),%rdx
mov    %r12,%rcx
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %r14,%rdi
call   1030 <unlink@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   10d0 <rename@plt>
lea    0x2(%r15),%eax
mov    %r15d,%ebx
cmp    $0x1,%eax
jbe    1410 <main+0x240>
lea    0x1(%rbx),%r8d
mov    %r14,%rdi
mov    -0x38(%rbp),%rsi
mov    %r13,%rdx
mov    -0x80(%rbp),%r12
mov    %r12,%rcx
xor    %eax,%eax
call   1050 <snprintf@plt>
sub    $0x1,%r15d
jb     1520 <main+0x350>
mov    -0x78(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    %r13,%rdx
mov    %r12,%rcx
mov    %ebx,%r8d
mov    %rdi,%rbx
xor    %eax,%eax
call   1050 <snprintf@plt>
jmp    153c <main+0x36c>
xchg   %ax,%ax
mov    0x2a01(%rip),%rax
mov    (%rax),%edi
mov    0x29e8(%rip),%rax
mov    (%rax),%ecx
lea    0xa5f(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    1448 <main+0x278>
nopl   0x0(%rax,%rax,1)
mov    %rax,%rbx
jmp    138a <main+0x1ba>
mov    0x29c9(%rip),%rax
mov    (%rax),%edi
lea    0xa1d(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    160f <main+0x43f>
mov    -0x40(%rbp),%r14
mov    %r14,%rsp
mov    %r13d,%edi
call   1070 <close@plt>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@.str = [5 x i8] c"r:n:\00"
@optarg = global i32 0
@optind = global i32 0
@stderr = global i32 0
@.str.1 = [19 x i8] c"log name expected\0A\00"
@.str.2 = [17 x i8] c"malloc() failed\0A\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@.str.3 = [34 x i8] c"open() failed: file=%s, errno=%d\0A\00"
@errno = global i32 0
@SEEK_END = global i32 0
@.str.4 = [26 x i8] c"lseek() failed: errno=%d\0A\00"
@BUFSIZ = global i32 0
@.str.5 = [27 x i8] c"read failed: %d, errno=%d\0A\00"
@.str.6 = [6 x i8] c"%s.%u\00"
@.str.7 = [3 x i8] c"%s\00"
@O_TRUNC = global i32 0
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 i64
%11 = alloca i32
%12 = alloca i32
%13 = alloca i64
%14 = alloca i8*
%15 = alloca i32
%16 = alloca i8*
%17 = alloca i64
%18 = alloca i64
%19 = alloca i32
%20 = alloca i32
%21 = alloca i64
%22 = alloca i8*
%23 = alloca i64
%24 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%25 = load i32, i32* @STDIN_FILENO
store i32 %25, i32* %6
%26 = load i32, i32* @STDOUT_FILENO
store i32 %26, i32* %7
store i32 -1, i32* %8
store i8* null, i8** %9
store i64 0, i64* %10
store i32 2, i32* %12
store i64 1024, i64* %13
store i8* null, i8** %14
br label %27
27: ; preds = %42, %2
%28 = load i32, i32* %4
%29 = load i8**, i8*** %5
%30 = call i32 @getopt(i32 %28, i8** %29, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 %30, i32* %11
%31 = icmp ne i32 %30, -1
br i1 %31, label %32, label %43
32: ; preds = %27
%33 = load i32, i32* %11
switch i32 %33, label %42 [
i32 110, label %34
i32 114, label %38
]
34: ; preds = %32
%35 = load i32, i32* @optarg
%36 = call i8* @atoi(i32 %35)
%37 = ptrtoint i8* %36 to i64
store i64 %37, i64* %13
br label %42
38: ; preds = %32
%39 = load i32, i32* @optarg
%40 = call i8* @atoi(i32 %39)
%41 = ptrtoint i8* %40 to i32
store i32 %41, i32* %12
br label %42
42: ; preds = %32, %38, %34
br label %27
43: ; preds = %27
%44 = load i32, i32* @optind
%45 = load i32, i32* %4
%46 = sub nsw i32 %45, 1
%47 = icmp ne i32 %44, %46
br i1 %47, label %48, label %51
48: ; preds = %43
%49 = load i32, i32* @stderr
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %211
51: ; preds = %43
%52 = load i8**, i8*** %5
%53 = load i32, i32* @optind
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8*, i8** %52, i64 %54
%56 = load i8*, i8** %55
store i8* %56, i8** %14
%57 = load i64, i64* %13
%58 = call i8* @malloc(i64 %57)
store i8* %58, i8** %9
%59 = load i8*, i8** %9
%60 = icmp eq i8* %59, null
br i1 %60, label %61, label %64
61: ; preds = %51
%62 = load i32, i32* @stderr
%63 = call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %211
64: ; preds = %51
%65 = load i8*, i8** %14
%66 = load i32, i32* @O_CREAT
%67 = load i32, i32* @O_RDWR
%68 = or i32 %66, %67
%69 = load i32, i32* @O_APPEND
%70 = or i32 %68, %69
%71 = call i32 @open(i8* %65, i32 %70, i32 420)
store i32 %71, i32* %8
%72 = load i32, i32* %8
%73 = icmp eq i32 %72, -1
br i1 %73, label %74, label %79
74: ; preds = %64
%75 = load i32, i32* @stderr
%76 = load i8*, i8** %14
%77 = load i32, i32* @errno
%78 = call i32 (i32, i8*, ...) @fprintf(i32 %75, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %76, i32 %77)
br label %79
79: ; preds = %74, %64
%80 = load i32, i32* %8
%81 = load i32, i32* @SEEK_END
%82 = call i32 @lseek(i32 %80, i32 0, i32 %81)
store i32 %82, i32* %15
%83 = load i32, i32* %15
%84 = icmp eq i32 %83, -1
br i1 %84, label %85, label %91
85: ; preds = %79
%86 = load i32, i32* @stderr
%87 = load i32, i32* @errno
%88 = call i32 (i32, i8*, ...) @fprintf(i32 %86, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %87)
%89 = load i32, i32* %8
%90 = call i32 @close(i32 %89)
store i32 -1, i32* %8
br label %91
91: ; preds = %85, %79
%92 = load i32, i32* %15
%93 = sext i32 %92 to i64
store i64 %93, i64* %10
br label %94
94: ; preds = %205, %91
%95 = load i32, i32* @BUFSIZ
%96 = zext i32 %95 to i64
%97 = call i8* @llvm.stacksave()
store i8* %97, i8** %16
%98 = alloca i8, i64 %96
store i64 %96, i64* %17
%99 = load i32, i32* %6
%100 = trunc i64 %96 to i32
%101 = call i64 @read(i32 %99, i8* %98, i32 %100)
store i64 %101, i64* %18
%102 = load i64, i64* %18
%103 = icmp ule i64 %102, 0
br i1 %103, label %104, label %113
104: ; preds = %94
%105 = load i64, i64* %18
%106 = icmp ult i64 %105, 0
br i1 %106, label %107, label %112
107: ; preds = %104
%108 = load i32, i32* @stderr
%109 = load i64, i64* %18
%110 = load i32, i32* @errno
%111 = call i32 @fpritnf(i32 %108, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i64 %109, i32 %110)
br label %112
112: ; preds = %107, %104
store i32 5, i32* %19
br label %202
113: ; preds = %94
br label %114
114: ; preds = %193, %113
%115 = load i64, i64* %10
%116 = load i64, i64* %18
%117 = add i64 %115, %116
%118 = load i64, i64* %13
%119 = icmp ugt i64 %117, %118
br i1 %119, label %120, label %195
120: ; preds = %114
%121 = load i32, i32* @BUFSIZ
%122 = zext i32 %121 to i64
%123 = call i8* @llvm.stacksave()
store i8* %123, i8** %22
%124 = alloca i8, i64 %122
store i64 %122, i64* %23
%125 = load i32, i32* @BUFSIZ
%126 = zext i32 %125 to i64
%127 = alloca i8, i64 %126
store i64 %126, i64* %24
%128 = load i64, i64* %10
%129 = load i64, i64* %18
%130 = add i64 %128, %129
%131 = load i64, i64* %13
%132 = sub i64 %130, %131
store i64 %132, i64* %21
%133 = load i64, i64* %18
%134 = load i64, i64* %21
%135 = sub i64 %133, %134
%136 = call i32 @xwrite(i32* %8, i8* %98, i64 %135)
%137 = load i64, i64* %18
%138 = getelementptr inbounds i8, i8* %98, i64 %137
%139 = load i64, i64* %21
%140 = sub i64 0, %139
%141 = getelementptr inbounds i8, i8* %138, i64 %140
%142 = load i64, i64* %21
%143 = call i32 @memmove(i8* %98, i8* %141, i64 %142)
%144 = load i64, i64* %21
store i64 %144, i64* %18
%145 = load i32, i32* %8
%146 = call i32 @close(i32 %145)
%147 = load i32, i32* %12
%148 = sub nsw i32 %147, 1
store i32 %148, i32* %20
br label %149
149: ; preds = %175, %120
%150 = load i32, i32* %20
%151 = icmp sge i32 %150, 0
br i1 %151, label %152, label %178
152: ; preds = %149
%153 = sub i64 %126, 1
%154 = trunc i64 %153 to i32
%155 = load i8*, i8** %14
%156 = load i32, i32* %20
%157 = add nsw i32 %156, 1
%158 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %127, i32 %154, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i8* %155, i32 %157)
%159 = load i32, i32* %20
%160 = icmp sgt i32 %159, 0
br i1 %160, label %161, label %167
161: ; preds = %152
%162 = sub i64 %122, 1
%163 = trunc i64 %162 to i32
%164 = load i8*, i8** %14
%165 = load i32, i32* %20
%166 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %124, i32 %163, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i8* %164, i32 %165)
br label %172
167: ; preds = %152
%168 = sub i64 %122, 1
%169 = trunc i64 %168 to i32
%170 = load i8*, i8** %14
%171 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %124, i32 %169, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %170)
br label %172
172: ; preds = %167, %161
%173 = call i32 @unlink(i8* %127)
%174 = call i32 @rename(i8* %124, i8* %127)
br label %175
175: ; preds = %172
%176 = load i32, i32* %20
%177 = add nsw i32 %176, -1
store i32 %177, i32* %20
br label %149
178: ; preds = %149
%179 = load i8*, i8** %14
%180 = load i32, i32* @O_CREAT
%181 = load i32, i32* @O_RDWR
%182 = or i32 %180, %181
%183 = load i32, i32* @O_TRUNC
%184 = or i32 %182, %183
%185 = call i32 @open(i8* %179, i32 %184, i32 420)
store i32 %185, i32* %8
%186 = load i32, i32* %8
%187 = icmp eq i32 %186, -1
br i1 %187, label %188, label %193
188: ; preds = %178
%189 = load i32, i32* @stderr
%190 = load i8*, i8** %14
%191 = load i32, i32* @errno
%192 = call i32 (i32, i8*, ...) @fprintf(i32 %189, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %190, i32 %191)
br label %193
193: ; preds = %188, %178
store i64 0, i64* %10
%194 = load i8*, i8** %22
br label %114
195: ; preds = %114
%196 = load i64, i64* %18
%197 = call i32 @xwrite(i32* %7, i8* %98, i64 %196)
%198 = load i64, i64* %18
%199 = call i32 @xwrite(i32* %8, i8* %98, i64 %198)
%200 = load i32, i32* %8
%201 = call i32 @fsync(i32 %200)
store i32 0, i32* %19
br label %202
202: ; preds = %195, %112
%203 = load i8*, i8** %16
%204 = load i32, i32* %19
switch i32 %204, label %213 [
i32 0, label %205
i32 5, label %206
]
205: ; preds = %202
br label %94
206: ; preds = %202
%207 = load i32, i32* %6
%208 = call i32 @close(i32 %207)
%209 = load i32, i32* %8
%210 = call i32 @close(i32 %209)
store i32 0, i32* %3
br label %211
211: ; preds = %206, %61, %48
%212 = load i32, i32* %3
ret i32 %212
213: ; preds = %202
unreachable
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@.str = [5 x i8] c"r:n:\00"
@optarg = global i32 0
@optind = global i32 0
@stderr = global i32 0
@.str.1 = [19 x i8] c"log name expected\0A\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@.str.3 = [34 x i8] c"open() failed: file=%s, errno=%d\0A\00"
@errno = global i32 0
@SEEK_END = global i32 0
@.str.4 = [26 x i8] c"lseek() failed: errno=%d\0A\00"
@BUFSIZ = global i32 0
@.str.6 = [6 x i8] c"%s.%u\00"
@.str.7 = [3 x i8] c"%s\00"
@O_TRUNC = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = load i32, i32* @STDIN_FILENO
%6 = bitcast i32* %3 to i8*
%7 = load i32, i32* @STDOUT_FILENO
store i32 %7, i32* %3
%8 = bitcast i32* %4 to i8*
br label %9
9: ; preds = %16, %2
%10 = phi i64 [ %19, %16 ], [ 1024, %2 ]
%11 = phi i32 [ %13, %16 ], [ 2, %2 ]
br label %12
12: ; preds = %9, %20
%13 = phi i32 [ %11, %9 ], [ %24, %20 ]
br label %14
14: ; preds = %12, %14
%15 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
switch i32 %15, label %14 [
i32 -1, label %25
i32 110, label %16
i32 114, label %20
]
16: ; preds = %14
%17 = load i32, i32* @optarg
%18 = tail call i8* @atoi(i32 %17)
%19 = ptrtoint i8* %18 to i64
br label %9
20: ; preds = %14
%21 = load i32, i32* @optarg
%22 = tail call i8* @atoi(i32 %21)
%23 = ptrtoint i8* %22 to i64
%24 = trunc i64 %23 to i32
br label %12
25: ; preds = %14
%26 = load i32, i32* @optind
%27 = add nsw i32 %0, -1
%28 = icmp eq i32 %26, %27
br i1 %28, label %32, label %29
29: ; preds = %25
%30 = load i32, i32* @stderr
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %140
32: ; preds = %25
%33 = sext i32 %26 to i64
%34 = getelementptr inbounds i8*, i8** %1, i64 %33
%35 = load i8*, i8** %34
%36 = load i32, i32* @O_CREAT
%37 = load i32, i32* @O_RDWR
%38 = or i32 %37, %36
%39 = load i32, i32* @O_APPEND
%40 = or i32 %38, %39
%41 = tail call i32 @open(i8* %35, i32 %40, i32 420)
store i32 %41, i32* %4
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %47
43: ; preds = %32
%44 = load i32, i32* @stderr
%45 = load i32, i32* @errno
%46 = tail call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %35, i32 %45)
br label %47
47: ; preds = %43, %32
%48 = load i32, i32* @SEEK_END
%49 = tail call i32 @lseek(i32 %41, i32 0, i32 %48)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %56
51: ; preds = %47
%52 = load i32, i32* @stderr
%53 = load i32, i32* @errno
%54 = tail call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %53)
%55 = tail call i32 @close(i32 %41)
store i32 -1, i32* %4
br label %56
56: ; preds = %51, %47
%57 = load i32, i32* @BUFSIZ
%58 = zext i32 %57 to i64
%59 = call i8* @llvm.stacksave()
%60 = alloca i8, i64 %58
%61 = call i64 @read(i32 %5, i8* %60, i32 %57)
%62 = icmp eq i64 %61, 0
br i1 %62, label %135, label %63
63: ; preds = %56
%64 = sext i32 %49 to i64
%65 = icmp sgt i32 %13, 0
br label %66
66: ; preds = %63, %122
%67 = phi i64 [ %61, %63 ], [ %133, %122 ]
%68 = phi i8* [ %60, %63 ], [ %132, %122 ]
%69 = phi i8* [ %59, %63 ], [ %131, %122 ]
%70 = phi i64 [ %64, %63 ], [ %124, %122 ]
%71 = add i64 %70, %67
%72 = icmp ugt i64 %71, %10
br i1 %72, label %73, label %122
73: ; preds = %66, %120
%74 = phi i64 [ %83, %120 ], [ %71, %66 ]
%75 = phi i64 [ %83, %120 ], [ %67, %66 ]
%76 = load i32, i32* @BUFSIZ
%77 = zext i32 %76 to i64
%78 = call i8* @llvm.stacksave()
%79 = alloca i8, i64 %77
%80 = load i32, i32* @BUFSIZ
%81 = zext i32 %80 to i64
%82 = alloca i8, i64 %81
%83 = sub i64 %74, %10
%84 = sub i64 %75, %83
%85 = call i32 @xwrite(i32* %4, i8* %68, i64 %84)
%86 = getelementptr inbounds i8, i8* %68, i64 %75
%87 = sub i64 0, %83
%88 = getelementptr inbounds i8, i8* %86, i64 %87
%89 = call i32 @memmove(i8* %68, i8* %88, i64 %83)
%90 = load i32, i32* %4
%91 = call i32 @close(i32 %90)
br i1 %65, label %92, label %108
92: ; preds = %73
%93 = add i32 %80, -1
%94 = add i32 %76, -1
br label %95
95: ; preds = %92, %104
%96 = phi i32 [ %13, %92 ], [ %97, %104 ]
%97 = add nsw i32 %96, -1
%98 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %82, i32 %93, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i8* %35, i32 %96)
%99 = icmp eq i32 %96, 1
br i1 %99, label %102, label %100
100: ; preds = %95
%101 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %79, i32 %94, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i8* %35, i32 %97)
br label %104
102: ; preds = %95
%103 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %79, i32 %94, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %35)
br label %104
104: ; preds = %102, %100
%105 = call i32 @unlink(i8* %82)
%106 = call i32 @rename(i8* %79, i8* %82)
%107 = icmp ugt i32 %96, 1
br i1 %107, label %95, label %108
108: ; preds = %104, %73
%109 = load i32, i32* @O_CREAT
%110 = load i32, i32* @O_RDWR
%111 = or i32 %110, %109
%112 = load i32, i32* @O_TRUNC
%113 = or i32 %111, %112
%114 = call i32 @open(i8* %35, i32 %113, i32 420)
store i32 %114, i32* %4
%115 = icmp eq i32 %114, -1
br i1 %115, label %116, label %120
116: ; preds = %108
%117 = load i32, i32* @stderr
%118 = load i32, i32* @errno
%119 = call i32 (i32, i8*, ...) @fprintf(i32 %117, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %35, i32 %118)
br label %120
120: ; preds = %116, %108
%121 = icmp ugt i64 %83, %10
br i1 %121, label %73, label %122
122: ; preds = %120, %66
%123 = phi i64 [ %67, %66 ], [ %83, %120 ]
%124 = phi i64 [ %70, %66 ], [ 0, %120 ]
%125 = call i32 @xwrite(i32* %3, i8* %68, i64 %123)
%126 = call i32 @xwrite(i32* %4, i8* %68, i64 %123)
%127 = load i32, i32* %4
%128 = call i32 @fsync(i32 %127)
%129 = load i32, i32* @BUFSIZ
%130 = zext i32 %129 to i64
%131 = call i8* @llvm.stacksave()
%132 = alloca i8, i64 %130
%133 = call i64 @read(i32 %5, i8* %132, i32 %129)
%134 = icmp eq i64 %133, 0
br i1 %134, label %135, label %66
135: ; preds = %122, %56
%136 = phi i8* [ %59, %56 ], [ %131, %122 ]
%137 = call i32 @close(i32 %5)
%138 = load i32, i32* %4
%139 = call i32 @close(i32 %138)
br label %140
140: ; preds = %135, %29
%141 = phi i32 [ -1, %29 ], [ 0, %135 ]
ret i32 %141
}