#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 */
typedef struct TYPE_5__ TYPE_2__;
typedef struct TYPE_4__ TYPE_1__;

/* Type definitions */
struct TYPE_4__ {
  int type;
  int sequence;
};
struct TYPE_5__ {
  TYPE_1__ request;
};
typedef TYPE_2__ drm_wait_vblank_t;

/* Variables and functions */
int /*<<< orphan*/ DRM_IOCTL_WAIT_VBLANK;
scalar_t__ EINTR;
int /*<<< orphan*/ O_RDWR;
scalar_t__ errno;
int ioctl(int, int /*<<< orphan*/, TYPE_2__);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
void platform_sync_to_vblank(void) {
  static char initilized = 0;
  static int fd;
  int ret;
  if (!initilized) {
    initilized = 1;
    printf("Linux vbesync: opening DRM card interface...\n");
    fd = open("/dev/dri/card0", O_RDWR);
    if (fd == -1) {
      printf(
          "Failed to open DRM interface; sync-to-vblank will not be done.\n");
      return;
    }
    printf("Linux vbesync: DRM-based sync-to-vblank activated.\n");
  } else if (fd == -1) {
    return;
  }
  drm_wait_vblank_t vbl;
  vbl.request.type = 0x1;
  vbl.request.sequence = 1;
  do {
    ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
    vbl.request.type &= ~0x1;
  } while (ret && errno == EINTR);
}


x86 assembly (clang, -O0):
<platform_sync_to_vblank>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
cmpb   $0x0,0x2f01(%rip)
jne    11a2 <platform_sync_to_vblank+0x72>
movb   $0x1,0x2ef4(%rip)
lea    0xead(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
lea    0xec6(%rip),%rdi
call   1050 <open@plt>
mov    %eax,0x2ed1(%rip)
cmpl   $0xffffffff,0x2eca(%rip)
jne    1191 <platform_sync_to_vblank+0x61>
lea    0xeb6(%rip),%rdi
call   1030 <printf@plt>
jmp    121f <platform_sync_to_vblank+0xef>
lea    0xee5(%rip),%rdi
call   1030 <printf@plt>
jmp    11b9 <platform_sync_to_vblank+0x89>
cmpl   $0xffffffff,0x2e9b(%rip)
jne    11b4 <platform_sync_to_vblank+0x84>
jmp    121f <platform_sync_to_vblank+0xef>
jmp    11b9 <platform_sync_to_vblank+0x89>
movl   $0x1,-0x10(%rbp)
movl   $0x1,-0xc(%rbp)
mov    0x2e77(%rip),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x4(%rbp)
mov    -0x10(%rbp),%eax
and    $0xfffffffe,%eax
mov    %eax,-0x10(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x4(%rbp)
mov    %al,-0x11(%rbp)
je     1214 <platform_sync_to_vblank+0xe4>
mov    0x2dcf(%rip),%rax
mov    (%rax),%rax
mov    0x2dbd(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    11c7 <platform_sync_to_vblank+0x97>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<platform_sync_to_vblank>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmpb   $0x0,0x2f01(%rip)
je     1151 <platform_sync_to_vblank+0x21>
mov    0x2efd(%rip),%edi
cmp    $0xffffffff,%edi
jne    1196 <platform_sync_to_vblank+0x66>
jmp    11ee <platform_sync_to_vblank+0xbe>
movb   $0x1,0x2ee8(%rip)
lea    0xeb0(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
lea    0xe8c(%rip),%rdi
call   1050 <open@plt>
mov    %eax,0x2ec5(%rip)
cmp    $0xffffffff,%eax
je     11fa <platform_sync_to_vblank+0xca>
lea    0xeb1(%rip),%rdi
call   1030 <puts@plt>
mov    0x2eae(%rip),%edi
movabs $0x100000000,%r14
mov    0x2e39(%rip),%r15
mov    (%r15),%esi
lea    0x1(%r14),%rdx
call   1040 <ioctl@plt>
test   %eax,%eax
je     11ee <platform_sync_to_vblank+0xbe>
mov    0x2e12(%rip),%r12
mov    0x2e03(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%rbx),%rax
cmp    %rax,(%r12)
jne    11ee <platform_sync_to_vblank+0xbe>
mov    0x2e65(%rip),%edi
mov    (%r15),%esi
mov    %r14,%rdx
call   1040 <ioctl@plt>
test   %eax,%eax
jne    11d0 <platform_sync_to_vblank+0xa0>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe6e(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <puts@plt>

LLVM IR (-O0):
%struct.TYPE_5__ = type { %struct.TYPE_4__ }
%struct.TYPE_4__ = type { i32, i32 }
@platform_sync_to_vblank.initilized = internal global i8 0
@platform_sync_to_vblank.fd = internal global i32 0
@.str = [46 x i8] c"Linux vbesync: opening DRM card interface...\0A\00"
@.str.1 = [15 x i8] c"/dev/dri/card0\00"
@O_RDWR = global i32 0
@.str.2 = [64 x i8] c"Failed to open DRM interface; sync-to-vblank will not be done.\0A\00"
@.str.3 = [52 x i8] c"Linux vbesync: DRM-based sync-to-vblank activated.\0A\00"
@DRM_IOCTL_WAIT_VBLANK = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define void @platform_sync_to_vblank() {
%1 = alloca i32
%2 = alloca %struct.TYPE_5__
%3 = load i8, i8* @platform_sync_to_vblank.initilized
%4 = icmp ne i8 %3, 0
br i1 %4, label %15, label %5
5: ; preds = %0
store i8 1, i8* @platform_sync_to_vblank.initilized
%6 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %7)
store i32 %8, i32* @platform_sync_to_vblank.fd
%9 = load i32, i32* @platform_sync_to_vblank.fd
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %5
%12 = call i32 @printf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.2, i64 0, i64 0))
br label %45
13: ; preds = %5
%14 = call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0))
br label %20
15: ; preds = %0
%16 = load i32, i32* @platform_sync_to_vblank.fd
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %15
br label %45
19: ; preds = %15
br label %20
20: ; preds = %19, %13
%21 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %2, i32 0, i32 0
%22 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %21, i32 0, i32 0
store i32 1, i32* %22
%23 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %2, i32 0, i32 0
%24 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %23, i32 0, i32 1
store i32 1, i32* %24
br label %25
25: ; preds = %43, %20
%26 = load i32, i32* @platform_sync_to_vblank.fd
%27 = load i32, i32* @DRM_IOCTL_WAIT_VBLANK
%28 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %2, i32 0, i32 0
%29 = bitcast %struct.TYPE_4__* %28 to i64*
%30 = load i64, i64* %29
%31 = call i32 @ioctl(i32 %26, i32 %27, i64 %30)
store i32 %31, i32* %1
%32 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %2, i32 0, i32 0
%33 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %32, i32 0, i32 0
%34 = load i32, i32* %33
%35 = and i32 %34, -2
store i32 %35, i32* %33
br label %36
36: ; preds = %25
%37 = load i32, i32* %1
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %36
%40 = load i64, i64* @errno
%41 = load i64, i64* @EINTR
%42 = icmp eq i64 %40, %41
br label %43
43: ; preds = %39, %36
%44 = phi i1 [ false, %36 ], [ %42, %39 ]
br i1 %44, label %25, label %45
45: ; preds = %11, %18, %43
ret void
}

LLVM IR (-O2):
@platform_sync_to_vblank.initilized = internal unnamed_addr global i1 false
@platform_sync_to_vblank.fd = internal unnamed_addr global i32 0
@.str.1 = [15 x i8] c"/dev/dri/card0\00"
@O_RDWR = global i32 0
@DRM_IOCTL_WAIT_VBLANK = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@str = [45 x i8] c"Linux vbesync: opening DRM card interface...\00"
@str.4 = [51 x i8] c"Linux vbesync: DRM-based sync-to-vblank activated.\00"
@str.5 = [63 x i8] c"Failed to open DRM interface; sync-to-vblank will not be done.\00"
define void @platform_sync_to_vblank() {
%1 = load i1, i1* @platform_sync_to_vblank.initilized
br i1 %1, label %12, label %2
2: ; preds = %0
store i1 true, i1* @platform_sync_to_vblank.initilized
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str, i64 0, i64 0))
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %4)
store i32 %5, i32* @platform_sync_to_vblank.fd
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([63 x i8], [63 x i8]* @str.5, i64 0, i64 0))
br label %33
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str.4, i64 0, i64 0))
%11 = load i32, i32* @platform_sync_to_vblank.fd
br label %15
12: ; preds = %0
%13 = load i32, i32* @platform_sync_to_vblank.fd
%14 = icmp eq i32 %13, -1
br i1 %14, label %33, label %15
15: ; preds = %12, %9
%16 = phi i32 [ %13, %12 ], [ %11, %9 ]
%17 = load i32, i32* @DRM_IOCTL_WAIT_VBLANK
%18 = tail call i32 @ioctl(i32 %16, i32 %17, i64 4294967297)
%19 = icmp ne i32 %18, 0
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
%23 = select i1 %19, i1 %22, i1 false
br i1 %23, label %24, label %33
24: ; preds = %15, %24
%25 = load i32, i32* @platform_sync_to_vblank.fd
%26 = load i32, i32* @DRM_IOCTL_WAIT_VBLANK
%27 = tail call i32 @ioctl(i32 %25, i32 %26, i64 4294967296)
%28 = icmp ne i32 %27, 0
%29 = load i64, i64* @errno
%30 = load i64, i64* @EINTR
%31 = icmp eq i64 %29, %30
%32 = select i1 %28, i1 %31, i1 false
br i1 %32, label %24, label %33
33: ; preds = %24, %15, %12, %7
ret void
}