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

/* Variables and functions */
scalar_t__ EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ fscanf(int /*<<< orphan*/ *, char *, char *, char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
int print_kernel_stack(int pid) {
  FILE *file;
  char file_name[128];
  char addr[512];
  char name[512];
  sprintf(file_name, "/proc/%d/stack", pid);
  if ((file = fopen(file_name, "r")) == NULL) {
    fprintf(stderr, "cannot open %s\n", file_name);
    return -1;
  }
  while (fscanf(file, "%s %s", addr, name) != EOF)
    printf("%s\n", name);
  fclose(file);
  return 0;
}


x86 assembly (clang, -O0):
<print_kernel_stack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x490,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe81(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x90(%rbp),%rdi
lea    0xe7d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11cd <print_kernel_stack+0x6d>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
lea    -0x90(%rbp),%rdx
lea    0xe55(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1228 <print_kernel_stack+0xc8>
jmp    11d2 <print_kernel_stack+0x72>
mov    -0x10(%rbp),%rdi
lea    -0x290(%rbp),%rdx
lea    -0x490(%rbp),%rcx
lea    0xe36(%rip),%rsi
call   1050 <fscanf@plt>
mov    0x2df1(%rip),%rcx
cmp    (%rcx),%rax
je     1218 <print_kernel_stack+0xb8>
lea    -0x490(%rbp),%rsi
lea    0xe0f(%rip),%rdi
call   1040 <printf@plt>
jmp    11d2 <print_kernel_stack+0x72>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x490,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_kernel_stack>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x488,%rsp
mov    %edi,%edx
lea    0xe86(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1080 <sprintf@plt>
lea    0xe83(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     121e <print_kernel_stack+0xbe>
mov    %rax,%r15
lea    0xe7a(%rip),%rsi
lea    0x280(%rsp),%rdx
lea    0x80(%rsp),%rcx
mov    %rax,%rdi
call   1050 <fscanf@plt>
mov    0x2e22(%rip),%rbp
cmp    0x0(%rbp),%rax
je     1212 <print_kernel_stack+0xb2>
lea    0xe4a(%rip),%r14
lea    0x80(%rsp),%rbx
lea    0xe3f(%rip),%r12
lea    0x280(%rsp),%r13
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
mov    %rbx,%rcx
call   1050 <fscanf@plt>
cmp    0x0(%rbp),%rax
jne    11f0 <print_kernel_stack+0x90>
mov    %r15,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    123b <print_kernel_stack+0xdb>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
lea    0xde3(%rip),%rsi
mov    %rsp,%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x488,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"/proc/%d/stack\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [16 x i8] c"cannot open %s\0A\00"
@.str.3 = [6 x i8] c"%s %s\00"
@EOF = global i64 0
@.str.4 = [4 x i8] c"%s\0A\00"
define i32 @print_kernel_stack(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca [128 x i8]
%6 = alloca [512 x i8]
%7 = alloca [512 x i8]
store i32 %0, i32* %3
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%9 = load i32, i32* %3
%10 = call i32 @sprintf(i8* %8, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %4
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %1
%15 = load i32, i32* @stderr
%16 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %16)
store i32 -1, i32* %2
br label %32
18: ; preds = %1
br label %19
19: ; preds = %26, %18
%20 = load i32*, i32** %4
%21 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%22 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%23 = call i64 @fscanf(i32* %20, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %21, i8* %22)
%24 = load i64, i64* @EOF
%25 = icmp ne i64 %23, %24
br i1 %25, label %26, label %29
26: ; preds = %19
%27 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%28 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %27)
br label %19
29: ; preds = %19
%30 = load i32*, i32** %4
%31 = call i32 @fclose(i32* %30)
store i32 0, i32* %2
br label %32
32: ; preds = %29, %14
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/%d/stack\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [16 x i8] c"cannot open %s\0A\00"
@.str.3 = [6 x i8] c"%s %s\00"
@EOF = global i64 0
@.str.4 = [4 x i8] c"%s\0A\00"
define i32 @print_kernel_stack(i32 %0) {
%2 = alloca [128 x i8]
%3 = alloca [512 x i8]
%4 = alloca [512 x i8]
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%8 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %0)
%9 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %15, label %11
11: ; preds = %1
%12 = call i64 @fscanf(i32* %9, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %6, i8* %7)
%13 = load i64, i64* @EOF
%14 = icmp eq i64 %12, %13
br i1 %14, label %23, label %18
15: ; preds = %1
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %5)
br label %25
18: ; preds = %11, %18
%19 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %7)
%20 = call i64 @fscanf(i32* %9, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %6, i8* %7)
%21 = load i64, i64* @EOF
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %18
23: ; preds = %18, %11
%24 = call i32 @fclose(i32* %9)
br label %25
25: ; preds = %23, %15
%26 = phi i32 [ -1, %15 ], [ 0, %23 ]
ret i32 %26
}