#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 O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ SWAPL(int *);
int /*<<< orphan*/ SWAPW(short *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int open(char const *, int, int);
int /*<<< orphan*/ printf(char *, int, char const *);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int awriteshort(char const *file, short *data, int length) {
  int fd;
  int size;
  int offset;
  if ((fd = open(file, O_CREAT | O_WRONLY | O_TRUNC, 0644)) < 0) {
    fprintf(stderr, "awriteshort: %s: can't create\n", file);
    return -1;
  }
  SWAPL(&length);
  if (write(fd, (char *)&length, 4) != 4) {
    fprintf(stderr, "awriteshort: %s: can't write length\n", file);
    close(fd);
    return -1;
  }
  SWAPL(&length);
  for (offset = 0; offset < length; offset++)
    SWAPW(data + offset);
  size = length * sizeof(short);
  if (write(fd, (char *)data, size) != size) {
    fprintf(stderr, "awriteshort: %s: can't write data\n", file);
    close(fd);
    return (-1);
  }
  for (offset = 0; offset < length; offset++)
    SWAPW(data + offset);
  printf("Wrote %d shorts in %s.\n", length, file);
  close(fd);
  return length;
}


x86 assembly (clang, -O0):
<awriteshort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
mov    0x2e29(%rip),%rax
or     (%rax),%esi
mov    0x2e50(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    11dd <awriteshort+0x6d>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe34(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1311 <awriteshort+0x1a1>
lea    -0x1c(%rbp),%rdi
call   1090 <SWAPL@plt>
mov    -0x20(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
je     122d <awriteshort+0xbd>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe0b(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1311 <awriteshort+0x1a1>
lea    -0x1c(%rbp),%rdi
call   1090 <SWAPL@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    126b <awriteshort+0xfb>
mov    -0x18(%rbp),%rdi
movslq -0x28(%rbp),%rax
shl    $0x1,%rax
add    %rax,%rdi
call   1070 <SWAPW@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    123d <awriteshort+0xcd>
movslq -0x1c(%rbp),%rax
shl    $0x1,%rax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <write@plt>
cmp    -0x24(%rbp),%eax
je     12bb <awriteshort+0x14b>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xda2(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1311 <awriteshort+0x1a1>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12f0 <awriteshort+0x180>
mov    -0x18(%rbp),%rdi
movslq -0x28(%rbp),%rax
shl    $0x1,%rax
add    %rax,%rdi
call   1070 <SWAPW@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    12c2 <awriteshort+0x152>
mov    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%rdx
lea    0xd69(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<awriteshort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %rdi,%r14
mov    0x2e70(%rip),%rax
mov    0x2e31(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %edx,0x4(%rsp)
mov    0x2e52(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1080 <open@plt>
test   %eax,%eax
js     1277 <awriteshort+0x107>
mov    %eax,%r15d
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1090 <SWAPL@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
jne    1291 <awriteshort+0x121>
lea    0x4(%rsp),%rdi
call   1090 <SWAPL@plt>
mov    0x4(%rsp),%r13d
test   %r13d,%r13d
jle    121a <awriteshort+0xaa>
xor    %ebp,%ebp
mov    %r12,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
call   1070 <SWAPW@plt>
add    $0x1,%rbp
movslq 0x4(%rsp),%r13
add    $0x2,%rbx
cmp    %r13,%rbp
jl     1200 <awriteshort+0x90>
add    %r13d,%r13d
mov    %r15d,%edi
mov    %r12,%rsi
mov    %r13d,%edx
call   1030 <write@plt>
cmp    %r13d,%eax
jne    12a3 <awriteshort+0x133>
mov    0x4(%rsp),%esi
test   %esi,%esi
jle    125a <awriteshort+0xea>
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1070 <SWAPW@plt>
add    $0x1,%rbx
movslq 0x4(%rsp),%rsi
add    $0x2,%r12
cmp    %rsi,%rbx
jl     1240 <awriteshort+0xd0>
lea    0xe06(%rip),%rdi
mov    %r14,%rdx
call   1040 <printf@plt>
mov    %r15d,%edi
call   1050 <close@plt>
mov    0x4(%rsp),%eax
jmp    12c8 <awriteshort+0x158>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
lea    0xd79(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
jmp    12c3 <awriteshort+0x153>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
lea    0xd7e(%rip),%rsi
jmp    12b3 <awriteshort+0x143>
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
lea    0xd91(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    %r15d,%edi
call   1050 <close@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"awriteshort: %s: can't create\0A\00"
@.str.1 = [37 x i8] c"awriteshort: %s: can't write length\0A\00"
@.str.2 = [35 x i8] c"awriteshort: %s: can't write data\0A\00"
@.str.3 = [24 x i8] c"Wrote %d shorts in %s.\0A\00"
define i32 @awriteshort(i8* %0, i16* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i16*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i16* %1, i16** %6
store i32 %2, i32* %7
%11 = load i8*, i8** %5
%12 = load i32, i32* @O_CREAT
%13 = load i32, i32* @O_WRONLY
%14 = or i32 %12, %13
%15 = load i32, i32* @O_TRUNC
%16 = or i32 %14, %15
%17 = call i32 @open(i8* %11, i32 %16, i32 420)
store i32 %17, i32* %8
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %3
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %5
%22 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %21)
store i32 -1, i32* %4
br label %89
23: ; preds = %3
%24 = call i32 @SWAPL(i32* %7)
%25 = load i32, i32* %8
%26 = bitcast i32* %7 to i8*
%27 = call i32 @write(i32 %25, i8* %26, i32 4)
%28 = icmp ne i32 %27, 4
br i1 %28, label %29, label %35
29: ; preds = %23
%30 = load i32, i32* @stderr
%31 = load i8*, i8** %5
%32 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %31)
%33 = load i32, i32* %8
%34 = call i32 @close(i32 %33)
store i32 -1, i32* %4
br label %89
35: ; preds = %23
%36 = call i32 @SWAPL(i32* %7)
store i32 0, i32* %10
br label %37
37: ; preds = %47, %35
%38 = load i32, i32* %10
%39 = load i32, i32* %7
%40 = icmp slt i32 %38, %39
br i1 %40, label %41, label %50
41: ; preds = %37
%42 = load i16*, i16** %6
%43 = load i32, i32* %10
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i16, i16* %42, i64 %44
%46 = call i32 @SWAPW(i16* %45)
br label %47
47: ; preds = %41
%48 = load i32, i32* %10
%49 = add nsw i32 %48, 1
store i32 %49, i32* %10
br label %37
50: ; preds = %37
%51 = load i32, i32* %7
%52 = sext i32 %51 to i64
%53 = mul i64 %52, 2
%54 = trunc i64 %53 to i32
store i32 %54, i32* %9
%55 = load i32, i32* %8
%56 = load i16*, i16** %6
%57 = bitcast i16* %56 to i8*
%58 = load i32, i32* %9
%59 = call i32 @write(i32 %55, i8* %57, i32 %58)
%60 = load i32, i32* %9
%61 = icmp ne i32 %59, %60
br i1 %61, label %62, label %68
62: ; preds = %50
%63 = load i32, i32* @stderr
%64 = load i8*, i8** %5
%65 = call i32 @fprintf(i32 %63, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %64)
%66 = load i32, i32* %8
%67 = call i32 @close(i32 %66)
store i32 -1, i32* %4
br label %89
68: ; preds = %50
store i32 0, i32* %10
br label %69
69: ; preds = %79, %68
%70 = load i32, i32* %10
%71 = load i32, i32* %7
%72 = icmp slt i32 %70, %71
br i1 %72, label %73, label %82
73: ; preds = %69
%74 = load i16*, i16** %6
%75 = load i32, i32* %10
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds i16, i16* %74, i64 %76
%78 = call i32 @SWAPW(i16* %77)
br label %79
79: ; preds = %73
%80 = load i32, i32* %10
%81 = add nsw i32 %80, 1
store i32 %81, i32* %10
br label %69
82: ; preds = %69
%83 = load i32, i32* %7
%84 = load i8*, i8** %5
%85 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %83, i8* %84)
%86 = load i32, i32* %8
%87 = call i32 @close(i32 %86)
%88 = load i32, i32* %7
store i32 %88, i32* %4
br label %89
89: ; preds = %82, %62, %29, %19
%90 = load i32, i32* %4
ret i32 %90
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"awriteshort: %s: can't create\0A\00"
@.str.1 = [37 x i8] c"awriteshort: %s: can't write length\0A\00"
@.str.2 = [35 x i8] c"awriteshort: %s: can't write data\0A\00"
@.str.3 = [24 x i8] c"Wrote %d shorts in %s.\0A\00"
define i32 @awriteshort(i8* %0, i16* %1, i32 %2) {
%4 = alloca i32
store i32 %2, i32* %4
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_WRONLY
%7 = or i32 %6, %5
%8 = load i32, i32* @O_TRUNC
%9 = or i32 %7, %8
%10 = tail call i32 @open(i8* %0, i32 %9, i32 420)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %62
15: ; preds = %3
%16 = call i32 @SWAPL(i32* %4)
%17 = bitcast i32* %4 to i8*
%18 = call i32 @write(i32 %10, i8* %17, i32 4)
%19 = icmp eq i32 %18, 4
br i1 %19, label %24, label %20
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%23 = call i32 @close(i32 %10)
br label %62
24: ; preds = %15
%25 = call i32 @SWAPL(i32* %4)
%26 = load i32, i32* %4
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %36
28: ; preds = %24, %28
%29 = phi i64 [ %32, %28 ], [ 0, %24 ]
%30 = getelementptr inbounds i16, i16* %1, i64 %29
%31 = call i32 @SWAPW(i16* %30)
%32 = add nuw nsw i64 %29, 1
%33 = load i32, i32* %4
%34 = sext i32 %33 to i64
%35 = icmp slt i64 %32, %34
br i1 %35, label %28, label %36
36: ; preds = %28, %24
%37 = phi i32 [ %26, %24 ], [ %33, %28 ]
%38 = shl i32 %37, 1
%39 = bitcast i16* %1 to i8*
%40 = call i32 @write(i32 %10, i8* %39, i32 %38)
%41 = icmp eq i32 %40, %38
br i1 %41, label %42, label %45
42: ; preds = %36
%43 = load i32, i32* %4
%44 = icmp sgt i32 %43, 0
br i1 %44, label %49, label %57
45: ; preds = %36
%46 = load i32, i32* @stderr
%47 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%48 = call i32 @close(i32 %10)
br label %62
49: ; preds = %42, %49
%50 = phi i64 [ %53, %49 ], [ 0, %42 ]
%51 = getelementptr inbounds i16, i16* %1, i64 %50
%52 = call i32 @SWAPW(i16* %51)
%53 = add nuw nsw i64 %50, 1
%54 = load i32, i32* %4
%55 = sext i32 %54 to i64
%56 = icmp slt i64 %53, %55
br i1 %56, label %49, label %57
57: ; preds = %49, %42
%58 = phi i32 [ %43, %42 ], [ %54, %49 ]
%59 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %58, i8* %0)
%60 = call i32 @close(i32 %10)
%61 = load i32, i32* %4
br label %62
62: ; preds = %57, %45, %20, %12
%63 = phi i32 [ -1, %12 ], [ -1, %20 ], [ -1, %45 ], [ %61, %57 ]
ret i32 %63
}