void rbuf_align(struct ringbuf *r, int w, char pad, _Bool left,
                const char *buf) {
  const char *p = buf;
  while (*p++ && w > 0)
    w--;
  if (!left)
    rbuf_nputc(r, w, pad);
  rbuf_print(r, buf);
  if (left)
    rbuf_nputc(r, w, pad);
}


x86 assembly (clang, -O0):
<rbuf_align>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %cl,%al
mov    %dl,%cl
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %cl,-0xd(%rbp)
and    $0x1,%al
mov    %al,-0xe(%rbp)
mov    %r8,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x21(%rbp)
je     1171 <rbuf_align+0x51>
cmpl   $0x0,-0xc(%rbp)
setg   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1181 <rbuf_align+0x61>
jmp    118f <rbuf_align+0x6f>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1147 <rbuf_align+0x27>
testb  $0x1,-0xe(%rbp)
jne    11ab <rbuf_align+0x8b>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
movsbl -0xd(%rbp),%edx
mov    $0x0,%al
call   1040 <rbuf_nputc@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x0,%al
call   1030 <rbuf_print@plt>
testb  $0x1,-0xe(%rbp)
je     11d6 <rbuf_align+0xb6>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
movsbl -0xd(%rbp),%edx
mov    $0x0,%al
call   1040 <rbuf_nputc@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rbuf_align>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %r8,%rbx
mov    %edx,%r15d
mov    %rdi,%r14
cmpb   $0x0,(%r8)
je     1155 <rbuf_align+0x35>
test   %esi,%esi
jle    1182 <rbuf_align+0x62>
lea    0x1(%rbx),%rax
xchg   %ax,%ax
lea    -0x1(%rsi),%ebp
cmpb   $0x0,(%rax)
je     1157 <rbuf_align+0x37>
add    $0x1,%rax
cmp    $0x1,%esi
mov    %ebp,%esi
ja     1140 <rbuf_align+0x20>
jmp    1157 <rbuf_align+0x37>
mov    %esi,%ebp
test   %cl,%cl
je     1188 <rbuf_align+0x68>
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1030 <rbuf_print@plt>
movsbl %r15b,%edx
mov    %r14,%rdi
mov    %ebp,%esi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <rbuf_nputc@plt>
mov    %esi,%ebp
test   %cl,%cl
jne    115b <rbuf_align+0x3b>
movsbl %r15b,%edx
mov    %r14,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <rbuf_nputc@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <rbuf_print@plt>

LLVM IR (-O0):
%struct.ringbuf = type opaque
define void @rbuf_align(%struct.ringbuf* %0, i32 %1, i8 signext %2, i1 zeroext %3, i8* %4) {
%6 = alloca %struct.ringbuf*
%7 = alloca i32
%8 = alloca i8
%9 = alloca i8
%10 = alloca i8*
%11 = alloca i8*
store %struct.ringbuf* %0, %struct.ringbuf** %6
store i32 %1, i32* %7
store i8 %2, i8* %8
%12 = zext i1 %3 to i8
store i8 %12, i8* %9
store i8* %4, i8** %10
%13 = load i8*, i8** %10
store i8* %13, i8** %11
br label %14
14: ; preds = %25, %5
%15 = load i8*, i8** %11
%16 = getelementptr inbounds i8, i8* %15, i32 1
store i8* %16, i8** %11
%17 = load i8, i8* %15
%18 = sext i8 %17 to i32
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = load i32, i32* %7
%22 = icmp sgt i32 %21, 0
br label %23
23: ; preds = %20, %14
%24 = phi i1 [ false, %14 ], [ %22, %20 ]
br i1 %24, label %25, label %28
25: ; preds = %23
%26 = load i32, i32* %7
%27 = add nsw i32 %26, -1
store i32 %27, i32* %7
br label %14
28: ; preds = %23
%29 = load i8, i8* %9
%30 = trunc i8 %29 to i1
br i1 %30, label %37, label %31
31: ; preds = %28
%32 = load %struct.ringbuf*, %struct.ringbuf** %6
%33 = load i32, i32* %7
%34 = load i8, i8* %8
%35 = sext i8 %34 to i32
%36 = call i32 (%struct.ringbuf*, i32, i32, ...) bitcast (i32 (...)* @rbuf_nputc to i32 (%struct.ringbuf*, i32, i32, ...)*)(%struct.ringbuf* %32, i32 %33, i32 %35)
br label %37
37: ; preds = %31, %28
%38 = load %struct.ringbuf*, %struct.ringbuf** %6
%39 = load i8*, i8** %10
%40 = call i32 (%struct.ringbuf*, i8*, ...) bitcast (i32 (...)* @rbuf_print to i32 (%struct.ringbuf*, i8*, ...)*)(%struct.ringbuf* %38, i8* %39)
%41 = load i8, i8* %9
%42 = trunc i8 %41 to i1
br i1 %42, label %43, label %49
43: ; preds = %37
%44 = load %struct.ringbuf*, %struct.ringbuf** %6
%45 = load i32, i32* %7
%46 = load i8, i8* %8
%47 = sext i8 %46 to i32
%48 = call i32 (%struct.ringbuf*, i32, i32, ...) bitcast (i32 (...)* @rbuf_nputc to i32 (%struct.ringbuf*, i32, i32, ...)*)(%struct.ringbuf* %44, i32 %45, i32 %47)
br label %49
49: ; preds = %43, %37
ret void
}

LLVM IR (-O2):
%struct.ringbuf = type opaque
define void @rbuf_align(%struct.ringbuf* %0, i32 %1, i8 signext %2, i1 zeroext %3, i8* %4) {
%6 = load i8, i8* %4
%7 = icmp ne i8 %6, 0
%8 = icmp sgt i32 %1, 0
%9 = and i1 %7, %8
br i1 %9, label %10, label %19
10: ; preds = %5, %10
%11 = phi i8* [ %13, %10 ], [ %4, %5 ]
%12 = phi i32 [ %14, %10 ], [ %1, %5 ]
%13 = getelementptr inbounds i8, i8* %11, i64 1
%14 = add nsw i32 %12, -1
%15 = load i8, i8* %13
%16 = icmp ne i8 %15, 0
%17 = icmp ugt i32 %12, 1
%18 = select i1 %16, i1 %17, i1 false
br i1 %18, label %10, label %19
19: ; preds = %10, %5
%20 = phi i32 [ %1, %5 ], [ %14, %10 ]
br i1 %3, label %25, label %21
21: ; preds = %19
%22 = sext i8 %2 to i32
%23 = tail call i32 (%struct.ringbuf*, i32, i32, ...) bitcast (i32 (...)* @rbuf_nputc to i32 (%struct.ringbuf*, i32, i32, ...)*)(%struct.ringbuf* %0, i32 %20, i32 %22)
%24 = tail call i32 (%struct.ringbuf*, i8*, ...) bitcast (i32 (...)* @rbuf_print to i32 (%struct.ringbuf*, i8*, ...)*)(%struct.ringbuf* %0, i8* %4)
br label %29
25: ; preds = %19
%26 = tail call i32 (%struct.ringbuf*, i8*, ...) bitcast (i32 (...)* @rbuf_print to i32 (%struct.ringbuf*, i8*, ...)*)(%struct.ringbuf* %0, i8* %4)
%27 = sext i8 %2 to i32
%28 = tail call i32 (%struct.ringbuf*, i32, i32, ...) bitcast (i32 (...)* @rbuf_nputc to i32 (%struct.ringbuf*, i32, i32, ...)*)(%struct.ringbuf* %0, i32 %20, i32 %27)
br label %29
29: ; preds = %21, %25
ret void
}