#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 */
struct buffer {
  int dummy;
};

/* Variables and functions */
scalar_t__ buf_blast(struct buffer *);
int /*<<< orphan*/ buf_inc_len(struct buffer *, int);
int /*<<< orphan*/ buf_safe(struct buffer *, int);
int /*<<< orphan*/ buf_write_u8(struct buffer *, int /*<<< orphan*/);
void buf_null_terminate(struct buffer *buf) {
  char *last = (char *)(buf_blast(buf));
  if (last && *last == '\0') {
    return;
  }
  if (!buf_safe(buf, 1)) {
    buf_inc_len(buf, -1);
  }
  buf_write_u8(buf, 0);
}


x86 assembly (clang, -O0):
<buf_null_terminate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <buf_blast@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1179 <buf_null_terminate+0x39>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1179 <buf_null_terminate+0x39>
jmp    11a9 <buf_null_terminate+0x69>
mov    -0x8(%rbp),%rdi
mov    $0x1,%esi
call   1040 <buf_safe@plt>
cmp    $0x0,%eax
jne    119e <buf_null_terminate+0x5e>
mov    -0x8(%rbp),%rdi
mov    $0xffffffff,%esi
call   1050 <buf_inc_len@plt>
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1060 <buf_write_u8@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buf_null_terminate>:
push   %rbx
mov    %rdi,%rbx
call   1030 <buf_blast@plt>
test   %rax,%rax
je     1153 <buf_null_terminate+0x13>
cmpb   $0x0,(%rax)
je     117c <buf_null_terminate+0x3c>
mov    %rbx,%rdi
mov    $0x1,%esi
call   1040 <buf_safe@plt>
test   %eax,%eax
jne    1171 <buf_null_terminate+0x31>
mov    %rbx,%rdi
mov    $0xffffffff,%esi
call   1050 <buf_inc_len@plt>
mov    %rbx,%rdi
xor    %esi,%esi
pop    %rbx
jmp    1060 <buf_write_u8@plt>
pop    %rbx
ret

LLVM IR (-O0):
%struct.buffer = type { i32 }
define void @buf_null_terminate(%struct.buffer* %0) {
%2 = alloca %struct.buffer*
%3 = alloca i8*
store %struct.buffer* %0, %struct.buffer** %2
%4 = load %struct.buffer*, %struct.buffer** %2
%5 = call i64 @buf_blast(%struct.buffer* %4)
%6 = inttoptr i64 %5 to i8*
store i8* %6, i8** %3
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %15
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %9
br label %25
15: ; preds = %9, %1
%16 = load %struct.buffer*, %struct.buffer** %2
%17 = call i32 @buf_safe(%struct.buffer* %16, i32 1)
%18 = icmp ne i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %15
%20 = load %struct.buffer*, %struct.buffer** %2
%21 = call i32 @buf_inc_len(%struct.buffer* %20, i32 -1)
br label %22
22: ; preds = %19, %15
%23 = load %struct.buffer*, %struct.buffer** %2
%24 = call i32 @buf_write_u8(%struct.buffer* %23, i32 0)
br label %25
25: ; preds = %22, %14
ret void
}

LLVM IR (-O2):
%struct.buffer = type { i32 }
define void @buf_null_terminate(%struct.buffer* %0) {
%2 = tail call i64 @buf_blast(%struct.buffer* %0)
%3 = icmp eq i64 %2, 0
br i1 %3, label %8, label %4
4: ; preds = %1
%5 = inttoptr i64 %2 to i8*
%6 = load i8, i8* %5
%7 = icmp eq i8 %6, 0
br i1 %7, label %15, label %8
8: ; preds = %4, %1
%9 = tail call i32 @buf_safe(%struct.buffer* %0, i32 1)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 @buf_inc_len(%struct.buffer* %0, i32 -1)
br label %13
13: ; preds = %11, %8
%14 = tail call i32 @buf_write_u8(%struct.buffer* %0, i32 0)
br label %15
15: ; preds = %4, %13
ret void
}