#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__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, scalar_t__);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ vfork();
int main(void) {
  int fd;
  pid_t pid;
  fd = open("/dev/myChar", O_RDWR);
  if (fd < 0)
    perror("Unable to open the device\n");
  else
    printf("File opened successfully %d\n", fd);
  pid = vfork();
  ioctl(fd, getpid(), pid);
  if (pid == 0) {
    exit(0);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
lea    0xe61(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11c2 <main+0x42>
lea    0xe54(%rip),%rdi
call   1080 <perror@plt>
jmp    11d1 <main+0x51>
mov    -0x8(%rbp),%esi
lea    0xe5b(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1090 <vfork@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x14(%rbp),%edi
mov    %eax,%esi
mov    -0x10(%rbp),%rdx
call   1050 <ioctl@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1209 <main+0x89>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
lea    0xe6d(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11de <main+0x5e>
lea    0xe82(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1090 <vfork@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %ebp,%edi
mov    %eax,%esi
mov    %rbx,%rdx
call   1050 <ioctl@plt>
test   %rbx,%rbx
je     11ec <main+0x6c>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe27(%rip),%rdi
call   1080 <perror@plt>
jmp    11ac <main+0x2c>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/myChar\00"
@O_RDWR = global i32 0
@.str.1 = [27 x i8] c"Unable to open the device\0A\00"
@.str.2 = [29 x i8] c"File opened successfully %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
store i32 0, i32* %1
%4 = load i32, i32* @O_RDWR
%5 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %13
10: ; preds = %0
%11 = load i32, i32* %2
%12 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %11)
br label %13
13: ; preds = %10, %8
%14 = call i64 (...) @vfork()
store i64 %14, i64* %3
%15 = load i32, i32* %2
%16 = call i32 (...) @getpid()
%17 = load i64, i64* %3
%18 = call i32 @ioctl(i32 %15, i32 %16, i64 %17)
%19 = load i64, i64* %3
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %13
%22 = call i32 @exit(i32 0)
unreachable
23: ; preds = %13
%24 = load i32, i32* %2
%25 = call i32 @close(i32 %24)
ret i32 0
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/myChar\00"
@O_RDWR = global i32 0
@.str.1 = [27 x i8] c"Unable to open the device\0A\00"
@.str.2 = [29 x i8] c"File opened successfully %d\0A\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %8
6: ; preds = %0
%7 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %2)
br label %8
8: ; preds = %6, %4
%9 = tail call i64 (...) @vfork()
%10 = tail call i32 (...) @getpid()
%11 = tail call i32 @ioctl(i32 %2, i32 %10, i64 %9)
%12 = icmp eq i64 %9, 0
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = tail call i32 @exit(i32 0)
unreachable
15: ; preds = %8
%16 = tail call i32 @close(i32 %2)
ret i32 0
}