#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*/ uint16_t;
typedef scalar_t__ ssize_t;

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int, int /*<<< orphan*/ *, unsigned int const);
int /*<<< orphan*/ revbits(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
scalar_t__ write(int, int /*<<< orphan*/ *, unsigned int const);
int main(int argc, char *argv[]) {
  int infd, outfd;
  uint16_t n, nr;
  ssize_t res;
  const unsigned int size = sizeof(uint16_t);
  if (argc != 3) {
    fprintf(stderr, "usage: %s <in file> <out file>\n", argv[0]);
    exit(1);
  }
  infd = open(argv[1], O_RDONLY);
  if (-1 == infd) {
    perror("open");
    exit(1);
  }
  outfd =
      open(argv[2], O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
  if (-1 == infd) {
    perror("open");
    exit(1);
  }
  while (1) {
    res = read(infd, &n, size);
    if (0 == res)
      break;
    nr = revbits(n);
    res = write(outfd, &nr, size);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x4,-0x2c(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11bd <main+0x4d>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe52(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    11fc <main+0x8c>
lea    0xe33(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
mov    0x2de4(%rip),%rax
or     (%rax),%esi
mov    0x2dbb(%rip),%rax
mov    (%rax),%edx
mov    0x2d8a(%rip),%rax
or     (%rax),%edx
mov    0x2d91(%rip),%rax
or     (%rax),%edx
mov    0x2d80(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    1268 <main+0xf8>
lea    0xdc7(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    126d <main+0xfd>
mov    -0x14(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    1293 <main+0x123>
jmp    12b8 <main+0x148>
mov    -0x1c(%rbp),%edi
call   1080 <revbits@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
jmp    126d <main+0xfd>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbp
cmp    $0x3,%edi
jne    124c <main+0xdc>
mov    0x8(%rbp),%rdi
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     126f <main+0xff>
mov    %eax,%ebx
mov    0x2df7(%rip),%rax
mov    0x2e48(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e1d(%rip),%rax
mov    0x2dee(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2df3(%rip),%rax
or     (%rax),%edx
mov    0x10(%rbp),%rdi
mov    0x2dde(%rip),%rax
or     (%rax),%edx
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1040 <read@plt>
test   %rax,%rax
je     123f <main+0xcf>
lea    0x4(%rsp),%r14
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%rsp),%edi
call   1080 <revbits@plt>
mov    %eax,0x4(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1040 <read@plt>
test   %rax,%rax
jne    1210 <main+0xa0>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rdx
lea    0xda0(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xdaa(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [32 x i8] c"usage: %s <in file> <out file>\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = 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 i32
%10 = alloca i64
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 4, i32* %11
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 3
br i1 %13, label %14, label %21
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = load i32, i32* @O_RDONLY
%26 = call i32 (i8*, i32, ...) @open(i8* %24, i32 %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp eq i32 -1, %27
br i1 %28, label %29, label %32
29: ; preds = %21
%30 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %21
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 2
%35 = load i8*, i8** %34
%36 = load i32, i32* @O_WRONLY
%37 = load i32, i32* @O_CREAT
%38 = or i32 %36, %37
%39 = load i32, i32* @S_IWUSR
%40 = load i32, i32* @S_IRUSR
%41 = or i32 %39, %40
%42 = load i32, i32* @S_IRGRP
%43 = or i32 %41, %42
%44 = load i32, i32* @S_IROTH
%45 = or i32 %43, %44
%46 = call i32 (i8*, i32, ...) @open(i8* %35, i32 %38, i32 %45)
store i32 %46, i32* %7
%47 = load i32, i32* %6
%48 = icmp eq i32 -1, %47
br i1 %48, label %49, label %52
49: ; preds = %32
%50 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %32
br label %53
53: ; preds = %52, %59
%54 = load i32, i32* %6
%55 = call i64 @read(i32 %54, i32* %8, i32 4)
store i64 %55, i64* %10
%56 = load i64, i64* %10
%57 = icmp eq i64 0, %56
br i1 %57, label %58, label %59
58: ; preds = %53
br label %64
59: ; preds = %53
%60 = load i32, i32* %8
%61 = call i32 @revbits(i32 %60)
store i32 %61, i32* %9
%62 = load i32, i32* %7
%63 = call i64 @write(i32 %62, i32* %9, i32 4)
store i64 %63, i64* %10
br label %53
64: ; preds = %58
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [32 x i8] c"usage: %s <in file> <out file>\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = icmp eq i32 %0, 3
br i1 %7, label %13, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_RDONLY
%17 = tail call i32 (i8*, i32, ...) @open(i8* %15, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %13
%23 = getelementptr inbounds i8*, i8** %1, i64 2
%24 = load i8*, i8** %23
%25 = load i32, i32* @O_WRONLY
%26 = load i32, i32* @O_CREAT
%27 = or i32 %26, %25
%28 = load i32, i32* @S_IWUSR
%29 = load i32, i32* @S_IRUSR
%30 = or i32 %29, %28
%31 = load i32, i32* @S_IRGRP
%32 = or i32 %30, %31
%33 = load i32, i32* @S_IROTH
%34 = or i32 %32, %33
%35 = tail call i32 (i8*, i32, ...) @open(i8* %24, i32 %27, i32 %34)
%36 = call i64 @read(i32 %17, i32* %3, i32 4)
%37 = icmp eq i64 %36, 0
br i1 %37, label %44, label %38
38: ; preds = %22, %38
%39 = load i32, i32* %3
%40 = tail call i32 @revbits(i32 %39)
store i32 %40, i32* %4
%41 = call i64 @write(i32 %35, i32* %4, i32 4)
%42 = call i64 @read(i32 %17, i32* %3, i32 4)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %38
44: ; preds = %38, %22
ret i32 0
}