#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 stat {
  int /*<<< orphan*/ st_size;
};
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SEEK_SET;
int close(int);
int errno;
int /*<<< orphan*/ error(int /*<<< orphan*/, int, char *, int, ...);
int /*<<< orphan*/ fstat(int, struct stat *);
int /*<<< orphan*/ fsync(int);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int mkstemp(char *);
int readahead(int, int /*<<< orphan*/, int /*<<< orphan*/);
int sendfile(int, int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int group(char *headers, int datafd, off_t datasz, off_t *groupedsz) {
  char tname[] = "/tmp/netshare_XXXXXX";
  int newfd = mkstemp(tname);
  if (newfd == -1) {
    error(EXIT_FAILURE, errno, "Error %d creating temporary file %s", errno,
          tname);
  }
  if (write(newfd, headers, strlen(headers)) == -1) {
    error(EXIT_FAILURE, errno, "Error %d writing headers to grouped file",
          errno);
  }
  if (sendfile(newfd, datafd, NULL, datasz) == -1) {
    error(EXIT_FAILURE, errno, "Error %d sendfile-ing payload to grouped file",
          errno);
  }
  fsync(newfd);
  lseek(newfd, 0, SEEK_SET);
  if (close(datafd) == -1) {
    error(EXIT_FAILURE, errno, "Error %d closing payload fd", errno);
  }
  struct stat datastat;
  fstat(newfd, &datastat);
  *groupedsz = datastat.st_size;
  if (readahead(newfd, 0, *groupedsz) == -1) {
    error(EXIT_FAILURE, errno, "Error %d readahead of grouped file", errno);
  }
  return newfd;
}


x86 assembly (clang, -O0):
<group>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    0xe43(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe40(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe3e(%rip),%eax
mov    %eax,-0x20(%rbp)
mov    0xe39(%rip),%al
mov    %al,-0x1c(%rbp)
lea    -0x30(%rbp),%rdi
call   1070 <mkstemp@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1221 <group+0x81>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
mov    0x2dd3(%rip),%rax
mov    (%rax),%ecx
lea    -0x30(%rbp),%r8
lea    0xdfb(%rip),%rdx
mov    $0x0,%al
call   1090 <error@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1278 <group+0xd8>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
mov    0x2d81(%rip),%rax
mov    (%rax),%esi
mov    0x2d78(%rip),%rax
mov    (%rax),%ecx
lea    0xdc8(%rip),%rdx
mov    $0x0,%al
call   1090 <error@plt>
mov    -0x34(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%ecx
xor    %eax,%eax
mov    %eax,%edx
call   1080 <sendfile@plt>
cmp    $0xffffffff,%eax
jne    12bc <group+0x11c>
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
mov    0x2d3d(%rip),%rax
mov    (%rax),%esi
mov    0x2d34(%rip),%rax
mov    (%rax),%ecx
lea    0xdad(%rip),%rdx
mov    $0x0,%al
call   1090 <error@plt>
mov    -0x34(%rbp),%edi
call   10a0 <fsync@plt>
mov    -0x34(%rbp),%edi
mov    0x2d0a(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    1311 <group+0x171>
mov    0x2ce1(%rip),%rax
mov    (%rax),%edi
mov    0x2ce8(%rip),%rax
mov    (%rax),%esi
mov    0x2cdf(%rip),%rax
mov    (%rax),%ecx
lea    0xd86(%rip),%rdx
mov    $0x0,%al
call   1090 <error@plt>
mov    -0x34(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   10c0 <fstat@plt>
mov    -0x38(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x34(%rbp),%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10b0 <readahead@plt>
cmp    $0xffffffff,%eax
jne    1368 <group+0x1c8>
mov    0x2c8a(%rip),%rax
mov    (%rax),%edi
mov    0x2c91(%rip),%rax
mov    (%rax),%esi
mov    0x2c88(%rip),%rax
mov    (%rax),%ecx
lea    0xd4b(%rip),%rdx
mov    $0x0,%al
call   1090 <error@plt>
mov    -0x34(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<group>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rcx,%r14
mov    %edx,%r15d
mov    %esi,%r12d
mov    %rdi,%rbp
movaps 0xe41(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movabs $0x5858585858585f,%rax
mov    %rax,0x1d(%rsp)
lea    0x10(%rsp),%rdi
call   1070 <mkstemp@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    120b <group+0x6b>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%ecx
lea    0xe18(%rip),%rdx
lea    0x10(%rsp),%r8
mov    %ecx,%esi
xor    %eax,%eax
call   1090 <error@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1246 <group+0xa6>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
mov    0x2dac(%rip),%rax
mov    (%rax),%ecx
lea    0xdfc(%rip),%rdx
mov    %ecx,%esi
xor    %eax,%eax
call   1090 <error@plt>
mov    %ebx,%edi
mov    %r12d,%esi
xor    %edx,%edx
mov    %r15d,%ecx
call   1080 <sendfile@plt>
cmp    $0xffffffff,%eax
jne    127c <group+0xdc>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
mov    0x2d76(%rip),%rax
mov    (%rax),%ecx
lea    0xdef(%rip),%rdx
mov    %ecx,%esi
xor    %eax,%eax
call   1090 <error@plt>
mov    %ebx,%edi
call   10a0 <fsync@plt>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %r12d,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    12c4 <group+0x124>
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
mov    0x2d2e(%rip),%rax
mov    (%rax),%ecx
lea    0xdd5(%rip),%rdx
mov    %ecx,%esi
xor    %eax,%eax
call   1090 <error@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   10c0 <fstat@plt>
mov    0x8(%rsp),%edx
mov    %edx,(%r14)
mov    %ebx,%edi
xor    %esi,%esi
call   10b0 <readahead@plt>
cmp    $0xffffffff,%eax
jne    1307 <group+0x167>
mov    0x2ce4(%rip),%rax
mov    (%rax),%edi
mov    0x2ceb(%rip),%rax
mov    (%rax),%ecx
lea    0xdae(%rip),%rdx
mov    %ecx,%esi
xor    %eax,%eax
call   1090 <error@plt>
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@__const.group.tname = [21 x i8] c"/tmp/netshare_XXXXXX\00"
@EXIT_FAILURE = global i32 0
@errno = global i32 0
@.str = [36 x i8] c"Error %d creating temporary file %s\00"
@.str.1 = [41 x i8] c"Error %d writing headers to grouped file\00"
@.str.2 = [46 x i8] c"Error %d sendfile-ing payload to grouped file\00"
@SEEK_SET = global i32 0
@.str.3 = [28 x i8] c"Error %d closing payload fd\00"
@.str.4 = [35 x i8] c"Error %d readahead of grouped file\00"
define i32 @group(i8* %0, i32 %1, i32 %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca [21 x i8]
%10 = alloca i32
%11 = alloca %struct.stat
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32* %3, i32** %8
%12 = bitcast [21 x i8]* %9 to i8*
%13 = getelementptr inbounds [21 x i8], [21 x i8]* %9, i64 0, i64 0
%14 = call i32 @mkstemp(i8* %13)
store i32 %14, i32* %10
%15 = load i32, i32* %10
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %23
17: ; preds = %4
%18 = load i32, i32* @EXIT_FAILURE
%19 = load i32, i32* @errno
%20 = load i32, i32* @errno
%21 = getelementptr inbounds [21 x i8], [21 x i8]* %9, i64 0, i64 0
%22 = call i32 (i32, i32, i8*, i32, ...) @error(i32 %18, i32 %19, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %20, i8* %21)
br label %23
23: ; preds = %17, %4
%24 = load i32, i32* %10
%25 = load i8*, i8** %5
%26 = load i8*, i8** %5
%27 = call i32 @strlen(i8* %26)
%28 = call i32 @write(i32 %24, i8* %25, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %35
30: ; preds = %23
%31 = load i32, i32* @EXIT_FAILURE
%32 = load i32, i32* @errno
%33 = load i32, i32* @errno
%34 = call i32 (i32, i32, i8*, i32, ...) @error(i32 %31, i32 %32, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %33)
br label %35
35: ; preds = %30, %23
%36 = load i32, i32* %10
%37 = load i32, i32* %6
%38 = load i32, i32* %7
%39 = call i32 @sendfile(i32 %36, i32 %37, i32* null, i32 %38)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %46
41: ; preds = %35
%42 = load i32, i32* @EXIT_FAILURE
%43 = load i32, i32* @errno
%44 = load i32, i32* @errno
%45 = call i32 (i32, i32, i8*, i32, ...) @error(i32 %42, i32 %43, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i32 %44)
br label %46
46: ; preds = %41, %35
%47 = load i32, i32* %10
%48 = call i32 @fsync(i32 %47)
%49 = load i32, i32* %10
%50 = load i32, i32* @SEEK_SET
%51 = call i32 @lseek(i32 %49, i32 0, i32 %50)
%52 = load i32, i32* %6
%53 = call i32 @close(i32 %52)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %60
55: ; preds = %46
%56 = load i32, i32* @EXIT_FAILURE
%57 = load i32, i32* @errno
%58 = load i32, i32* @errno
%59 = call i32 (i32, i32, i8*, i32, ...) @error(i32 %56, i32 %57, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %58)
br label %60
60: ; preds = %55, %46
%61 = load i32, i32* %10
%62 = call i32 @fstat(i32 %61, %struct.stat* %11)
%63 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%64 = load i32, i32* %63
%65 = load i32*, i32** %8
store i32 %64, i32* %65
%66 = load i32, i32* %10
%67 = load i32*, i32** %8
%68 = load i32, i32* %67
%69 = call i32 @readahead(i32 %66, i32 0, i32 %68)
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %76
71: ; preds = %60
%72 = load i32, i32* @EXIT_FAILURE
%73 = load i32, i32* @errno
%74 = load i32, i32* @errno
%75 = call i32 (i32, i32, i8*, i32, ...) @error(i32 %72, i32 %73, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i32 %74)
br label %76
76: ; preds = %71, %60
%77 = load i32, i32* %10
ret i32 %77
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@__const.group.tname = [21 x i8] c"/tmp/netshare_XXXXXX\00"
@EXIT_FAILURE = global i32 0
@errno = global i32 0
@.str = [36 x i8] c"Error %d creating temporary file %s\00"
@.str.1 = [41 x i8] c"Error %d writing headers to grouped file\00"
@.str.2 = [46 x i8] c"Error %d sendfile-ing payload to grouped file\00"
@SEEK_SET = global i32 0
@.str.3 = [28 x i8] c"Error %d closing payload fd\00"
@.str.4 = [35 x i8] c"Error %d readahead of grouped file\00"
define i32 @group(i8* %0, i32 %1, i32 %2, i32* nocapture writeonly %3) {
%5 = alloca [21 x i8]
%6 = alloca %struct.stat
%7 = getelementptr inbounds [21 x i8], [21 x i8]* %5, i64 0, i64 0
%8 = call i32 @mkstemp(i8* %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %14
10: ; preds = %4
%11 = load i32, i32* @EXIT_FAILURE
%12 = load i32, i32* @errno
%13 = call i32 (i32, i32, i8*, i32, ...) @error(i32 %11, i32 %12, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %12, i8* %7)
br label %14
14: ; preds = %10, %4
%15 = call i32 @strlen(i8* %0)
%16 = call i32 @write(i32 %8, i8* %0, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = load i32, i32* @EXIT_FAILURE
%20 = load i32, i32* @errno
%21 = call i32 (i32, i32, i8*, i32, ...) @error(i32 %19, i32 %20, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %18, %14
%23 = call i32 @sendfile(i32 %8, i32 %1, i32* null, i32 %2)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i32, i32* @EXIT_FAILURE
%27 = load i32, i32* @errno
%28 = call i32 (i32, i32, i8*, i32, ...) @error(i32 %26, i32 %27, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %25, %22
%30 = call i32 @fsync(i32 %8)
%31 = load i32, i32* @SEEK_SET
%32 = call i32 @lseek(i32 %8, i32 0, i32 %31)
%33 = call i32 @close(i32 %1)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %39
35: ; preds = %29
%36 = load i32, i32* @EXIT_FAILURE
%37 = load i32, i32* @errno
%38 = call i32 (i32, i32, i8*, i32, ...) @error(i32 %36, i32 %37, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %37)
br label %39
39: ; preds = %35, %29
%40 = bitcast %struct.stat* %6 to i8*
%41 = call i32 @fstat(i32 %8, %struct.stat* %6)
%42 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 0
%43 = load i32, i32* %42
store i32 %43, i32* %3
%44 = call i32 @readahead(i32 %8, i32 0, i32 %43)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %50
46: ; preds = %39
%47 = load i32, i32* @EXIT_FAILURE
%48 = load i32, i32* @errno
%49 = call i32 (i32, i32, i8*, i32, ...) @error(i32 %47, i32 %48, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i32 %48)
br label %50
50: ; preds = %46, %39
ret i32 %8
}