#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 int16_t;
typedef int /*<<< orphan*/ d;

/* Variables and functions */
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int const getbytes(char *, int const);
scalar_t__ malloc(int const);
int /*<<< orphan*/ stderr;
int get16(int *data) {
  int16_t d = 0;
  const int size = sizeof(d);
  char *buf = (char *)malloc(size);
  char *byte;
  if (getbytes(buf, size) < size) {
    fputs("error reading data\n", stderr);
    free(buf);
    return 0;
  }
  for (byte = buf; byte < (buf + size); byte++) {
    d = d << 8 | (int16_t)(*byte);
  }
  free(buf);
  *data = (int)d;
  return size;
}


x86 assembly (clang, -O0):
<get16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x4,-0x18(%rbp)
mov    $0x4,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x4,%esi
call   1040 <getbytes@plt>
cmp    $0x4,%eax
jge    11a9 <get16+0x69>
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1050 <fputs@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1202 <get16+0xc2>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
add    $0x4,%rcx
cmp    %rcx,%rax
jae    11e9 <get16+0xa9>
mov    -0x14(%rbp),%eax
shl    $0x8,%eax
mov    -0x28(%rbp),%rcx
movsbl (%rcx),%ecx
or     %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11b1 <get16+0x71>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x4,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get16>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x4,%r15d
mov    $0x4,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x4,%esi
call   1040 <getbytes@plt>
cmp    $0x4,%eax
jge    1191 <get16+0x51>
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1050 <fputs@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
jmp    11ba <get16+0x7a>
movzbl (%rbx),%eax
movsbl 0x1(%rbx),%ecx
shl    $0x10,%eax
shl    $0x8,%ecx
or     %eax,%ecx
movsbl 0x2(%rbx),%eax
or     %ecx,%eax
shl    $0x8,%eax
movsbl 0x3(%rbx),%ebp
or     %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,(%r14)
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"error reading data\0A\00"
@stderr = global i32 0
define i32 @get16(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
store i32* %0, i32** %3
store i32 0, i32* %4
store i32 4, i32* %5
%8 = call i64 @malloc(i32 4)
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %6
%10 = load i8*, i8** %6
%11 = call i32 @getbytes(i8* %10, i32 4)
%12 = icmp slt i32 %11, 4
br i1 %12, label %13, label %18
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = call i32 @fputs(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = load i8*, i8** %6
%17 = call i32 @free(i8* %16)
store i32 0, i32* %2
br label %40
18: ; preds = %1
%19 = load i8*, i8** %6
store i8* %19, i8** %7
br label %20
20: ; preds = %32, %18
%21 = load i8*, i8** %7
%22 = load i8*, i8** %6
%23 = getelementptr inbounds i8, i8* %22, i64 4
%24 = icmp ult i8* %21, %23
br i1 %24, label %25, label %35
25: ; preds = %20
%26 = load i32, i32* %4
%27 = shl i32 %26, 8
%28 = load i8*, i8** %7
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = or i32 %27, %30
store i32 %31, i32* %4
br label %32
32: ; preds = %25
%33 = load i8*, i8** %7
%34 = getelementptr inbounds i8, i8* %33, i32 1
store i8* %34, i8** %7
br label %20
35: ; preds = %20
%36 = load i8*, i8** %6
%37 = call i32 @free(i8* %36)
%38 = load i32, i32* %4
%39 = load i32*, i32** %3
store i32 %38, i32* %39
store i32 4, i32* %2
br label %40
40: ; preds = %35, %13
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
@.str = [20 x i8] c"error reading data\0A\00"
@stderr = global i32 0
define i32 @get16(i32* nocapture writeonly %0) {
%2 = tail call i64 @malloc(i32 4)
%3 = inttoptr i64 %2 to i8*
%4 = tail call i32 @getbytes(i8* %3, i32 4)
%5 = icmp slt i32 %4, 4
br i1 %5, label %25, label %6
6: ; preds = %1
%7 = load i8, i8* %3
%8 = zext i8 %7 to i32
%9 = getelementptr inbounds i8, i8* %3, i64 1
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = getelementptr inbounds i8, i8* %3, i64 2
%13 = shl nuw nsw i32 %8, 16
%14 = shl nsw i32 %11, 8
%15 = or i32 %13, %14
%16 = load i8, i8* %12
%17 = sext i8 %16 to i32
%18 = or i32 %15, %17
%19 = getelementptr inbounds i8, i8* %3, i64 3
%20 = shl i32 %18, 8
%21 = load i8, i8* %19
%22 = sext i8 %21 to i32
%23 = or i32 %20, %22
%24 = tail call i32 @free(i8* %3)
store i32 %23, i32* %0
br label %29
25: ; preds = %1
%26 = load i32, i32* @stderr
%27 = tail call i32 @fputs(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %26)
%28 = tail call i32 @free(i8* %3)
br label %29
29: ; preds = %6, %25
%30 = phi i32 [ 0, %25 ], [ 4, %6 ]
ret i32 %30
}