#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 */
struct timeval {
  int /*<<< orphan*/ tv_usec;
  int /*<<< orphan*/ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
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 /*<<< orphan*/ snprintf(char *, int, char *, int /*<<< orphan*/,
                            int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int file_desc;
  char buf[1024];
  struct timeval tv;
  file_desc = open("chardev", 0);
  if (file_desc < 0) {
    printf("Can't open device file: %s\n", "chardev");
    exit(-1);
  }
  gettimeofday(&tv, NULL);
  snprintf(buf, 1024, "%ld.%06ld:Message passed by ioctl\n", tv.tv_sec,
           tv.tv_usec);
  ioctl_get_nth_byte(file_desc);
  ioctl_get_msg(file_desc);
  ioctl_set_msg(file_desc, buf);
  close(file_desc);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe50(%rip),%rdi
xor    %esi,%esi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11e1 <main+0x51>
lea    0xe3d(%rip),%rdi
lea    0xe2e(%rip),%rsi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
lea    -0x428(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
lea    -0x420(%rbp),%rdi
mov    -0x424(%rbp),%ecx
mov    -0x428(%rbp),%r8d
mov    $0x400,%esi
lea    0xe13(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x14(%rbp),%edi
call   1030 <ioctl_get_nth_byte@plt>
mov    -0x14(%rbp),%edi
call   1090 <ioctl_get_msg@plt>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
call   1080 <ioctl_set_msg@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x418,%rsp
lea    0xe5f(%rip),%rdi
xor    %esi,%esi
call   10a0 <open@plt>
test   %eax,%eax
js     1208 <main+0x78>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
mov    0x8(%rsp),%r8d
mov    0xc(%rsp),%ecx
lea    0xe5a(%rip),%rdx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x400,%esi
call   1050 <snprintf@plt>
mov    %ebx,%edi
call   1030 <ioctl_get_nth_byte@plt>
mov    %ebx,%edi
call   1090 <ioctl_get_msg@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1080 <ioctl_set_msg@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdf9(%rip),%rdi
lea    0xdea(%rip),%rsi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

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

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@.str = [8 x i8] c"chardev\00"
@.str.1 = [28 x i8] c"Can't open device file: %s\0A\00"
@.str.2 = [35 x i8] c"%ld.%06ld:Message passed by ioctl\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [1024 x i8]
%4 = alloca %struct.timeval
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.timeval* %4 to i8*
%7 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %2
%13 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%15 = load i32, i32* %14
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @snprintf(i8* %5, i32 1024, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %15, i32 %17)
%19 = call i32 @ioctl_get_nth_byte(i32 %7)
%20 = call i32 @ioctl_get_msg(i32 %7)
%21 = call i32 @ioctl_set_msg(i32 %7, i8* %5)
%22 = call i32 @close(i32 %7)
ret i32 0
}