#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 */
typedef scalar_t__ uint32_t;
struct drm_prime_handle {
  int fd;
  scalar_t__ handle;
  scalar_t__ flags;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ DRM_IOCTL_PRIME_FD_TO_HANDLE;
int ioctl(int, int /*<<< orphan*/, struct drm_prime_handle *);
int import_vgem_fd(int vgem_fd, int dma_buf_fd, uint32_t *handle) {
  struct drm_prime_handle import_handle = {0};
  int ret;
  import_handle.fd = dma_buf_fd;
  import_handle.flags = 0;
  import_handle.handle = 0;
  ret = ioctl(vgem_fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &import_handle);
  if (ret == 0)
    *handle = import_handle.handle;
  return ret;
}


x86 assembly (clang, -O0):
<import_vgem_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    1185 <import_vgem_fd+0x65>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<import_vgem_fd>:
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%rbx
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movaps %xmm0,0x10(%rsp)
mov    %esi,(%rsp)
movups %xmm0,0x8(%rsp)
mov    0x2eb5(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1030 <ioctl@plt>
test   %eax,%eax
jne    1149 <import_vgem_fd+0x39>
mov    0x8(%rsp),%rcx
mov    %rcx,(%rbx)
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.drm_prime_handle = type { i32, i64, i64, i32 }
@DRM_IOCTL_PRIME_FD_TO_HANDLE = global i32 0
define i32 @import_vgem_fd(i32 %0, i32 %1, i64* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64*
%7 = alloca %struct.drm_prime_handle
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i64* %2, i64** %6
%9 = bitcast %struct.drm_prime_handle* %7 to i8*
%10 = load i32, i32* %5
%11 = getelementptr inbounds %struct.drm_prime_handle, %struct.drm_prime_handle* %7, i32 0, i32 0
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.drm_prime_handle, %struct.drm_prime_handle* %7, i32 0, i32 2
store i64 0, i64* %12
%13 = getelementptr inbounds %struct.drm_prime_handle, %struct.drm_prime_handle* %7, i32 0, i32 1
store i64 0, i64* %13
%14 = load i32, i32* %4
%15 = load i32, i32* @DRM_IOCTL_PRIME_FD_TO_HANDLE
%16 = call i32 @ioctl(i32 %14, i32 %15, %struct.drm_prime_handle* %7)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %3
%20 = getelementptr inbounds %struct.drm_prime_handle, %struct.drm_prime_handle* %7, i32 0, i32 1
%21 = load i64, i64* %20
%22 = load i64*, i64** %6
store i64 %21, i64* %22
br label %23
23: ; preds = %19, %3
%24 = load i32, i32* %8
ret i32 %24
}

LLVM IR (-O2):
%struct.drm_prime_handle = type { i32, i64, i64, i32 }
@DRM_IOCTL_PRIME_FD_TO_HANDLE = global i32 0
define i32 @import_vgem_fd(i32 %0, i32 %1, i64* nocapture writeonly %2) {
%4 = alloca %struct.drm_prime_handle
%5 = bitcast %struct.drm_prime_handle* %4 to i8*
%6 = getelementptr inbounds %struct.drm_prime_handle, %struct.drm_prime_handle* %4, i64 0, i32 0
store i32 %1, i32* %6
%7 = getelementptr inbounds %struct.drm_prime_handle, %struct.drm_prime_handle* %4, i64 0, i32 1
%8 = bitcast i64* %7 to i8*
%9 = load i32, i32* @DRM_IOCTL_PRIME_FD_TO_HANDLE
%10 = call i32 @ioctl(i32 %0, i32 %9, %struct.drm_prime_handle* %4)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = load i64, i64* %7
store i64 %13, i64* %2
br label %14
14: ; preds = %12, %3
ret i32 %10
}