#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 dummy;
};

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int O_RDWR;
int O_WRONLY;
int PROT_READ;
int PROT_WRITE;
int clear_buf(unsigned char *, unsigned int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
unsigned char *mmap(int /*<<< orphan*/ *, unsigned int, int, int /*<<< orphan*/,
                    int, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ print_diff_time(struct timeval, struct timeval);
int /*<<< orphan*/ printf(char *, unsigned int, int);
int /*<<< orphan*/ sprintf(unsigned char *, char *, unsigned int);
int /*<<< orphan*/ strlen(unsigned char *);
int /*<<< orphan*/ write(int, unsigned char *, int /*<<< orphan*/);
void clear_buf_test(unsigned int size, unsigned int sync_mode, int o_sync) {
  int fd;
  unsigned char attr[1024];
  struct timeval start_time, end_time;
  int error_count;
  unsigned char *buf;
  if ((fd = open("/sys/class/u-dma-buf/udmabuf0/sync_mode", O_WRONLY)) != -1) {
    sprintf(attr, "%d", sync_mode);
    write(fd, attr, strlen(attr));
    close(fd);
  }
  printf("sync_mode=%d, O_SYNC=%d, ", sync_mode, (o_sync) ? 1 : 0);
  if ((fd = open("/dev/udmabuf0", O_RDWR | o_sync)) != -1) {
    buf = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    gettimeofday(&start_time, NULL);
    error_count = clear_buf(buf, size);
    gettimeofday(&end_time, NULL);
    print_diff_time(start_time, end_time);
    close(fd);
  }
}


x86 assembly (clang, -O0):
<clear_buf_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
lea    0xe3c(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
je     122a <clear_buf_test+0x8a>
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe42(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x43c(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x438(%rbp)
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x43c(%rbp),%edi
mov    -0x438(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x10(%rbp),%edi
call   1080 <close@plt>
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%ecx
xor    %edx,%edx
mov    $0x1,%eax
cmp    $0x0,%ecx
cmovne %eax,%edx
lea    0xde7(%rip),%rdi
call   1060 <printf@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
or     -0xc(%rbp),%esi
lea    0xde9(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
je     12f0 <clear_buf_test+0x150>
mov    -0x4(%rbp),%esi
mov    0x2d81(%rip),%rax
mov    (%rax),%edx
mov    0x2d48(%rip),%rax
or     (%rax),%edx
mov    0x2d5f(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x430(%rbp)
lea    -0x418(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <gettimeofday@plt>
mov    -0x430(%rbp),%rdi
mov    -0x4(%rbp),%esi
call   10c0 <clear_buf@plt>
mov    %eax,-0x424(%rbp)
lea    -0x420(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <gettimeofday@plt>
mov    -0x418(%rbp),%edi
mov    -0x420(%rbp),%esi
call   1090 <print_diff_time@plt>
mov    -0x10(%rbp),%edi
call   1080 <close@plt>
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clear_buf_test>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x410,%rsp
mov    %edx,%ebp
mov    %esi,%r15d
mov    %edi,%r14d
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
lea    0xe39(%rip),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     1205 <clear_buf_test+0x65>
mov    %eax,%ebx
lea    0xe4e(%rip),%rsi
lea    0x10(%rsp),%r12
mov    %r12,%rdi
mov    %r15d,%edx
call   10b0 <sprintf@plt>
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %edx,%edx
test   %ebp,%ebp
setne  %dl
lea    0xe18(%rip),%rdi
mov    %r15d,%esi
call   1060 <printf@plt>
mov    0x2db6(%rip),%rax
or     (%rax),%ebp
lea    0xe1a(%rip),%rdi
mov    %ebp,%esi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     129b <clear_buf_test+0xfb>
mov    %eax,%ebp
mov    0x2db8(%rip),%rax
mov    0x2d81(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d96(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r14d,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1070 <gettimeofday@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   10c0 <clear_buf@plt>
mov    %rsp,%rdi
xor    %esi,%esi
call   1070 <gettimeofday@plt>
mov    0x8(%rsp),%edi
mov    (%rsp),%esi
call   1090 <print_diff_time@plt>
mov    %ebp,%edi
call   1080 <close@plt>
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
@.str = [40 x i8] c"/sys/class/u-dma-buf/udmabuf0/sync_mode\00"
@O_WRONLY = global i32 0
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [26 x i8] c"sync_mode=%d, O_SYNC=%d, \00"
@.str.3 = [14 x i8] c"/dev/udmabuf0\00"
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
define void @clear_buf_test(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [1024 x i8]
%9 = alloca %struct.timeval
%10 = alloca %struct.timeval
%11 = alloca i32
%12 = alloca i8*
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%13 = load i32, i32* @O_WRONLY
%14 = call i32 @open(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* %7
%15 = icmp ne i32 %14, -1
br i1 %15, label %16, label %27
16: ; preds = %3
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%18 = load i32, i32* %5
%19 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = load i32, i32* %7
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%23 = call i32 @strlen(i8* %22)
%24 = call i32 @write(i32 %20, i8* %21, i32 %23)
%25 = load i32, i32* %7
%26 = call i32 @close(i32 %25)
br label %27
27: ; preds = %16, %3
%28 = load i32, i32* %5
%29 = load i32, i32* %6
%30 = icmp ne i32 %29, 0
%31 = zext i1 %30 to i64
%32 = select i1 %30, i32 1, i32 0
%33 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %28, i32 %32)
%34 = load i32, i32* @O_RDWR
%35 = load i32, i32* %6
%36 = or i32 %34, %35
%37 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %36)
store i32 %37, i32* %7
%38 = icmp ne i32 %37, -1
br i1 %38, label %39, label %59
39: ; preds = %27
%40 = load i32, i32* %4
%41 = load i32, i32* @PROT_READ
%42 = load i32, i32* @PROT_WRITE
%43 = or i32 %41, %42
%44 = load i32, i32* @MAP_SHARED
%45 = load i32, i32* %7
%46 = call i8* @mmap(i32* null, i32 %40, i32 %43, i32 %44, i32 %45, i32 0)
store i8* %46, i8** %12
%47 = call i32 @gettimeofday(%struct.timeval* %9, i32* null)
%48 = load i8*, i8** %12
%49 = load i32, i32* %4
%50 = call i32 @clear_buf(i8* %48, i32 %49)
store i32 %50, i32* %11
%51 = call i32 @gettimeofday(%struct.timeval* %10, i32* null)
%52 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
%53 = load i32, i32* %52
%54 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
%55 = load i32, i32* %54
%56 = call i32 @print_diff_time(i32 %53, i32 %55)
%57 = load i32, i32* %7
%58 = call i32 @close(i32 %57)
br label %59
59: ; preds = %39, %27
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@.str = [40 x i8] c"/sys/class/u-dma-buf/udmabuf0/sync_mode\00"
@O_WRONLY = global i32 0
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [26 x i8] c"sync_mode=%d, O_SYNC=%d, \00"
@.str.3 = [14 x i8] c"/dev/udmabuf0\00"
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
define void @clear_buf_test(i32 %0, i32 %1, i32 %2) {
%4 = alloca [1024 x i8]
%5 = alloca %struct.timeval
%6 = alloca %struct.timeval
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%8 = bitcast %struct.timeval* %5 to i8*
%9 = bitcast %struct.timeval* %6 to i8*
%10 = load i32, i32* @O_WRONLY
%11 = tail call i32 @open(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %18, label %13
13: ; preds = %3
%14 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%15 = call i32 @strlen(i8* %7)
%16 = call i32 @write(i32 %11, i8* %7, i32 %15)
%17 = call i32 @close(i32 %11)
br label %18
18: ; preds = %13, %3
%19 = icmp ne i32 %2, 0
%20 = zext i1 %19 to i32
%21 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %1, i32 %20)
%22 = load i32, i32* @O_RDWR
%23 = or i32 %22, %2
%24 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %41, label %26
26: ; preds = %18
%27 = load i32, i32* @PROT_READ
%28 = load i32, i32* @PROT_WRITE
%29 = or i32 %28, %27
%30 = load i32, i32* @MAP_SHARED
%31 = call i8* @mmap(i32* null, i32 %0, i32 %29, i32 %30, i32 %24, i32 0)
%32 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%33 = call i32 @clear_buf(i8* %31, i32 %0)
%34 = call i32 @gettimeofday(%struct.timeval* %6, i32* null)
%35 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%36 = load i32, i32* %35
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
%38 = load i32, i32* %37
%39 = call i32 @print_diff_time(i32 %36, i32 %38)
%40 = call i32 @close(i32 %24)
br label %41
41: ; preds = %26, %18
ret void
}