#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 ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
int /*<<< orphan*/ strncmp(char const *, char *, int);
const char *read_wchan_file(unsigned pid) {
  static char buf[64];
  const char *ret = buf;
  ssize_t num;
  int fd;
  snprintf(buf, sizeof buf, "/proc/%d/wchan", pid);
  fd = open(buf, O_RDONLY);
  if (fd == -1) {
    return "?";
  }
  num = read(fd, buf, sizeof buf - 1);
  close(fd);
  if (num < 1) {
    return "?";
  }
  buf[num] = '\0';
  if (buf[0] == '0' && buf[1] == '\0') {
    return "-";
  }
  if (*ret == '.') {
    ret++;
  }
  switch (*ret) {
  case 's':
    if (!strncmp(ret, "sys_", 4)) {
      ret += 4;
    }
    break;
  case 'd':
    if (!strncmp(ret, "do_", 3)) {
      ret += 3;
    }
    break;
  case '_':
    while (*ret == '_') {
      ret++;
    }
    break;
  }
  return ret;
}


x86 assembly (clang, -O0):
<read_wchan_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
lea    0x2efe(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%ecx
lea    0x2ef0(%rip),%rdi
mov    $0x40,%esi
lea    0xe84(%rip),%rdx
call   1040 <snprintf@plt>
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
lea    0x2ecf(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11b3 <read_wchan_file+0x63>
lea    0xe65(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1312 <read_wchan_file+0x1c2>
mov    -0x20(%rbp),%edi
lea    0x2ea3(%rip),%rsi
mov    $0x3f,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x1,-0x1c(%rbp)
jge    11ec <read_wchan_file+0x9c>
lea    0xe2c(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1312 <read_wchan_file+0x1c2>
movslq -0x1c(%rbp),%rcx
lea    0x2e69(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
movsbl 0x2e5e(%rip),%eax
cmp    $0x30,%eax
jne    122b <read_wchan_file+0xdb>
movsbl 0x2e4f(%rip),%eax
cmp    $0x0,%eax
jne    122b <read_wchan_file+0xdb>
lea    0xdef(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1312 <read_wchan_file+0x1c2>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1247 <read_wchan_file+0xf7>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x24(%rbp)
sub    $0x5f,%eax
je     12df <read_wchan_file+0x18f>
jmp    125f <read_wchan_file+0x10f>
mov    -0x24(%rbp),%eax
sub    $0x64,%eax
je     12b0 <read_wchan_file+0x160>
jmp    1270 <read_wchan_file+0x120>
mov    -0x24(%rbp),%eax
sub    $0x73,%eax
jne    130a <read_wchan_file+0x1ba>
jmp    1281 <read_wchan_file+0x131>
mov    -0x18(%rbp),%rdi
lea    0xd87(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    12ab <read_wchan_file+0x15b>
mov    -0x18(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x18(%rbp)
jmp    130a <read_wchan_file+0x1ba>
mov    -0x18(%rbp),%rdi
lea    0xd5d(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    12da <read_wchan_file+0x18a>
mov    -0x18(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x18(%rbp)
jmp    130a <read_wchan_file+0x1ba>
jmp    12e4 <read_wchan_file+0x194>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5f,%eax
jne    1305 <read_wchan_file+0x1b5>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    12e4 <read_wchan_file+0x194>
jmp    130a <read_wchan_file+0x1ba>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_wchan_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ecx
lea    0x2f00(%rip),%rbx
lea    0xe99(%rip),%rdx
mov    %rbx,%rdi
mov    $0x40,%esi
call   1040 <snprintf@plt>
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
lea    0xe83(%rip),%rbx
cmp    $0xffffffff,%eax
je     1273 <read_wchan_file+0x123>
mov    %eax,%ebp
lea    0x2ec2(%rip),%r14
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x3f,%edx
call   1060 <read@plt>
mov    %eax,%r15d
mov    %ebp,%edi
call   1050 <close@plt>
test   %r15d,%r15d
jle    1273 <read_wchan_file+0x123>
mov    %r15d,%eax
movb   $0x0,(%rax,%r14,1)
mov    0x2e92(%rip),%al
cmp    $0x30,%al
jne    11e7 <read_wchan_file+0x97>
cmpb   $0x0,0x2e88(%rip)
jne    11e7 <read_wchan_file+0x97>
lea    0xe2f(%rip),%rbx
jmp    1273 <read_wchan_file+0x123>
cmp    $0x2e,%al
lea    0x2e71(%rip),%r15
cmovne %r14,%r15
mov    (%r15),%cl
movsbl %cl,%edx
cmp    $0x73,%edx
je     1225 <read_wchan_file+0xd5>
cmp    $0x64,%edx
je     124d <read_wchan_file+0xfd>
cmp    $0x5f,%edx
jne    1220 <read_wchan_file+0xd0>
cmp    $0x5f,%cl
jne    1220 <read_wchan_file+0xd0>
xchg   %ax,%ax
lea    0x1(%r15),%rbx
cmpb   $0x5f,0x1(%r15)
mov    %rbx,%r15
je     1210 <read_wchan_file+0xc0>
jmp    1273 <read_wchan_file+0x123>
mov    %r15,%rbx
jmp    1273 <read_wchan_file+0x123>
xor    %ebx,%ebx
cmp    $0x2e,%al
sete   %bl
lea    0xde0(%rip),%rsi
mov    %r15,%rdi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
lea    0x4(%rbx,%r14,1),%rbx
cmovne %r15,%rbx
jmp    1273 <read_wchan_file+0x123>
xor    %ebx,%ebx
cmp    $0x2e,%al
sete   %bl
lea    0xdbd(%rip),%rsi
mov    %r15,%rdi
mov    $0x3,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
lea    0x3(%rbx,%r14,1),%rbx
cmovne %r15,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@read_wchan_file.buf = internal global [64 x i8] zeroinitializer
@.str = [15 x i8] c"/proc/%d/wchan\00"
@O_RDONLY = global i32 0
@.str.1 = [2 x i8] c"?\00"
@.str.2 = [2 x i8] c"-\00"
@.str.3 = [5 x i8] c"sys_\00"
@.str.4 = [4 x i8] c"do_\00"
define i8* @read_wchan_file(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i8* getelementptr inbounds ([64 x i8], [64 x i8]* @read_wchan_file.buf, i64 0, i64 0), i8** %4
%7 = load i32, i32* %3
%8 = call i32 @snprintf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @read_wchan_file.buf, i64 0, i64 0), i32 64, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @read_wchan_file.buf, i64 0, i64 0), i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %1
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %2
br label %74
14: ; preds = %1
%15 = load i32, i32* %6
%16 = call i32 @read(i32 %15, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @read_wchan_file.buf, i64 0, i64 0), i32 63)
store i32 %16, i32* %5
%17 = load i32, i32* %6
%18 = call i32 @close(i32 %17)
%19 = load i32, i32* %5
%20 = icmp slt i32 %19, 1
br i1 %20, label %21, label %22
21: ; preds = %14
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %2
br label %74
22: ; preds = %14
%23 = load i32, i32* %5
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [64 x i8], [64 x i8]* @read_wchan_file.buf, i64 0, i64 %24
store i8 0, i8* %25
%26 = load i8, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @read_wchan_file.buf, i64 0, i64 0)
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 48
br i1 %28, label %29, label %34
29: ; preds = %22
%30 = load i8, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @read_wchan_file.buf, i64 0, i64 1)
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %29
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %2
br label %74
34: ; preds = %29, %22
%35 = load i8*, i8** %4
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 46
br i1 %38, label %39, label %42
39: ; preds = %34
%40 = load i8*, i8** %4
%41 = getelementptr inbounds i8, i8* %40, i32 1
store i8* %41, i8** %4
br label %42
42: ; preds = %39, %34
%43 = load i8*, i8** %4
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
switch i32 %45, label %72 [
i32 115, label %46
i32 100, label %54
i32 95, label %62
]
46: ; preds = %42
%47 = load i8*, i8** %4
%48 = call i32 @strncmp(i8* %47, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 4)
%49 = icmp ne i32 %48, 0
br i1 %49, label %53, label %50
50: ; preds = %46
%51 = load i8*, i8** %4
%52 = getelementptr inbounds i8, i8* %51, i64 4
store i8* %52, i8** %4
br label %53
53: ; preds = %50, %46
br label %72
54: ; preds = %42
%55 = load i8*, i8** %4
%56 = call i32 @strncmp(i8* %55, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 3)
%57 = icmp ne i32 %56, 0
br i1 %57, label %61, label %58
58: ; preds = %54
%59 = load i8*, i8** %4
%60 = getelementptr inbounds i8, i8* %59, i64 3
store i8* %60, i8** %4
br label %61
61: ; preds = %58, %54
br label %72
62: ; preds = %42
br label %63
63: ; preds = %68, %62
%64 = load i8*, i8** %4
%65 = load i8, i8* %64
%66 = sext i8 %65 to i32
%67 = icmp eq i32 %66, 95
br i1 %67, label %68, label %71
68: ; preds = %63
%69 = load i8*, i8** %4
%70 = getelementptr inbounds i8, i8* %69, i32 1
store i8* %70, i8** %4
br label %63
71: ; preds = %63
br label %72
72: ; preds = %42, %71, %61, %53
%73 = load i8*, i8** %4
store i8* %73, i8** %2
br label %74
74: ; preds = %72, %33, %21, %13
%75 = load i8*, i8** %2
ret i8* %75
}

LLVM IR (-O2):
@read_wchan_file.buf = internal global [64 x i8] zeroinitializer
@.str = [15 x i8] c"/proc/%d/wchan\00"
@O_RDONLY = global i32 0
@.str.1 = [2 x i8] c"?\00"
@.str.2 = [2 x i8] c"-\00"
@.str.3 = [5 x i8] c"sys_\00"
@.str.4 = [4 x i8] c"do_\00"
define i8* @read_wchan_file(i32 %0) {
%2 = tail call i32 @snprintf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @read_wchan_file.buf, i64 0, i64 0), i32 64, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %0)
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @read_wchan_file.buf, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %40, label %6
6: ; preds = %1
%7 = tail call i32 @read(i32 %4, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @read_wchan_file.buf, i64 0, i64 0), i32 63)
%8 = tail call i32 @close(i32 %4)
%9 = icmp slt i32 %7, 1
br i1 %9, label %40, label %10
10: ; preds = %6
%11 = zext i32 %7 to i64
%12 = getelementptr inbounds [64 x i8], [64 x i8]* @read_wchan_file.buf, i64 0, i64 %11
store i8 0, i8* %12
%13 = load i8, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @read_wchan_file.buf, i64 0, i64 0)
%14 = icmp eq i8 %13, 48
%15 = load i8, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @read_wchan_file.buf, i64 0, i64 1)
%16 = icmp eq i8 %15, 0
%17 = select i1 %14, i1 %16, i1 false
br i1 %17, label %40, label %18
18: ; preds = %10
%19 = icmp eq i8 %13, 46
%20 = select i1 %19, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @read_wchan_file.buf, i64 0, i64 1), i8* getelementptr inbounds ([64 x i8], [64 x i8]* @read_wchan_file.buf, i64 0, i64 0)
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
switch i32 %22, label %40 [
i32 115, label %25
i32 100, label %30
i32 95, label %23
]
23: ; preds = %18
%24 = icmp eq i8 %21, 95
br i1 %24, label %35, label %40
25: ; preds = %18
%26 = tail call i32 @strncmp(i8* %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 4)
%27 = icmp eq i32 %26, 0
%28 = select i1 %19, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @read_wchan_file.buf, i64 0, i64 5), i8* getelementptr inbounds ([64 x i8], [64 x i8]* @read_wchan_file.buf, i64 0, i64 4)
%29 = select i1 %27, i8* %28, i8* %20
br label %40
30: ; preds = %18
%31 = tail call i32 @strncmp(i8* %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 3)
%32 = icmp eq i32 %31, 0
%33 = select i1 %19, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @read_wchan_file.buf, i64 0, i64 4), i8* getelementptr inbounds ([64 x i8], [64 x i8]* @read_wchan_file.buf, i64 0, i64 3)
%34 = select i1 %32, i8* %33, i8* %20
br label %40
35: ; preds = %23, %35
%36 = phi i8* [ %37, %35 ], [ %20, %23 ]
%37 = getelementptr inbounds i8, i8* %36, i64 1
%38 = load i8, i8* %37
%39 = icmp eq i8 %38, 95
br i1 %39, label %35, label %40
40: ; preds = %35, %23, %30, %25, %18, %10, %6, %1
%41 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %1 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %6 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), %10 ], [ %20, %18 ], [ %29, %25 ], [ %34, %30 ], [ %20, %23 ], [ %37, %35 ]
ret i8* %41
}