#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 /*<<< orphan*/ decimalToBinary(long, int, int, char *);
int /*<<< orphan*/ invert(int /*<<< orphan*/, int, char *);
void onesComplement(long n, int size, char result[size + 1]) {
  result[size] = '\0';
  int toInvert = 0;
  if (n < 0) {
    n *= -1;
    toInvert = 1;
  }
  result[0] = '0';
  decimalToBinary(n, 1, size, result);
  if (toInvert) {
    invert(0, size, result);
  }
}


x86 assembly (clang, -O0):
<onesComplement>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    1161 <onesComplement+0x41>
imul   $0xffffffffffffffff,-0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x30,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <decimalToBinary@plt>
cmpl   $0x0,-0x1c(%rbp)
je     1195 <onesComplement+0x75>
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
xor    %edi,%edi
call   1030 <invert@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<onesComplement>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %rdi,%rbp
movslq %esi,%rdx
movb   $0x0,(%rbx,%rdx,1)
neg    %rdi
cmovs  %rbp,%rdi
movb   $0x30,(%rbx)
mov    $0x1,%esi
mov    %rbx,%rcx
call   1040 <decimalToBinary@plt>
test   %rbp,%rbp
js     1155 <onesComplement+0x35>
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %edi,%edi
mov    %r14d,%esi
mov    %rbx,%rdx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <invert@plt>

LLVM IR (-O0):
define void @onesComplement(i64 %0, i32 %1, i8* %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i64 %0, i64* %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%8 = load i32, i32* %5
%9 = add nsw i32 %8, 1
%10 = zext i32 %9 to i64
%11 = load i8*, i8** %6
%12 = load i32, i32* %5
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %11, i64 %13
store i8 0, i8* %14
store i32 0, i32* %7
%15 = load i64, i64* %4
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = load i64, i64* %4
%19 = mul nsw i64 %18, -1
store i64 %19, i64* %4
store i32 1, i32* %7
br label %20
20: ; preds = %17, %3
%21 = load i8*, i8** %6
%22 = getelementptr inbounds i8, i8* %21, i64 0
store i8 48, i8* %22
%23 = load i64, i64* %4
%24 = load i32, i32* %5
%25 = load i8*, i8** %6
%26 = call i32 @decimalToBinary(i64 %23, i32 1, i32 %24, i8* %25)
%27 = load i32, i32* %7
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %20
%30 = load i32, i32* %5
%31 = load i8*, i8** %6
%32 = call i32 @invert(i32 0, i32 %30, i8* %31)
br label %33
33: ; preds = %29, %20
ret void
}

LLVM IR (-O2):
define void @onesComplement(i64 %0, i32 %1, i8* %2) {
%4 = sext i32 %1 to i64
%5 = getelementptr inbounds i8, i8* %2, i64 %4
store i8 0, i8* %5
%6 = icmp slt i64 %0, 0
%7 = sub nsw i64 0, %0
%8 = select i1 %6, i64 %7, i64 %0
store i8 48, i8* %2
%9 = tail call i32 @decimalToBinary(i64 %8, i32 1, i32 %1, i8* %2)
br i1 %6, label %10, label %12
10: ; preds = %3
%11 = tail call i32 @invert(i32 0, i32 %1, i8* %2)
br label %12
12: ; preds = %10, %3
ret void
}