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

/* Variables and functions */
char *asctime(int /*<<< orphan*/);
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 /*<<< orphan*/ localtime(int /*<<< orphan*/ *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, ...);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main() {
  int file_desc;
  char msg[50];
  time_t t0 = time(NULL);
  char *ts = asctime(localtime(&t0));
  strcat(msg, "Message passed by ioctl");
  strcat(msg, "\n");
  strcat(msg, ts);
  file_desc = open("/dev/char_dev", 0);
  if (file_desc < 0) {
    printf("Can't open device file: %s\n", "/dev/char_dev");
    exit(-1);
  }
  printf("File %s, %d \n", "/dev/char_dev", file_desc);
  ioctl_get_nth_byte(file_desc);
  ioctl_get_msg(file_desc);
  ioctl_set_msg(file_desc, msg);
  close(file_desc);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %eax,-0x44(%rbp)
lea    -0x44(%rbp),%rdi
call   1030 <localtime@plt>
mov    %eax,%edi
call   1050 <asctime@plt>
mov    %rax,-0x50(%rbp)
lea    -0x40(%rbp),%rdi
lea    0xe16(%rip),%rsi
call   10c0 <strcat@plt>
lea    -0x40(%rbp),%rdi
lea    0xe54(%rip),%rsi
call   10c0 <strcat@plt>
lea    -0x40(%rbp),%rdi
mov    -0x50(%rbp),%rsi
call   10c0 <strcat@plt>
lea    0xe05(%rip),%rdi
xor    %esi,%esi
call   10b0 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1246 <main+0x96>
lea    0xdf8(%rip),%rdi
lea    0xde3(%rip),%rsi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    -0x8(%rbp),%edx
lea    0xdf2(%rip),%rdi
lea    0xdc1(%rip),%rsi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
call   1040 <ioctl_get_nth_byte@plt>
mov    -0x8(%rbp),%edi
call   10a0 <ioctl_get_msg@plt>
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   1090 <ioctl_set_msg@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x48,%rsp
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,0xc(%rsp)
lea    0xc(%rsp),%rdi
call   1030 <localtime@plt>
mov    %eax,%edi
call   1050 <asctime@plt>
mov    %rax,%r14
lea    0xe23(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   10c0 <strcat@plt>
lea    0xe5d(%rip),%rsi
mov    %rbx,%rdi
call   10c0 <strcat@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   10c0 <strcat@plt>
lea    0xe0d(%rip),%rdi
xor    %esi,%esi
call   10b0 <open@plt>
test   %eax,%eax
js     125a <main+0xaa>
mov    %eax,%ebx
lea    0xe23(%rip),%rdi
lea    0xdf2(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
call   1040 <ioctl_get_nth_byte@plt>
mov    %ebx,%edi
call   10a0 <ioctl_get_msg@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
call   1090 <ioctl_set_msg@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdc5(%rip),%rdi
lea    0xdb0(%rip),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"Message passed by ioctl\00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [14 x i8] c"/dev/char_dev\00"
@.str.3 = [28 x i8] c"Can't open device file: %s\0A\00"
@.str.4 = [14 x i8] c"File %s, %d \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [50 x i8]
%4 = alloca i32
%5 = alloca i8*
store i32 0, i32* %1
%6 = call i32 @time(i32* null)
store i32 %6, i32* %4
%7 = call i32 @localtime(i32* %4)
%8 = call i8* @asctime(i32 %7)
store i8* %8, i8** %5
%9 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%10 = call i32 @strcat(i8* %9, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%11 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%12 = call i32 @strcat(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%14 = load i8*, i8** %5
%15 = call i32 @strcat(i8* %13, i8* %14)
%16 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 0)
store i32 %16, i32* %2
%17 = load i32, i32* %2
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %0
%20 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %0
%23 = load i32, i32* %2
%24 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = load i32, i32* %2
%26 = call i32 @ioctl_get_nth_byte(i32 %25)
%27 = load i32, i32* %2
%28 = call i32 @ioctl_get_msg(i32 %27)
%29 = load i32, i32* %2
%30 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%31 = call i32 @ioctl_set_msg(i32 %29, i8* %30)
%32 = load i32, i32* %2
%33 = call i32 @close(i32 %32)
ret i32 0
}

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