#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 /*<<< orphan*/ O_RDWR;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, unsigned short, ...);
int /*<<< orphan*/ read(int, unsigned short *, int);
int /*<<< orphan*/ write(int, unsigned short *, int);
int main(int rgc, char *argv[]) {
  int fp, i, cnt;
  unsigned short buf[1024];
  unsigned short buf_rb[1024];
  fp = open("/dev/usrp1_e0", O_RDWR);
  printf("fp = %d\n", fp);
  for (i = 0; i < 1024; i++)
    buf[i] = i * 256;
  write(fp, buf, 2048);
  read(fp, buf_rb, 2048);
  printf("Read back %hX %hX\n", buf_rb[0], buf_rb[1]);
  for (i = 0; i < 1024; i++) {
    if (buf[i] != buf_rb[i])
      printf("Read - %hX, expected - %hX\n", buf_rb[i], buf[i]);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
lea    0xe97(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
lea    0xe93(%rip),%rdi
movzwl %ax,%esi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x400,-0x18(%rbp)
jge    11bc <main+0x7c>
mov    -0x18(%rbp),%eax
shl    $0x8,%eax
mov    %ax,%cx
movslq -0x18(%rbp),%rax
mov    %cx,-0x820(%rbp,%rax,2)
ff
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    118c <main+0x4c>
mov    -0x14(%rbp),%edi
lea    -0x820(%rbp),%rsi
mov    $0x800,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x800,%edx
call   1050 <read@plt>
mov    -0x1020(%rbp),%ax
movzwl -0x101e(%rbp),%edx
lea    0xe1e(%rip),%rdi
movzwl %ax,%esi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x400,-0x18(%rbp)
jge    1273 <main+0x133>
movslq -0x18(%rbp),%rax
movzwl -0x820(%rbp,%rax,2),%eax
ff
movslq -0x18(%rbp),%rcx
movzwl -0x1020(%rbp,%rcx,2),%ecx
ff
cmp    %ecx,%eax
je     1260 <main+0x120>
movslq -0x18(%rbp),%rax
mov    -0x1020(%rbp,%rax,2),%ax
ff
movslq -0x18(%rbp),%rcx
movzwl -0x820(%rbp,%rcx,2),%edx
ff
lea    0xdd4(%rip),%rdi
movzwl %ax,%esi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1265 <main+0x125>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    120a <main+0xca>
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1000,%rsp
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
lea    0xef5(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebp
movzwl %bp,%esi
lea    0xef2(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <printf@plt>
movdqa 0xe83(%rip),%xmm0
movdqa 0xe8b(%rip),%xmm1
movdqa 0xe93(%rip),%xmm2
movdqa 0xe9b(%rip),%xmm3
movdqa 0xea3(%rip),%xmm4
nopl   (%rax)
movdqa %xmm0,%xmm5
psllw  $0x8,%xmm5
movdqa %xmm5,%xmm6
paddw  %xmm1,%xmm6
movdqa %xmm5,(%rsp,%rbx,2)
movdqa %xmm6,0x10(%rsp,%rbx,2)
movdqa %xmm5,%xmm6
paddw  %xmm2,%xmm6
paddw  %xmm3,%xmm5
movdqa %xmm6,0x20(%rsp,%rbx,2)
movdqa %xmm5,0x30(%rsp,%rbx,2)
add    $0x20,%rbx
paddw  %xmm4,%xmm0
cmp    $0x400,%rbx
jne    11a0 <main+0x60>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x800,%edx
call   1030 <write@plt>
lea    0x800(%rsp),%rsi
mov    %ebp,%edi
mov    $0x800,%edx
call   1050 <read@plt>
movzwl 0x800(%rsp),%ebp
movzwl 0x802(%rsp),%edx
movzwl %bp,%esi
lea    0xe45(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe4a(%rip),%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzwl (%rsp,%rbx,2),%edx
cmp    %bp,%dx
je     1256 <main+0x116>
movzwl %bp,%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
cmp    $0x3ff,%rbx
je     126d <main+0x12d>
movzwl 0x802(%rsp,%rbx,2),%ebp
add    $0x1,%rbx
jmp    1240 <main+0x100>
xor    %eax,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/dev/usrp1_e0\00"
@O_RDWR = global i32 0
@.str.1 = [9 x i8] c"fp = %d\0A\00"
@.str.2 = [19 x i8] c"Read back %hX %hX\0A\00"
@.str.3 = [28 x i8] c"Read - %hX, expected - %hX\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 [1024 x i16]
%10 = alloca [1024 x i16]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = trunc i32 %13 to i16
%15 = call i32 (i8*, i16, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i16 zeroext %14)
store i32 0, i32* %7
br label %16
16: ; preds = %26, %2
%17 = load i32, i32* %7
%18 = icmp slt i32 %17, 1024
br i1 %18, label %19, label %29
19: ; preds = %16
%20 = load i32, i32* %7
%21 = mul nsw i32 %20, 256
%22 = trunc i32 %21 to i16
%23 = load i32, i32* %7
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [1024 x i16], [1024 x i16]* %9, i64 0, i64 %24
store i16 %22, i16* %25
br label %26
26: ; preds = %19
%27 = load i32, i32* %7
%28 = add nsw i32 %27, 1
store i32 %28, i32* %7
br label %16
29: ; preds = %16
%30 = load i32, i32* %6
%31 = getelementptr inbounds [1024 x i16], [1024 x i16]* %9, i64 0, i64 0
%32 = call i32 @write(i32 %30, i16* %31, i32 2048)
%33 = load i32, i32* %6
%34 = getelementptr inbounds [1024 x i16], [1024 x i16]* %10, i64 0, i64 0
%35 = call i32 @read(i32 %33, i16* %34, i32 2048)
%36 = getelementptr inbounds [1024 x i16], [1024 x i16]* %10, i64 0, i64 0
%37 = load i16, i16* %36
%38 = getelementptr inbounds [1024 x i16], [1024 x i16]* %10, i64 0, i64 1
%39 = load i16, i16* %38
%40 = zext i16 %39 to i32
%41 = call i32 (i8*, i16, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i16 zeroext %37, i32 %40)
store i32 0, i32* %7
br label %42
42: ; preds = %69, %29
%43 = load i32, i32* %7
%44 = icmp slt i32 %43, 1024
br i1 %44, label %45, label %72
45: ; preds = %42
%46 = load i32, i32* %7
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [1024 x i16], [1024 x i16]* %9, i64 0, i64 %47
%49 = load i16, i16* %48
%50 = zext i16 %49 to i32
%51 = load i32, i32* %7
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [1024 x i16], [1024 x i16]* %10, i64 0, i64 %52
%54 = load i16, i16* %53
%55 = zext i16 %54 to i32
%56 = icmp ne i32 %50, %55
br i1 %56, label %57, label %68
57: ; preds = %45
%58 = load i32, i32* %7
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds [1024 x i16], [1024 x i16]* %10, i64 0, i64 %59
%61 = load i16, i16* %60
%62 = load i32, i32* %7
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds [1024 x i16], [1024 x i16]* %9, i64 0, i64 %63
%65 = load i16, i16* %64
%66 = zext i16 %65 to i32
%67 = call i32 (i8*, i16, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i16 zeroext %61, i32 %66)
br label %68
68: ; preds = %57, %45
br label %69
69: ; preds = %68
%70 = load i32, i32* %7
%71 = add nsw i32 %70, 1
store i32 %71, i32* %7
br label %42
72: ; preds = %42
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
@.str = [14 x i8] c"/dev/usrp1_e0\00"
@O_RDWR = global i32 0
@.str.1 = [9 x i8] c"fp = %d\0A\00"
@.str.2 = [19 x i8] c"Read back %hX %hX\0A\00"
@.str.3 = [28 x i8] c"Read - %hX, expected - %hX\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [1024 x i16]
%4 = alloca [1024 x i16]
%5 = bitcast [1024 x i16]* %3 to i8*
%6 = bitcast [1024 x i16]* %4 to i8*
%7 = load i32, i32* @O_RDWR
%8 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = trunc i32 %8 to i16
%10 = tail call i32 (i8*, i16, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i16 zeroext %9)
br label %11
11: ; preds = %11, %2
%12 = phi i64 [ 0, %2 ], [ %30, %11 ]
%13 = phi <8 x i16> [ <i16 0, i16 1, i16 2, i16 3, i16 4, i16 5, i16 6, i16 7>, %2 ], [ %31, %11 ]
%14 = shl <8 x i16> %13, <i16 8, i16 8, i16 8, i16 8, i16 8, i16 8, i16 8, i16 8>
%15 = shl <8 x i16> %13, <i16 8, i16 8, i16 8, i16 8, i16 8, i16 8, i16 8, i16 8>
%16 = add <8 x i16> %15, <i16 2048, i16 2048, i16 2048, i16 2048, i16 2048, i16 2048, i16 2048, i16 2048>
%17 = getelementptr inbounds [1024 x i16], [1024 x i16]* %3, i64 0, i64 %12
%18 = bitcast i16* %17 to <8 x i16>*
store <8 x i16> %14, <8 x i16>* %18
%19 = getelementptr inbounds i16, i16* %17, i64 8
%20 = bitcast i16* %19 to <8 x i16>*
store <8 x i16> %16, <8 x i16>* %20
%21 = or i64 %12, 16
%22 = add <8 x i16> %13, <i16 16, i16 16, i16 16, i16 16, i16 16, i16 16, i16 16, i16 16>
%23 = shl <8 x i16> %22, <i16 8, i16 8, i16 8, i16 8, i16 8, i16 8, i16 8, i16 8>
%24 = shl <8 x i16> %22, <i16 8, i16 8, i16 8, i16 8, i16 8, i16 8, i16 8, i16 8>
%25 = add <8 x i16> %24, <i16 2048, i16 2048, i16 2048, i16 2048, i16 2048, i16 2048, i16 2048, i16 2048>
%26 = getelementptr inbounds [1024 x i16], [1024 x i16]* %3, i64 0, i64 %21
%27 = bitcast i16* %26 to <8 x i16>*
store <8 x i16> %23, <8 x i16>* %27
%28 = getelementptr inbounds i16, i16* %26, i64 8
%29 = bitcast i16* %28 to <8 x i16>*
store <8 x i16> %25, <8 x i16>* %29
%30 = add nuw nsw i64 %12, 32
%31 = add <8 x i16> %13, <i16 32, i16 32, i16 32, i16 32, i16 32, i16 32, i16 32, i16 32>
%32 = icmp eq i64 %30, 1024
br i1 %32, label %33, label %11
33: ; preds = %11
%34 = getelementptr inbounds [1024 x i16], [1024 x i16]* %3, i64 0, i64 0
%35 = call i32 @write(i32 %8, i16* %34, i32 2048)
%36 = getelementptr inbounds [1024 x i16], [1024 x i16]* %4, i64 0, i64 0
%37 = call i32 @read(i32 %8, i16* %36, i32 2048)
%38 = load i16, i16* %36
%39 = getelementptr inbounds [1024 x i16], [1024 x i16]* %4, i64 0, i64 1
%40 = load i16, i16* %39
%41 = zext i16 %40 to i32
%42 = tail call i32 (i8*, i16, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i16 zeroext %38, i32 %41)
br label %43
43: ; preds = %55, %33
%44 = phi i16 [ %38, %33 ], [ %57, %55 ]
%45 = phi i64 [ 0, %33 ], [ %53, %55 ]
%46 = getelementptr inbounds [1024 x i16], [1024 x i16]* %3, i64 0, i64 %45
%47 = load i16, i16* %46
%48 = icmp eq i16 %47, %44
br i1 %48, label %52, label %49
49: ; preds = %43
%50 = zext i16 %47 to i32
%51 = tail call i32 (i8*, i16, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i16 zeroext %44, i32 %50)
br label %52
52: ; preds = %43, %49
%53 = add nuw nsw i64 %45, 1
%54 = icmp eq i64 %53, 1024
br i1 %54, label %58, label %55
55: ; preds = %52
%56 = getelementptr inbounds [1024 x i16], [1024 x i16]* %4, i64 0, i64 %53
%57 = load i16, i16* %56
br label %43
58: ; preds = %52
ret i32 0
}