#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*/ buf;
typedef int __u64;

/* Variables and functions */
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int time_get_ns();
int /*<<< orphan*/ write(int, char *, int);
void test_task_rename(int cpu) {
  __u64 start_time;
  char buf[] = "test\n";
  int i, fd;
  fd = open("/proc/self/comm", O_WRONLY | O_TRUNC);
  if (fd < 0) {
    printf("couldn't open /proc\n");
    exit(1);
  }
  start_time = time_get_ns();
  for (i = 0; i < 1000000; i++) {
    write(fd, buf, sizeof(buf));
  }
  printf("task_rename:%d: %lld events per sec\n", cpu,
         1000000 * 1000000000ll / (time_get_ns() - start_time));
  close(fd);
}


x86 assembly (clang, -O0):
<test_task_rename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    0xe8f(%rip),%eax
mov    %eax,-0xe(%rbp)
mov    0xe89(%rip),%ax
mov    %ax,-0xa(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
mov    0x2e69(%rip),%rax
or     (%rax),%esi
lea    0xe6e(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11c2 <test_task_rename+0x62>
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1060 <time_get_ns@plt>
mov    %eax,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0xf4240,-0x14(%rbp)
jge    11ff <test_task_rename+0x9f>
mov    -0x18(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11d3 <test_task_rename+0x73>
mov    -0x4(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1060 <time_get_ns@plt>
mov    -0x1c(%rbp),%esi
sub    -0x8(%rbp),%eax
movslq %eax,%rcx
movabs $0x38d7ea4c68000,%rax
cqto
idiv   %rcx
mov    %rax,%rdx
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_task_rename>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
movw   $0xa,0xc(%rsp)
movl   $0x74736574,0x8(%rsp)
74
mov    0x2e3b(%rip),%rax
mov    0x2e5c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe5f(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1227 <test_task_rename+0xb7>
mov    %eax,%ebp
xor    %eax,%eax
call   1070 <time_get_ns@plt>
mov    %eax,%r15d
mov    $0xf4240,%ebx
lea    0x8(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x6,%edx
call   1040 <write@plt>
add    $0xffffffff,%ebx
jne    11d0 <test_task_rename+0x60>
xor    %eax,%eax
call   1070 <time_get_ns@plt>
sub    %r15d,%eax
movslq %eax,%rcx
movabs $0x38d7ea4c68000,%rax
xor    %edx,%edx
idiv   %rcx
lea    0xe0f(%rip),%rdi
mov    %r14d,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <close@plt>
lea    0xe0d(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@__const.test_task_rename.buf = [6 x i8] c"test\0A\00"
@.str = [16 x i8] c"/proc/self/comm\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [21 x i8] c"couldn't open /proc\0A\00"
@.str.2 = [37 x i8] c"task_rename:%d: %lld events per sec\0A\00"
define void @test_task_rename(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [6 x i8]
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %2
%7 = bitcast [6 x i8]* %4 to i8*
%8 = load i32, i32* @O_WRONLY
%9 = load i32, i32* @O_TRUNC
%10 = or i32 %8, %9
%11 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %1
%18 = call i32 (...) @time_get_ns()
store i32 %18, i32* %3
store i32 0, i32* %5
br label %19
19: ; preds = %26, %17
%20 = load i32, i32* %5
%21 = icmp slt i32 %20, 1000000
br i1 %21, label %22, label %29
22: ; preds = %19
%23 = load i32, i32* %6
%24 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 0
%25 = call i32 @write(i32 %23, i8* %24, i32 6)
br label %26
26: ; preds = %22
%27 = load i32, i32* %5
%28 = add nsw i32 %27, 1
store i32 %28, i32* %5
br label %19
29: ; preds = %19
%30 = load i32, i32* %2
%31 = call i32 (...) @time_get_ns()
%32 = load i32, i32* %3
%33 = sub nsw i32 %31, %32
%34 = sext i32 %33 to i64
%35 = sdiv i64 1000000000000000, %34
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %30, i64 %35)
%37 = load i32, i32* %6
%38 = call i32 @close(i32 %37)
ret void
}

LLVM IR (-O2):
@__const.test_task_rename.buf = [6 x i8] c"test\0A\00"
@.str = [16 x i8] c"/proc/self/comm\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [37 x i8] c"task_rename:%d: %lld events per sec\0A\00"
@str = [20 x i8] c"couldn't open /proc\00"
define void @test_task_rename(i32 %0) {
%2 = alloca [6 x i8]
%3 = getelementptr inbounds [6 x i8], [6 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @O_WRONLY
%5 = load i32, i32* @O_TRUNC
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %1
%13 = tail call i32 (...) @time_get_ns()
br label %14
14: ; preds = %12, %14
%15 = phi i32 [ 0, %12 ], [ %17, %14 ]
%16 = call i32 @write(i32 %7, i8* %3, i32 6)
%17 = add nuw nsw i32 %15, 1
%18 = icmp eq i32 %17, 1000000
br i1 %18, label %19, label %14
19: ; preds = %14
%20 = tail call i32 (...) @time_get_ns()
%21 = sub nsw i32 %20, %13
%22 = sext i32 %21 to i64
%23 = sdiv i64 1000000000000000, %22
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %0, i64 %23)
%25 = tail call i32 @close(i32 %7)
ret void
}