#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_int64_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_u64(struct sshbuf *buf, u_int64_t *valp) {
  const u_char *p = sshbuf_ptr(buf);
  int r;
  if ((r = sshbuf_consume(buf, 8)) < 0)
    return r;
  if (valp != NULL)
    *valp = (((u_int64_t)(((const u_char *)(p))[0]) << 56) |
             ((u_int64_t)(((const u_char *)(p))[1]) << 48) |
             ((u_int64_t)(((const u_char *)(p))[2]) << 40) |
             ((u_int64_t)(((const u_char *)(p))[3]) << 32) |
             ((u_int64_t)(((const u_char *)(p))[4]) << 24) |
             ((u_int64_t)(((const u_char *)(p))[5]) << 16) |
             ((u_int64_t)(((const u_char *)(p))[6]) << 8) |
             (u_int64_t)(((const u_char *)(p))[7]));
  return 0;
}


x86 assembly (clang, -O0):
<sshbuf_get_u64>:
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    $0x8,%esi
call   1030 <sshbuf_consume@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1162 <sshbuf_get_u64+0x42>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11fa <sshbuf_get_u64+0xda>
cmpq   $0x0,-0x18(%rbp)
je     11f3 <sshbuf_get_u64+0xd3>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    $0x38,%ecx
shl    %cl,%eax
mov    -0x20(%rbp),%rcx
mov    0x8(%rcx),%rcx
mov    %ecx,%edx
mov    $0x30,%ecx
shl    %cl,%edx
mov    %edx,%ecx
or     %ecx,%eax
mov    -0x20(%rbp),%rcx
mov    0x10(%rcx),%rcx
mov    %ecx,%edx
mov    $0x28,%ecx
shl    %cl,%edx
mov    %edx,%ecx
or     %ecx,%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    0x18(%rax),%rax
mov    $0x20,%ecx
shl    %cl,%eax
mov    -0x28(%rbp),%ecx
or     %eax,%ecx
mov    -0x20(%rbp),%rax
mov    0x20(%rax),%rax
shl    $0x18,%eax
or     %eax,%ecx
mov    -0x20(%rbp),%rax
mov    0x28(%rax),%rax
shl    $0x10,%eax
or     %eax,%ecx
mov    -0x20(%rbp),%rax
mov    0x30(%rax),%rax
shl    $0x8,%eax
or     %eax,%ecx
mov    -0x20(%rbp),%rax
mov    0x38(%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_u64>:
push   %rbx
mov    %rdi,%rbx
call   1040 <sshbuf_ptr@plt>
mov    %rbx,%rdi
mov    $0x8,%esi
call   1030 <sshbuf_consume@plt>
mov    %eax,%ecx
sar    $0x1f,%ecx
and    %ecx,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.sshbuf = type { i32 }
define i32 @sshbuf_get_u64(%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 8)
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 %67
15: ; preds = %2
%16 = load i32*, i32** %5
%17 = icmp ne i32* %16, null
br i1 %17, label %18, label %66
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, 56
%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 = shl i32 %27, 48
%29 = or i32 %23, %28
%30 = load i64*, i64** %6
%31 = getelementptr inbounds i64, i64* %30, i64 2
%32 = load i64, i64* %31
%33 = trunc i64 %32 to i32
%34 = shl i32 %33, 40
%35 = or i32 %29, %34
%36 = load i64*, i64** %6
%37 = getelementptr inbounds i64, i64* %36, i64 3
%38 = load i64, i64* %37
%39 = trunc i64 %38 to i32
%40 = shl i32 %39, 32
%41 = or i32 %35, %40
%42 = load i64*, i64** %6
%43 = getelementptr inbounds i64, i64* %42, i64 4
%44 = load i64, i64* %43
%45 = trunc i64 %44 to i32
%46 = shl i32 %45, 24
%47 = or i32 %41, %46
%48 = load i64*, i64** %6
%49 = getelementptr inbounds i64, i64* %48, i64 5
%50 = load i64, i64* %49
%51 = trunc i64 %50 to i32
%52 = shl i32 %51, 16
%53 = or i32 %47, %52
%54 = load i64*, i64** %6
%55 = getelementptr inbounds i64, i64* %54, i64 6
%56 = load i64, i64* %55
%57 = trunc i64 %56 to i32
%58 = shl i32 %57, 8
%59 = or i32 %53, %58
%60 = load i64*, i64** %6
%61 = getelementptr inbounds i64, i64* %60, i64 7
%62 = load i64, i64* %61
%63 = trunc i64 %62 to i32
%64 = or i32 %59, %63
%65 = load i32*, i32** %5
store i32 %64, i32* %65
br label %66
66: ; preds = %18, %15
store i32 0, i32* %3
br label %67
67: ; preds = %66, %13
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
%struct.sshbuf = type { i32 }
define i32 @sshbuf_get_u64(%struct.sshbuf* %0, i32* readnone %1) {
%3 = tail call i64* @sshbuf_ptr(%struct.sshbuf* %0)
%4 = tail call i32 @sshbuf_consume(%struct.sshbuf* %0, i32 8)
%5 = icmp slt i32 %4, 0
%6 = select i1 %5, i32 %4, i32 0
ret i32 %6
}