#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 */
struct buffer {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ buffer_cstr(struct buffer *);
int /*<<< orphan*/ buffer_length(struct buffer *);
char *malloc(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
char *buffer_copy(struct buffer *b) {
  char *c;
  if (b == NULL)
    return NULL;
  c = malloc(buffer_length(b));
  if (c == NULL) {
    perror("malloc");
    return NULL;
  }
  memcpy(c, buffer_cstr(b), buffer_length(b));
  return c;
}


x86 assembly (clang, -O0):
<buffer_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1174 <buffer_copy+0x24>
movq   $0x0,-0x8(%rbp)
jmp    11df <buffer_copy+0x8f>
mov    -0x10(%rbp),%rdi
call   1040 <buffer_length@plt>
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ac <buffer_copy+0x5c>
lea    0xe66(%rip),%rdi
call   1070 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    11df <buffer_copy+0x8f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <buffer_cstr@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <buffer_length@plt>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buffer_copy>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     119c <buffer_copy+0x4c>
mov    %rdi,%rbx
call   1040 <buffer_length@plt>
mov    %eax,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1190 <buffer_copy+0x40>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <buffer_cstr@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <buffer_length@plt>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1050 <memcpy@plt>
jmp    119f <buffer_copy+0x4f>
lea    0xe69(%rip),%rdi
call   1070 <perror@plt>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.buffer = type { i32 }
@.str = [7 x i8] c"malloc\00"
define i8* @buffer_copy(%struct.buffer* %0) {
%2 = icmp eq %struct.buffer* %0, null
br i1 %2, label %13, label %3
3: ; preds = %1
%4 = tail call i32 @buffer_length(%struct.buffer* %0)
%5 = tail call i8* @malloc(i32 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %13
9: ; preds = %3
%10 = tail call i32 @buffer_cstr(%struct.buffer* %0)
%11 = tail call i32 @buffer_length(%struct.buffer* %0)
%12 = tail call i32 @memcpy(i8* %5, i32 %10, i32 %11)
br label %13
13: ; preds = %1, %9, %7
%14 = phi i8* [ null, %7 ], [ %5, %9 ], [ null, %1 ]
ret i8* %14
}