#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 u_int16_t;
typedef scalar_t__ u_char;
struct sshbuf {
  int dummy;
};

/* Variables and functions */
int sshbuf_consume(struct sshbuf *, int);
scalar_t__ *sshbuf_ptr(struct sshbuf *);
int sshbuf_get_u16(struct sshbuf *buf, u_int16_t *valp) {
  const u_char *p = sshbuf_ptr(buf);
  int r;
  if ((r = sshbuf_consume(buf, 2)) < 0)
    return r;
  if (valp != NULL)
    *valp = (((u_int16_t)(((const u_char *)(p))[0]) << 8) |
             (u_int16_t)(((const u_char *)(p))[1]));
  return 0;
}


x86 assembly (clang, -O0):
<sshbuf_get_u16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <sshbuf_ptr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2,%esi
call   1030 <sshbuf_consume@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1162 <sshbuf_get_u16+0x42>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1190 <sshbuf_get_u16+0x70>
cmpq   $0x0,-0x18(%rbp)
je     1189 <sshbuf_get_u16+0x69>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %eax,%ecx
shl    $0x8,%ecx
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
or     %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sshbuf_get_u16>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <sshbuf_ptr@plt>
mov    %rax,%r15
mov    %rbx,%rdi
mov    $0x2,%esi
call   1030 <sshbuf_consume@plt>
test   %eax,%eax
js     1158 <sshbuf_get_u16+0x38>
xor    %eax,%eax
test   %r14,%r14
je     1158 <sshbuf_get_u16+0x38>
mov    (%r15),%ecx
shl    $0x8,%ecx
or     0x8(%r15),%ecx
mov    %ecx,(%r14)
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sshbuf = type { i32 }
define i32 @sshbuf_get_u16(%struct.sshbuf* %0, i32* %1) {
%3 = alloca i32
%4 = alloca %struct.sshbuf*
%5 = alloca i32*
%6 = alloca i64*
%7 = alloca i32
store %struct.sshbuf* %0, %struct.sshbuf** %4
store i32* %1, i32** %5
%8 = load %struct.sshbuf*, %struct.sshbuf** %4
%9 = call i64* @sshbuf_ptr(%struct.sshbuf* %8)
store i64* %9, i64** %6
%10 = load %struct.sshbuf*, %struct.sshbuf** %4
%11 = call i32 @sshbuf_consume(%struct.sshbuf* %10, i32 2)
store i32 %11, i32* %7
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = load i32, i32* %7
store i32 %14, i32* %3
br label %31
15: ; preds = %2
%16 = load i32*, i32** %5
%17 = icmp ne i32* %16, null
br i1 %17, label %18, label %30
18: ; preds = %15
%19 = load i64*, i64** %6
%20 = getelementptr inbounds i64, i64* %19, i64 0
%21 = load i64, i64* %20
%22 = trunc i64 %21 to i32
%23 = shl i32 %22, 8
%24 = load i64*, i64** %6
%25 = getelementptr inbounds i64, i64* %24, i64 1
%26 = load i64, i64* %25
%27 = trunc i64 %26 to i32
%28 = or i32 %23, %27
%29 = load i32*, i32** %5
store i32 %28, i32* %29
br label %30
30: ; preds = %18, %15
store i32 0, i32* %3
br label %31
31: ; preds = %30, %13
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
%struct.sshbuf = type { i32 }
define i32 @sshbuf_get_u16(%struct.sshbuf* %0, i32* writeonly %1) {
%3 = tail call i64* @sshbuf_ptr(%struct.sshbuf* %0)
%4 = tail call i32 @sshbuf_consume(%struct.sshbuf* %0, i32 2)
%5 = icmp slt i32 %4, 0
br i1 %5, label %16, label %6
6: ; preds = %2
%7 = icmp eq i32* %1, null
br i1 %7, label %16, label %8
8: ; preds = %6
%9 = load i64, i64* %3
%10 = trunc i64 %9 to i32
%11 = shl i32 %10, 8
%12 = getelementptr inbounds i64, i64* %3, i64 1
%13 = load i64, i64* %12
%14 = trunc i64 %13 to i32
%15 = or i32 %11, %14
store i32 %15, i32* %1
br label %16
16: ; preds = %6, %8, %2
%17 = phi i32 [ %4, %2 ], [ 0, %8 ], [ 0, %6 ]
ret i32 %17
}