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

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ logger(int, int /*<<< orphan*/, char *);
int sscanf(char *, char *, int *);
int get_thrmax(int *thrmax) {
  FILE *fd;
  char str[128];
  if (thrmax == NULL)
    return 1;
  if ((fd = fopen("/proc/sys/kernel/threads-max", "r")) == NULL) {
    logger(-1, errno,
           "Unable to open "
           "/proc/sys/kernel/threads-max");
    return 1;
  }
  if (fgets(str, sizeof(str), fd) == NULL) {
    fclose(fd);
    return 1;
  }
  fclose(fd);
  if (sscanf(str, "%du", thrmax) != 1)
    return 1;
  return 0;
}


x86 assembly (clang, -O0):
<get_thrmax>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1176 <get_thrmax+0x26>
movl   $0x1,-0x4(%rbp)
jmp    122d <get_thrmax+0xdd>
lea    0xe94(%rip),%rdi
lea    0xe7c(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11bd <get_thrmax+0x6d>
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
mov    $0xffffffff,%edi
lea    0xe56(%rip),%rdx
call   1050 <logger@plt>
movl   $0x1,-0x4(%rbp)
jmp    122d <get_thrmax+0xdd>
lea    -0xa0(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x80,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    11f1 <get_thrmax+0xa1>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    122d <get_thrmax+0xdd>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0xa0(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe22(%rip),%rsi
call   1060 <sscanf@plt>
cmp    $0x1,%eax
je     1226 <get_thrmax+0xd6>
movl   $0x1,-0x4(%rbp)
jmp    122d <get_thrmax+0xdd>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_thrmax>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    $0x1,%r14d
test   %rdi,%rdi
je     11de <get_thrmax+0x8e>
mov    %rdi,%rbx
lea    0xe9f(%rip),%rdi
lea    0xe87(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11c4 <get_thrmax+0x74>
mov    %rax,%rbp
mov    %rsp,%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
mov    %rax,%r15
mov    %rbp,%rdi
call   1030 <fclose@plt>
test   %r15,%r15
je     11de <get_thrmax+0x8e>
lea    0xe81(%rip),%rsi
mov    %rsp,%rdi
mov    %rbx,%rdx
call   1060 <sscanf@plt>
xor    %r14d,%r14d
cmp    $0x1,%eax
setne  %r14b
jmp    11de <get_thrmax+0x8e>
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
lea    0xe2e(%rip),%rdx
mov    $0xffffffff,%edi
call   1050 <logger@plt>
mov    %r14d,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"/proc/sys/kernel/threads-max\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i32 0
@.str.2 = [44 x i8] c"Unable to open /proc/sys/kernel/threads-max\00"
@.str.3 = [4 x i8] c"%du\00"
define i32 @get_thrmax(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca [128 x i8]
store i32* %0, i32** %3
%6 = load i32*, i32** %3
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 1, i32* %2
br label %32
9: ; preds = %1
%10 = call i32* @fopen(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %4
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i32, i32* @errno
%14 = call i32 @logger(i32 -1, i32 %13, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %2
br label %32
15: ; preds = %9
%16 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%17 = load i32*, i32** %4
%18 = call i32* @fgets(i8* %16, i32 128, i32* %17)
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i32*, i32** %4
%22 = call i32 @fclose(i32* %21)
store i32 1, i32* %2
br label %32
23: ; preds = %15
%24 = load i32*, i32** %4
%25 = call i32 @fclose(i32* %24)
%26 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%27 = load i32*, i32** %3
%28 = call i32 @sscanf(i8* %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32* %27)
%29 = icmp ne i32 %28, 1
br i1 %29, label %30, label %31
30: ; preds = %23
store i32 1, i32* %2
br label %32
31: ; preds = %23
store i32 0, i32* %2
br label %32
32: ; preds = %31, %30, %20, %12, %8
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
@.str = [29 x i8] c"/proc/sys/kernel/threads-max\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i32 0
@.str.2 = [44 x i8] c"Unable to open /proc/sys/kernel/threads-max\00"
@.str.3 = [4 x i8] c"%du\00"
define i32 @get_thrmax(i32* %0) {
%2 = alloca [128 x i8]
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%4 = icmp eq i32* %0, null
br i1 %4, label %19, label %5
5: ; preds = %1
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %5
%9 = load i32, i32* @errno
%10 = tail call i32 @logger(i32 -1, i32 %9, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
br label %19
11: ; preds = %5
%12 = call i32* @fgets(i8* %3, i32 128, i32* %6)
%13 = icmp eq i32* %12, null
%14 = call i32 @fclose(i32* %6)
br i1 %13, label %19, label %15
15: ; preds = %11
%16 = call i32 @sscanf(i8* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32* %0)
%17 = icmp ne i32 %16, 1
%18 = zext i1 %17 to i32
br label %19
19: ; preds = %11, %15, %1, %8
%20 = phi i32 [ 1, %8 ], [ 1, %1 ], [ %18, %15 ], [ 1, %11 ]
ret i32 %20
}