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

/* Variables and functions */
long INT_MAX;
int /*<<< orphan*/ _PC_PATH_MAX;
int errno;
long pathconf(char const *, int /*<<< orphan*/);
int lmsc_get_max_per_path(const char *pth) {
  int pm = -1;
  long l;
  int en = errno;
  errno = 0;
  l = pathconf(pth, _PC_PATH_MAX);
  if (l < 0) {
    if (errno) {
      return -1;
    }
    pm = -1;
  } else {
    pm = (int)((INT_MAX) < (l) ? (INT_MAX) : (l));
  }
  errno = en;
  if (pm == -1) {
    pm = 1024;
  }
  return pm;
}


x86 assembly (clang, -O0):
<lmsc_get_max_per_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    0x2ea6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e9a(%rip),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
call   1030 <pathconf@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    1185 <lmsc_get_max_per_path+0x75>
mov    0x2e6c(%rip),%rax
cmpl   $0x0,(%rax)
je     1179 <lmsc_get_max_per_path+0x69>
movl   $0xffffffff,-0x4(%rbp)
jmp    11de <lmsc_get_max_per_path+0xce>
movl   $0xffffffff,-0x14(%rbp)
jmp    11bb <lmsc_get_max_per_path+0xab>
mov    0x2e5c(%rip),%rax
mov    (%rax),%rax
cmp    -0x20(%rbp),%rax
jge    11ac <lmsc_get_max_per_path+0x9c>
mov    0x2e48(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    11b4 <lmsc_get_max_per_path+0xa4>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %eax,-0x14(%rbp)
mov    -0x24(%rbp),%ecx
mov    0x2e0b(%rip),%rax
mov    %ecx,(%rax)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11d8 <lmsc_get_max_per_path+0xc8>
movl   $0x400,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lmsc_get_max_per_path>:
push   %rbp
push   %rbx
push   %rax
mov    0x2eb6(%rip),%rbx
mov    (%rbx),%ebp
movl   $0x0,(%rbx)
mov    0x2eaf(%rip),%rax
mov    (%rax),%esi
call   1030 <pathconf@plt>
test   %rax,%rax
js     1151 <lmsc_get_max_per_path+0x41>
mov    %rax,%rcx
mov    0x2ea9(%rip),%rax
mov    (%rax),%rax
cmp    %rcx,%rax
cmovge %ecx,%eax
mov    %ebp,(%rbx)
cmp    $0xffffffff,%eax
jne    1162 <lmsc_get_max_per_path+0x52>
jmp    115d <lmsc_get_max_per_path+0x4d>
mov    $0xffffffff,%eax
cmpl   $0x0,(%rbx)
jne    1162 <lmsc_get_max_per_path+0x52>
mov    %ebp,(%rbx)
mov    $0x400,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@_PC_PATH_MAX = global i32 0
@INT_MAX = global i64 0
define i32 @lmsc_get_max_per_path(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
store i8* %0, i8** %3
store i32 -1, i32* %4
%7 = load i32, i32* @errno
store i32 %7, i32* %6
store i32 0, i32* @errno
%8 = load i8*, i8** %3
%9 = load i32, i32* @_PC_PATH_MAX
%10 = call i64 @pathconf(i8* %8, i32 %9)
store i64 %10, i64* %5
%11 = load i64, i64* %5
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %1
%14 = load i32, i32* @errno
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %13
store i32 -1, i32* %2
br label %36
17: ; preds = %13
store i32 -1, i32* %4
br label %29
18: ; preds = %1
%19 = load i64, i64* @INT_MAX
%20 = load i64, i64* %5
%21 = icmp slt i64 %19, %20
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = load i64, i64* @INT_MAX
br label %26
24: ; preds = %18
%25 = load i64, i64* %5
br label %26
26: ; preds = %24, %22
%27 = phi i64 [ %23, %22 ], [ %25, %24 ]
%28 = trunc i64 %27 to i32
store i32 %28, i32* %4
br label %29
29: ; preds = %26, %17
%30 = load i32, i32* %6
store i32 %30, i32* @errno
%31 = load i32, i32* %4
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %34
33: ; preds = %29
store i32 1024, i32* %4
br label %34
34: ; preds = %33, %29
%35 = load i32, i32* %4
store i32 %35, i32* %2
br label %36
36: ; preds = %34, %16
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
@errno = global i32 0
@_PC_PATH_MAX = global i32 0
@INT_MAX = global i64 0
define i32 @lmsc_get_max_per_path(i8* %0) {
%2 = load i32, i32* @errno
store i32 0, i32* @errno
%3 = load i32, i32* @_PC_PATH_MAX
%4 = tail call i64 @pathconf(i8* %0, i32 %3)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %1
%7 = load i32, i32* @errno
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %17
9: ; preds = %6
store i32 %2, i32* @errno
br label %16
10: ; preds = %1
%11 = load i64, i64* @INT_MAX
%12 = icmp slt i64 %11, %4
%13 = select i1 %12, i64 %11, i64 %4
%14 = trunc i64 %13 to i32
store i32 %2, i32* @errno
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %9, %10
br label %17
17: ; preds = %16, %10, %6
%18 = phi i32 [ -1, %6 ], [ 1024, %16 ], [ %14, %10 ]
ret i32 %18
}