#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*/ u_int8_t;
struct message {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ log_error(char *, int);
int /*<<< orphan*/ *malloc(int);
scalar_t__ message_add_payload(struct message *, int, int /*<<< orphan*/ *, int,
                               int);
u_int8_t *ipsec_add_hash_payload(struct message *msg, size_t hashsize) {
  u_int8_t *buf;
  buf = malloc(4 + hashsize);
  if (!buf) {
    log_error("ipsec_add_hash_payload: malloc (%lu) failed",
              4 + (unsigned long)hashsize);
    return 0;
  }
  if (message_add_payload(msg, 8, buf, 4 + hashsize, 1)) {
    free(buf);
    return 0;
  }
  return buf;
}


x86 assembly (clang, -O0):
<ipsec_add_hash_payload>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x4,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1191 <ipsec_add_hash_payload+0x51>
mov    -0x18(%rbp),%rax
add    $0x4,%rax
mov    %eax,%esi
lea    0xe81(%rip),%rdi
call   1040 <log_error@plt>
movq   $0x0,-0x8(%rbp)
jmp    11db <ipsec_add_hash_payload+0x9b>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rax
add    $0x4,%rax
mov    %eax,%ecx
mov    $0x8,%esi
mov    $0x1,%r8d
call   1050 <message_add_payload@plt>
cmp    $0x0,%rax
je     11d3 <ipsec_add_hash_payload+0x93>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11db <ipsec_add_hash_payload+0x9b>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipsec_add_hash_payload>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r15
add    $0x4,%ebx
mov    %ebx,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1184 <ipsec_add_hash_payload+0x44>
mov    %rax,%r14
mov    %r15,%rdi
mov    $0x8,%esi
mov    %rax,%rdx
mov    %ebx,%ecx
mov    $0x1,%r8d
call   1050 <message_add_payload@plt>
test   %rax,%rax
je     1195 <ipsec_add_hash_payload+0x55>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    1192 <ipsec_add_hash_payload+0x52>
lea    0xe75(%rip),%rdi
mov    %ebx,%esi
call   1040 <log_error@plt>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.message = type { i32 }
@.str = [44 x i8] c"ipsec_add_hash_payload: malloc (%lu) failed\00"
define i32* @ipsec_add_hash_payload(%struct.message* %0, i64 %1) {
%3 = alloca i32*
%4 = alloca %struct.message*
%5 = alloca i64
%6 = alloca i32*
store %struct.message* %0, %struct.message** %4
store i64 %1, i64* %5
%7 = load i64, i64* %5
%8 = add i64 4, %7
%9 = trunc i64 %8 to i32
%10 = call i32* @malloc(i32 %9)
store i32* %10, i32** %6
%11 = load i32*, i32** %6
%12 = icmp ne i32* %11, null
br i1 %12, label %18, label %13
13: ; preds = %2
%14 = load i64, i64* %5
%15 = add i64 4, %14
%16 = trunc i64 %15 to i32
%17 = call i32 @log_error(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %16)
store i32* null, i32** %3
br label %31
18: ; preds = %2
%19 = load %struct.message*, %struct.message** %4
%20 = load i32*, i32** %6
%21 = load i64, i64* %5
%22 = add i64 4, %21
%23 = trunc i64 %22 to i32
%24 = call i64 @message_add_payload(%struct.message* %19, i32 8, i32* %20, i32 %23, i32 1)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %18
%27 = load i32*, i32** %6
%28 = call i32 @free(i32* %27)
store i32* null, i32** %3
br label %31
29: ; preds = %18
%30 = load i32*, i32** %6
store i32* %30, i32** %3
br label %31
31: ; preds = %29, %26, %13
%32 = load i32*, i32** %3
ret i32* %32
}

LLVM IR (-O2):
%struct.message = type { i32 }
@.str = [44 x i8] c"ipsec_add_hash_payload: malloc (%lu) failed\00"
define i32* @ipsec_add_hash_payload(%struct.message* %0, i64 %1) {
%3 = trunc i64 %1 to i32
%4 = add i32 %3, 4
%5 = tail call i32* @malloc(i32 %4)
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @log_error(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %4)
br label %14
9: ; preds = %2
%10 = tail call i64 @message_add_payload(%struct.message* %0, i32 8, i32* %5, i32 %4, i32 1)
%11 = icmp eq i64 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = tail call i32 @free(i32* %5)
br label %14
14: ; preds = %9, %12, %7
%15 = phi i32* [ null, %12 ], [ null, %7 ], [ %5, %9 ]
ret i32* %15
}