#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*/ FILE;

/* Variables and functions */
int ENODEV;
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fprintf(int /*<<< orphan*/ *, char *, unsigned int, unsigned int,
                   unsigned int, unsigned int, int);
int igvt_create_instance(unsigned int domid, unsigned int aperture_size,
                         unsigned int gm_size, unsigned int fence_count) {
  FILE *fd;
  char *path = "/sys/kernel/vgt"
               "/control/create_vgt_instance";
  int retval = 0;
  fd = fopen(path, "w");
  if (!fd)
    return -ENODEV;
  if (fprintf(fd, "%d,%u,%u,%u,%d\n", domid, aperture_size, gm_size,
              fence_count, 1) < 0) {
    retval = -errno;
  }
  fclose(fd);
  return retval;
}


x86 assembly (clang, -O0):
<igvt_create_instance>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
lea    0xeb5(%rip),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xecb(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1188 <igvt_create_instance+0x58>
xor    %eax,%eax
mov    0x2e5a(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11d4 <igvt_create_instance+0xa4>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%edx
mov    -0xc(%rbp),%ecx
mov    -0x10(%rbp),%r8d
mov    -0x14(%rbp),%r9d
lea    0xe8d(%rip),%rsi
movl   $0x1,(%rsp)
call   1040 <fprintf@plt>
cmp    $0x0,%rax
jge    11c5 <igvt_create_instance+0x95>
xor    %eax,%eax
mov    0x2e20(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<igvt_create_instance>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %esi,%r12d
mov    %edi,%ebp
lea    0xeb2(%rip),%rdi
lea    0xed7(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11a1 <igvt_create_instance+0x71>
mov    %rax,%rbx
movl   $0x1,(%rsp)
lea    0xebe(%rip),%rsi
mov    %rax,%rdi
mov    %ebp,%edx
mov    %r12d,%ecx
mov    %r15d,%r8d
mov    %r14d,%r9d
call   1040 <fprintf@plt>
mov    %rax,%rbp
mov    0x2e53(%rip),%rax
xor    %ecx,%ecx
sub    (%rax),%ecx
sar    $0x3f,%rbp
and    %ecx,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    11ac <igvt_create_instance+0x7c>
mov    0x2e30(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [44 x i8] c"/sys/kernel/vgt/control/create_vgt_instance\00"
@.str.1 = [2 x i8] c"w\00"
@ENODEV = global i32 0
@.str.2 = [16 x i8] c"%d,%u,%u,%u,%d\0A\00"
@errno = global i32 0
define i32 @igvt_create_instance(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i8*
%12 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8** %11
store i32 0, i32* %12
%13 = load i8*, i8** %11
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %10
%15 = load i32*, i32** %10
%16 = icmp ne i32* %15, null
br i1 %16, label %20, label %17
17: ; preds = %4
%18 = load i32, i32* @ENODEV
%19 = sub nsw i32 0, %18
store i32 %19, i32* %5
br label %35
20: ; preds = %4
%21 = load i32*, i32** %10
%22 = load i32, i32* %6
%23 = load i32, i32* %7
%24 = load i32, i32* %8
%25 = load i32, i32* %9
%26 = call i64 @fprintf(i32* %21, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %22, i32 %23, i32 %24, i32 %25, i32 1)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %20
%29 = load i32, i32* @errno
%30 = sub nsw i32 0, %29
store i32 %30, i32* %12
br label %31
31: ; preds = %28, %20
%32 = load i32*, i32** %10
%33 = call i32 @fclose(i32* %32)
%34 = load i32, i32* %12
store i32 %34, i32* %5
br label %35
35: ; preds = %31, %17
%36 = load i32, i32* %5
ret i32 %36
}

LLVM IR (-O2):
@.str = [44 x i8] c"/sys/kernel/vgt/control/create_vgt_instance\00"
@.str.1 = [2 x i8] c"w\00"
@ENODEV = global i32 0
@.str.2 = [16 x i8] c"%d,%u,%u,%u,%d\0A\00"
@errno = global i32 0
define i32 @igvt_create_instance(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %4
%8 = load i32, i32* @ENODEV
%9 = sub nsw i32 0, %8
br label %17
10: ; preds = %4
%11 = tail call i64 @fprintf(i32* %5, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %0, i32 %1, i32 %2, i32 %3, i32 1)
%12 = icmp slt i64 %11, 0
%13 = load i32, i32* @errno
%14 = sub nsw i32 0, %13
%15 = select i1 %12, i32 %14, i32 0
%16 = tail call i32 @fclose(i32* %5)
br label %17
17: ; preds = %10, %7
%18 = phi i32 [ %15, %10 ], [ %9, %7 ]
ret i32 %18
}