#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(int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int *realloc(int *, int);
int main() {
  int *p = NULL, *q = NULL;
  int nbytes = 100;
  if ((p = (int *)malloc(nbytes)) == NULL) {
    printf("Insuficiente espacio de memoria\n");
    return -1;
  }
  printf("Se han asignado %d bytes de memoria\n", nbytes);
  if (nbytes == 0) {
    free(p);
    printf("\nEl bloque ha sido liberado\n");
    return -1;
  }
  q = realloc(p, nbytes * 2);
  if (q == NULL) {
    printf("La reasignación no ha sido posible\n");
    printf("Se conserva el bloque original\n");
  } else {
    p = q;
    printf("Bloque reasignado\n");
    printf("Nuevo tamaño %d bytes\n", nbytes * 2);
  }
  free(p);
  printf("\nEl bloque ha sido liberado\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x64,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1196 <main+0x56>
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125b <main+0x11b>
mov    -0x1c(%rbp),%esi
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    11d4 <main+0x94>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
lea    0xe85(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125b <main+0x11b>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
shl    $0x1,%esi
call   1060 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1213 <main+0xd3>
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    123d <main+0xfd>
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
lea    0xe86(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%esi
shl    $0x1,%esi
lea    0xe85(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    $0x64,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11b2 <main+0x62>
mov    %rax,%rbx
lea    0xe93(%rip),%rdi
mov    $0x64,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0xc8,%esi
call   1070 <realloc@plt>
test   %rax,%rax
je     11c0 <main+0x70>
mov    %rax,%r14
lea    0xea8(%rip),%rdi
call   1040 <puts@plt>
lea    0xe84(%rip),%rdi
mov    $0xc8,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rbx
jmp    11d8 <main+0x88>
mov    $0xffffffff,%ebx
lea    0xef0(%rip),%rdi
jmp    11e9 <main+0x99>
lea    0xea4(%rip),%rdi
call   1040 <puts@plt>
lea    0xebc(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
lea    0xe66(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [33 x i8] c"Insuficiente espacio de memoria\0A\00"
@.str.1 = [37 x i8] c"Se han asignado %d bytes de memoria\0A\00"
@.str.2 = [29 x i8] c"\0AEl bloque ha sido liberado\0A\00"
@.str.3 = [37 x i8] c"La reasignaci\C3\B3n no ha sido posible\0A\00"
@.str.4 = [32 x i8] c"Se conserva el bloque original\0A\00"
@.str.5 = [19 x i8] c"Bloque reasignado\0A\00"
@.str.6 = [24 x i8] c"Nuevo tama\C3\B1o %d bytes\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i32
store i32 0, i32* %1
store i32* null, i32** %2
store i32* null, i32** %3
store i32 100, i32* %4
%5 = load i32, i32* %4
%6 = call i64 @malloc(i32 %5)
%7 = inttoptr i64 %6 to i32*
store i32* %7, i32** %2
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %40
11: ; preds = %0
%12 = load i32, i32* %4
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = load i32, i32* %4
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %11
%17 = load i32*, i32** %2
%18 = call i32 @free(i32* %17)
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %40
20: ; preds = %11
%21 = load i32*, i32** %2
%22 = load i32, i32* %4
%23 = mul nsw i32 %22, 2
%24 = call i32* @realloc(i32* %21, i32 %23)
store i32* %24, i32** %3
%25 = load i32*, i32** %3
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %30
27: ; preds = %20
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
br label %36
30: ; preds = %20
%31 = load i32*, i32** %3
store i32* %31, i32** %2
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%33 = load i32, i32* %4
%34 = mul nsw i32 %33, 2
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %30, %27
%37 = load i32*, i32** %2
%38 = call i32 @free(i32* %37)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %40
40: ; preds = %36, %16, %9
%41 = load i32, i32* %1
ret i32 %41
}

LLVM IR (-O2):
@.str.1 = [37 x i8] c"Se han asignado %d bytes de memoria\0A\00"
@.str.6 = [24 x i8] c"Nuevo tama\C3\B1o %d bytes\0A\00"
@str = [18 x i8] c"Bloque reasignado\00"
@str.7 = [28 x i8] c"\0AEl bloque ha sido liberado\00"
@str.8 = [36 x i8] c"La reasignaci\C3\B3n no ha sido posible\00"
@str.9 = [31 x i8] c"Se conserva el bloque original\00"
@str.10 = [32 x i8] c"Insuficiente espacio de memoria\00"
define i32 @main() {
%1 = tail call i64 @malloc(i32 100)
%2 = inttoptr i64 %1 to i32*
%3 = icmp eq i64 %1, 0
br i1 %3, label %17, label %4
4: ; preds = %0
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 100)
%6 = tail call i32* @realloc(i32* %2, i32 200)
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %4
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.8, i64 0, i64 0))
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.9, i64 0, i64 0))
br label %14
11: ; preds = %4
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i32 200)
br label %14
14: ; preds = %11, %8
%15 = phi i32* [ %2, %8 ], [ %6, %11 ]
%16 = tail call i32 @free(i32* %15)
br label %17
17: ; preds = %0, %14
%18 = phi i8* [ getelementptr inbounds ([28 x i8], [28 x i8]* @str.7, i64 0, i64 0), %14 ], [ getelementptr inbounds ([32 x i8], [32 x i8]* @str.10, i64 0, i64 0), %0 ]
%19 = phi i32 [ 0, %14 ], [ -1, %0 ]
%20 = tail call i32 @puts(i8* dereferenceable(1) %18)
ret i32 %19
}