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

/* Variables and functions */
int /*<<< orphan*/ *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ _IO(unsigned char, int);
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int ioctl(int, int /*<<< orphan*/);
int /*<<< orphan*/ *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int,
                         int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
u_long *map_shared_clock(int fd_spec) {
  u_long *clock;
  int fd = ioctl(fd_spec, _IO('u', 22));
  if (fd < 0) {
    fprintf(stderr, "map_shared_clock: iotcl returned %d, errno=%d\n", fd,
            errno);
    return NULL;
  }
  clock = mmap(0, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (clock == MAP_FAILED) {
    fprintf(stderr, "Cannot setup shared page for clock\n");
    return NULL;
  }
  close(fd);
  return clock;
}


x86 assembly (clang, -O0):
<map_shared_clock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    $0x75,%edi
mov    $0x16,%esi
call   1040 <_IO@plt>
mov    -0x20(%rbp),%edi
mov    %eax,%esi
call   1050 <ioctl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11b7 <map_shared_clock+0x67>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
mov    0x2e2e(%rip),%rax
mov    (%rax),%ecx
lea    0xe5d(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1232 <map_shared_clock+0xe2>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
mov    0x2df9(%rip),%rax
or     (%rax),%edx
mov    0x2e18(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %r9d,%r9d
mov    $0x1000,%esi
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2dc3(%rip),%rcx
cmp    (%rcx),%rax
jne    1222 <map_shared_clock+0xd2>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
lea    0xe21(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1232 <map_shared_clock+0xe2>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_shared_clock>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
mov    $0x75,%edi
mov    $0x16,%esi
call   1040 <_IO@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   1050 <ioctl@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11b9 <map_shared_clock+0x69>
mov    0x2e7d(%rip),%rax
mov    0x2e3e(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e5b(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x1000,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%r14
mov    0x2e0d(%rip),%rax
cmp    (%rax),%r14
je     11e0 <map_shared_clock+0x90>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    11fa <map_shared_clock+0xaa>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
mov    0x2dff(%rip),%rax
mov    (%rax),%ecx
lea    0xe2e(%rip),%rsi
xor    %r14d,%r14d
mov    %ebx,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    11fa <map_shared_clock+0xaa>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
lea    0xe3f(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [47 x i8] c"map_shared_clock: iotcl returned %d, errno=%d\0A\00"
@errno = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.1 = [36 x i8] c"Cannot setup shared page for clock\0A\00"
define i32* @map_shared_clock(i32 %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = call i32 @_IO(i8 zeroext 117, i32 22)
%8 = call i32 @ioctl(i32 %6, i32 %7)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = load i32, i32* %5
%14 = load i32, i32* @errno
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %14)
store i32* null, i32** %2
br label %33
16: ; preds = %1
%17 = load i32, i32* @PROT_READ
%18 = load i32, i32* @PROT_WRITE
%19 = or i32 %17, %18
%20 = load i32, i32* @MAP_SHARED
%21 = load i32, i32* %5
%22 = call i32* @mmap(i32 0, i32 4096, i32 %19, i32 %20, i32 %21, i32 0)
store i32* %22, i32** %4
%23 = load i32*, i32** %4
%24 = load i32*, i32** @MAP_FAILED
%25 = icmp eq i32* %23, %24
br i1 %25, label %26, label %29
26: ; preds = %16
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
store i32* null, i32** %2
br label %33
29: ; preds = %16
%30 = load i32, i32* %5
%31 = call i32 @close(i32 %30)
%32 = load i32*, i32** %4
store i32* %32, i32** %2
br label %33
33: ; preds = %29, %26, %11
%34 = load i32*, i32** %2
ret i32* %34
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [47 x i8] c"map_shared_clock: iotcl returned %d, errno=%d\0A\00"
@errno = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.1 = [36 x i8] c"Cannot setup shared page for clock\0A\00"
define i32* @map_shared_clock(i32 %0) {
%2 = tail call i32 @_IO(i8 zeroext 117, i32 22)
%3 = tail call i32 @ioctl(i32 %0, i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %1
%6 = load i32, i32* @stderr
%7 = load i32, i32* @errno
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %7)
br label %22
9: ; preds = %1
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %11, %10
%13 = load i32, i32* @MAP_SHARED
%14 = tail call i32* @mmap(i32 0, i32 4096, i32 %12, i32 %13, i32 %3, i32 0)
%15 = load i32*, i32** @MAP_FAILED
%16 = icmp eq i32* %14, %15
br i1 %16, label %17, label %20
17: ; preds = %9
%18 = load i32, i32* @stderr
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
br label %22
20: ; preds = %9
%21 = tail call i32 @close(i32 %3)
br label %22
22: ; preds = %20, %17, %5
%23 = phi i32* [ null, %5 ], [ null, %17 ], [ %14, %20 ]
ret i32* %23
}