#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 target {
  int dummy;
};
struct dm_ioctl {
  int dummy;
};
struct dm_interface {
  int dummy;
};

/* Variables and functions */
int ENOMEM;
struct dm_ioctl *alloc_ctl(size_t);
int /*<<< orphan*/ free_ctl(struct dm_ioctl *);
int /*<<< orphan*/ get_status(struct dm_interface *, struct dm_ioctl *,
                              char const *, unsigned int, int *);
struct dm_ioctl *realloc_ctl(struct dm_ioctl *, size_t);
int unpack_status(struct dm_ioctl *, struct target **);
int status_cmd(struct dm_interface *dmi, const char *name,
               struct target **targets, unsigned flags) {
  int r;
  size_t payload_size = 8192;
  struct dm_ioctl *ctl = NULL;
  ctl = alloc_ctl(payload_size);
  if (!ctl)
    return -ENOMEM;
retry:
  if (!get_status(dmi, ctl, name, flags, &r)) {
    payload_size *= 2;
    ctl = realloc_ctl(ctl, payload_size);
    if (!ctl)
      return -ENOMEM;
    goto retry;
  }
  if (r)
    return r;
  r = unpack_status(ctl, targets);
  free_ctl(ctl);
  return r;
}


x86 assembly (clang, -O0):
<status_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
movq   $0x2000,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1060 <alloc_ctl@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11a2 <status_cmd+0x52>
xor    %eax,%eax
mov    0x2e48(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    123c <status_cmd+0xec>
jmp    11a7 <status_cmd+0x57>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x24(%rbp),%ecx
lea    -0x28(%rbp),%r8
call   1030 <get_status@plt>
cmp    $0x0,%eax
jne    1208 <status_cmd+0xb8>
mov    -0x30(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1050 <realloc_ctl@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1203 <status_cmd+0xb3>
xor    %eax,%eax
mov    0x2de7(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    123c <status_cmd+0xec>
jmp    11a7 <status_cmd+0x57>
cmpl   $0x0,-0x28(%rbp)
je     121d <status_cmd+0xcd>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    123c <status_cmd+0xec>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <unpack_status@plt>
mov    %eax,-0x28(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <free_ctl@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<status_cmd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r15d
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r13
mov    $0x2000,%ebp
mov    $0x2000,%edi
call   1060 <alloc_ctl@plt>
test   %rax,%rax
je     11be <status_cmd+0x6e>
mov    %rax,%rbx
mov    %r14,0x10(%rsp)
lea    0xc(%rsp),%r14
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
mov    %r15d,%ecx
mov    %r14,%r8
call   1030 <get_status@plt>
test   %eax,%eax
jne    11cb <status_cmd+0x7b>
add    %rbp,%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1050 <realloc_ctl@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1190 <status_cmd+0x40>
mov    0x2e1b(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
jmp    11f0 <status_cmd+0xa0>
mov    0xc(%rsp),%eax
test   %eax,%eax
jne    11f0 <status_cmd+0xa0>
mov    %rbx,%rdi
mov    0x10(%rsp),%rsi
call   1070 <unpack_status@plt>
mov    %eax,0xc(%rsp)
mov    %rbx,%rdi
call   1040 <free_ctl@plt>
mov    0xc(%rsp),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dm_interface = type { i32 }
%struct.target = type { i32 }
%struct.dm_ioctl = type { i32 }
@ENOMEM = global i32 0
define i32 @status_cmd(%struct.dm_interface* %0, i8* %1, %struct.target** %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.dm_interface*
%7 = alloca i8*
%8 = alloca %struct.target**
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
%12 = alloca %struct.dm_ioctl*
store %struct.dm_interface* %0, %struct.dm_interface** %6
store i8* %1, i8** %7
store %struct.target** %2, %struct.target*** %8
store i32 %3, i32* %9
store i64 8192, i64* %11
store %struct.dm_ioctl* null, %struct.dm_ioctl** %12
%13 = load i64, i64* %11
%14 = call %struct.dm_ioctl* @alloc_ctl(i64 %13)
store %struct.dm_ioctl* %14, %struct.dm_ioctl** %12
%15 = load %struct.dm_ioctl*, %struct.dm_ioctl** %12
%16 = icmp ne %struct.dm_ioctl* %15, null
br i1 %16, label %20, label %17
17: ; preds = %4
%18 = load i32, i32* @ENOMEM
%19 = sub nsw i32 0, %18
store i32 %19, i32* %5
br label %52
20: ; preds = %4
br label %21
21: ; preds = %39, %20
%22 = load %struct.dm_interface*, %struct.dm_interface** %6
%23 = load %struct.dm_ioctl*, %struct.dm_ioctl** %12
%24 = load i8*, i8** %7
%25 = load i32, i32* %9
%26 = call i32 @get_status(%struct.dm_interface* %22, %struct.dm_ioctl* %23, i8* %24, i32 %25, i32* %10)
%27 = icmp ne i32 %26, 0
br i1 %27, label %40, label %28
28: ; preds = %21
%29 = load i64, i64* %11
%30 = mul i64 %29, 2
store i64 %30, i64* %11
%31 = load %struct.dm_ioctl*, %struct.dm_ioctl** %12
%32 = load i64, i64* %11
%33 = call %struct.dm_ioctl* @realloc_ctl(%struct.dm_ioctl* %31, i64 %32)
store %struct.dm_ioctl* %33, %struct.dm_ioctl** %12
%34 = load %struct.dm_ioctl*, %struct.dm_ioctl** %12
%35 = icmp ne %struct.dm_ioctl* %34, null
br i1 %35, label %39, label %36
36: ; preds = %28
%37 = load i32, i32* @ENOMEM
%38 = sub nsw i32 0, %37
store i32 %38, i32* %5
br label %52
39: ; preds = %28
br label %21
40: ; preds = %21
%41 = load i32, i32* %10
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = load i32, i32* %10
store i32 %44, i32* %5
br label %52
45: ; preds = %40
%46 = load %struct.dm_ioctl*, %struct.dm_ioctl** %12
%47 = load %struct.target**, %struct.target*** %8
%48 = call i32 @unpack_status(%struct.dm_ioctl* %46, %struct.target** %47)
store i32 %48, i32* %10
%49 = load %struct.dm_ioctl*, %struct.dm_ioctl** %12
%50 = call i32 @free_ctl(%struct.dm_ioctl* %49)
%51 = load i32, i32* %10
store i32 %51, i32* %5
br label %52
52: ; preds = %45, %43, %36, %17
%53 = load i32, i32* %5
ret i32 %53
}

LLVM IR (-O2):
%struct.dm_interface = type { i32 }
%struct.target = type { i32 }
%struct.dm_ioctl = type { i32 }
@ENOMEM = global i32 0
define i32 @status_cmd(%struct.dm_interface* %0, i8* %1, %struct.target** %2, i32 %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = tail call %struct.dm_ioctl* @alloc_ctl(i64 8192)
%8 = icmp eq %struct.dm_ioctl* %7, null
br i1 %8, label %9, label %12
9: ; preds = %4
%10 = load i32, i32* @ENOMEM
%11 = sub nsw i32 0, %10
br label %31
12: ; preds = %4, %17
%13 = phi i64 [ %18, %17 ], [ 8192, %4 ]
%14 = phi %struct.dm_ioctl* [ %19, %17 ], [ %7, %4 ]
%15 = call i32 @get_status(%struct.dm_interface* %0, %struct.dm_ioctl* %14, i8* %1, i32 %3, i32* %5)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %12
%18 = shl i64 %13, 1
%19 = call %struct.dm_ioctl* @realloc_ctl(%struct.dm_ioctl* %14, i64 %18)
%20 = icmp eq %struct.dm_ioctl* %19, null
br i1 %20, label %21, label %12
21: ; preds = %17
%22 = load i32, i32* @ENOMEM
%23 = sub nsw i32 0, %22
br label %31
24: ; preds = %12
%25 = load i32, i32* %5
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = call i32 @unpack_status(%struct.dm_ioctl* %14, %struct.target** %2)
store i32 %28, i32* %5
%29 = call i32 @free_ctl(%struct.dm_ioctl* %14)
%30 = load i32, i32* %5
br label %31
31: ; preds = %24, %27, %21, %9
%32 = phi i32 [ %30, %27 ], [ %23, %21 ], [ %11, %9 ], [ %25, %24 ]
ret i32 %32
}