void writei(int fd, char *s, int l, int i) {
  int neg = i < 0;
  char b[10], *p;
  if (neg)
    i = -i;
  p = b + sizeof b;
  do
    *--p = '0' + i % 10;
  while (i /= 10);
  write(2, s, l);
  if (neg)
    write(2, "-", 1);
  write(2, p, b + sizeof b - p);
}


x86 assembly (clang, -O0):
<writei>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
setl   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1146 <writei+0x36>
xor    %eax,%eax
sub    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
lea    -0x26(%rbp),%rax
add    $0xa,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
add    $0x30,%edx
mov    %dl,%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,-0x1(%rax)
mov    -0x18(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jne    1152 <writei+0x42>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    $0x2,%edi
mov    $0x0,%al
call   1030 <write@plt>
cmpl   $0x0,-0x1c(%rbp)
je     11c0 <writei+0xb0>
mov    $0x2,%edi
lea    0xe4c(%rip),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1030 <write@plt>
mov    -0x30(%rbp),%rsi
lea    -0x26(%rbp),%rdx
add    $0xa,%rdx
mov    -0x30(%rbp),%rax
sub    %rax,%rdx
mov    $0x2,%edi
mov    $0x0,%al
call   1030 <write@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writei>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r15d
mov    %ecx,%eax
neg    %eax
cmovs  %ecx,%eax
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
movslq %eax,%rcx
imul   $0x66666667,%rcx,%rax
mov    %rax,%rdi
shr    $0x3f,%rdi
sar    $0x22,%rax
add    %edi,%eax
lea    (%rax,%rax,1),%edi
lea    (%rdi,%rdi,4),%edi
mov    %ecx,%ebp
sub    %edi,%ebp
add    $0x30,%bpl
mov    %bpl,0x17(%rsp,%rbx,1)
add    $0x9,%ecx
add    $0xffffffffffffffff,%rbx
cmp    $0x12,%ecx
ja     1130 <writei+0x20>
mov    $0x2,%edi
xor    %eax,%eax
call   1030 <write@plt>
lea    (%rsp,%rbx,1),%r14
add    $0x18,%r14
test   %r15d,%r15d
jns    1197 <writei+0x87>
lea    0xe7a(%rip),%rsi
mov    $0x2,%edi
mov    $0x1,%edx
xor    %eax,%eax
call   1030 <write@plt>
neg    %rbx
mov    $0x2,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <write@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"-\00"
define void @writei(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca [10 x i8]
%6 = icmp slt i32 %3, 0
%7 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%8 = sub nsw i32 0, %3
%9 = select i1 %6, i32 %8, i32 %3
%10 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 10
br label %11
11: ; preds = %11, %4
%12 = phi i32 [ %9, %4 ], [ %18, %11 ]
%13 = phi i8* [ %10, %4 ], [ %17, %11 ]
%14 = srem i32 %12, 10
%15 = trunc i32 %14 to i8
%16 = add nsw i8 %15, 48
%17 = getelementptr inbounds i8, i8* %13, i64 -1
store i8 %16, i8* %17
%18 = sdiv i32 %12, 10
%19 = add i32 %12, 9
%20 = icmp ult i32 %19, 19
br i1 %20, label %21, label %11
21: ; preds = %11
%22 = tail call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i32, ...)*)(i32 2, i8* %1, i32 %2)
br i1 %6, label %23, label %25
23: ; preds = %21
%24 = tail call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i32, ...)*)(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
br label %25
25: ; preds = %23, %21
%26 = ptrtoint i8* %10 to i64
%27 = ptrtoint i8* %17 to i64
%28 = sub i64 %26, %27
%29 = call i32 (i32, i8*, i64, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i64, ...)*)(i32 2, i8* %17, i64 %28)
ret void
}