#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*/ path;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ memcmp(char *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ test_msg(char *, char *, int, char *);
int num_of_handles(int fd) {
  char path[64];
  char buf[512];
  int ret = 0;
  FILE *f;
  snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", fd);
  f = fopen(path, "r");
  if (!f) {
    test_msg("ERR: %s:%d: "
             "Can't open %s",
             "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
             "2022-01-22/02-19-57/repos/AuthenticEshkinKot/criu/refs/heads/"
             "master/test/zdtm/live/static/inotify02.c",
             40, path);
    return -1;
  }
  while (fgets(buf, sizeof(buf), f)) {
    if (memcmp(buf, "inotify ", 8))
      continue;
    ret++;
  }
  fclose(f);
  return ret;
}


x86 assembly (clang, -O0):
<num_of_handles>:
push   %rbp
mov    %rsp,%rbp
sub    $0x260,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x254(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x40,%esi
lea    0xe75(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x50(%rbp),%rdi
lea    0xe7a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x260(%rbp)
cmpq   $0x0,-0x260(%rbp)
jne    11dd <num_of_handles+0x7d>
lea    -0x50(%rbp),%rcx
lea    0xe57(%rip),%rdi
lea    0xe6a(%rip),%rsi
mov    $0x28,%edx
call   1050 <test_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1254 <num_of_handles+0xf4>
jmp    11e2 <num_of_handles+0x82>
lea    -0x250(%rbp),%rdi
mov    -0x260(%rbp),%rdx
mov    $0x200,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     123f <num_of_handles+0xdf>
lea    -0x250(%rbp),%rdi
lea    0xec5(%rip),%rsi
mov    $0x8,%edx
call   1060 <memcmp@plt>
cmp    $0x0,%rax
je     122b <num_of_handles+0xcb>
jmp    11e2 <num_of_handles+0x82>
mov    -0x254(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x254(%rbp)
jmp    11e2 <num_of_handles+0x82>
mov    -0x260(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x254(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x260,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<num_of_handles>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x248,%rsp
mov    %edi,%ecx
lea    0xe8a(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
call   1040 <snprintf@plt>
lea    0xe88(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1206 <num_of_handles+0xa6>
mov    %rax,%r15
lea    0x40(%rsp),%rdi
mov    $0x200,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     11fc <num_of_handles+0x9c>
lea    0xf1a(%rip),%r14
lea    0x40(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x8,%edx
call   1060 <memcmp@plt>
cmp    $0x1,%rax
adc    $0x0,%ebp
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %r15,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
jne    11d0 <num_of_handles+0x70>
mov    %r15,%rdi
call   1030 <fclose@plt>
jmp    1226 <num_of_handles+0xc6>
lea    0xe0a(%rip),%rdi
lea    0xe1d(%rip),%rsi
mov    %rsp,%rcx
mov    $0x28,%edx
call   1050 <test_msg@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x248,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"/proc/self/fdinfo/%d\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [26 x i8] c"ERR: %s:%d: Can't open %s\00"
@.str.3 = [166 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/AuthenticEshkinKot/criu/refs/heads/master/test/zdtm/live/static/inotify02.c\00"
@.str.4 = [9 x i8] c"inotify \00"
define i32 @num_of_handles(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [64 x i8]
%5 = alloca [512 x i8]
%6 = alloca i32
%7 = alloca i32*
store i32 %0, i32* %3
store i32 0, i32* %6
%8 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%9 = load i32, i32* %3
%10 = call i32 @snprintf(i8* %8, i32 64, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [64 x i8], [64 x i8]* %4, 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** %7
%13 = load i32*, i32** %7
%14 = icmp ne i32* %13, null
br i1 %14, label %18, label %15
15: ; preds = %1
%16 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%17 = call i32 @test_msg(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([166 x i8], [166 x i8]* @.str.3, i64 0, i64 0), i32 40, i8* %16)
store i32 -1, i32* %2
br label %36
18: ; preds = %1
br label %19
19: ; preds = %29, %28, %18
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%21 = load i32*, i32** %7
%22 = call i64 @fgets(i8* %20, i32 512, i32* %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %32
24: ; preds = %19
%25 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%26 = call i64 @memcmp(i8* %25, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 8)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %24
br label %19
29: ; preds = %24
%30 = load i32, i32* %6
%31 = add nsw i32 %30, 1
store i32 %31, i32* %6
br label %19
32: ; preds = %19
%33 = load i32*, i32** %7
%34 = call i32 @fclose(i32* %33)
%35 = load i32, i32* %6
store i32 %35, i32* %2
br label %36
36: ; preds = %32, %15
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
@.str = [21 x i8] c"/proc/self/fdinfo/%d\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [26 x i8] c"ERR: %s:%d: Can't open %s\00"
@.str.3 = [166 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/AuthenticEshkinKot/criu/refs/heads/master/test/zdtm/live/static/inotify02.c\00"
@.str.4 = [9 x i8] c"inotify \00"
define i32 @num_of_handles(i32 %0) {
%2 = alloca [64 x i8]
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%6 = call i32 @snprintf(i8* %4, i32 64, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %12, label %9
9: ; preds = %1
%10 = call i64 @fgets(i8* %5, i32 512, i32* %7)
%11 = icmp eq i64 %10, 0
br i1 %11, label %22, label %14
12: ; preds = %1
%13 = call i32 @test_msg(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([166 x i8], [166 x i8]* @.str.3, i64 0, i64 0), i32 40, i8* %4)
br label %25
14: ; preds = %9, %14
%15 = phi i32 [ %19, %14 ], [ 0, %9 ]
%16 = call i64 @memcmp(i8* %5, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 8)
%17 = icmp eq i64 %16, 0
%18 = zext i1 %17 to i32
%19 = add nuw nsw i32 %15, %18
%20 = call i64 @fgets(i8* %5, i32 512, i32* %7)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %14
22: ; preds = %14, %9
%23 = phi i32 [ 0, %9 ], [ %19, %14 ]
%24 = call i32 @fclose(i32* %7)
br label %25
25: ; preds = %22, %12
%26 = phi i32 [ %23, %22 ], [ -1, %12 ]
ret i32 %26
}