#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *malloc(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
char *binary(int n, int m) {
  int i = 0;
  char *s = NULL;
  s = malloc(m + 1);
  if (!s) {
    fprintf(stderr, "malloc() failed\n");
    exit(EXIT_FAILURE);
  }
  s[0] = '\0';
  for (i = (0x1 << (m - 1)); i > 0; i >>= 1) {
    strcat(s, ((n & i) == i) ? "1" : "0");
  }
  return s;
}


x86 assembly (clang, -O0):
<binary>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    119a <binary+0x5a>
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
lea    0xe79(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    11ef <binary+0xaf>
mov    -0x18(%rbp),%rdi
mov    -0x4(%rbp),%ecx
and    -0xc(%rbp),%ecx
mov    -0xc(%rbp),%edx
lea    0xe44(%rip),%rsi
lea    0xe3b(%rip),%rax
cmp    %edx,%ecx
cmove  %rax,%rsi
call   1050 <strcat@plt>
mov    -0xc(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11b1 <binary+0x71>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<binary>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%r15d
lea    0x1(%r14),%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     11bd <binary+0x7d>
mov    %rax,%r13
movb   $0x0,(%rax)
add    $0xffffffff,%r14d
cmp    $0x1f,%r14d
je     11ab <binary+0x6b>
mov    $0x1,%ebx
mov    %r14d,%ecx
shl    %cl,%ebx
not    %r15d
lea    0xe8e(%rip),%r14
lea    0xe89(%rip),%r12
mov    %ebx,%ebp
nopl   0x0(%rax)
test   %r15d,%ebx
mov    %r12,%rsi
cmove  %r14,%rsi
mov    %r13,%rdi
call   1050 <strcat@plt>
shr    %ebp
cmp    $0x1,%ebx
mov    %ebp,%ebx
ja     1190 <binary+0x50>
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
lea    0xe33(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [17 x i8] c"malloc() failed\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"1\00"
@.str.2 = [2 x i8] c"0\00"
define i8* @binary(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %5
store i8* null, i8** %6
%7 = load i32, i32* %4
%8 = add nsw i32 %7, 1
%9 = call i8* @malloc(i32 %8)
store i8* %9, i8** %6
%10 = load i8*, i8** %6
%11 = icmp ne i8* %10, null
br i1 %11, label %17, label %12
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
%18 = load i8*, i8** %6
%19 = getelementptr inbounds i8, i8* %18, i64 0
store i8 0, i8* %19
%20 = load i32, i32* %4
%21 = sub nsw i32 %20, 1
%22 = shl i32 1, %21
store i32 %22, i32* %5
br label %23
23: ; preds = %36, %17
%24 = load i32, i32* %5
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %39
26: ; preds = %23
%27 = load i8*, i8** %6
%28 = load i32, i32* %3
%29 = load i32, i32* %5
%30 = and i32 %28, %29
%31 = load i32, i32* %5
%32 = icmp eq i32 %30, %31
%33 = zext i1 %32 to i64
%34 = select i1 %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0)
%35 = call i32 @strcat(i8* %27, i8* %34)
br label %36
36: ; preds = %26
%37 = load i32, i32* %5
%38 = ashr i32 %37, 1
store i32 %38, i32* %5
br label %23
39: ; preds = %23
%40 = load i8*, i8** %6
ret i8* %40
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [17 x i8] c"malloc() failed\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"1\00"
@.str.2 = [2 x i8] c"0\00"
define i8* @binary(i32 %0, i32 %1) {
%3 = add nsw i32 %1, 1
%4 = tail call i8* @malloc(i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %11
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %2
store i8 0, i8* %4
%12 = add nsw i32 %1, -1
%13 = icmp eq i32 %12, 31
br i1 %13, label %24, label %14
14: ; preds = %11
%15 = shl nuw i32 1, %12
br label %16
16: ; preds = %14, %16
%17 = phi i32 [ %22, %16 ], [ %15, %14 ]
%18 = and i32 %17, %0
%19 = icmp eq i32 %18, %17
%20 = select i1 %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0)
%21 = tail call i32 @strcat(i8* %4, i8* %20)
%22 = lshr i32 %17, 1
%23 = icmp ult i32 %17, 2
br i1 %23, label %24, label %16
24: ; preds = %16, %11
ret i8* %4
}