#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 *);
int /*<<< orphan*/ obsd_strlcpy(char *, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ tfree(char *);
char *tmalloc(int);
int foo(char *arg) {
  char *p;
  char *q;
  if ((p = tmalloc(500)) == NULL) {
    fprintf(stderr, "tmalloc failure\n");
    exit(EXIT_FAILURE);
  }
  if ((q = tmalloc(300)) == NULL) {
    fprintf(stderr, "tmalloc failure\n");
    exit(EXIT_FAILURE);
  }
  tfree(p);
  tfree(q);
  if ((p = tmalloc(1024)) == NULL) {
    fprintf(stderr, "tmalloc failure\n");
    exit(EXIT_FAILURE);
  }
  obsd_strlcpy(p, arg, 1024);
  tfree(q);
  return 0;
}


x86 assembly (clang, -O0):
<foo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x1f4,%edi
call   1070 <tmalloc@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1197 <foo+0x47>
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
lea    0xe7c(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    $0x12c,%edi
call   1070 <tmalloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11d2 <foo+0x82>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1050 <tfree@plt>
mov    -0x18(%rbp),%rdi
call   1050 <tfree@plt>
mov    $0x400,%edi
call   1070 <tmalloc@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    121f <foo+0xcf>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
lea    0xdf4(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x400,%edx
call   1030 <obsd_strlcpy@plt>
mov    -0x18(%rbp),%rdi
call   1050 <tfree@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<foo>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x1f4,%edi
call   1070 <tmalloc@plt>
test   %rax,%rax
je     11bb <foo+0x6b>
mov    %rax,%rbx
mov    $0x12c,%edi
call   1070 <tmalloc@plt>
test   %rax,%rax
je     11bb <foo+0x6b>
mov    %rax,%r15
mov    %rbx,%rdi
call   1050 <tfree@plt>
mov    %r15,%rdi
call   1050 <tfree@plt>
mov    $0x400,%edi
call   1070 <tmalloc@plt>
test   %rax,%rax
je     11bb <foo+0x6b>
mov    %rax,%rdi
mov    %r14,%rsi
mov    $0x400,%edx
call   1030 <obsd_strlcpy@plt>
mov    %r15,%rdi
call   1050 <tfree@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
lea    0xe35(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [17 x i8] c"tmalloc failure\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @foo(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %2
%5 = call i8* @tmalloc(i32 500)
store i8* %5, i8** %3
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %12
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %1
%13 = call i8* @tmalloc(i32 300)
store i8* %13, i8** %4
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %12
%21 = load i8*, i8** %3
%22 = call i32 @tfree(i8* %21)
%23 = load i8*, i8** %4
%24 = call i32 @tfree(i8* %23)
%25 = call i8* @tmalloc(i32 1024)
store i8* %25, i8** %3
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %32
27: ; preds = %20
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %20
%33 = load i8*, i8** %3
%34 = load i8*, i8** %2
%35 = call i32 @obsd_strlcpy(i8* %33, i8* %34, i32 1024)
%36 = load i8*, i8** %4
%37 = call i32 @tfree(i8* %36)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [17 x i8] c"tmalloc failure\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @foo(i8* %0) {
%2 = tail call i8* @tmalloc(i32 500)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %9
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %1
%10 = tail call i8* @tmalloc(i32 300)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %17
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = tail 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 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %9
%18 = tail call i32 @tfree(i8* %2)
%19 = tail call i32 @tfree(i8* %10)
%20 = tail call i8* @tmalloc(i32 1024)
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %27
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = tail call i32 @exit(i32 %25)
unreachable
27: ; preds = %17
%28 = tail call i32 @obsd_strlcpy(i8* %20, i8* %0, i32 1024)
%29 = tail call i32 @tfree(i8* %10)
ret i32 0
}