#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 str_compare_with_null_check(const char *first, const char *second,
                                int (*compare_func)(const char *,
                                                    const char *)) {
  if (first != NULL) {
    if (second != NULL) {
      const int result = compare_func(first, second);
      if (result > 0)
        return +1;
      if (result < 0)
        return -1;
      return 0;
    } else {
      return +1;
    }
  } else {
    if (second != NULL) {
      return -1;
    } else {
      return 0;
    }
  }
}


x86 assembly (clang, -O0):
<str_compare_with_null_check>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     117f <str_compare_with_null_check+0x7f>
cmpq   $0x0,-0x18(%rbp)
je     1173 <str_compare_with_null_check+0x73>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   *%rax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    1151 <str_compare_with_null_check+0x51>
movl   $0x1,-0x4(%rbp)
jmp    119d <str_compare_with_null_check+0x9d>
cmpl   $0x0,-0x24(%rbp)
jge    1167 <str_compare_with_null_check+0x67>
movl   $0xffffffff,-0x4(%rbp)
jmp    119d <str_compare_with_null_check+0x9d>
movl   $0x0,-0x4(%rbp)
jmp    119d <str_compare_with_null_check+0x9d>
movl   $0x1,-0x4(%rbp)
jmp    119d <str_compare_with_null_check+0x9d>
cmpq   $0x0,-0x18(%rbp)
je     1196 <str_compare_with_null_check+0x96>
movl   $0xffffffff,-0x4(%rbp)
jmp    119d <str_compare_with_null_check+0x9d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_compare_with_null_check>:
test   %rdi,%rdi
je     1121 <str_compare_with_null_check+0x21>
test   %rsi,%rsi
je     1127 <str_compare_with_null_check+0x27>
push   %rax
call   *%rdx
mov    %eax,%ecx
sar    $0x1f,%ecx
test   %eax,%eax
mov    $0x1,%eax
cmovle %ecx,%eax
add    $0x8,%rsp
ret
neg    %rsi
sbb    %eax,%eax
ret
mov    $0x1,%eax
ret

LLVM IR (-O0):
define i32 @str_compare_with_null_check(i8* %0, i8* %1, i32 (i8*, i8*)* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32 (i8*, i8*)*
%8 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 (i8*, i8*)* %2, i32 (i8*, i8*)** %7
%9 = load i8*, i8** %5
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %28
11: ; preds = %3
%12 = load i8*, i8** %6
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %27
14: ; preds = %11
%15 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %7
%16 = load i8*, i8** %5
%17 = load i8*, i8** %6
%18 = call i32 %15(i8* %16, i8* %17)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %14
store i32 1, i32* %4
br label %33
22: ; preds = %14
%23 = load i32, i32* %8
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %22
store i32 -1, i32* %4
br label %33
26: ; preds = %22
store i32 0, i32* %4
br label %33
27: ; preds = %11
store i32 1, i32* %4
br label %33
28: ; preds = %3
%29 = load i8*, i8** %6
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %32
31: ; preds = %28
store i32 -1, i32* %4
br label %33
32: ; preds = %28
store i32 0, i32* %4
br label %33
33: ; preds = %32, %31, %27, %26, %25, %21
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
define i32 @str_compare_with_null_check(i8* %0, i8* %1, i32 (i8*, i8*)* nocapture %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %12, label %5
5: ; preds = %3
%6 = icmp eq i8* %1, null
br i1 %6, label %15, label %7
7: ; preds = %5
%8 = tail call i32 %2(i8* %0, i8* %1)
%9 = ashr i32 %8, 31
%10 = icmp slt i32 %8, 1
%11 = select i1 %10, i32 %9, i32 1
br label %15
12: ; preds = %3
%13 = icmp ne i8* %1, null
%14 = sext i1 %13 to i32
br label %15
15: ; preds = %12, %5, %7
%16 = phi i32 [ %11, %7 ], [ 1, %5 ], [ %14, %12 ]
ret i32 %16
}