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

/* Variables and functions */
int ENOMEM;
struct dm_ioctl *alloc_ctl(size_t);
int /*<<< orphan*/ free_ctl(struct dm_ioctl *);
int /*<<< orphan*/ list_devices(struct dm_interface *, struct dm_ioctl *,
                                size_t, struct dev_list **, int *);
struct dm_ioctl *realloc_ctl(struct dm_ioctl *, size_t);
int dm_list_devices(struct dm_interface *dmi, struct dev_list **devs) {
  int r;
  struct dm_ioctl *ctl;
  size_t payload_size = 8192;
  ctl = alloc_ctl(payload_size);
  if (!ctl)
    return -ENOMEM;
  while (!list_devices(dmi, ctl, payload_size, devs, &r)) {
    payload_size *= 2;
    ctl = realloc_ctl(ctl, payload_size);
    if (!ctl)
      return -ENOMEM;
  }
  free_ctl(ctl);
  return r;
}


x86 assembly (clang, -O0):
<dm_list_devices>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x2000,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1060 <alloc_ctl@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1183 <dm_list_devices+0x43>
xor    %eax,%eax
mov    0x2e67(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1205 <dm_list_devices+0xc5>
jmp    1188 <dm_list_devices+0x48>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    -0x1c(%rbp),%r8
call   1030 <list_devices@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11b6 <dm_list_devices+0x76>
jmp    11f6 <dm_list_devices+0xb6>
mov    -0x30(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1050 <realloc_ctl@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f1 <dm_list_devices+0xb1>
xor    %eax,%eax
mov    0x2df9(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1205 <dm_list_devices+0xc5>
jmp    1188 <dm_list_devices+0x48>
mov    -0x28(%rbp),%rdi
call   1040 <free_ctl@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dm_list_devices>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x2000,%ebx
mov    $0x2000,%edi
call   1060 <alloc_ctl@plt>
test   %rax,%rax
je     119e <dm_list_devices+0x5e>
mov    %rax,%r12
lea    0xc(%rsp),%r13
nop
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
mov    %r13,%r8
call   1030 <list_devices@plt>
test   %eax,%eax
jne    11ab <dm_list_devices+0x6b>
add    %rbx,%rbx
mov    %r12,%rdi
mov    %rbx,%rsi
call   1050 <realloc_ctl@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1170 <dm_list_devices+0x30>
mov    0x2e3b(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
jmp    11b7 <dm_list_devices+0x77>
mov    %r12,%rdi
call   1040 <free_ctl@plt>
mov    0xc(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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