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

/* Variables and functions */
int /*<<< orphan*/ free(void *);
void *malloc(size_t);
int /*<<< orphan*/ memcpy(void *, void const *, size_t);
scalar_t__ stack_push(struct stack **, void *);
int stack_push_dup(struct stack **s, const void *data, size_t dlen) {
  void *dup = malloc(dlen);
  if (dup == NULL)
    return -1;
  memcpy(dup, data, dlen);
  if (stack_push(s, dup)) {
    free(dup);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<stack_push_dup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1178 <stack_push_dup+0x38>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bc <stack_push_dup+0x7c>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1060 <stack_push@plt>
cmp    $0x0,%rax
je     11b5 <stack_push_dup+0x75>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    11bc <stack_push_dup+0x7c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stack_push_dup>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
mov    %rdx,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     118e <stack_push_dup+0x4e>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <stack_push@plt>
test   %rax,%rax
je     1195 <stack_push_dup+0x55>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0x1,%eax
jmp    1197 <stack_push_dup+0x57>
mov    $0xffffffff,%eax
jmp    1197 <stack_push_dup+0x57>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stack = type { i32 }
define i32 @stack_push_dup(%struct.stack** %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca %struct.stack**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
store %struct.stack** %0, %struct.stack*** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load i64, i64* %7
%10 = call i8* @malloc(i64 %9)
store i8* %10, i8** %8
%11 = load i8*, i8** %8
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 -1, i32* %4
br label %27
14: ; preds = %3
%15 = load i8*, i8** %8
%16 = load i8*, i8** %6
%17 = load i64, i64* %7
%18 = call i32 @memcpy(i8* %15, i8* %16, i64 %17)
%19 = load %struct.stack**, %struct.stack*** %5
%20 = load i8*, i8** %8
%21 = call i64 @stack_push(%struct.stack** %19, i8* %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %14
%24 = load i8*, i8** %8
%25 = call i32 @free(i8* %24)
store i32 1, i32* %4
br label %27
26: ; preds = %14
store i32 0, i32* %4
br label %27
27: ; preds = %26, %23, %13
%28 = load i32, i32* %4
ret i32 %28
}

LLVM IR (-O2):
%struct.stack = type { i32 }
define i32 @stack_push_dup(%struct.stack** %0, i8* %1, i64 %2) {
%4 = tail call i8* @malloc(i64 %2)
%5 = icmp eq i8* %4, null
br i1 %5, label %12, label %6
6: ; preds = %3
%7 = tail call i32 @memcpy(i8* %4, i8* %1, i64 %2)
%8 = tail call i64 @stack_push(%struct.stack** %0, i8* %4)
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %6
%11 = tail call i32 @free(i8* %4)
br label %12
12: ; preds = %6, %3, %10
%13 = phi i32 [ 1, %10 ], [ -1, %3 ], [ 0, %6 ]
ret i32 %13
}