#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 */
int /*<<< orphan*/ abort();
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *);
int sscanf(char *, char *, int *);
int count_number_of_threads(void) {
  FILE *p = fopen("/proc/self/status", "r");
  while (!feof(p)) {
    int threadsleft;
    char buf[128];
    fgets(buf, sizeof buf, p);
    if (sscanf(buf, "Threads: %d\n", &threadsleft) != 1)
      continue;
    fclose(p);
    return threadsleft;
  }
  printf("Internal error, please send email to gback@cs.vt.edu\n");
  abort();
}


x86 assembly (clang, -O0):
<count_number_of_threads>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
lea    0xe7e(%rip),%rdi
lea    0xe89(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11b0 <count_number_of_threads+0x40>
jmp    11ff <count_number_of_threads+0x8f>
lea    -0x90(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x80,%esi
call   1060 <fgets@plt>
lea    -0x90(%rbp),%rdi
lea    0xe41(%rip),%rsi
lea    -0xc(%rbp),%rdx
call   1080 <sscanf@plt>
cmp    $0x1,%eax
je     11ea <count_number_of_threads+0x7a>
jmp    1192 <count_number_of_threads+0x22>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0xc(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret
lea    0xe1b(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>

x86 assembly (clang, -O2):
<count_number_of_threads>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x98,%rsp
lea    0xe7b(%rip),%rdi
lea    0xe86(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    11ef <count_number_of_threads+0x7f>
lea    0x10(%rsp),%r14
lea    0xe68(%rip),%r15
lea    0xc(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    $0x80,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1080 <sscanf@plt>
cmp    $0x1,%eax
je     1202 <count_number_of_threads+0x92>
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %eax,%eax
je     11c0 <count_number_of_threads+0x50>
lea    0xe2b(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1030 <abort@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0xc(%rsp),%eax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [18 x i8] c"/proc/self/status\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [13 x i8] c"Threads: %d\0A\00"
@.str.3 = [54 x i8] c"Internal error, please send email to gback@cs.vt.edu\0A\00"
define i32 @count_number_of_threads() {
%1 = alloca i32*
%2 = alloca i32
%3 = alloca [128 x i8]
%4 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %1
br label %5
5: ; preds = %17, %0
%6 = load i32*, i32** %1
%7 = call i32 @feof(i32* %6)
%8 = icmp ne i32 %7, 0
%9 = xor i1 %8, true
br i1 %9, label %10, label %22
10: ; preds = %5
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%12 = load i32*, i32** %1
%13 = call i32 @fgets(i8* %11, i32 128, i32* %12)
%14 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%15 = call i32 @sscanf(i8* %14, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%16 = icmp ne i32 %15, 1
br i1 %16, label %17, label %18
17: ; preds = %10
br label %5
18: ; preds = %10
%19 = load i32*, i32** %1
%20 = call i32 @fclose(i32* %19)
%21 = load i32, i32* %2
ret i32 %21
22: ; preds = %5
%23 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0))
%24 = call i32 (...) @abort()
unreachable
}

LLVM IR (-O2):
@.str = [18 x i8] c"/proc/self/status\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [13 x i8] c"Threads: %d\0A\00"
@str = [53 x i8] c"Internal error, please send email to gback@cs.vt.edu\00"
define i32 @count_number_of_threads() {
%1 = alloca i32
%2 = alloca [128 x i8]
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = call i32 @feof(i32* %3)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %16
6: ; preds = %0
%7 = bitcast i32* %1 to i8*
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
br label %9
9: ; preds = %6, %13
%10 = call i32 @fgets(i8* %8, i32 128, i32* %3)
%11 = call i32 @sscanf(i8* %8, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%12 = icmp eq i32 %11, 1
br i1 %12, label %19, label %13
13: ; preds = %9
%14 = call i32 @feof(i32* %3)
%15 = icmp eq i32 %14, 0
br i1 %15, label %9, label %16
16: ; preds = %13, %0
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str, i64 0, i64 0))
%18 = call i32 (...) @abort()
unreachable
19: ; preds = %9
%20 = call i32 @fclose(i32* %3)
%21 = load i32, i32* %1
ret i32 %21
}