#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 xwii_monitor {
  int dummy;
};

/* Variables and functions */
int EINVAL;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ printf(char *, ...);
struct xwii_monitor *xwii_monitor_new(int, int);
char *xwii_monitor_poll(struct xwii_monitor *);
int /*<<< orphan*/ xwii_monitor_unref(struct xwii_monitor *);
int enumerate() {
  struct xwii_monitor *mon;
  char *ent;
  int num = 0;
  mon = xwii_monitor_new(false, false);
  if (!mon) {
    printf("Cannot create monitor\n");
    return -EINVAL;
  }
  while ((ent = xwii_monitor_poll(mon))) {
    printf("  Found device #%d: %s\n", ++num, ent);
    free(ent);
  }
  xwii_monitor_unref(mon);
  return 0;
}


x86 assembly (clang, -O0):
<enumerate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x1c(%rbp)
xor    %esi,%esi
mov    %esi,%edi
call   1070 <xwii_monitor_new@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1198 <enumerate+0x48>
lea    0xe82(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
mov    0x2e52(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11ed <enumerate+0x9d>
jmp    119d <enumerate+0x4d>
mov    -0x10(%rbp),%rdi
call   1040 <xwii_monitor_poll@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11dd <enumerate+0x8d>
mov    -0x1c(%rbp),%esi
add    $0x1,%esi
mov    %esi,-0x1c(%rbp)
mov    -0x18(%rbp),%rdx
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
jmp    119d <enumerate+0x4d>
mov    -0x10(%rbp),%rdi
call   1060 <xwii_monitor_unref@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<enumerate>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %ebx,%ebx
xor    %edi,%edi
xor    %esi,%esi
call   1080 <xwii_monitor_new@plt>
test   %rax,%rax
je     11d6 <enumerate+0x76>
mov    %rax,%r15
mov    %rax,%rdi
call   1050 <xwii_monitor_poll@plt>
test   %rax,%rax
je     11ca <enumerate+0x6a>
mov    %rax,%rbx
mov    $0x1,%ebp
lea    0xe6a(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1050 <xwii_monitor_poll@plt>
mov    %rax,%rbx
add    $0x1,%ebp
test   %rax,%rax
jne    11a0 <enumerate+0x40>
mov    %r15,%rdi
call   1070 <xwii_monitor_unref@plt>
xor    %ebx,%ebx
jmp    11eb <enumerate+0x8b>
lea    0xe3b(%rip),%rdi
call   1040 <puts@plt>
mov    0x2df7(%rip),%rax
sub    (%rax),%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.xwii_monitor = type { i32 }
@.str = [23 x i8] c"Cannot create monitor\0A\00"
@EINVAL = global i32 0
@.str.1 = [24 x i8] c" Found device #%d: %s\0A\00"
define i32 @enumerate() {
%1 = alloca i32
%2 = alloca %struct.xwii_monitor*
%3 = alloca i8*
%4 = alloca i32
store i32 0, i32* %4
%5 = call %struct.xwii_monitor* @xwii_monitor_new(i32 0, i32 0)
store %struct.xwii_monitor* %5, %struct.xwii_monitor** %2
%6 = load %struct.xwii_monitor*, %struct.xwii_monitor** %2
%7 = icmp ne %struct.xwii_monitor* %6, null
br i1 %7, label %12, label %8
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EINVAL
%11 = sub nsw i32 0, %10
store i32 %11, i32* %1
br label %27
12: ; preds = %0
br label %13
13: ; preds = %17, %12
%14 = load %struct.xwii_monitor*, %struct.xwii_monitor** %2
%15 = call i8* @xwii_monitor_poll(%struct.xwii_monitor* %14)
store i8* %15, i8** %3
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %24
17: ; preds = %13
%18 = load i32, i32* %4
%19 = add nsw i32 %18, 1
store i32 %19, i32* %4
%20 = load i8*, i8** %3
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %19, i8* %20)
%22 = load i8*, i8** %3
%23 = call i32 @free(i8* %22)
br label %13
24: ; preds = %13
%25 = load %struct.xwii_monitor*, %struct.xwii_monitor** %2
%26 = call i32 @xwii_monitor_unref(%struct.xwii_monitor* %25)
store i32 0, i32* %1
br label %27
27: ; preds = %24, %8
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
%struct.xwii_monitor = type { i32 }
@EINVAL = global i32 0
@.str.1 = [24 x i8] c" Found device #%d: %s\0A\00"
@str = [22 x i8] c"Cannot create monitor\00"
define i32 @enumerate() {
%1 = tail call %struct.xwii_monitor* @xwii_monitor_new(i32 0, i32 0)
%2 = icmp eq %struct.xwii_monitor* %1, null
br i1 %2, label %6, label %3
3: ; preds = %0
%4 = tail call i8* @xwii_monitor_poll(%struct.xwii_monitor* %1)
%5 = icmp eq i8* %4, null
br i1 %5, label %18, label %10
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%8 = load i32, i32* @EINVAL
%9 = sub nsw i32 0, %8
br label %20
10: ; preds = %3, %10
%11 = phi i8* [ %16, %10 ], [ %4, %3 ]
%12 = phi i32 [ %13, %10 ], [ 0, %3 ]
%13 = add nuw nsw i32 %12, 1
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %13, i8* %11)
%15 = tail call i32 @free(i8* %11)
%16 = tail call i8* @xwii_monitor_poll(%struct.xwii_monitor* %1)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %10
18: ; preds = %10, %3
%19 = tail call i32 @xwii_monitor_unref(%struct.xwii_monitor* %1)
br label %20
20: ; preds = %18, %6
%21 = phi i32 [ 0, %18 ], [ %9, %6 ]
ret i32 %21
}