#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*/ assert(int);
int /*<<< orphan*/ close(int);
int dup(int);
int sync_merge(char const *, int, int);
int sync_accumulate(const char *name, int *fd1, int fd2) {
  int ret;
  assert(fd2 >= 0);
  if (*fd1 < 0) {
    *fd1 = dup(fd2);
    return 0;
  }
  ret = sync_merge(name, *fd1, fd2);
  if (ret < 0) {
    return ret;
  }
  close(*fd1);
  *fd1 = ret;
  return 0;
}


x86 assembly (clang, -O0):
<sync_accumulate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
jge    118d <sync_accumulate+0x4d>
mov    -0x1c(%rbp),%edi
call   1030 <dup@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11d2 <sync_accumulate+0x92>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
call   1050 <sync_merge@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11b7 <sync_accumulate+0x77>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d2 <sync_accumulate+0x92>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sync_accumulate>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %edx,%edi
not    %edi
shr    $0x1f,%edi
call   1060 <assert@plt>
mov    (%rbx),%esi
test   %esi,%esi
js     1177 <sync_accumulate+0x37>
mov    %r14,%rdi
mov    %ebp,%edx
call   1050 <sync_merge@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1184 <sync_accumulate+0x44>
mov    (%rbx),%edi
call   1040 <close@plt>
jmp    1180 <sync_accumulate+0x40>
mov    %ebp,%edi
call   1030 <dup@plt>
mov    %eax,%ebp
mov    %ebp,(%rbx)
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @sync_accumulate(i8* %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%9 = load i32, i32* %7
%10 = icmp sge i32 %9, 0
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = load i32*, i32** %6
%14 = load i32, i32* %13
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %3
%17 = load i32, i32* %7
%18 = call i32 @dup(i32 %17)
%19 = load i32*, i32** %6
store i32 %18, i32* %19
store i32 0, i32* %4
br label %36
20: ; preds = %3
%21 = load i8*, i8** %5
%22 = load i32*, i32** %6
%23 = load i32, i32* %22
%24 = load i32, i32* %7
%25 = call i32 @sync_merge(i8* %21, i32 %23, i32 %24)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %20
%29 = load i32, i32* %8
store i32 %29, i32* %4
br label %36
30: ; preds = %20
%31 = load i32*, i32** %6
%32 = load i32, i32* %31
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* %8
%35 = load i32*, i32** %6
store i32 %34, i32* %35
store i32 0, i32* %4
br label %36
36: ; preds = %30, %28, %16
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
define i32 @sync_accumulate(i8* %0, i32* nocapture %1, i32 %2) {
%4 = xor i32 %2, -1
%5 = lshr i32 %4, 31
%6 = tail call i32 @assert(i32 %5)
%7 = load i32, i32* %1
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = tail call i32 @dup(i32 %2)
br label %17
11: ; preds = %3
%12 = tail call i32 @sync_merge(i8* %0, i32 %7, i32 %2)
%13 = icmp slt i32 %12, 0
br i1 %13, label %19, label %14
14: ; preds = %11
%15 = load i32, i32* %1
%16 = tail call i32 @close(i32 %15)
br label %17
17: ; preds = %9, %14
%18 = phi i32 [ %12, %14 ], [ %10, %9 ]
store i32 %18, i32* %1
br label %19
19: ; preds = %17, %11
%20 = phi i32 [ %12, %11 ], [ 0, %17 ]
ret i32 %20
}