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

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
void doGrabKProfile(int jitterusec, char *fileName) {
  int fdSnapshot;
  int fdProfile;
  int readBytes;
  char readBuf[4096];
  if ((fdSnapshot = open(fileName, O_WRONLY | O_CREAT)) <= 0) {
    fprintf(stderr, "Could not open file %s.\n", fileName);
    perror("Error:");
    return;
  }
  if ((fdProfile = open("/proc/profile", O_RDWR)) <= 0) {
    fprintf(stderr, "Could not open file /proc/profile. Make sure you booted "
                    "with profile=2\n");
    close(fdSnapshot);
    return;
  }
  while ((readBytes = read(fdProfile, readBuf, sizeof(readBuf))) > 0) {
    write(fdSnapshot, readBuf, readBytes);
  }
  close(fdSnapshot);
  if (write(fdProfile, readBuf, 1) != 1) {
    perror("Could Not clear profile by writing to /proc/profile:");
  }
  close(fdProfile);
}


x86 assembly (clang, -O0):
<doGrabKProfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
mov    0x2e72(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jg     11c5 <doGrabKProfile+0x65>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe53(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
lea    0xe5e(%rip),%rdi
call   1080 <perror@plt>
jmp    127f <doGrabKProfile+0x11f>
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
lea    0xe4b(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jg     120a <doGrabKProfile+0xaa>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
lea    0xe38(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
jmp    127f <doGrabKProfile+0x11f>
jmp    120f <doGrabKProfile+0xaf>
mov    -0x18(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    1246 <doGrabKProfile+0xe6>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
jmp    120f <doGrabKProfile+0xaf>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     1277 <doGrabKProfile+0x117>
lea    0xe04(%rip),%rdi
call   1080 <perror@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<doGrabKProfile>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1000,%rsp
mov    %rsi,%rbp
mov    0x2e4b(%rip),%rax
mov    0x2e7c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbp,%rdi
call   1070 <open@plt>
test   %eax,%eax
jle    123a <doGrabKProfile+0xda>
mov    %eax,%r14d
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
lea    0xe7d(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
jle    1210 <doGrabKProfile+0xb0>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    11f0 <doGrabKProfile+0x90>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %eax,%eax
jg     11d0 <doGrabKProfile+0x70>
mov    %r14d,%edi
call   1040 <close@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
jne    126b <doGrabKProfile+0x10b>
mov    %ebp,%edi
jmp    122a <doGrabKProfile+0xca>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
lea    0xe0e(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r14d,%edi
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <close@plt>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
lea    0xdb6(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
lea    0xdbe(%rip),%rdi
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1080 <perror@plt>
lea    0xe04(%rip),%rdi
call   1080 <perror@plt>
jmp    120c <doGrabKProfile+0xac>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"Could not open file %s.\0A\00"
@.str.1 = [7 x i8] c"Error:\00"
@.str.2 = [14 x i8] c"/proc/profile\00"
@O_RDWR = global i32 0
@.str.3 = [72 x i8] c"Could not open file /proc/profile. Make sure you booted with profile=2\0A\00"
@.str.4 = [53 x i8] c"Could Not clear profile by writing to /proc/profile:\00"
define void @doGrabKProfile(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [4096 x i8]
store i32 %0, i32* %3
store i8* %1, i8** %4
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_WRONLY
%11 = load i32, i32* @O_CREAT
%12 = or i32 %10, %11
%13 = call i32 @open(i8* %9, i32 %12)
store i32 %13, i32* %5
%14 = icmp sle i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %4
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %52
20: ; preds = %2
%21 = load i32, i32* @O_RDWR
%22 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %21)
store i32 %22, i32* %6
%23 = icmp sle i32 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.3, i64 0, i64 0))
%27 = load i32, i32* %5
%28 = call i32 @close(i32 %27)
br label %52
29: ; preds = %20
br label %30
30: ; preds = %35, %29
%31 = load i32, i32* %6
%32 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%33 = call i32 @read(i32 %31, i8* %32, i32 4096)
store i32 %33, i32* %7
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %30
%36 = load i32, i32* %5
%37 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%38 = load i32, i32* %7
%39 = call i32 @write(i32 %36, i8* %37, i32 %38)
br label %30
40: ; preds = %30
%41 = load i32, i32* %5
%42 = call i32 @close(i32 %41)
%43 = load i32, i32* %6
%44 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%45 = call i32 @write(i32 %43, i8* %44, i32 1)
%46 = icmp ne i32 %45, 1
br i1 %46, label %47, label %49
47: ; preds = %40
%48 = call i32 @perror(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0))
br label %49
49: ; preds = %47, %40
%50 = load i32, i32* %6
%51 = call i32 @close(i32 %50)
br label %52
52: ; preds = %49, %24, %15
ret void
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"Could not open file %s.\0A\00"
@.str.1 = [7 x i8] c"Error:\00"
@.str.2 = [14 x i8] c"/proc/profile\00"
@O_RDWR = global i32 0
@.str.3 = [72 x i8] c"Could not open file /proc/profile. Make sure you booted with profile=2\0A\00"
@.str.4 = [53 x i8] c"Could Not clear profile by writing to /proc/profile:\00"
define void @doGrabKProfile(i32 %0, i8* %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* %1, i32 %7)
%9 = icmp slt i32 %8, 1
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %1)
%13 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %38
14: ; preds = %2
%15 = load i32, i32* @O_RDWR
%16 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = icmp slt i32 %16, 1
br i1 %17, label %21, label %18
18: ; preds = %14
%19 = call i32 @read(i32 %16, i8* %4, i32 4096)
%20 = icmp sgt i32 %19, 0
br i1 %20, label %25, label %30
21: ; preds = %14
%22 = load i32, i32* @stderr
%23 = tail call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.3, i64 0, i64 0))
%24 = tail call i32 @close(i32 %8)
br label %38
25: ; preds = %18, %25
%26 = phi i32 [ %28, %25 ], [ %19, %18 ]
%27 = call i32 @write(i32 %8, i8* %4, i32 %26)
%28 = call i32 @read(i32 %16, i8* %4, i32 4096)
%29 = icmp sgt i32 %28, 0
br i1 %29, label %25, label %30
30: ; preds = %25, %18
%31 = tail call i32 @close(i32 %8)
%32 = call i32 @write(i32 %16, i8* %4, i32 1)
%33 = icmp eq i32 %32, 1
br i1 %33, label %36, label %34
34: ; preds = %30
%35 = tail call i32 @perror(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0))
br label %36
36: ; preds = %34, %30
%37 = tail call i32 @close(i32 %16)
br label %38
38: ; preds = %36, %21, %10
ret void
}