#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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int main(void) {
  int fd;
  char *ptr = NULL;
  fd = open("/dev/myChar", O_RDWR);
  if (fd < 0)
    perror("Unable to open the device\n");
  else
    printf("File opened successfully %d\n", fd);
  ptr = (char *)malloc(sizeof(char));
  strcpy(ptr, "Hello");
  ioctl(fd, getpid(), 0);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
lea    0xe59(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11ca <main+0x4a>
lea    0xe4c(%rip),%rdi
call   10a0 <perror@plt>
jmp    11d9 <main+0x59>
mov    -0x8(%rbp),%esi
lea    0xe53(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe52(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x14(%rbp),%edi
mov    %eax,%esi
xor    %edx,%edx
call   1060 <ioctl@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
lea    0xe6f(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11e0 <main+0x60>
lea    0xe84(%rip),%rdi
mov    %ebx,%esi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <malloc@plt>
lea    0xe89(%rip),%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %ebx,%edi
mov    %eax,%esi
xor    %edx,%edx
call   1060 <ioctl@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe25(%rip),%rdi
call   10a0 <perror@plt>
jmp    11aa <main+0x2a>

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"
@.str.3 = [6 x i8] c"Hello\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
store i32 0, i32* %1
store i8* null, i8** %3
%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 @malloc(i32 1)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %3
%16 = load i8*, i8** %3
%17 = call i32 @strcpy(i8* %16, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%18 = load i32, i32* %2
%19 = call i32 (...) @getpid()
%20 = call i32 @ioctl(i32 %18, i32 %19, i32 0)
%21 = load i32, i32* %2
%22 = call i32 @close(i32 %21)
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"
@.str.3 = [6 x i8] c"Hello\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 @malloc(i32 1)
%10 = inttoptr i64 %9 to i8*
%11 = tail call i32 @strcpy(i8* %10, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%12 = tail call i32 (...) @getpid()
%13 = tail call i32 @ioctl(i32 %2, i32 %12, i32 0)
%14 = tail call i32 @close(i32 %2)
ret i32 0
}