#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 */
int memcmp(void const *, void const *, size_t const);
int compare_with_length(const void *s1, size_t l1, const void *s2, size_t l2) {
  const size_t min_l = (l1 < l2) ? l1 : l2;
  int r = memcmp(s1, s2, min_l);
  if (r == 0) {
    if (l1 < l2)
      r = -1;
    else if (l1 > l2)
      r = +1;
  }
  return r;
}


x86 assembly (clang, -O0):
<compare_with_length>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1143 <compare_with_length+0x33>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    114b <compare_with_length+0x3b>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <memcmp@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    11aa <compare_with_length+0x9a>
mov    -0x10(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    118b <compare_with_length+0x7b>
movl   $0xffffffff,-0x2c(%rbp)
jmp    11a5 <compare_with_length+0x95>
mov    -0x10(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    11a0 <compare_with_length+0x90>
movl   $0x1,-0x2c(%rbp)
jmp    11a5 <compare_with_length+0x95>
jmp    11aa <compare_with_length+0x9a>
mov    -0x2c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<compare_with_length>:
push   %rbp
push   %r14
push   %rbx
mov    %rcx,%r14
mov    %rsi,%rbx
xor    %ebp,%ebp
cmp    %rcx,%rsi
mov    %rcx,%rax
cmovb  %rsi,%rax
seta   %bpl
mov    %rdx,%rsi
mov    %rax,%rdx
call   1030 <memcmp@plt>
cmp    $0x1,%eax
sbb    %ecx,%ecx
or     %eax,%ecx
test   %eax,%eax
cmovne %ecx,%ebp
cmp    %r14,%rbx
cmovb  %ecx,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @compare_with_length(i8* nocapture %0, i64 %1, i8* nocapture %2, i64 %3) {
%5 = icmp ult i64 %1, %3
%6 = select i1 %5, i64 %1, i64 %3
%7 = tail call i32 @memcmp(i8* %0, i8* %2, i64 %6)
%8 = icmp ne i32 %7, 0
%9 = or i1 %5, %8
%10 = select i1 %8, i32 %7, i32 -1
%11 = icmp ugt i64 %1, %3
%12 = zext i1 %11 to i32
%13 = select i1 %9, i32 %10, i32 %12
ret i32 %13
}