#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 */
void *malloc(int const);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int const);
int /*<<< orphan*/ prog_fatal(char *, int const);
void *smalloc(const int size) {
  void *p;
  if (size <= 0) {
    prog_fatal("impossible smalloc(%d)", size);
    return NULL;
  }
  p = malloc(size);
  if (!p) {
    prog_fatal("out of memory (%d bytes)", size);
    return NULL;
  }
  memset(p, 0, size);
  return p;
}


x86 assembly (clang, -O0):
<smalloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jg     1161 <smalloc+0x31>
mov    -0xc(%rbp),%esi
lea    0xeb1(%rip),%rdi
call   1050 <prog_fatal@plt>
movq   $0x0,-0x8(%rbp)
jmp    11aa <smalloc+0x7a>
mov    -0xc(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1194 <smalloc+0x64>
mov    -0xc(%rbp),%esi
lea    0xe95(%rip),%rdi
call   1050 <prog_fatal@plt>
movq   $0x0,-0x8(%rbp)
jmp    11aa <smalloc+0x7a>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smalloc>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
test   %edi,%edi
jle    1157 <smalloc+0x27>
mov    %ebx,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     1160 <smalloc+0x30>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1030 <memset@plt>
jmp    1171 <smalloc+0x41>
lea    0xea2(%rip),%rdi
jmp    1167 <smalloc+0x37>
lea    0xeb0(%rip),%rdi
mov    %ebx,%esi
call   1050 <prog_fatal@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [23 x i8] c"impossible smalloc(%d)\00"
@.str.1 = [25 x i8] c"out of memory (%d bytes)\00"
define i8* @smalloc(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = icmp sle i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* %3
%9 = call i32 @prog_fatal(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %8)
store i8* null, i8** %2
br label %23
10: ; preds = %1
%11 = load i32, i32* %3
%12 = call i8* @malloc(i32 %11)
store i8* %12, i8** %4
%13 = load i8*, i8** %4
%14 = icmp ne i8* %13, null
br i1 %14, label %18, label %15
15: ; preds = %10
%16 = load i32, i32* %3
%17 = call i32 @prog_fatal(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %16)
store i8* null, i8** %2
br label %23
18: ; preds = %10
%19 = load i8*, i8** %4
%20 = load i32, i32* %3
%21 = call i32 @memset(i8* %19, i32 0, i32 %20)
%22 = load i8*, i8** %4
store i8* %22, i8** %2
br label %23
23: ; preds = %18, %15, %7
%24 = load i8*, i8** %2
ret i8* %24
}

LLVM IR (-O2):
@.str = [23 x i8] c"impossible smalloc(%d)\00"
@.str.1 = [25 x i8] c"out of memory (%d bytes)\00"
define i8* @smalloc(i32 %0) {
%2 = icmp slt i32 %0, 1
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = tail call i32 @prog_fatal(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %12
5: ; preds = %1
%6 = tail call i8* @malloc(i32 %0)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @prog_fatal(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %12
10: ; preds = %5
%11 = tail call i32 @memset(i8* %6, i32 0, i32 %0)
br label %12
12: ; preds = %10, %8, %3
%13 = phi i8* [ null, %3 ], [ %6, %10 ], [ null, %8 ]
ret i8* %13
}