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

/* Variables and functions */
float INFINITY;
float copysignf(float, float);
int /*<<< orphan*/ wbuf_write(struct wbuf *, float *, int);
void write_triangle(struct wbuf *buf, unsigned indices[3], float weight,
                    bool invert, float (*vertices)[2]) {
  float q[3][3], one, winv, s;
  float(*p)[2];
  size_t i;
  s = invert ? -1.0 : 1.0;
  one = copysignf(1.0f, s);
  winv = copysignf((weight <= 0.f) ? INFINITY : 1.f / weight, s);
  q[0][0] = 0.f;
  q[0][1] = 0.f;
  q[0][2] = one;
  q[1][0] = 0.5f * winv;
  q[1][1] = 0.0f;
  q[1][2] = winv;
  q[2][0] = one;
  q[2][1] = one;
  q[2][2] = one;
  for (i = 0; i < 3; i++) {
    p = vertices + indices[i];
    (void)wbuf_write(buf, *p, sizeof *p);
    (void)wbuf_write(buf, q[i], sizeof q[i]);
  }
}


x86 assembly (clang, -O0):
<write_triangle>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movss  %xmm0,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%eax
movsd  0xef6(%rip),%xmm0
movsd  0xef6(%rip),%xmm1
movsd  %xmm1,-0x80(%rbp)
cmp    $0x0,%eax
movsd  %xmm0,-0x78(%rbp)
jne    115f <write_triangle+0x4f>
movsd  -0x80(%rbp),%xmm0
movsd  %xmm0,-0x78(%rbp)
movsd  -0x78(%rbp),%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x5c(%rbp)
movss  -0x5c(%rbp),%xmm0
movaps 0xe87(%rip),%xmm1
pand   %xmm1,%xmm0
movaps 0xe8c(%rip),%xmm1
por    %xmm1,%xmm0
movss  %xmm0,-0x54(%rbp)
xorps  %xmm0,%xmm0
ucomiss -0x14(%rbp),%xmm0
jb     11b2 <write_triangle+0xa2>
mov    0x2e47(%rip),%rax
movss  (%rax),%xmm0
movss  %xmm0,-0x84(%rbp)
ff
jmp    11c7 <write_triangle+0xb7>
movss  0xe86(%rip),%xmm0
divss  -0x14(%rbp),%xmm0
movss  %xmm0,-0x84(%rbp)
ff
movss  -0x84(%rbp),%xmm0
ff
movss  -0x5c(%rbp),%xmm1
movaps 0xe25(%rip),%xmm2
pand   %xmm2,%xmm1
movaps 0xe3a(%rip),%xmm2
pand   %xmm2,%xmm0
por    %xmm1,%xmm0
movss  %xmm0,-0x58(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x50(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x4c(%rbp)
movss  -0x54(%rbp),%xmm0
movss  %xmm0,-0x48(%rbp)
movss  0xe2f(%rip),%xmm0
mulss  -0x58(%rbp),%xmm0
movss  %xmm0,-0x44(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x40(%rbp)
movss  -0x58(%rbp),%xmm0
movss  %xmm0,-0x3c(%rbp)
movss  -0x54(%rbp),%xmm0
movss  %xmm0,-0x38(%rbp)
movss  -0x54(%rbp),%xmm0
movss  %xmm0,-0x34(%rbp)
movss  -0x54(%rbp),%xmm0
movss  %xmm0,-0x30(%rbp)
movq   $0x0,-0x70(%rbp)
cmpq   $0x3,-0x70(%rbp)
jae    12b9 <write_triangle+0x1a9>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    -0x70(%rbp),%rdx
mov    (%rcx,%rdx,4),%ecx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x68(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x68(%rbp),%rsi
mov    $0x8,%edx
call   1030 <wbuf_write@plt>
mov    -0x8(%rbp),%rdi
lea    -0x50(%rbp),%rsi
imul   $0xc,-0x70(%rbp),%rax
add    %rax,%rsi
mov    $0xc,%edx
call   1030 <wbuf_write@plt>
mov    -0x70(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x70(%rbp)
jmp    1257 <write_triangle+0x147>
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_triangle>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rcx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %eax,%eax
test   %edx,%edx
sete   %al
lea    0xecc(%rip),%rcx
movss  (%rcx,%rax,4),%xmm2
andps  0xed0(%rip),%xmm2
movaps 0xed9(%rip),%xmm1
orps   %xmm2,%xmm1
xorps  %xmm3,%xmm3
ucomiss %xmm0,%xmm3
jae    1160 <write_triangle+0x50>
movss  0xee6(%rip),%xmm3
divss  %xmm0,%xmm3
jmp    116b <write_triangle+0x5b>
mov    0x2e81(%rip),%rax
movss  (%rax),%xmm3
andps  0xebe(%rip),%xmm3
orps   %xmm2,%xmm3
movq   $0x0,(%rsp)
movss  %xmm1,0x8(%rsp)
movaps %xmm3,%xmm0
mulss  0xeb6(%rip),%xmm0
lea    0xc(%rsp),%r13
movss  %xmm0,0xc(%rsp)
movl   $0x0,0x10(%rsp)
movss  %xmm3,0x14(%rsp)
lea    0x18(%rsp),%r12
movss  %xmm1,0x18(%rsp)
movss  %xmm1,0x1c(%rsp)
movss  %xmm1,0x20(%rsp)
mov    (%r15),%eax
lea    (%r14,%rax,8),%rsi
mov    %rbx,%rdi
mov    $0x8,%edx
call   1030 <wbuf_write@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    $0xc,%edx
call   1030 <wbuf_write@plt>
mov    0x4(%r15),%eax
lea    (%r14,%rax,8),%rsi
mov    %rbx,%rdi
mov    $0x8,%edx
call   1030 <wbuf_write@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    $0xc,%edx
call   1030 <wbuf_write@plt>
mov    0x8(%r15),%eax
lea    (%r14,%rax,8),%rsi
mov    %rbx,%rdi
mov    $0x8,%edx
call   1030 <wbuf_write@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0xc,%edx
call   1030 <wbuf_write@plt>
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.wbuf = type { i32 }
@INFINITY = global float 0.000000e+00
define void @write_triangle(%struct.wbuf* %0, i32* %1, float %2, i32 %3, [2 x float]* %4) {
%6 = alloca %struct.wbuf*
%7 = alloca i32*
%8 = alloca float
%9 = alloca i32
%10 = alloca [2 x float]*
%11 = alloca [3 x [3 x float]]
%12 = alloca float
%13 = alloca float
%14 = alloca float
%15 = alloca [2 x float]*
%16 = alloca i64
store %struct.wbuf* %0, %struct.wbuf** %6
store i32* %1, i32** %7
store float %2, float* %8
store i32 %3, i32* %9
store [2 x float]* %4, [2 x float]** %10
%17 = load i32, i32* %9
%18 = icmp ne i32 %17, 0
%19 = zext i1 %18 to i64
%20 = select i1 %18, double -1.000000e+00, double 1.000000e+00
%21 = fptrunc double %20 to float
store float %21, float* %14
%22 = load float, float* %14
%23 = call float @llvm.copysign.f32(float 1.000000e+00, float %22)
store float %23, float* %12
%24 = load float, float* %8
%25 = fcmp ole float %24, 0.000000e+00
br i1 %25, label %26, label %28
26: ; preds = %5
%27 = load float, float* @INFINITY
br label %31
28: ; preds = %5
%29 = load float, float* %8
%30 = fdiv float 1.000000e+00, %29
br label %31
31: ; preds = %28, %26
%32 = phi float [ %27, %26 ], [ %30, %28 ]
%33 = load float, float* %14
%34 = call float @llvm.copysign.f32(float %32, float %33)
store float %34, float* %13
%35 = getelementptr inbounds [3 x [3 x float]], [3 x [3 x float]]* %11, i64 0, i64 0
%36 = getelementptr inbounds [3 x float], [3 x float]* %35, i64 0, i64 0
store float 0.000000e+00, float* %36
%37 = getelementptr inbounds [3 x [3 x float]], [3 x [3 x float]]* %11, i64 0, i64 0
%38 = getelementptr inbounds [3 x float], [3 x float]* %37, i64 0, i64 1
store float 0.000000e+00, float* %38
%39 = load float, float* %12
%40 = getelementptr inbounds [3 x [3 x float]], [3 x [3 x float]]* %11, i64 0, i64 0
%41 = getelementptr inbounds [3 x float], [3 x float]* %40, i64 0, i64 2
store float %39, float* %41
%42 = load float, float* %13
%43 = fmul float 5.000000e-01, %42
%44 = getelementptr inbounds [3 x [3 x float]], [3 x [3 x float]]* %11, i64 0, i64 1
%45 = getelementptr inbounds [3 x float], [3 x float]* %44, i64 0, i64 0
store float %43, float* %45
%46 = getelementptr inbounds [3 x [3 x float]], [3 x [3 x float]]* %11, i64 0, i64 1
%47 = getelementptr inbounds [3 x float], [3 x float]* %46, i64 0, i64 1
store float 0.000000e+00, float* %47
%48 = load float, float* %13
%49 = getelementptr inbounds [3 x [3 x float]], [3 x [3 x float]]* %11, i64 0, i64 1
%50 = getelementptr inbounds [3 x float], [3 x float]* %49, i64 0, i64 2
store float %48, float* %50
%51 = load float, float* %12
%52 = getelementptr inbounds [3 x [3 x float]], [3 x [3 x float]]* %11, i64 0, i64 2
%53 = getelementptr inbounds [3 x float], [3 x float]* %52, i64 0, i64 0
store float %51, float* %53
%54 = load float, float* %12
%55 = getelementptr inbounds [3 x [3 x float]], [3 x [3 x float]]* %11, i64 0, i64 2
%56 = getelementptr inbounds [3 x float], [3 x float]* %55, i64 0, i64 1
store float %54, float* %56
%57 = load float, float* %12
%58 = getelementptr inbounds [3 x [3 x float]], [3 x [3 x float]]* %11, i64 0, i64 2
%59 = getelementptr inbounds [3 x float], [3 x float]* %58, i64 0, i64 2
store float %57, float* %59
store i64 0, i64* %16
br label %60
60: ; preds = %80, %31
%61 = load i64, i64* %16
%62 = icmp ult i64 %61, 3
br i1 %62, label %63, label %83
63: ; preds = %60
%64 = load [2 x float]*, [2 x float]** %10
%65 = load i32*, i32** %7
%66 = load i64, i64* %16
%67 = getelementptr inbounds i32, i32* %65, i64 %66
%68 = load i32, i32* %67
%69 = zext i32 %68 to i64
%70 = getelementptr inbounds [2 x float], [2 x float]* %64, i64 %69
store [2 x float]* %70, [2 x float]** %15
%71 = load %struct.wbuf*, %struct.wbuf** %6
%72 = load [2 x float]*, [2 x float]** %15
%73 = getelementptr inbounds [2 x float], [2 x float]* %72, i64 0, i64 0
%74 = call i32 @wbuf_write(%struct.wbuf* %71, float* %73, i32 8)
%75 = load %struct.wbuf*, %struct.wbuf** %6
%76 = load i64, i64* %16
%77 = getelementptr inbounds [3 x [3 x float]], [3 x [3 x float]]* %11, i64 0, i64 %76
%78 = getelementptr inbounds [3 x float], [3 x float]* %77, i64 0, i64 0
%79 = call i32 @wbuf_write(%struct.wbuf* %75, float* %78, i32 12)
br label %80
80: ; preds = %63
%81 = load i64, i64* %16
%82 = add i64 %81, 1
store i64 %82, i64* %16
br label %60
83: ; preds = %60
ret void
}

LLVM IR (-O2):
%struct.wbuf = type { i32 }
@INFINITY = global float 0.000000e+00
define void @write_triangle(%struct.wbuf* %0, i32* nocapture %1, float %2, i32 %3, [2 x float]* %4) {
%6 = alloca [3 x [3 x float]]
%7 = bitcast [3 x [3 x float]]* %6 to i8*
%8 = icmp eq i32 %3, 0
%9 = select i1 %8, float 1.000000e+00, float -1.000000e+00
%10 = tail call float @llvm.copysign.f32(float 1.000000e+00, float %9)
%11 = fcmp ugt float %2, 0.000000e+00
br i1 %11, label %14, label %12
12: ; preds = %5
%13 = load float, float* @INFINITY
br label %16
14: ; preds = %5
%15 = fdiv float 1.000000e+00, %2
br label %16
16: ; preds = %14, %12
%17 = phi float [ %13, %12 ], [ %15, %14 ]
%18 = tail call float @llvm.copysign.f32(float %17, float %9)
%19 = getelementptr inbounds [3 x [3 x float]], [3 x [3 x float]]* %6, i64 0, i64 0, i64 0
store float 0.000000e+00, float* %19
%20 = getelementptr inbounds [3 x [3 x float]], [3 x [3 x float]]* %6, i64 0, i64 0, i64 1
store float 0.000000e+00, float* %20
%21 = getelementptr inbounds [3 x [3 x float]], [3 x [3 x float]]* %6, i64 0, i64 0, i64 2
store float %10, float* %21
%22 = fmul float %18, 5.000000e-01
%23 = getelementptr inbounds [3 x [3 x float]], [3 x [3 x float]]* %6, i64 0, i64 1, i64 0
store float %22, float* %23
%24 = getelementptr inbounds [3 x [3 x float]], [3 x [3 x float]]* %6, i64 0, i64 1, i64 1
store float 0.000000e+00, float* %24
%25 = getelementptr inbounds [3 x [3 x float]], [3 x [3 x float]]* %6, i64 0, i64 1, i64 2
store float %18, float* %25
%26 = getelementptr inbounds [3 x [3 x float]], [3 x [3 x float]]* %6, i64 0, i64 2, i64 0
store float %10, float* %26
%27 = getelementptr inbounds [3 x [3 x float]], [3 x [3 x float]]* %6, i64 0, i64 2, i64 1
store float %10, float* %27
%28 = getelementptr inbounds [3 x [3 x float]], [3 x [3 x float]]* %6, i64 0, i64 2, i64 2
store float %10, float* %28
%29 = load i32, i32* %1
%30 = zext i32 %29 to i64
%31 = getelementptr inbounds [2 x float], [2 x float]* %4, i64 %30, i64 0
%32 = call i32 @wbuf_write(%struct.wbuf* %0, float* %31, i32 8)
%33 = call i32 @wbuf_write(%struct.wbuf* %0, float* %19, i32 12)
%34 = getelementptr inbounds i32, i32* %1, i64 1
%35 = load i32, i32* %34
%36 = zext i32 %35 to i64
%37 = getelementptr inbounds [2 x float], [2 x float]* %4, i64 %36, i64 0
%38 = call i32 @wbuf_write(%struct.wbuf* %0, float* %37, i32 8)
%39 = call i32 @wbuf_write(%struct.wbuf* %0, float* %23, i32 12)
%40 = getelementptr inbounds i32, i32* %1, i64 2
%41 = load i32, i32* %40
%42 = zext i32 %41 to i64
%43 = getelementptr inbounds [2 x float], [2 x float]* %4, i64 %42, i64 0
%44 = call i32 @wbuf_write(%struct.wbuf* %0, float* %43, i32 8)
%45 = call i32 @wbuf_write(%struct.wbuf* %0, float* %26, i32 12)
ret void
}