#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 /*<<< orphan*/ O_RDWR;
int O_WRONLY;
int /*<<< orphan*/ S_IRWXU;
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, S_IRWXU)) <= 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) {
    int writeBytes = write(fdSnapshot, readBuf, readBytes);
    if (writeBytes != readBytes) {
      perror("write error");
      break;
    }
  }
  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    $0x1030,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    0x2e72(%rip),%rax
or     (%rax),%esi
mov    0x2e51(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jg     11d0 <doGrabKProfile+0x70>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe48(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
lea    0xe53(%rip),%rdi
call   1080 <perror@plt>
jmp    12b2 <doGrabKProfile+0x152>
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jg     1217 <doGrabKProfile+0xb7>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
jmp    12b2 <doGrabKProfile+0x152>
jmp    121c <doGrabKProfile+0xbc>
mov    -0x18(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    1279 <doGrabKProfile+0x119>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1024(%rbp)
mov    -0x1024(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     1274 <doGrabKProfile+0x114>
lea    0xe0c(%rip),%rdi
call   1080 <perror@plt>
jmp    1279 <doGrabKProfile+0x119>
jmp    121c <doGrabKProfile+0xbc>
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     12aa <doGrabKProfile+0x14a>
lea    0xddd(%rip),%rdi
call   1080 <perror@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<doGrabKProfile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %rsi,%rbp
mov    0x2e41(%rip),%rax
mov    0x2e7a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e57(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
xor    %eax,%eax
call   1070 <open@plt>
test   %eax,%eax
jle    1251 <doGrabKProfile+0xf1>
mov    %eax,%r14d
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
lea    0xe70(%rip),%rdi
xor    %eax,%eax
call   1070 <open@plt>
test   %eax,%eax
jle    1225 <doGrabKProfile+0xc5>
mov    %eax,%r15d
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    1203 <doGrabKProfile+0xa3>
mov    %eax,%ebp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
je     11d0 <doGrabKProfile+0x70>
lea    0xe78(%rip),%rdi
call   1080 <perror@plt>
mov    %r14d,%edi
call   1040 <close@plt>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
jne    1284 <doGrabKProfile+0x124>
mov    %r15d,%edi
jmp    123f <doGrabKProfile+0xdf>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
lea    0xdf9(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r14d,%edi
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <close@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
lea    0xd9f(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
lea    0xda7(%rip),%rdi
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <perror@plt>
lea    0xdf7(%rip),%rdi
call   1080 <perror@plt>
jmp    1220 <doGrabKProfile+0xc0>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = 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 = [12 x i8] c"write error\00"
@.str.5 = [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]
%9 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_WRONLY
%12 = load i32, i32* @O_CREAT
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IRWXU
%15 = call i32 (i8*, i32, ...) @open(i8* %10, i32 %13, i32 %14)
store i32 %15, i32* %5
%16 = icmp sle i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %4
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %60
22: ; preds = %2
%23 = load i32, i32* @O_RDWR
%24 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %23)
store i32 %24, i32* %6
%25 = icmp sle i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.3, i64 0, i64 0))
%29 = load i32, i32* %5
%30 = call i32 @close(i32 %29)
br label %60
31: ; preds = %22
br label %32
32: ; preds = %47, %31
%33 = load i32, i32* %6
%34 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%35 = call i32 @read(i32 %33, i8* %34, i32 4096)
store i32 %35, i32* %7
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %48
37: ; preds = %32
%38 = load i32, i32* %5
%39 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%40 = load i32, i32* %7
%41 = call i32 @write(i32 %38, i8* %39, i32 %40)
store i32 %41, i32* %9
%42 = load i32, i32* %9
%43 = load i32, i32* %7
%44 = icmp ne i32 %42, %43
br i1 %44, label %45, label %47
45: ; preds = %37
%46 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %48
47: ; preds = %37
br label %32
48: ; preds = %45, %32
%49 = load i32, i32* %5
%50 = call i32 @close(i32 %49)
%51 = load i32, i32* %6
%52 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%53 = call i32 @write(i32 %51, i8* %52, i32 1)
%54 = icmp ne i32 %53, 1
br i1 %54, label %55, label %57
55: ; preds = %48
%56 = call i32 @perror(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.5, i64 0, i64 0))
br label %57
57: ; preds = %55, %48
%58 = load i32, i32* %6
%59 = call i32 @close(i32 %58)
br label %60
60: ; preds = %57, %26, %17
ret void
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = 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 = [12 x i8] c"write error\00"
@.str.5 = [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 = load i32, i32* @S_IRWXU
%9 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %7, i32 %8)
%10 = icmp slt i32 %9, 1
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %1)
%14 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %39
15: ; preds = %2
%16 = load i32, i32* @O_RDWR
%17 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = icmp slt i32 %17, 1
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.3, i64 0, i64 0))
%22 = tail call i32 @close(i32 %9)
br label %39
23: ; preds = %15, %26
%24 = call i32 @read(i32 %17, i8* %4, i32 4096)
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %23
%27 = call i32 @write(i32 %9, i8* %4, i32 %24)
%28 = icmp eq i32 %27, %24
br i1 %28, label %23, label %29
29: ; preds = %26
%30 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %31
31: ; preds = %23, %29
%32 = tail call i32 @close(i32 %9)
%33 = call i32 @write(i32 %17, i8* %4, i32 1)
%34 = icmp eq i32 %33, 1
br i1 %34, label %37, label %35
35: ; preds = %31
%36 = tail call i32 @perror(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.5, i64 0, i64 0))
br label %37
37: ; preds = %35, %31
%38 = tail call i32 @close(i32 %17)
br label %39
39: ; preds = %37, %19, %11
ret void
}