#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 errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ printf(char *, int);
int write(int, char *, int);
void test_write(int fd, char const *buf1, int len1, char const *buf2,
                int len2) {
  int len = len1 + len2;
  char *buf = (char *)malloc(len);
  memcpy(buf, buf1, len1);
  memcpy(buf + len1, buf2, len2);
  int ret = write(fd, buf, len);
  free(buf);
  if (ret == -1) {
    printf("fatal error while write, errno %d\n", errno);
    exit(-1);
  }
}


x86 assembly (clang, -O0):
<test_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0x14(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x30(%rbp),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x4(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0xffffffff,-0x34(%rbp)
jne    11fa <test_write+0x9a>
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
lea    0xe15(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r15d
mov    %rcx,%r14
mov    %edx,%ebx
mov    %rsi,%r12
mov    %edi,0x4(%rsp)
lea    (%r15,%rbx,1),%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1060 <memcpy@plt>
movslq %ebx,%rdi
add    %r13,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1060 <memcpy@plt>
mov    0x4(%rsp),%edi
mov    %r13,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
mov    %eax,%ebx
mov    %r13,%rdi
call   1030 <free@plt>
cmp    $0xffffffff,%ebx
je     11d2 <test_write+0x72>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
lea    0xe1e(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [35 x i8] c"fatal error while write, errno %d\0A\00"
@errno = global i32 0
define void @test_write(i32 %0, i8* %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i32 %4, i32* %10
%14 = load i32, i32* %8
%15 = load i32, i32* %10
%16 = add nsw i32 %14, %15
store i32 %16, i32* %11
%17 = load i32, i32* %11
%18 = call i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to i8*
store i8* %19, i8** %12
%20 = load i8*, i8** %12
%21 = load i8*, i8** %7
%22 = load i32, i32* %8
%23 = call i32 @memcpy(i8* %20, i8* %21, i32 %22)
%24 = load i8*, i8** %12
%25 = load i32, i32* %8
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i8*, i8** %9
%29 = load i32, i32* %10
%30 = call i32 @memcpy(i8* %27, i8* %28, i32 %29)
%31 = load i32, i32* %6
%32 = load i8*, i8** %12
%33 = load i32, i32* %11
%34 = call i32 @write(i32 %31, i8* %32, i32 %33)
store i32 %34, i32* %13
%35 = load i8*, i8** %12
%36 = call i32 @free(i8* %35)
%37 = load i32, i32* %13
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %43
39: ; preds = %5
%40 = load i32, i32* @errno
%41 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %40)
%42 = call i32 @exit(i32 -1)
unreachable
43: ; preds = %5
ret void
}

LLVM IR (-O2):
@.str = [35 x i8] c"fatal error while write, errno %d\0A\00"
@errno = global i32 0
define void @test_write(i32 %0, i8* %1, i32 %2, i8* %3, i32 %4) {
%6 = add nsw i32 %4, %2
%7 = tail call i64 @malloc(i32 %6)
%8 = inttoptr i64 %7 to i8*
%9 = tail call i32 @memcpy(i8* %8, i8* %1, i32 %2)
%10 = sext i32 %2 to i64
%11 = getelementptr inbounds i8, i8* %8, i64 %10
%12 = tail call i32 @memcpy(i8* %11, i8* %3, i32 %4)
%13 = tail call i32 @write(i32 %0, i8* %8, i32 %6)
%14 = tail call i32 @free(i8* %8)
%15 = icmp eq i32 %13, -1
br i1 %15, label %16, label %20
16: ; preds = %5
%17 = load i32, i32* @errno
%18 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = tail call i32 @exit(i32 -1)
unreachable
20: ; preds = %5
ret void
}