#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*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int write(int, char *, int);
int writeheader(int destfd, const char *path, int mode, int uid, int gid,
                int size, int mtime, int type) {
  char header[512];
  int i, sum;
  memset(header, 0, 512);
  sprintf(header, "%s", path);
  sprintf(header + 100, "%07o", mode & 0777);
  sprintf(header + 108, "%07o", uid);
  sprintf(header + 116, "%07o", gid);
  sprintf(header + 124, "%011o", size);
  sprintf(header + 136, "%07o", mtime);
  sprintf(header + 148, "        %1d", type);
  for (i = sum = 0; i < 512; i++)
    sum += header[i];
  sprintf(header + 148, "%06o", sum);
  return write(destfd, header, 512);
}


x86 assembly (clang, -O0):
<writeheader>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    0x18(%rbp),%eax
mov    0x10(%rbp),%eax
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    %r9d,-0x20(%rbp)
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1040 <memset@plt>
lea    -0x220(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe85(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
lea    -0x220(%rbp),%rdi
add    $0x64,%rdi
mov    -0x14(%rbp),%edx
and    $0x1ff,%edx
lea    0xe66(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
lea    -0x220(%rbp),%rdi
add    $0x6c,%rdi
mov    -0x18(%rbp),%edx
lea    0xe4a(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
lea    -0x220(%rbp),%rdi
add    $0x74,%rdi
mov    -0x1c(%rbp),%edx
lea    0xe2e(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
lea    -0x220(%rbp),%rdi
add    $0x7c,%rdi
mov    -0x20(%rbp),%edx
lea    0xe17(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
lea    -0x220(%rbp),%rdi
add    $0x88,%rdi
mov    0x10(%rbp),%edx
lea    0xdf3(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
lea    -0x220(%rbp),%rdi
add    $0x94,%rdi
mov    0x18(%rbp),%edx
lea    0xddf(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
movl   $0x0,-0x228(%rbp)
movl   $0x0,-0x224(%rbp)
cmpl   $0x200,-0x224(%rbp)
jge    1289 <writeheader+0x159>
movslq -0x224(%rbp),%rax
movsbl -0x220(%rbp,%rax,1),%eax
ff
add    -0x228(%rbp),%eax
mov    %eax,-0x228(%rbp)
mov    -0x224(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x224(%rbp)
jmp    124a <writeheader+0x11a>
lea    -0x220(%rbp),%rdi
add    $0x94,%rdi
mov    -0x228(%rbp),%edx
lea    0xd76(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
mov    -0x4(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1030 <write@plt>
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeheader>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %r9d,0x8(%rsp)
mov    %r8d,0x4(%rsp)
mov    %ecx,%r13d
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %edi,0xc(%rsp)
lea    0x10(%rsp),%rbp
xor    %r12d,%r12d
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1050 <memset@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
lea    0x74(%rsp),%rdi
and    $0x1ff,%r15d
lea    0xe63(%rip),%r14
mov    %r14,%rsi
mov    %r15d,%edx
xor    %eax,%eax
call   1060 <sprintf@plt>
lea    0x7c(%rsp),%rdi
mov    %r14,%rsi
mov    %r13d,%edx
xor    %eax,%eax
call   1060 <sprintf@plt>
lea    0x84(%rsp),%rdi
mov    %r14,%rsi
mov    0x4(%rsp),%edx
xor    %eax,%eax
call   1060 <sprintf@plt>
lea    0x8c(%rsp),%rdi
lea    0xe24(%rip),%rsi
mov    0x8(%rsp),%edx
xor    %eax,%eax
call   1060 <sprintf@plt>
lea    0x98(%rsp),%rdi
mov    %r14,%rsi
mov    0x250(%rsp),%edx
xor    %eax,%eax
call   1060 <sprintf@plt>
lea    0xa4(%rsp),%rbx
lea    0xdf7(%rip),%rsi
mov    %rbx,%rdi
mov    0x258(%rsp),%edx
xor    %eax,%eax
call   1060 <sprintf@plt>
pxor   %xmm0,%xmm0
pxor   %xmm1,%xmm1
nopl   (%rax)
movd   0x14(%rsp,%r12,1),%xmm2
punpcklbw %xmm2,%xmm2
punpcklwd %xmm2,%xmm2
punpcklbw 0x10(%rsp,%r12,1),%xmm3
punpcklwd %xmm3,%xmm3
psrad  $0x18,%xmm3
paddd  %xmm0,%xmm3
psrad  $0x18,%xmm2
paddd  %xmm1,%xmm2
movq   0x18(%rsp,%r12,1),%xmm0
punpcklbw %xmm0,%xmm0
punpcklwd %xmm0,%xmm0
movd   0x1c(%rsp,%r12,1),%xmm1
punpcklbw %xmm1,%xmm1
punpcklwd %xmm1,%xmm1
psrad  $0x18,%xmm0
paddd  %xmm3,%xmm0
psrad  $0x18,%xmm1
paddd  %xmm2,%xmm1
add    $0x10,%r12
cmp    $0x200,%r12
jne    1230 <writeheader+0xf0>
paddd  %xmm0,%xmm1
pshufd $0xee,%xmm1,%xmm0
paddd  %xmm1,%xmm0
pshufd $0x55,%xmm0,%xmm1
paddd  %xmm0,%xmm1
movd   %xmm1,%edx
lea    0xd5d(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <sprintf@plt>
lea    0x10(%rsp),%rsi
mov    0xc(%rsp),%edi
mov    $0x200,%edx
call   1040 <write@plt>
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@.str.1 = [5 x i8] c"%07o\00"
@.str.2 = [6 x i8] c"%011o\00"
@.str.3 = [12 x i8] c" %1d\00"
@.str.4 = [5 x i8] c"%06o\00"
define i32 @writeheader(i32 %0, i8* %1, i32 %2, i32 %3, i32 %4, i32 %5, i32 %6, i32 %7) {
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca [512 x i8]
%18 = alloca i32
%19 = alloca i32
store i32 %0, i32* %9
store i8* %1, i8** %10
store i32 %2, i32* %11
store i32 %3, i32* %12
store i32 %4, i32* %13
store i32 %5, i32* %14
store i32 %6, i32* %15
store i32 %7, i32* %16
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %17, i64 0, i64 0
%21 = call i32 @memset(i8* %20, i32 0, i32 512)
%22 = getelementptr inbounds [512 x i8], [512 x i8]* %17, i64 0, i64 0
%23 = load i8*, i8** %10
%24 = call i32 (i8*, i8*, ...) @sprintf(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = getelementptr inbounds [512 x i8], [512 x i8]* %17, i64 0, i64 0
%26 = getelementptr inbounds i8, i8* %25, i64 100
%27 = load i32, i32* %11
%28 = and i32 %27, 511
%29 = call i32 (i8*, i8*, ...) @sprintf(i8* %26, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %28)
%30 = getelementptr inbounds [512 x i8], [512 x i8]* %17, i64 0, i64 0
%31 = getelementptr inbounds i8, i8* %30, i64 108
%32 = load i32, i32* %12
%33 = call i32 (i8*, i8*, ...) @sprintf(i8* %31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %32)
%34 = getelementptr inbounds [512 x i8], [512 x i8]* %17, i64 0, i64 0
%35 = getelementptr inbounds i8, i8* %34, i64 116
%36 = load i32, i32* %13
%37 = call i32 (i8*, i8*, ...) @sprintf(i8* %35, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %36)
%38 = getelementptr inbounds [512 x i8], [512 x i8]* %17, i64 0, i64 0
%39 = getelementptr inbounds i8, i8* %38, i64 124
%40 = load i32, i32* %14
%41 = call i32 (i8*, i8*, ...) @sprintf(i8* %39, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %40)
%42 = getelementptr inbounds [512 x i8], [512 x i8]* %17, i64 0, i64 0
%43 = getelementptr inbounds i8, i8* %42, i64 136
%44 = load i32, i32* %15
%45 = call i32 (i8*, i8*, ...) @sprintf(i8* %43, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %44)
%46 = getelementptr inbounds [512 x i8], [512 x i8]* %17, i64 0, i64 0
%47 = getelementptr inbounds i8, i8* %46, i64 148
%48 = load i32, i32* %16
%49 = call i32 (i8*, i8*, ...) @sprintf(i8* %47, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %48)
store i32 0, i32* %19
store i32 0, i32* %18
br label %50
50: ; preds = %61, %8
%51 = load i32, i32* %18
%52 = icmp slt i32 %51, 512
br i1 %52, label %53, label %64
53: ; preds = %50
%54 = load i32, i32* %18
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [512 x i8], [512 x i8]* %17, i64 0, i64 %55
%57 = load i8, i8* %56
%58 = sext i8 %57 to i32
%59 = load i32, i32* %19
%60 = add nsw i32 %59, %58
store i32 %60, i32* %19
br label %61
61: ; preds = %53
%62 = load i32, i32* %18
%63 = add nsw i32 %62, 1
store i32 %63, i32* %18
br label %50
64: ; preds = %50
%65 = getelementptr inbounds [512 x i8], [512 x i8]* %17, i64 0, i64 0
%66 = getelementptr inbounds i8, i8* %65, i64 148
%67 = load i32, i32* %19
%68 = call i32 (i8*, i8*, ...) @sprintf(i8* %66, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %67)
%69 = load i32, i32* %9
%70 = getelementptr inbounds [512 x i8], [512 x i8]* %17, i64 0, i64 0
%71 = call i32 @write(i32 %69, i8* %70, i32 512)
ret i32 %71
}

LLVM IR (-O2):
@.str.1 = [5 x i8] c"%07o\00"
@.str.2 = [6 x i8] c"%011o\00"
@.str.3 = [12 x i8] c" %1d\00"
@.str.4 = [5 x i8] c"%06o\00"
define i32 @writeheader(i32 %0, i8* nocapture %1, i32 %2, i32 %3, i32 %4, i32 %5, i32 %6, i32 %7) {
%9 = alloca [512 x i8]
%10 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%11 = call i32 @memset(i8* %10, i32 0, i32 512)
%12 = call i8* @strcpy(i8* %10, i8* dereferenceable(1) %1)
%13 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 100
%14 = and i32 %2, 511
%15 = call i32 (i8*, i8*, ...) @sprintf(i8* %13, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 108
%17 = call i32 (i8*, i8*, ...) @sprintf(i8* %16, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %3)
%18 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 116
%19 = call i32 (i8*, i8*, ...) @sprintf(i8* %18, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 124
%21 = call i32 (i8*, i8*, ...) @sprintf(i8* %20, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %5)
%22 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 136
%23 = call i32 (i8*, i8*, ...) @sprintf(i8* %22, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%24 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 148
%25 = call i32 (i8*, i8*, ...) @sprintf(i8* %24, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %7)
br label %26
26: ; preds = %26, %8
%27 = phi i64 [ 0, %8 ], [ %51, %26 ]
%28 = phi <4 x i32> [ zeroinitializer, %8 ], [ %49, %26 ]
%29 = phi <4 x i32> [ zeroinitializer, %8 ], [ %50, %26 ]
%30 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 %27
%31 = bitcast i8* %30 to <4 x i8>*
%32 = load <4 x i8>, <4 x i8>* %31
%33 = getelementptr inbounds i8, i8* %30, i64 4
%34 = bitcast i8* %33 to <4 x i8>*
%35 = load <4 x i8>, <4 x i8>* %34
%36 = sext <4 x i8> %32 to <4 x i32>
%37 = sext <4 x i8> %35 to <4 x i32>
%38 = add <4 x i32> %28, %36
%39 = add <4 x i32> %29, %37
%40 = or i64 %27, 8
%41 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 %40
%42 = bitcast i8* %41 to <4 x i8>*
%43 = load <4 x i8>, <4 x i8>* %42
%44 = getelementptr inbounds i8, i8* %41, i64 4
%45 = bitcast i8* %44 to <4 x i8>*
%46 = load <4 x i8>, <4 x i8>* %45
%47 = sext <4 x i8> %43 to <4 x i32>
%48 = sext <4 x i8> %46 to <4 x i32>
%49 = add <4 x i32> %38, %47
%50 = add <4 x i32> %39, %48
%51 = add nuw nsw i64 %27, 16
%52 = icmp eq i64 %51, 512
br i1 %52, label %53, label %26
53: ; preds = %26
%54 = add <4 x i32> %50, %49
%55 = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %54)
%56 = call i32 (i8*, i8*, ...) @sprintf(i8* %24, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %55)
%57 = call i32 @write(i32 %0, i8* %10, i32 512)
ret i32 %57
}