#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*/ l;
typedef int int32_t;

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int atoi(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ pause();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stderr;
int write(int, int *, int);
int main(int argc, char **argv) {
  int32_t l;
  int fd;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <latency in us>\n", argv[0]);
    return 2;
  }
  l = atoi(argv[1]);
  printf("setting latency to %d us\n", l);
  fd = open("/dev/cpu_dma_latency", O_WRONLY);
  if (fd < 0) {
    perror("open /dev/cpu_dma_latency");
    return 1;
  }
  if (write(fd, &l, sizeof(l)) != sizeof(l)) {
    perror("write to /dev/cpu_dma_latency");
    return 1;
  }
  while (1)
    pause();
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11b8 <main+0x48>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe59(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1257 <main+0xe7>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe49(%rip),%rdi
call   1040 <printf@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
lea    0xe53(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1211 <main+0xa1>
lea    0xe35(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1257 <main+0xe7>
mov    -0x18(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cltq
cmp    $0x4,%rax
je     1246 <main+0xd6>
lea    0xe1a(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1257 <main+0xe7>
jmp    124b <main+0xdb>
mov    $0x0,%al
call   1060 <pause@plt>
jmp    124b <main+0xdb>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
cmp    $0x2,%edi
jne    11c9 <main+0x59>
mov    0x8(%rsi),%rdi
call   1090 <atoi@plt>
mov    %eax,0x4(%rsp)
lea    0xe91(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
lea    0xe99(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11e8 <main+0x78>
lea    0x4(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
jne    11f1 <main+0x81>
xor    %eax,%eax
call   1060 <pause@plt>
jmp    11c0 <main+0x50>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xe24(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x2,%eax
pop    %rcx
ret
lea    0xe46(%rip),%rdi
jmp    11f8 <main+0x88>
lea    0xe57(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
pop    %rcx
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [27 x i8] c"Usage: %s <latency in us>\0A\00"
@.str.1 = [26 x i8] c"setting latency to %d us\0A\00"
@.str.2 = [21 x i8] c"/dev/cpu_dma_latency\00"
@O_WRONLY = global i32 0
@.str.3 = [26 x i8] c"open /dev/cpu_dma_latency\00"
@.str.4 = [30 x i8] c"write to /dev/cpu_dma_latency\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %16
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %14)
store i32 2, i32* %3
br label %39
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @atoi(i8* %19)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = load i32, i32* @O_WRONLY
%24 = call i32 @open(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %23)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %16
%28 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %39
29: ; preds = %16
%30 = load i32, i32* %7
%31 = call i32 @write(i32 %30, i32* %6, i32 4)
%32 = sext i32 %31 to i64
%33 = icmp ne i64 %32, 4
br i1 %33, label %34, label %36
34: ; preds = %29
%35 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %39
36: ; preds = %29
br label %37
37: ; preds = %36, %37
%38 = call i32 (...) @pause()
br label %37
39: ; preds = %34, %27, %10
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [27 x i8] c"Usage: %s <latency in us>\0A\00"
@.str.1 = [26 x i8] c"setting latency to %d us\0A\00"
@.str.2 = [21 x i8] c"/dev/cpu_dma_latency\00"
@O_WRONLY = global i32 0
@.str.3 = [26 x i8] c"open /dev/cpu_dma_latency\00"
@.str.4 = [30 x i8] c"write to /dev/cpu_dma_latency\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %27
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32 @atoi(i8* %12)
store i32 %13, i32* %3
%14 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = load i32, i32* @O_WRONLY
%16 = tail call i32 @open(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %10
%19 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %27
20: ; preds = %10
%21 = call i32 @write(i32 %16, i32* %3, i32 4)
%22 = icmp eq i32 %21, 4
br i1 %22, label %25, label %23
23: ; preds = %20
%24 = tail call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %27
25: ; preds = %20, %25
%26 = tail call i32 (...) @pause()
br label %25
27: ; preds = %23, %18, %6
%28 = phi i32 [ 2, %6 ], [ 1, %18 ], [ 1, %23 ]
ret i32 %28
}