#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ errno;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fwrite(char const *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ putc(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ show_error(char *, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ show_msg(char *, int, int, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void write_atr(const char *out, const char *data, int ssec, int nsec) {
  int size = (nsec > 3) ? ssec * (nsec - 3) + 128 * 3 : 128 * nsec;
  show_msg("writing image with %d sectors of %d bytes, total %d bytes.\n", nsec,
           ssec, size);
  FILE *f = fopen(out, "wb");
  if (!f)
    show_error("can't open output file '%s': %s\n", out, strerror(errno));
  putc(0x96, f);
  putc(0x02, f);
  putc(size >> 4, f);
  putc(size >> 12, f);
  putc(ssec, f);
  putc(ssec >> 8, f);
  putc(size >> 20, f);
  putc(0, f);
  putc(0, f);
  putc(0, f);
  putc(0, f);
  putc(0, f);
  putc(0, f);
  putc(0, f);
  putc(0, f);
  putc(0, f);
  for (int i = 0; i < nsec; i++) {
    if (i < 3)
      fwrite(data + ssec * i, 128, 1, f);
    else
      fwrite(data + ssec * i, ssec, 1, f);
  }
  if (0 != fclose(f))
    show_error("can't write output fil '%s': %s\n", out, strerror(errno));
}


x86 assembly (clang, -O0):
<write_atr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
cmpl   $0x3,-0x18(%rbp)
jle    11a9 <write_atr+0x39>
mov    -0x14(%rbp),%eax
mov    -0x18(%rbp),%ecx
sub    $0x3,%ecx
imul   %ecx,%eax
add    $0x180,%eax
mov    %eax,-0x30(%rbp)
jmp    11b2 <write_atr+0x42>
mov    -0x18(%rbp),%eax
shl    $0x7,%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x1c(%rbp),%ecx
lea    0xe38(%rip),%rdi
call   1040 <show_msg@plt>
mov    -0x8(%rbp),%rdi
lea    0xe64(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1214 <write_atr+0xa4>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    0xe30(%rip),%rdi
call   1060 <show_error@plt>
mov    -0x28(%rbp),%rsi
mov    $0x96,%edi
call   1050 <putc@plt>
mov    -0x28(%rbp),%rsi
mov    $0x2,%edi
call   1050 <putc@plt>
mov    -0x1c(%rbp),%edi
sar    $0x4,%edi
mov    -0x28(%rbp),%rsi
call   1050 <putc@plt>
mov    -0x1c(%rbp),%edi
sar    $0xc,%edi
mov    -0x28(%rbp),%rsi
call   1050 <putc@plt>
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1050 <putc@plt>
mov    -0x14(%rbp),%edi
sar    $0x8,%edi
mov    -0x28(%rbp),%rsi
call   1050 <putc@plt>
mov    -0x1c(%rbp),%edi
sar    $0x14,%edi
mov    -0x28(%rbp),%rsi
call   1050 <putc@plt>
mov    -0x28(%rbp),%rsi
xor    %edi,%edi
call   1050 <putc@plt>
mov    -0x28(%rbp),%rsi
xor    %edi,%edi
call   1050 <putc@plt>
mov    -0x28(%rbp),%rsi
xor    %edi,%edi
call   1050 <putc@plt>
mov    -0x28(%rbp),%rsi
xor    %edi,%edi
call   1050 <putc@plt>
mov    -0x28(%rbp),%rsi
xor    %edi,%edi
call   1050 <putc@plt>
mov    -0x28(%rbp),%rsi
xor    %edi,%edi
call   1050 <putc@plt>
mov    -0x28(%rbp),%rsi
xor    %edi,%edi
call   1050 <putc@plt>
mov    -0x28(%rbp),%rsi
xor    %edi,%edi
call   1050 <putc@plt>
mov    -0x28(%rbp),%rsi
xor    %edi,%edi
call   1050 <putc@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1354 <write_atr+0x1e4>
cmpl   $0x3,-0x2c(%rbp)
jge    1320 <write_atr+0x1b0>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%eax
imul   -0x2c(%rbp),%eax
cltq
add    %rax,%rdi
mov    -0x28(%rbp),%rcx
mov    $0x80,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
jmp    1341 <write_atr+0x1d1>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%eax
imul   -0x2c(%rbp),%eax
cltq
add    %rax,%rdi
mov    -0x14(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1080 <fwrite@plt>
jmp    1346 <write_atr+0x1d6>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    12e2 <write_atr+0x172>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     1393 <write_atr+0x223>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2c66(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
lea    0xcd2(%rip),%rdi
call   1060 <show_error@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_atr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r12d
mov    %edx,%r15d
mov    %rsi,%r13
mov    %rdi,%r14
lea    -0x3(%r12),%eax
imul   %edx,%eax
add    $0x180,%eax
mov    %ecx,%ebx
shl    $0x7,%ebx
cmp    $0x4,%ecx
cmovge %eax,%ebx
lea    0xe5a(%rip),%rdi
mov    %ecx,%esi
mov    %ebx,%ecx
call   1040 <show_msg@plt>
lea    0xe86(%rip),%rsi
mov    %r14,%rdi
call   1070 <fopen@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11e5 <write_atr+0x75>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe64(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1060 <show_error@plt>
mov    %r14,(%rsp)
mov    $0x96,%edi
mov    %rbp,%rsi
call   1050 <putc@plt>
mov    $0x2,%edi
mov    %rbp,%rsi
call   1050 <putc@plt>
mov    %ebx,%edi
sar    $0x4,%edi
mov    %rbp,%rsi
call   1050 <putc@plt>
mov    %ebx,%edi
sar    $0xc,%edi
mov    %rbp,%rsi
call   1050 <putc@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
call   1050 <putc@plt>
mov    %r15d,%edi
sar    $0x8,%edi
mov    %rbp,%rsi
call   1050 <putc@plt>
sar    $0x14,%ebx
mov    %ebx,%edi
mov    %rbp,%rsi
call   1050 <putc@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   1050 <putc@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   1050 <putc@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   1050 <putc@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   1050 <putc@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   1050 <putc@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   1050 <putc@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   1050 <putc@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   1050 <putc@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   1050 <putc@plt>
test   %r12d,%r12d
jle    133c <write_atr+0x1cc>
mov    %r13,%rdi
mov    $0x80,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1080 <fwrite@plt>
cmp    $0x1,%r12d
je     133c <write_atr+0x1cc>
movslq %r15d,%r14
lea    (%r14,%r13,1),%rdi
mov    $0x80,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1080 <fwrite@plt>
cmp    $0x2,%r12d
je     133c <write_atr+0x1cc>
lea    0x0(,%r14,2),%rdi
add    %r13,%rdi
mov    $0x80,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1080 <fwrite@plt>
cmp    $0x3,%r12d
je     133c <write_atr+0x1cc>
mov    %r12d,%ebx
lea    (%r14,%r14,2),%rax
add    %rax,%r13
add    $0xfffffffffffffffd,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %r15d,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1080 <fwrite@plt>
add    %r14,%r13
add    $0xffffffffffffffff,%rbx
jne    1320 <write_atr+0x1b0>
mov    %rbp,%rdi
call   1030 <fclose@plt>
test   %rax,%rax
je     1377 <write_atr+0x207>
mov    0x2c90(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd02(%rip),%rdi
mov    (%rsp),%rsi
mov    %eax,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <show_error@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [60 x i8] c"writing image with %d sectors of %d bytes, total %d bytes.\0A\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [33 x i8] c"can't open output file '%s': %s\0A\00"
@errno = global i32 0
@.str.3 = [33 x i8] c"can't write output fil '%s': %s\0A\00"
define void @write_atr(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%12 = load i32, i32* %8
%13 = icmp sgt i32 %12, 3
br i1 %13, label %14, label %20
14: ; preds = %4
%15 = load i32, i32* %7
%16 = load i32, i32* %8
%17 = sub nsw i32 %16, 3
%18 = mul nsw i32 %15, %17
%19 = add nsw i32 %18, 384
br label %23
20: ; preds = %4
%21 = load i32, i32* %8
%22 = mul nsw i32 128, %21
br label %23
23: ; preds = %20, %14
%24 = phi i32 [ %19, %14 ], [ %22, %20 ]
store i32 %24, i32* %9
%25 = load i32, i32* %8
%26 = load i32, i32* %7
%27 = load i32, i32* %9
%28 = call i32 @show_msg(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i32 %25, i32 %26, i32 %27)
%29 = load i8*, i8** %5
%30 = call i32* @fopen(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %30, i32** %10
%31 = load i32*, i32** %10
%32 = icmp ne i32* %31, null
br i1 %32, label %38, label %33
33: ; preds = %23
%34 = load i8*, i8** %5
%35 = load i32, i32* @errno
%36 = call i32 @strerror(i32 %35)
%37 = call i32 @show_error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %34, i32 %36)
br label %38
38: ; preds = %33, %23
%39 = load i32*, i32** %10
%40 = call i32 @putc(i32 150, i32* %39)
%41 = load i32*, i32** %10
%42 = call i32 @putc(i32 2, i32* %41)
%43 = load i32, i32* %9
%44 = ashr i32 %43, 4
%45 = load i32*, i32** %10
%46 = call i32 @putc(i32 %44, i32* %45)
%47 = load i32, i32* %9
%48 = ashr i32 %47, 12
%49 = load i32*, i32** %10
%50 = call i32 @putc(i32 %48, i32* %49)
%51 = load i32, i32* %7
%52 = load i32*, i32** %10
%53 = call i32 @putc(i32 %51, i32* %52)
%54 = load i32, i32* %7
%55 = ashr i32 %54, 8
%56 = load i32*, i32** %10
%57 = call i32 @putc(i32 %55, i32* %56)
%58 = load i32, i32* %9
%59 = ashr i32 %58, 20
%60 = load i32*, i32** %10
%61 = call i32 @putc(i32 %59, i32* %60)
%62 = load i32*, i32** %10
%63 = call i32 @putc(i32 0, i32* %62)
%64 = load i32*, i32** %10
%65 = call i32 @putc(i32 0, i32* %64)
%66 = load i32*, i32** %10
%67 = call i32 @putc(i32 0, i32* %66)
%68 = load i32*, i32** %10
%69 = call i32 @putc(i32 0, i32* %68)
%70 = load i32*, i32** %10
%71 = call i32 @putc(i32 0, i32* %70)
%72 = load i32*, i32** %10
%73 = call i32 @putc(i32 0, i32* %72)
%74 = load i32*, i32** %10
%75 = call i32 @putc(i32 0, i32* %74)
%76 = load i32*, i32** %10
%77 = call i32 @putc(i32 0, i32* %76)
%78 = load i32*, i32** %10
%79 = call i32 @putc(i32 0, i32* %78)
store i32 0, i32* %11
br label %80
80: ; preds = %107, %38
%81 = load i32, i32* %11
%82 = load i32, i32* %8
%83 = icmp slt i32 %81, %82
br i1 %83, label %84, label %110
84: ; preds = %80
%85 = load i32, i32* %11
%86 = icmp slt i32 %85, 3
br i1 %86, label %87, label %96
87: ; preds = %84
%88 = load i8*, i8** %6
%89 = load i32, i32* %7
%90 = load i32, i32* %11
%91 = mul nsw i32 %89, %90
%92 = sext i32 %91 to i64
%93 = getelementptr inbounds i8, i8* %88, i64 %92
%94 = load i32*, i32** %10
%95 = call i32 @fwrite(i8* %93, i32 128, i32 1, i32* %94)
br label %106
96: ; preds = %84
%97 = load i8*, i8** %6
%98 = load i32, i32* %7
%99 = load i32, i32* %11
%100 = mul nsw i32 %98, %99
%101 = sext i32 %100 to i64
%102 = getelementptr inbounds i8, i8* %97, i64 %101
%103 = load i32, i32* %7
%104 = load i32*, i32** %10
%105 = call i32 @fwrite(i8* %102, i32 %103, i32 1, i32* %104)
br label %106
106: ; preds = %96, %87
br label %107
107: ; preds = %106
%108 = load i32, i32* %11
%109 = add nsw i32 %108, 1
store i32 %109, i32* %11
br label %80
110: ; preds = %80
%111 = load i32*, i32** %10
%112 = call i64 @fclose(i32* %111)
%113 = icmp ne i64 0, %112
br i1 %113, label %114, label %119
114: ; preds = %110
%115 = load i8*, i8** %5
%116 = load i32, i32* @errno
%117 = call i32 @strerror(i32 %116)
%118 = call i32 @show_error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %115, i32 %117)
br label %119
119: ; preds = %114, %110
ret void
}

LLVM IR (-O2):
@.str = [60 x i8] c"writing image with %d sectors of %d bytes, total %d bytes.\0A\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [33 x i8] c"can't open output file '%s': %s\0A\00"
@errno = global i32 0
@.str.3 = [33 x i8] c"can't write output fil '%s': %s\0A\00"
define void @write_atr(i8* %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = icmp sgt i32 %3, 3
%6 = add nsw i32 %3, -3
%7 = mul nsw i32 %6, %2
%8 = add nsw i32 %7, 384
%9 = shl nsw i32 %3, 7
%10 = select i1 %5, i32 %8, i32 %9
%11 = tail call i32 @show_msg(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %2, i32 %10)
%12 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %4
%15 = load i32, i32* @errno
%16 = tail call i32 @strerror(i32 %15)
%17 = tail call i32 @show_error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %16)
br label %18
18: ; preds = %14, %4
%19 = tail call i32 @putc(i32 150, i32* %12)
%20 = tail call i32 @putc(i32 2, i32* %12)
%21 = ashr i32 %10, 4
%22 = tail call i32 @putc(i32 %21, i32* %12)
%23 = ashr i32 %10, 12
%24 = tail call i32 @putc(i32 %23, i32* %12)
%25 = tail call i32 @putc(i32 %2, i32* %12)
%26 = ashr i32 %2, 8
%27 = tail call i32 @putc(i32 %26, i32* %12)
%28 = ashr i32 %10, 20
%29 = tail call i32 @putc(i32 %28, i32* %12)
%30 = tail call i32 @putc(i32 0, i32* %12)
%31 = tail call i32 @putc(i32 0, i32* %12)
%32 = tail call i32 @putc(i32 0, i32* %12)
%33 = tail call i32 @putc(i32 0, i32* %12)
%34 = tail call i32 @putc(i32 0, i32* %12)
%35 = tail call i32 @putc(i32 0, i32* %12)
%36 = tail call i32 @putc(i32 0, i32* %12)
%37 = tail call i32 @putc(i32 0, i32* %12)
%38 = tail call i32 @putc(i32 0, i32* %12)
%39 = icmp sgt i32 %3, 0
br i1 %39, label %40, label %54
40: ; preds = %18
%41 = sext i32 %2 to i64
%42 = zext i32 %3 to i64
%43 = tail call i32 @fwrite(i8* %1, i32 128, i32 1, i32* %12)
%44 = icmp eq i32 %3, 1
br i1 %44, label %54, label %45
45: ; preds = %40
%46 = getelementptr inbounds i8, i8* %1, i64 %41
%47 = tail call i32 @fwrite(i8* %46, i32 128, i32 1, i32* %12)
%48 = icmp eq i32 %3, 2
br i1 %48, label %54, label %49
49: ; preds = %45
%50 = shl nsw i64 %41, 1
%51 = getelementptr inbounds i8, i8* %1, i64 %50
%52 = tail call i32 @fwrite(i8* %51, i32 128, i32 1, i32* %12)
%53 = icmp eq i32 %3, 3
br i1 %53, label %54, label %57
54: ; preds = %57, %40, %45, %49, %18
%55 = tail call i64 @fclose(i32* %12)
%56 = icmp eq i64 %55, 0
br i1 %56, label %68, label %64
57: ; preds = %49, %57
%58 = phi i64 [ %62, %57 ], [ 3, %49 ]
%59 = mul nsw i64 %58, %41
%60 = getelementptr inbounds i8, i8* %1, i64 %59
%61 = tail call i32 @fwrite(i8* %60, i32 %2, i32 1, i32* %12)
%62 = add nuw nsw i64 %58, 1
%63 = icmp eq i64 %62, %42
br i1 %63, label %54, label %57
64: ; preds = %54
%65 = load i32, i32* @errno
%66 = tail call i32 @strerror(i32 %65)
%67 = tail call i32 @show_error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %0, i32 %66)
br label %68
68: ; preds = %64, %54
ret void
}