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

/* 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 = {
      .fd = dma_buf_fd,
      .flags = 0,
      .handle = 0,
  };
  int ret;
  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    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2ea8(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1161 <import_vgem_fd+0x51>
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<import_vgem_fd>:
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %esi,(%rsp)
movq   $0x0,0x4(%rsp)
mov    0x2ebd(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1030 <ioctl@plt>
test   %eax,%eax
jne    113f <import_vgem_fd+0x2f>
mov    0x4(%rsp),%ecx
mov    %ecx,(%rbx)
add    $0x10,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.drm_prime_handle = type { i32, i32, i32 }
@DRM_IOCTL_PRIME_FD_TO_HANDLE = global i32 0
define i32 @import_vgem_fd(i32 %0, i32 %1, i32* 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
store i32 0, i32* %7
%8 = getelementptr inbounds %struct.drm_prime_handle, %struct.drm_prime_handle* %4, i64 0, i32 2
store i32 0, i32* %8
%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 i32, i32* %7
store i32 %13, i32* %2
br label %14
14: ; preds = %12, %3
ret i32 %10
}