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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int close(int);
int /*<<< orphan*/ ioctl(int, int, void *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int perform_ioctl(int func, void *args) {
  int fd;
  fd = open("/dev/temp", O_RDWR);
  if (fd == -1) {
    do {
      printf("[ TOOL ] ");
      printf("Error in opening file\n");
    } while (0);
    return -1;
  }
  ioctl(fd, func, args);
  return close(fd);
}


x86 assembly (clang, -O0):
<perform_ioctl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
lea    0xea1(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    119a <perform_ioctl+0x5a>
jmp    1176 <perform_ioctl+0x36>
lea    0xe8d(%rip),%rdi
call   1030 <printf@plt>
lea    0xe8b(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b4 <perform_ioctl+0x74>
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1040 <ioctl@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<perform_ioctl>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
lea    0xe97(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     118c <perform_ioctl+0x3c>
mov    %eax,%ebx
mov    %eax,%edi
mov    %ebp,%esi
mov    %r14,%rdx
call   1050 <ioctl@plt>
mov    %ebx,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <close@plt>
lea    0xe77(%rip),%rdi
call   1040 <printf@plt>
lea    0xe75(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/temp\00"
@O_RDWR = global i32 0
@.str.1 = [10 x i8] c"[ TOOL ] \00"
@.str.2 = [23 x i8] c"Error in opening file\0A\00"
define i32 @perform_ioctl(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %16
11: ; preds = %2
br label %12
12: ; preds = %11
%13 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %12
store i32 -1, i32* %3
br label %23
16: ; preds = %2
%17 = load i32, i32* %6
%18 = load i32, i32* %4
%19 = load i8*, i8** %5
%20 = call i32 @ioctl(i32 %17, i32 %18, i8* %19)
%21 = load i32, i32* %6
%22 = call i32 @close(i32 %21)
store i32 %22, i32* %3
br label %23
23: ; preds = %16, %15
%24 = load i32, i32* %3
ret i32 %24
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/temp\00"
@O_RDWR = global i32 0
@.str.1 = [10 x i8] c"[ TOOL ] \00"
@str = [22 x i8] c"Error in opening file\00"
define i32 @perform_ioctl(i32 %0, i8* %1) {
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %12
9: ; preds = %2
%10 = tail call i32 @ioctl(i32 %4, i32 %0, i8* %1)
%11 = tail call i32 @close(i32 %4)
br label %12
12: ; preds = %9, %6
%13 = phi i32 [ -1, %6 ], [ %11, %9 ]
ret i32 %13
}