#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 /*<<< orphan*/ uint8_t;
typedef int /*<<< orphan*/ X509;

/* Variables and functions */
int /*<<< orphan*/ SHA256(unsigned char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int i2d_X509(int /*<<< orphan*/ *, unsigned char **);
unsigned char *malloc(int);
int hash_cert(uint8_t *output, X509 *cert) {
  int length = i2d_X509(cert, NULL);
  if (length < 0)
    return -1;
  unsigned char *encoded = malloc(length), *ptr = encoded;
  int length2 = i2d_X509(cert, &ptr);
  if (length2 != length || ptr != encoded + length2) {
    free(encoded);
    return -1;
  }
  SHA256(encoded, length, output);
  free(encoded);
  return 0;
}


x86 assembly (clang, -O0):
<hash_cert>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <i2d_X509@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1176 <hash_cert+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f3 <hash_cert+0xb3>
mov    -0x1c(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <i2d_X509@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    11be <hash_cert+0x7e>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
movslq -0x34(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
je     11d3 <hash_cert+0x93>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f3 <hash_cert+0xb3>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1030 <SHA256@plt>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hash_cert>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
xor    %esi,%esi
call   1050 <i2d_X509@plt>
test   %eax,%eax
js     11a6 <hash_cert+0x66>
mov    %eax,%ebp
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %rax,(%rsp)
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1050 <i2d_X509@plt>
mov    $0xffffffff,%ebx
cmp    %ebp,%eax
jne    119c <hash_cert+0x5c>
mov    %ebp,%eax
mov    %r15,%rcx
add    %rax,%rcx
cmp    %rcx,(%rsp)
jne    119c <hash_cert+0x5c>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %r14,%rdx
call   1030 <SHA256@plt>
xor    %ebx,%ebx
mov    %r15,%rdi
call   1040 <free@plt>
jmp    11ab <hash_cert+0x6b>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @hash_cert(i32* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
%10 = load i32*, i32** %5
%11 = call i32 @i2d_X509(i32* %10, i8** null)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 -1, i32* %3
br label %41
15: ; preds = %2
%16 = load i32, i32* %6
%17 = call i8* @malloc(i32 %16)
store i8* %17, i8** %7
%18 = load i8*, i8** %7
store i8* %18, i8** %8
%19 = load i32*, i32** %5
%20 = call i32 @i2d_X509(i32* %19, i8** %8)
store i32 %20, i32* %9
%21 = load i32, i32* %9
%22 = load i32, i32* %6
%23 = icmp ne i32 %21, %22
br i1 %23, label %31, label %24
24: ; preds = %15
%25 = load i8*, i8** %8
%26 = load i8*, i8** %7
%27 = load i32, i32* %9
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = icmp ne i8* %25, %29
br i1 %30, label %31, label %34
31: ; preds = %24, %15
%32 = load i8*, i8** %7
%33 = call i32 @free(i8* %32)
store i32 -1, i32* %3
br label %41
34: ; preds = %24
%35 = load i8*, i8** %7
%36 = load i32, i32* %6
%37 = load i32*, i32** %4
%38 = call i32 @SHA256(i8* %35, i32 %36, i32* %37)
%39 = load i8*, i8** %7
%40 = call i32 @free(i8* %39)
store i32 0, i32* %3
br label %41
41: ; preds = %34, %31, %14
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
define i32 @hash_cert(i32* %0, i32* %1) {
%3 = alloca i8*
%4 = tail call i32 @i2d_X509(i32* %1, i8** null)
%5 = icmp slt i32 %4, 0
br i1 %5, label %21, label %6
6: ; preds = %2
%7 = tail call i8* @malloc(i32 %4)
%8 = bitcast i8** %3 to i8*
store i8* %7, i8** %3
%9 = call i32 @i2d_X509(i32* %1, i8** %3)
%10 = icmp eq i32 %9, %4
%11 = load i8*, i8** %3
%12 = zext i32 %4 to i64
%13 = getelementptr inbounds i8, i8* %7, i64 %12
%14 = icmp eq i8* %11, %13
%15 = select i1 %10, i1 %14, i1 false
br i1 %15, label %16, label %18
16: ; preds = %6
%17 = call i32 @SHA256(i8* %7, i32 %4, i32* %0)
br label %18
18: ; preds = %6, %16
%19 = phi i32 [ 0, %16 ], [ -1, %6 ]
%20 = call i32 @free(i8* %7)
br label %21
21: ; preds = %2, %18
%22 = phi i32 [ %19, %18 ], [ -1, %2 ]
ret i32 %22
}