void CWriteNT(const volatile void *p, const volatile void *hi, int what,
              unsigned int sz, const char *text, const char *file, int line,
              const char *func) {
  if (p == hi) {
    int isNull = 0;
    switch (sz) {
    case 1:
      isNull = (*((const volatile char *)p) == 0);
      break;
    case 2:
      isNull = (*((const volatile short *)p) == 0);
      break;
    case 4:
      isNull = (*((const volatile int *)p) == 0);
      break;
    }
    if (!(!isNull || what == 0) || 0) {
      deputy_fail_mayreturn("nullterm write check", text, file, line, func);
    };
  }
}


x86 assembly (clang, -O0):
<CWriteNT>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%eax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    %r9,-0x28(%rbp)
mov    -0x8(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    1201 <CWriteNT+0xf1>
movl   $0x0,-0x2c(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x30(%rbp)
sub    $0x1,%eax
je     1180 <CWriteNT+0x70>
jmp    115e <CWriteNT+0x4e>
mov    -0x30(%rbp),%eax
sub    $0x2,%eax
je     119c <CWriteNT+0x8c>
jmp    116f <CWriteNT+0x5f>
mov    -0x30(%rbp),%eax
sub    $0x4,%eax
je     11b7 <CWriteNT+0xa7>
jmp    11cb <CWriteNT+0xbb>
mov    -0x8(%rbp),%rax
mov    (%rax),%al
movsbl %al,%eax
cmp    $0x0,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x2c(%rbp)
jmp    11cb <CWriteNT+0xbb>
mov    -0x8(%rbp),%rax
mov    (%rax),%ax
cwtl
cmp    $0x0,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x2c(%rbp)
jmp    11cb <CWriteNT+0xbb>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
cmp    $0x0,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     11fc <CWriteNT+0xec>
cmpl   $0x0,-0x14(%rbp)
je     11fc <CWriteNT+0xec>
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    0x10(%rbp),%ecx
mov    0x18(%rbp),%r8
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1030 <deputy_fail_mayreturn@plt>
jmp    1201 <CWriteNT+0xf1>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CWriteNT>:
cmp    %rsi,%rdi
je     1116 <CWriteNT+0x6>
ret
mov    %ecx,%eax
mov    0x10(%rsp),%r10
mov    0x8(%rsp),%ecx
cmp    $0x4,%eax
je     1135 <CWriteNT+0x25>
cmp    $0x2,%eax
je     113a <CWriteNT+0x2a>
cmp    $0x1,%eax
jne    1115 <CWriteNT+0x5>
cmpb   $0x0,(%rdi)
jmp    113e <CWriteNT+0x2e>
cmpl   $0x0,(%rdi)
jmp    113e <CWriteNT+0x2e>
cmpw   $0x0,(%rdi)
sete   %al
test   %edx,%edx
je     1115 <CWriteNT+0x5>
test   %al,%al
je     1115 <CWriteNT+0x5>
lea    0xeb0(%rip),%rdi
mov    %r8,%rsi
mov    %r9,%rdx
mov    %r10,%r8
xor    %eax,%eax
jmp    1030 <deputy_fail_mayreturn@plt>

LLVM IR (-O0):
@.str = [21 x i8] c"nullterm write check\00"
define void @CWriteNT(i8* %0, i8* %1, i32 %2, i32 %3, i8* %4, i8* %5, i32 %6, i8* %7) {
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i32
%16 = alloca i8*
%17 = alloca i32
store i8* %0, i8** %9
store i8* %1, i8** %10
store i32 %2, i32* %11
store i32 %3, i32* %12
store i8* %4, i8** %13
store i8* %5, i8** %14
store i32 %6, i32* %15
store i8* %7, i8** %16
%18 = load i8*, i8** %9
%19 = load i8*, i8** %10
%20 = icmp eq i8* %18, %19
br i1 %20, label %21, label %55
21: ; preds = %8
store i32 0, i32* %17
%22 = load i32, i32* %12
switch i32 %22, label %42 [
i32 1, label %23
i32 2, label %29
i32 4, label %36
]
23: ; preds = %21
%24 = load i8*, i8** %9
%25 = load volatile i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 0
%28 = zext i1 %27 to i32
store i32 %28, i32* %17
br label %42
29: ; preds = %21
%30 = load i8*, i8** %9
%31 = bitcast i8* %30 to i16*
%32 = load volatile i16, i16* %31
%33 = sext i16 %32 to i32
%34 = icmp eq i32 %33, 0
%35 = zext i1 %34 to i32
store i32 %35, i32* %17
br label %42
36: ; preds = %21
%37 = load i8*, i8** %9
%38 = bitcast i8* %37 to i32*
%39 = load volatile i32, i32* %38
%40 = icmp eq i32 %39, 0
%41 = zext i1 %40 to i32
store i32 %41, i32* %17
br label %42
42: ; preds = %21, %36, %29, %23
%43 = load i32, i32* %17
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %54
45: ; preds = %42
%46 = load i32, i32* %11
%47 = icmp eq i32 %46, 0
br i1 %47, label %54, label %48
48: ; preds = %45
%49 = load i8*, i8** %13
%50 = load i8*, i8** %14
%51 = load i32, i32* %15
%52 = load i8*, i8** %16
%53 = call i32 (i8*, i8*, i8*, i32, i8*, ...) bitcast (i32 (...)* @deputy_fail_mayreturn to i32 (i8*, i8*, i8*, i32, i8*, ...)*)(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %49, i8* %50, i32 %51, i8* %52)
br label %54
54: ; preds = %48, %45, %42
br label %55
55: ; preds = %54, %8
ret void
}

LLVM IR (-O2):
@.str = [21 x i8] c"nullterm write check\00"
define void @CWriteNT(i8* %0, i8* readnone %1, i32 %2, i32 %3, i8* %4, i8* %5, i32 %6, i8* %7) {
%9 = icmp eq i8* %0, %1
br i1 %9, label %10, label %28
10: ; preds = %8
switch i32 %3, label %28 [
i32 1, label %11
i32 2, label %14
i32 4, label %18
]
11: ; preds = %10
%12 = load volatile i8, i8* %0
%13 = icmp eq i8 %12, 0
br label %22
14: ; preds = %10
%15 = bitcast i8* %0 to i16*
%16 = load volatile i16, i16* %15
%17 = icmp eq i16 %16, 0
br label %22
18: ; preds = %10
%19 = bitcast i8* %0 to i32*
%20 = load volatile i32, i32* %19
%21 = icmp eq i32 %20, 0
br label %22
22: ; preds = %18, %14, %11
%23 = phi i1 [ %21, %18 ], [ %17, %14 ], [ %13, %11 ]
%24 = icmp ne i32 %2, 0
%25 = and i1 %24, %23
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = tail call i32 (i8*, i8*, i8*, i32, i8*, ...) bitcast (i32 (...)* @deputy_fail_mayreturn to i32 (i8*, i8*, i8*, i32, i8*, ...)*)(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %4, i8* %5, i32 %6, i8* %7)
br label %28
28: ; preds = %10, %22, %26, %8
ret void
}