#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*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ swab(char *, char *, int);
int swap_buffer(char *buf, int no) {
  char *b2;
  if (NULL == (b2 = malloc(no))) {
    printf("Out of mem for %d\n", no);
    return 1;
  }
  memcpy(b2, buf, no);
  swab(b2, buf, no);
  free(b2);
  return 0;
}


x86 assembly (clang, -O0):
<swap_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    1194 <swap_buffer+0x44>
mov    -0x14(%rbp),%esi
lea    0xe7d(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11c4 <swap_buffer+0x74>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <swab@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<swap_buffer>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
mov    %esi,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     118e <swap_buffer+0x3e>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1060 <memcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1040 <swab@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    11a1 <swap_buffer+0x51>
lea    0xe6b(%rip),%rdi
mov    %ebp,%esi
call   1050 <printf@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"Out of mem for %d\0A\00"
define i32 @swap_buffer(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i32, i32* %5
%8 = call i8* @malloc(i32 %7)
store i8* %8, i8** %6
%9 = icmp eq i8* null, %8
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* %5
%12 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 1, i32* %3
br label %24
13: ; preds = %2
%14 = load i8*, i8** %6
%15 = load i8*, i8** %4
%16 = load i32, i32* %5
%17 = call i32 @memcpy(i8* %14, i8* %15, i32 %16)
%18 = load i8*, i8** %6
%19 = load i8*, i8** %4
%20 = load i32, i32* %5
%21 = call i32 @swab(i8* %18, i8* %19, i32 %20)
%22 = load i8*, i8** %6
%23 = call i32 @free(i8* %22)
store i32 0, i32* %3
br label %24
24: ; preds = %13, %10
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
@.str = [19 x i8] c"Out of mem for %d\0A\00"
define i32 @swap_buffer(i8* %0, i32 %1) {
%3 = tail call i8* @malloc(i32 %1)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %1)
br label %11
7: ; preds = %2
%8 = tail call i32 @memcpy(i8* %3, i8* %0, i32 %1)
%9 = tail call i32 @swab(i8* %3, i8* %0, i32 %1)
%10 = tail call i32 @free(i8* %3)
br label %11
11: ; preds = %7, %5
%12 = phi i32 [ 1, %5 ], [ 0, %7 ]
ret i32 %12
}