#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 /*<<< 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 *);
char *get_dev(int num) {
  struct xwii_monitor *mon;
  char *ent;
  int i = 0;
  mon = xwii_monitor_new(false, false);
  if (!mon) {
    printf("Cannot create monitor\n");
    return NULL;
  }
  while ((ent = xwii_monitor_poll(mon))) {
    if (++i == num)
      break;
    free(ent);
  }
  xwii_monitor_unref(mon);
  if (!ent)
    printf("Cannot find device with number #%d\n", num);
  return ent;
}


x86 assembly (clang, -O0):
<get_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
movl   $0x0,-0x24(%rbp)
xor    %esi,%esi
mov    %esi,%edi
call   1070 <xwii_monitor_new@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1195 <get_dev+0x45>
lea    0xe7f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1203 <get_dev+0xb3>
jmp    119a <get_dev+0x4a>
mov    -0x18(%rbp),%rdi
call   1040 <xwii_monitor_poll@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11d6 <get_dev+0x86>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
cmp    -0xc(%rbp),%eax
jne    11c8 <get_dev+0x78>
jmp    11d6 <get_dev+0x86>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    119a <get_dev+0x4a>
mov    -0x18(%rbp),%rdi
call   1060 <xwii_monitor_unref@plt>
cmpq   $0x0,-0x20(%rbp)
jne    11fb <get_dev+0xab>
mov    -0xc(%rbp),%esi
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_dev>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
xor    %edi,%edi
xor    %esi,%esi
call   1080 <xwii_monitor_new@plt>
test   %rax,%rax
je     11ca <get_dev+0x6a>
mov    %rax,%r15
mov    %rax,%rdi
call   1050 <xwii_monitor_poll@plt>
test   %rax,%rax
je     11ad <get_dev+0x4d>
mov    %rax,%rbx
mov    %r14d,%ebp
xchg   %ax,%ax
add    $0xffffffff,%ebp
je     11da <get_dev+0x7a>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1050 <xwii_monitor_poll@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1190 <get_dev+0x30>
mov    %r15,%rdi
call   1070 <xwii_monitor_unref@plt>
lea    0xe44(%rip),%rdi
xor    %ebx,%ebx
mov    %r14d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    11e2 <get_dev+0x82>
lea    0xe53(%rip),%rdi
call   1040 <puts@plt>
xor    %ebx,%ebx
jmp    11e2 <get_dev+0x82>
mov    %r15,%rdi
call   1070 <xwii_monitor_unref@plt>
mov    %rbx,%rax
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"
@.str.1 = [36 x i8] c"Cannot find device with number #%d\0A\00"
define i8* @get_dev(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.xwii_monitor*
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %6
%7 = call %struct.xwii_monitor* @xwii_monitor_new(i32 0, i32 0)
store %struct.xwii_monitor* %7, %struct.xwii_monitor** %4
%8 = load %struct.xwii_monitor*, %struct.xwii_monitor** %4
%9 = icmp ne %struct.xwii_monitor* %8, null
br i1 %9, label %12, label %10
10: ; preds = %1
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %36
12: ; preds = %1
br label %13
13: ; preds = %23, %12
%14 = load %struct.xwii_monitor*, %struct.xwii_monitor** %4
%15 = call i8* @xwii_monitor_poll(%struct.xwii_monitor* %14)
store i8* %15, i8** %5
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %26
17: ; preds = %13
%18 = load i32, i32* %6
%19 = add nsw i32 %18, 1
store i32 %19, i32* %6
%20 = load i32, i32* %3
%21 = icmp eq i32 %19, %20
br i1 %21, label %22, label %23
22: ; preds = %17
br label %26
23: ; preds = %17
%24 = load i8*, i8** %5
%25 = call i32 @free(i8* %24)
br label %13
26: ; preds = %22, %13
%27 = load %struct.xwii_monitor*, %struct.xwii_monitor** %4
%28 = call i32 @xwii_monitor_unref(%struct.xwii_monitor* %27)
%29 = load i8*, i8** %5
%30 = icmp ne i8* %29, null
br i1 %30, label %34, label %31
31: ; preds = %26
%32 = load i32, i32* %3
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %32)
br label %34
34: ; preds = %31, %26
%35 = load i8*, i8** %5
store i8* %35, i8** %2
br label %36
36: ; preds = %34, %10
%37 = load i8*, i8** %2
ret i8* %37
}

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