#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 gbuf {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ assert(int /*<<< orphan*/);
void *gbuf_salloc(struct gbuf *, size_t);
void *memcpy(void *, void const *, size_t);
void *gbuf_swrite(struct gbuf *buf, void const *data, size_t size) {
  assert(buf != NULL);
  void *result = gbuf_salloc(buf, size);
  return result ? memcpy(result, data, size) : NULL;
}


x86 assembly (clang, -O0):
<gbuf_swrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <gbuf_salloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1194 <gbuf_swrite+0x64>
mov    -0x20(%rbp),%rdi
mov    %rdi,-0x30(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    119f <gbuf_swrite+0x6f>
xor    %eax,%eax
mov    %rax,-0x28(%rbp)
jmp    119f <gbuf_swrite+0x6f>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gbuf_swrite>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1030 <assert@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <gbuf_salloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     116d <gbuf_swrite+0x3d>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.gbuf = type { i32 }
define i8* @gbuf_swrite(%struct.gbuf* %0, i8* %1, i64 %2) {
%4 = alloca %struct.gbuf*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
store %struct.gbuf* %0, %struct.gbuf** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%8 = load %struct.gbuf*, %struct.gbuf** %4
%9 = icmp ne %struct.gbuf* %8, null
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = load %struct.gbuf*, %struct.gbuf** %4
%13 = load i64, i64* %6
%14 = call i8* @gbuf_salloc(%struct.gbuf* %12, i64 %13)
store i8* %14, i8** %7
%15 = load i8*, i8** %7
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %21
17: ; preds = %3
%18 = load i8*, i8** %7
%19 = load i8*, i8** %5
%20 = load i64, i64* %6
br label %22
21: ; preds = %3
br label %22
22: ; preds = %21, %17
%23 = phi i8* [ %18, %17 ], [ null, %21 ]
ret i8* %23
}

LLVM IR (-O2):
%struct.gbuf = type { i32 }
define i8* @gbuf_swrite(%struct.gbuf* %0, i8* nocapture %1, i64 %2) {
%4 = icmp ne %struct.gbuf* %0, null
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = tail call i8* @gbuf_salloc(%struct.gbuf* %0, i64 %2)
%8 = icmp eq i8* %7, null
br i1 %8, label %10, label %9
9: ; preds = %3
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %7, i8* align 1 %1, i64 %2, i1 false)
br label %10
10: ; preds = %3, %9
ret i8* %7
}