#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*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ ioctl_get_msg(int);
int /*<<< orphan*/ ioctl_get_nth_byte(int);
int /*<<< orphan*/ ioctl_set_msg(int, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int main() {
  int file_desc;
  char *msg = "Message passed by ioctl\n";
  file_desc = open("char_dev", 0);
  if (file_desc < 0) {
    printf("Can't open device file: %s\n", "char_dev");
    exit(-1);
  }
  ioctl_set_msg(file_desc, msg);
  ioctl_get_nth_byte(file_desc);
  ioctl_get_msg(file_desc);
  close(file_desc);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0xe88(%rip),%rdi
xor    %esi,%esi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11c2 <main+0x52>
lea    0xe76(%rip),%rdi
lea    0xe66(%rip),%rsi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1060 <ioctl_set_msg@plt>
mov    -0x8(%rbp),%edi
call   1030 <ioctl_get_nth_byte@plt>
mov    -0x8(%rbp),%edi
call   1070 <ioctl_get_msg@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xea1(%rip),%rdi
xor    %esi,%esi
call   1080 <open@plt>
test   %eax,%eax
js     11ac <main+0x3c>
mov    %eax,%ebx
lea    0xe74(%rip),%rsi
mov    %eax,%edi
call   1060 <ioctl_set_msg@plt>
mov    %ebx,%edi
call   1030 <ioctl_get_nth_byte@plt>
mov    %ebx,%edi
call   1070 <ioctl_get_msg@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe6f(%rip),%rdi
lea    0xe5f(%rip),%rsi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"Message passed by ioctl\0A\00"
@.str.1 = [9 x i8] c"char_dev\00"
@.str.2 = [28 x i8] c"Can't open device file: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
store i32 0, i32* %1
store i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8** %3
%4 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 0)
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @exit(i32 -1)
unreachable
10: ; preds = %0
%11 = load i32, i32* %2
%12 = load i8*, i8** %3
%13 = call i32 @ioctl_set_msg(i32 %11, i8* %12)
%14 = load i32, i32* %2
%15 = call i32 @ioctl_get_nth_byte(i32 %14)
%16 = load i32, i32* %2
%17 = call i32 @ioctl_get_msg(i32 %16)
%18 = load i32, i32* %2
%19 = call i32 @close(i32 %18)
ret i32 0
}

LLVM IR (-O2):
@.str = [25 x i8] c"Message passed by ioctl\0A\00"
@.str.1 = [9 x i8] c"char_dev\00"
@.str.2 = [28 x i8] c"Can't open device file: %s\0A\00"
define i32 @main() {
%1 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 0)
%2 = icmp slt i32 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call i32 @exit(i32 -1)
unreachable
6: ; preds = %0
%7 = tail call i32 @ioctl_set_msg(i32 %1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @ioctl_get_nth_byte(i32 %1)
%9 = tail call i32 @ioctl_get_msg(i32 %1)
%10 = tail call i32 @close(i32 %1)
ret i32 0
}