#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*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ sscanf(char const *, char *, int *);
int writeall(int, char *, int);
int decode_and_write(int fd, const char *data, int count) {
  char buffer[4096];
  int d, i, r, x = 0;
  while (count > 0) {
    for (i = 0; count > 0 && i < sizeof(buffer); ++i) {
      if (*data == '%') {
        sscanf(data + 1, "%02x", &d);
        buffer[i] = (char)d;
        data += 3;
        count -= 3;
      } else {
        buffer[i] = *data;
        ++data;
        --count;
      }
    }
    r = writeall(fd, buffer, i);
    if (r <= 0) {
      return r;
    }
    x += r;
  }
  return x;
}


x86 assembly (clang, -O0):
<decode_and_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1030(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1278 <decode_and_write+0x158>
movl   $0x0,-0x1028(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x1031(%rbp)
jle    117b <decode_and_write+0x5b>
movslq -0x1028(%rbp),%rax
cmp    $0x1000,%rax
setb   %al
mov    %al,-0x1031(%rbp)
mov    -0x1031(%rbp),%al
test   $0x1,%al
jne    118e <decode_and_write+0x6e>
jmp    122b <decode_and_write+0x10b>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x25,%eax
jne    11e9 <decode_and_write+0xc9>
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
lea    0xe53(%rip),%rsi
lea    -0x1024(%rbp),%rdx
call   1040 <sscanf@plt>
mov    -0x1024(%rbp),%eax
mov    %al,%cl
movslq -0x1028(%rbp),%rax
mov    %cl,-0x1020(%rbp,%rax,1)
mov    -0x10(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x3,%eax
mov    %eax,-0x14(%rbp)
jmp    1212 <decode_and_write+0xf2>
mov    -0x10(%rbp),%rax
mov    (%rax),%cl
movslq -0x1028(%rbp),%rax
mov    %cl,-0x1020(%rbp,%rax,1)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1217 <decode_and_write+0xf7>
mov    -0x1028(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1028(%rbp)
jmp    1153 <decode_and_write+0x33>
mov    -0x8(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x1028(%rbp),%edx
call   1030 <writeall@plt>
mov    %eax,-0x102c(%rbp)
cmpl   $0x0,-0x102c(%rbp)
jg     1261 <decode_and_write+0x141>
mov    -0x102c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1281 <decode_and_write+0x161>
mov    -0x102c(%rbp),%eax
add    -0x1030(%rbp),%eax
mov    %eax,-0x1030(%rbp)
jmp    113f <decode_and_write+0x1f>
mov    -0x1030(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<decode_and_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %edi,0x8(%rsp)
test   %edx,%edx
jle    11d7 <decode_and_write+0xb7>
mov    %edx,%ebp
mov    %rsi,%rbx
xor    %r15d,%r15d
lea    0xeb4(%rip),%r12
lea    0xc(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movzbl (%rbx),%eax
lea    0x1(%rbx),%rdi
mov    $0xffffffff,%ecx
cmp    $0x25,%al
jne    11a0 <decode_and_write+0x80>
mov    %r12,%rsi
mov    %r13,%rdx
call   1040 <sscanf@plt>
movzbl 0xc(%rsp),%eax
add    $0x3,%rbx
mov    $0xfffffffd,%ecx
jmp    11a3 <decode_and_write+0x83>
nopl   0x0(%rax,%rax,1)
mov    %rdi,%rbx
lea    0x1(%r14),%rdx
mov    %al,0x10(%rsp,%r14,1)
add    %ecx,%ebp
jle    11bc <decode_and_write+0x9c>
cmp    $0xfff,%r14
mov    %rdx,%r14
jb     1170 <decode_and_write+0x50>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rsi
call   1030 <writeall@plt>
test   %eax,%eax
jle    11dc <decode_and_write+0xbc>
add    %eax,%r15d
test   %ebp,%ebp
jg     1160 <decode_and_write+0x40>
jmp    11df <decode_and_write+0xbf>
xor    %r15d,%r15d
jmp    11df <decode_and_write+0xbf>
mov    %eax,%r15d
mov    %r15d,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%02x\00"
define i32 @decode_and_write(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [4096 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %12
br label %13
13: ; preds = %67, %3
%14 = load i32, i32* %7
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %71
16: ; preds = %13
store i32 0, i32* %10
br label %17
17: ; preds = %55, %16
%18 = load i32, i32* %7
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i32, i32* %10
%22 = sext i32 %21 to i64
%23 = icmp ult i64 %22, 4096
br label %24
24: ; preds = %20, %17
%25 = phi i1 [ false, %17 ], [ %23, %20 ]
br i1 %25, label %26, label %58
26: ; preds = %24
%27 = load i8*, i8** %6
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 37
br i1 %30, label %31, label %44
31: ; preds = %26
%32 = load i8*, i8** %6
%33 = getelementptr inbounds i8, i8* %32, i64 1
%34 = call i32 @sscanf(i8* %33, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32* %9)
%35 = load i32, i32* %9
%36 = trunc i32 %35 to i8
%37 = load i32, i32* %10
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 %38
store i8 %36, i8* %39
%40 = load i8*, i8** %6
%41 = getelementptr inbounds i8, i8* %40, i64 3
store i8* %41, i8** %6
%42 = load i32, i32* %7
%43 = sub nsw i32 %42, 3
store i32 %43, i32* %7
br label %54
44: ; preds = %26
%45 = load i8*, i8** %6
%46 = load i8, i8* %45
%47 = load i32, i32* %10
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 %48
store i8 %46, i8* %49
%50 = load i8*, i8** %6
%51 = getelementptr inbounds i8, i8* %50, i32 1
store i8* %51, i8** %6
%52 = load i32, i32* %7
%53 = add nsw i32 %52, -1
store i32 %53, i32* %7
br label %54
54: ; preds = %44, %31
br label %55
55: ; preds = %54
%56 = load i32, i32* %10
%57 = add nsw i32 %56, 1
store i32 %57, i32* %10
br label %17
58: ; preds = %24
%59 = load i32, i32* %5
%60 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%61 = load i32, i32* %10
%62 = call i32 @writeall(i32 %59, i8* %60, i32 %61)
store i32 %62, i32* %11
%63 = load i32, i32* %11
%64 = icmp sle i32 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %58
%66 = load i32, i32* %11
store i32 %66, i32* %4
br label %73
67: ; preds = %58
%68 = load i32, i32* %11
%69 = load i32, i32* %12
%70 = add nsw i32 %69, %68
store i32 %70, i32* %12
br label %13
71: ; preds = %13
%72 = load i32, i32* %12
store i32 %72, i32* %4
br label %73
73: ; preds = %71, %65
%74 = load i32, i32* %4
ret i32 %74
}

LLVM IR (-O2):
@.str = [5 x i8] c"%02x\00"
define i32 @decode_and_write(i32 %0, i8* nocapture %1, i32 %2) {
%4 = alloca [4096 x i8]
%5 = alloca i32
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%7 = bitcast i32* %5 to i8*
%8 = icmp sgt i32 %2, 0
br i1 %8, label %9, label %42
9: ; preds = %3, %39
%10 = phi i32 [ %40, %39 ], [ 0, %3 ]
%11 = phi i32 [ %29, %39 ], [ %2, %3 ]
%12 = phi i8* [ %28, %39 ], [ %1, %3 ]
br label %13
13: ; preds = %9, %25
%14 = phi i64 [ 0, %9 ], [ %31, %25 ]
%15 = phi i32 [ %11, %9 ], [ %29, %25 ]
%16 = phi i8* [ %12, %9 ], [ %28, %25 ]
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 37
%19 = getelementptr inbounds i8, i8* %16, i64 1
br i1 %18, label %20, label %25
20: ; preds = %13
%21 = call i32 @sscanf(i8* %19, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32* %5)
%22 = load i32, i32* %5
%23 = trunc i32 %22 to i8
%24 = getelementptr inbounds i8, i8* %16, i64 3
br label %25
25: ; preds = %13, %20
%26 = phi i32 [ -3, %20 ], [ -1, %13 ]
%27 = phi i8 [ %23, %20 ], [ %17, %13 ]
%28 = phi i8* [ %24, %20 ], [ %19, %13 ]
%29 = add nsw i32 %15, %26
%30 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 %14
store i8 %27, i8* %30
%31 = add nuw nsw i64 %14, 1
%32 = icmp sgt i32 %29, 0
%33 = icmp ult i64 %14, 4095
%34 = select i1 %32, i1 %33, i1 false
br i1 %34, label %13, label %35
35: ; preds = %25
%36 = trunc i64 %31 to i32
%37 = call i32 @writeall(i32 %0, i8* %6, i32 %36)
%38 = icmp slt i32 %37, 1
br i1 %38, label %42, label %39
39: ; preds = %35
%40 = add nsw i32 %37, %10
%41 = icmp sgt i32 %29, 0
br i1 %41, label %9, label %42
42: ; preds = %35, %39, %3
%43 = phi i32 [ 0, %3 ], [ %40, %39 ], [ %37, %35 ]
ret i32 %43
}