#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*/ FILE;

/* Variables and functions */
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ fread(unsigned short *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(unsigned short *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  FILE *fdi, *fdo;
  unsigned short wi;
  unsigned short wo;
  int i;
  if (argc != 3) {
    fprintf(stderr, "Usage: byteswap <infile> <outfile>\n");
    return 1;
  }
  fdi = fopen(argv[1], "rb");
  if (!fdi) {
    perror("Unable to open input file");
    return 1;
  }
  fdo = fopen(argv[2], "w");
  if (!fdo) {
    perror("Unable to open output file");
    fclose(fdi);
    return 1;
  }
  while (1) {
    if (fread(&wi, 2, 1, fdi) <= 0)
      break;
    wo = 0;
    for (i = 0; i < 16; i++)
      if (wi & (1 << i))
        wo |= (0x8000 >> i);
    wo = ((wo & 0x00ff) << 8) | ((wo & 0xff00) >> 8);
    fwrite(&wo, 2, 1, fdo);
  }
  fclose(fdi);
  if (fclose(fdo) != 0) {
    perror("Unable to close output file");
    return 1;
  }
  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)
cmpl   $0x3,-0x8(%rbp)
je     11a1 <main+0x41>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
lea    0xe70(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    131c <main+0x1bc>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe74(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11dc <main+0x7c>
lea    0xe5c(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    131c <main+0x1bc>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe56(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1220 <main+0xc0>
lea    0xe3d(%rip),%rdi
call   1070 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    131c <main+0x1bc>
jmp    1225 <main+0xc5>
mov    -0x18(%rbp),%rcx
lea    -0x22(%rbp),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x0,%rax
jg     124b <main+0xeb>
jmp    12e1 <main+0x181>
movw   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0x10,-0x28(%rbp)
jge    12a6 <main+0x146>
movzwl -0x22(%rbp),%eax
mov    -0x28(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     1293 <main+0x133>
mov    -0x28(%rbp),%ecx
mov    $0x8000,%eax
sar    %cl,%eax
mov    %eax,%ecx
movzwl -0x24(%rbp),%eax
or     %ecx,%eax
mov    %ax,-0x24(%rbp)
jmp    1298 <main+0x138>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1258 <main+0xf8>
movzwl -0x24(%rbp),%eax
and    $0xff,%eax
shl    $0x8,%eax
movzwl -0x24(%rbp),%ecx
and    $0xff00,%ecx
sar    $0x8,%ecx
or     %ecx,%eax
mov    %ax,-0x24(%rbp)
mov    -0x20(%rbp),%rcx
lea    -0x24(%rbp),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
jmp    1225 <main+0xc5>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     1315 <main+0x1b5>
lea    0xd5a(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    131c <main+0x1bc>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    $0x3,%edi
jne    1276 <main+0x116>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0xea5(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     129c <main+0x13c>
mov    %rax,%r14
mov    0x10(%rbx),%rdi
lea    0xea6(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     12aa <main+0x14a>
mov    %rax,%rbx
lea    0x4(%rsp),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
test   %rax,%rax
jle    1253 <main+0xf3>
lea    0x6(%rsp),%r15
lea    0x4(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
movzwl 0x4(%rsp),%eax
mov    %eax,%ecx
and    $0xf0f,%ecx
shl    $0x4,%ecx
shr    $0x4,%eax
and    $0xf0f,%eax
or     %ecx,%eax
mov    %eax,%ecx
and    $0x3333,%ecx
shr    $0x2,%eax
and    $0x3333,%eax
lea    (%rax,%rcx,4),%eax
mov    %eax,%ecx
and    $0x5555,%ecx
shr    %eax
and    $0x5555,%eax
lea    (%rax,%rcx,2),%eax
mov    %ax,0x6(%rsp)
mov    %r15,%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1080 <fwrite@plt>
mov    %r12,%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
test   %rax,%rax
jg     11e0 <main+0x80>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
je     1290 <main+0x130>
lea    0xdea(%rip),%rdi
jmp    12a3 <main+0x143>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
lea    0xd7a(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xd84(%rip),%rdi
call   1070 <perror@plt>
jmp    128b <main+0x12b>
lea    0xd92(%rip),%rdi
call   1070 <perror@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
jmp    128b <main+0x12b>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [36 x i8] c"Usage: byteswap <infile> <outfile>\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [26 x i8] c"Unable to open input file\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [27 x i8] c"Unable to open output file\00"
@.str.5 = [28 x i8] c"Unable to close output file\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 i16
%9 = alloca i16
%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 ne i32 %11, 3
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %86
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %20, i32** %6
%21 = load i32*, i32** %6
%22 = icmp ne i32* %21, null
br i1 %22, label %25, label %23
23: ; preds = %16
%24 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %86
25: ; preds = %16
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
%29 = call i32* @fopen(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %29, i32** %7
%30 = load i32*, i32** %7
%31 = icmp ne i32* %30, null
br i1 %31, label %36, label %32
32: ; preds = %25
%33 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
%34 = load i32*, i32** %6
%35 = call i64 @fclose(i32* %34)
store i32 1, i32* %3
br label %86
36: ; preds = %25
br label %37
37: ; preds = %36, %64
%38 = load i32*, i32** %6
%39 = call i64 @fread(i16* %8, i32 2, i32 1, i32* %38)
%40 = icmp sle i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %37
br label %77
42: ; preds = %37
store i16 0, i16* %9
store i32 0, i32* %10
br label %43
43: ; preds = %61, %42
%44 = load i32, i32* %10
%45 = icmp slt i32 %44, 16
br i1 %45, label %46, label %64
46: ; preds = %43
%47 = load i16, i16* %8
%48 = zext i16 %47 to i32
%49 = load i32, i32* %10
%50 = shl i32 1, %49
%51 = and i32 %48, %50
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %60
53: ; preds = %46
%54 = load i32, i32* %10
%55 = ashr i32 32768, %54
%56 = load i16, i16* %9
%57 = zext i16 %56 to i32
%58 = or i32 %57, %55
%59 = trunc i32 %58 to i16
store i16 %59, i16* %9
br label %60
60: ; preds = %53, %46
br label %61
61: ; preds = %60
%62 = load i32, i32* %10
%63 = add nsw i32 %62, 1
store i32 %63, i32* %10
br label %43
64: ; preds = %43
%65 = load i16, i16* %9
%66 = zext i16 %65 to i32
%67 = and i32 %66, 255
%68 = shl i32 %67, 8
%69 = load i16, i16* %9
%70 = zext i16 %69 to i32
%71 = and i32 %70, 65280
%72 = ashr i32 %71, 8
%73 = or i32 %68, %72
%74 = trunc i32 %73 to i16
store i16 %74, i16* %9
%75 = load i32*, i32** %7
%76 = call i32 @fwrite(i16* %9, i32 2, i32 1, i32* %75)
br label %37
77: ; preds = %41
%78 = load i32*, i32** %6
%79 = call i64 @fclose(i32* %78)
%80 = load i32*, i32** %7
%81 = call i64 @fclose(i32* %80)
%82 = icmp ne i64 %81, 0
br i1 %82, label %83, label %85
83: ; preds = %77
%84 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %86
85: ; preds = %77
store i32 0, i32* %3
br label %86
86: ; preds = %85, %83, %32, %23, %13
%87 = load i32, i32* %3
ret i32 %87
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [36 x i8] c"Usage: byteswap <infile> <outfile>\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [26 x i8] c"Unable to open input file\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [27 x i8] c"Unable to open output file\00"
@.str.5 = [28 x i8] c"Unable to close output file\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i16
%4 = alloca i16
%5 = bitcast i16* %3 to i8*
%6 = bitcast i16* %4 to i8*
%7 = icmp eq i32 %0, 3
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %42
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %42
18: ; preds = %11
%19 = getelementptr inbounds i8*, i8** %1, i64 2
%20 = load i8*, i8** %19
%21 = tail call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %26, label %23
23: ; preds = %18
%24 = call i64 @fread(i16* %3, i32 2, i32 1, i32* %14)
%25 = icmp slt i64 %24, 1
br i1 %25, label %36, label %29
26: ; preds = %18
%27 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
%28 = tail call i64 @fclose(i32* %14)
br label %42
29: ; preds = %23, %29
%30 = load i16, i16* %3
%31 = call i16 @llvm.bitreverse.i16(i16 %30)
%32 = tail call i16 @llvm.bswap.i16(i16 %31)
store i16 %32, i16* %4
%33 = call i32 @fwrite(i16* %4, i32 2, i32 1, i32* %21)
%34 = call i64 @fread(i16* %3, i32 2, i32 1, i32* %14)
%35 = icmp slt i64 %34, 1
br i1 %35, label %36, label %29
36: ; preds = %29, %23
%37 = tail call i64 @fclose(i32* %14)
%38 = tail call i64 @fclose(i32* %21)
%39 = icmp eq i64 %38, 0
br i1 %39, label %42, label %40
40: ; preds = %36
%41 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
br label %42
42: ; preds = %36, %40, %26, %16, %8
%43 = phi i32 [ 1, %8 ], [ 1, %40 ], [ 1, %26 ], [ 1, %16 ], [ 0, %36 ]
ret i32 %43
}