#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*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ puts(char *);
char *realloc(char *, int);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char *);
int main(int argc, char **argv) {
  char *p, *p2;
  int i;
  p = malloc(0);
  free(p);
  p = malloc(100);
  memset(p, 0, 100);
  free(p);
  p = malloc(10000);
  memset(p, 0, 10000);
  free(p);
  p = malloc(1000);
  strcpy(p, "hello");
  p[999] = 'x';
  p2 = malloc(1000);
  p = realloc(p, 10000);
  if (strcmp(p, "hello"))
    return 1;
  if (p[999] != 'x')
    return 1;
  free(p);
  free(p2);
  free(NULL);
  p = malloc(1);
  p2 = malloc(10);
  for (i = 2; i < 1000; i++) {
    p = realloc(p, i);
    p2 = realloc(p2, i * 2);
  }
  free(p);
  p = strdup("hello");
  if (strcmp("hello", p))
    return 1;
  puts("ok");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %edi,%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    $0x64,%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1060 <memset@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    $0x2710,%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x2710,%edx
call   1060 <memset@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    $0x3e8,%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xdef(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rax
movb   $0x78,0x3e7(%rax)
mov    $0x3e8,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x2710,%esi
call   1090 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xdb4(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1267 <main+0xe7>
movl   $0x1,-0x4(%rbp)
jmp    1355 <main+0x1d5>
mov    -0x18(%rbp),%rax
movsbl 0x3e7(%rax),%eax
cmp    $0x78,%eax
je     1287 <main+0x107>
movl   $0x1,-0x4(%rbp)
jmp    1355 <main+0x1d5>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1030 <free@plt>
mov    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    $0xa,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x2,-0x24(%rbp)
cmpl   $0x3e8,-0x24(%rbp)
jge    1303 <main+0x183>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1090 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
shl    $0x1,%esi
call   1090 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    12c5 <main+0x145>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
lea    0xced(%rip),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xcd9(%rip),%rdi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1342 <main+0x1c2>
movl   $0x1,-0x4(%rbp)
jmp    1355 <main+0x1d5>
lea    0xcbd(%rip),%rdi
call   1050 <puts@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %edi,%edi
call   1080 <malloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
mov    $0x64,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1060 <memset@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0x2710,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x2710,%edx
call   1060 <memset@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0x3e8,%edi
call   1080 <malloc@plt>
mov    %rax,%rbp
lea    0xe0e(%rip),%rbx
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
movb   $0x78,0x3e7(%rbp)
mov    $0x3e8,%edi
call   1080 <malloc@plt>
mov    %rax,%r14
mov    %rbp,%rdi
mov    $0x2710,%esi
call   1090 <realloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
call   1070 <strcmp@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
jne    12f5 <main+0x175>
cmpb   $0x78,0x3e7(%rbp)
jne    12f5 <main+0x175>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %edi,%edi
call   1030 <free@plt>
mov    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,%r14
mov    $0xa,%edi
call   1080 <malloc@plt>
mov    %rax,%r15
mov    $0x2,%ebp
mov    $0x4,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
mov    %ebp,%esi
call   1090 <realloc@plt>
mov    %rax,%r14
mov    %r15,%rdi
mov    %ebx,%esi
call   1090 <realloc@plt>
mov    %rax,%r15
add    $0x1,%ebp
add    $0x2,%ebx
cmp    $0x7d0,%ebx
jne    1290 <main+0x110>
mov    %r14,%rdi
call   1030 <free@plt>
lea    0xd39(%rip),%rbx
mov    %rbx,%rdi
call   10a0 <strdup@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1070 <strcmp@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
jne    12f5 <main+0x175>
lea    0xd18(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"hello\00"
@.str.1 = [3 x i8] c"ok\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i8* @malloc(i32 0)
store i8* %9, i8** %6
%10 = load i8*, i8** %6
%11 = call i32 @free(i8* %10)
%12 = call i8* @malloc(i32 100)
store i8* %12, i8** %6
%13 = load i8*, i8** %6
%14 = call i32 @memset(i8* %13, i32 0, i32 100)
%15 = load i8*, i8** %6
%16 = call i32 @free(i8* %15)
%17 = call i8* @malloc(i32 10000)
store i8* %17, i8** %6
%18 = load i8*, i8** %6
%19 = call i32 @memset(i8* %18, i32 0, i32 10000)
%20 = load i8*, i8** %6
%21 = call i32 @free(i8* %20)
%22 = call i8* @malloc(i32 1000)
store i8* %22, i8** %6
%23 = load i8*, i8** %6
%24 = call i32 @strcpy(i8* %23, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%25 = load i8*, i8** %6
%26 = getelementptr inbounds i8, i8* %25, i64 999
store i8 120, i8* %26
%27 = call i8* @malloc(i32 1000)
store i8* %27, i8** %7
%28 = load i8*, i8** %6
%29 = call i8* @realloc(i8* %28, i32 10000)
store i8* %29, i8** %6
%30 = load i8*, i8** %6
%31 = call i64 @strcmp(i8* %30, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %2
store i32 1, i32* %3
br label %73
34: ; preds = %2
%35 = load i8*, i8** %6
%36 = getelementptr inbounds i8, i8* %35, i64 999
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp ne i32 %38, 120
br i1 %39, label %40, label %41
40: ; preds = %34
store i32 1, i32* %3
br label %73
41: ; preds = %34
%42 = load i8*, i8** %6
%43 = call i32 @free(i8* %42)
%44 = load i8*, i8** %7
%45 = call i32 @free(i8* %44)
%46 = call i32 @free(i8* null)
%47 = call i8* @malloc(i32 1)
store i8* %47, i8** %6
%48 = call i8* @malloc(i32 10)
store i8* %48, i8** %7
store i32 2, i32* %8
br label %49
49: ; preds = %60, %41
%50 = load i32, i32* %8
%51 = icmp slt i32 %50, 1000
br i1 %51, label %52, label %63
52: ; preds = %49
%53 = load i8*, i8** %6
%54 = load i32, i32* %8
%55 = call i8* @realloc(i8* %53, i32 %54)
store i8* %55, i8** %6
%56 = load i8*, i8** %7
%57 = load i32, i32* %8
%58 = mul nsw i32 %57, 2
%59 = call i8* @realloc(i8* %56, i32 %58)
store i8* %59, i8** %7
br label %60
60: ; preds = %52
%61 = load i32, i32* %8
%62 = add nsw i32 %61, 1
store i32 %62, i32* %8
br label %49
63: ; preds = %49
%64 = load i8*, i8** %6
%65 = call i32 @free(i8* %64)
%66 = call i8* @strdup(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i8* %66, i8** %6
%67 = load i8*, i8** %6
%68 = call i64 @strcmp(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %67)
%69 = icmp ne i64 %68, 0
br i1 %69, label %70, label %71
70: ; preds = %63
store i32 1, i32* %3
br label %73
71: ; preds = %63
%72 = call i32 @puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %73
73: ; preds = %71, %70, %40, %33
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
@.str = [6 x i8] c"hello\00"
@.str.1 = [3 x i8] c"ok\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i8* @malloc(i32 0)
%4 = tail call i32 @free(i8* %3)
%5 = tail call dereferenceable_or_null(100) i8* @malloc(i32 100)
%6 = tail call i32 @memset(i8* %5, i32 0, i32 100)
%7 = tail call i32 @free(i8* %5)
%8 = tail call dereferenceable_or_null(10000) i8* @malloc(i32 10000)
%9 = tail call i32 @memset(i8* %8, i32 0, i32 10000)
%10 = tail call i32 @free(i8* %8)
%11 = tail call dereferenceable_or_null(1000) i8* @malloc(i32 1000)
%12 = tail call i32 @strcpy(i8* %11, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%13 = getelementptr inbounds i8, i8* %11, i64 999
store i8 120, i8* %13
%14 = tail call dereferenceable_or_null(1000) i8* @malloc(i32 1000)
%15 = tail call i8* @realloc(i8* %11, i32 10000)
%16 = tail call i64 @strcmp(i8* %15, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %44
18: ; preds = %2
%19 = getelementptr inbounds i8, i8* %15, i64 999
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 120
br i1 %21, label %22, label %44
22: ; preds = %18
%23 = tail call i32 @free(i8* %15)
%24 = tail call i32 @free(i8* %14)
%25 = tail call i32 @free(i8* null)
%26 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
%27 = tail call dereferenceable_or_null(10) i8* @malloc(i32 10)
br label %28
28: ; preds = %22, %28
%29 = phi i32 [ 2, %22 ], [ %35, %28 ]
%30 = phi i8* [ %27, %22 ], [ %34, %28 ]
%31 = phi i8* [ %26, %22 ], [ %32, %28 ]
%32 = tail call i8* @realloc(i8* %31, i32 %29)
%33 = shl nuw nsw i32 %29, 1
%34 = tail call i8* @realloc(i8* %30, i32 %33)
%35 = add nuw nsw i32 %29, 1
%36 = icmp eq i32 %35, 1000
br i1 %36, label %37, label %28
37: ; preds = %28
%38 = tail call i32 @free(i8* %32)
%39 = tail call dereferenceable_or_null(6) i8* @strdup(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%40 = tail call i64 @strcmp(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %39)
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %37
%43 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %44
44: ; preds = %37, %18, %2, %42
%45 = phi i32 [ 0, %42 ], [ 1, %2 ], [ 1, %18 ], [ 1, %37 ]
ret i32 %45
}