#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 dm_ioctl {
  int data_size;
};

/* Variables and functions */
struct dm_ioctl *alloc_ctl(size_t);
int /*<<< orphan*/ free_ctl(struct dm_ioctl *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ payload(struct dm_ioctl *);
struct dm_ioctl *realloc_ctl(struct dm_ioctl *old, size_t extra_payload) {
  struct dm_ioctl *ctl;
  size_t old_payload_size = old->data_size - sizeof(struct dm_ioctl);
  size_t new_payload_size = old_payload_size + extra_payload;
  ctl = alloc_ctl(new_payload_size);
  if (ctl)
    memcpy(payload(ctl), payload(old), sizeof(*ctl));
  free_ctl(old);
  return ctl;
}


x86 assembly (clang, -O0):
<realloc_ctl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movslq (%rax),%rax
sub    $0x4,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <alloc_ctl@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11a7 <realloc_ctl+0x67>
mov    -0x18(%rbp),%rdi
call   1060 <payload@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <payload@plt>
mov    -0x2c(%rbp),%edi
mov    %eax,%esi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free_ctl@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<realloc_ctl>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
movslq (%rdi),%rax
lea    (%rsi,%rax,1),%rdi
add    $0xfffffffffffffffc,%rdi
call   1040 <alloc_ctl@plt>
mov    %rax,%rbx
test   %rax,%rax
je     117f <realloc_ctl+0x3f>
mov    %rbx,%rdi
call   1060 <payload@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1060 <payload@plt>
mov    %ebp,%edi
mov    %eax,%esi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    %r14,%rdi
call   1030 <free_ctl@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.dm_ioctl = type { i32 }
define %struct.dm_ioctl* @realloc_ctl(%struct.dm_ioctl* %0, i64 %1) {
%3 = alloca %struct.dm_ioctl*
%4 = alloca i64
%5 = alloca %struct.dm_ioctl*
%6 = alloca i64
%7 = alloca i64
store %struct.dm_ioctl* %0, %struct.dm_ioctl** %3
store i64 %1, i64* %4
%8 = load %struct.dm_ioctl*, %struct.dm_ioctl** %3
%9 = getelementptr inbounds %struct.dm_ioctl, %struct.dm_ioctl* %8, i32 0, i32 0
%10 = load i32, i32* %9
%11 = sext i32 %10 to i64
%12 = sub i64 %11, 4
store i64 %12, i64* %6
%13 = load i64, i64* %6
%14 = load i64, i64* %4
%15 = add i64 %13, %14
store i64 %15, i64* %7
%16 = load i64, i64* %7
%17 = call %struct.dm_ioctl* @alloc_ctl(i64 %16)
store %struct.dm_ioctl* %17, %struct.dm_ioctl** %5
%18 = load %struct.dm_ioctl*, %struct.dm_ioctl** %5
%19 = icmp ne %struct.dm_ioctl* %18, null
br i1 %19, label %20, label %26
20: ; preds = %2
%21 = load %struct.dm_ioctl*, %struct.dm_ioctl** %5
%22 = call i32 @payload(%struct.dm_ioctl* %21)
%23 = load %struct.dm_ioctl*, %struct.dm_ioctl** %3
%24 = call i32 @payload(%struct.dm_ioctl* %23)
%25 = call i32 @memcpy(i32 %22, i32 %24, i32 4)
br label %26
26: ; preds = %20, %2
%27 = load %struct.dm_ioctl*, %struct.dm_ioctl** %3
%28 = call i32 @free_ctl(%struct.dm_ioctl* %27)
%29 = load %struct.dm_ioctl*, %struct.dm_ioctl** %5
ret %struct.dm_ioctl* %29
}

LLVM IR (-O2):
%struct.dm_ioctl = type { i32 }
define %struct.dm_ioctl* @realloc_ctl(%struct.dm_ioctl* %0, i64 %1) {
%3 = getelementptr inbounds %struct.dm_ioctl, %struct.dm_ioctl* %0, i64 0, i32 0
%4 = load i32, i32* %3
%5 = sext i32 %4 to i64
%6 = add i64 %1, -4
%7 = add i64 %6, %5
%8 = tail call %struct.dm_ioctl* @alloc_ctl(i64 %7)
%9 = icmp eq %struct.dm_ioctl* %8, null
br i1 %9, label %14, label %10
10: ; preds = %2
%11 = tail call i32 @payload(%struct.dm_ioctl* %8)
%12 = tail call i32 @payload(%struct.dm_ioctl* %0)
%13 = tail call i32 @memcpy(i32 %11, i32 %12, i32 4)
br label %14
14: ; preds = %10, %2
%15 = tail call i32 @free_ctl(%struct.dm_ioctl* %0)
ret %struct.dm_ioctl* %8
}