#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*/ uint8_t;
typedef int uint32_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ *ber_fprintf(int /*<<< orphan*/ *, char *, int, int, char *);
int /*<<< orphan*/ *ber_sscanf(int /*<<< orphan*/ *, char *, int *, int *,
                               char **);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ hexdump(char *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ printf(char *);
scalar_t__ strcmp(char *, char *);
void ber_fprintf_test(uint8_t *buf, uint8_t *buf_end) {
  uint8_t *enc_out, *dec_out;
  uint32_t num1, num2;
  char *str = NULL;
  printf("# Testing fprintf syntax-like coding.\n");
  printf("ber_fprintf(\"%%u%%u%%s\", 64, 103, \"testing_strings_123\")");
  enc_out = ber_fprintf(buf_end, "%u%u%s", 64, 103, "testing_strings_123");
  hexdump("", enc_out + 1, buf_end - enc_out);
  dec_out = ber_sscanf(enc_out, "%u%u%ms", &num1, &num2, &str);
  assert(dec_out == buf_end + 1);
  assert(num1 == 64);
  assert(num2 == 103);
  assert(strcmp("testing_strings_123", str) <= 0);
  free(str);
  printf("\n");
}


x86 assembly (clang, -O0):
<ber_fprintf_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x30(%rbp)
lea    0xe71(%rip),%rdi
call   1040 <printf@plt>
lea    0xe8c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    0xeb5(%rip),%rsi
mov    $0x40,%edx
mov    $0x67,%ecx
lea    0xeab(%rip),%r8
call   1090 <ber_fprintf@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
add    $0x4,%rsi
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
sar    $0x2,%rax
mov    %eax,%edx
lea    0xe41(%rip),%rdi
call   1080 <hexdump@plt>
mov    -0x18(%rbp),%rdi
lea    0xe86(%rip),%rsi
lea    -0x24(%rbp),%rdx
lea    -0x28(%rbp),%rcx
lea    -0x30(%rbp),%r8
call   1070 <ber_sscanf@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
add    $0x4,%rcx
cmp    %rcx,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
cmpl   $0x40,-0x24(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
cmpl   $0x67,-0x28(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x30(%rbp),%rsi
lea    0xe14(%rip),%rdi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
setle  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
lea    0xdac(%rip),%rdi
call   1040 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ber_fprintf_test>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
movq   $0x0,(%rsp)
lea    0xeb1(%rip),%rdi
call   1050 <puts@plt>
lea    0xe49(%rip),%rdi
call   1060 <printf@plt>
lea    0xe76(%rip),%rsi
lea    0xe76(%rip),%r14
mov    %r15,%rdi
mov    $0x40,%edx
mov    $0x67,%ecx
mov    %r14,%r8
call   10b0 <ber_fprintf@plt>
mov    %rax,%rbx
lea    0x4(%rax),%rsi
mov    %r15,%rdx
sub    %rax,%rdx
shr    $0x2,%rdx
lea    0xe41(%rip),%rdi
call   10a0 <hexdump@plt>
lea    0xe51(%rip),%rsi
lea    0xc(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %rsp,%r8
mov    %rbx,%rdi
call   1090 <ber_sscanf@plt>
add    $0x4,%r15
xor    %edi,%edi
cmp    %r15,%rax
sete   %dil
call   1080 <assert@plt>
xor    %edi,%edi
cmpl   $0x40,0xc(%rsp)
sete   %dil
call   1080 <assert@plt>
xor    %edi,%edi
cmpl   $0x67,0x8(%rsp)
sete   %dil
call   1080 <assert@plt>
mov    (%rsp),%rsi
mov    %r14,%rdi
call   1070 <strcmp@plt>
xor    %edi,%edi
test   %rax,%rax
setle  %dil
call   1080 <assert@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [39 x i8] c"# Testing fprintf syntax-like coding.\0A\00"
@.str.1 = [57 x i8] c"ber_fprintf(\22%%u%%u%%s\22, 64, 103, \22testing_strings_123\22)\00"
@.str.2 = [7 x i8] c"%u%u%s\00"
@.str.3 = [20 x i8] c"testing_strings_123\00"
@.str.4 = [1 x i8] zeroinitializer
@.str.5 = [8 x i8] c"%u%u%ms\00"
@.str.6 = [2 x i8] c"\0A\00"
define void @ber_fprintf_test(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i32* %0, i32** %3
store i32* %1, i32** %4
store i8* null, i8** %9
%10 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32*, i32** %4
%13 = call i32* @ber_fprintf(i32* %12, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 64, i32 103, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
store i32* %13, i32** %5
%14 = load i32*, i32** %5
%15 = getelementptr inbounds i32, i32* %14, i64 1
%16 = load i32*, i32** %4
%17 = load i32*, i32** %5
%18 = ptrtoint i32* %16 to i64
%19 = ptrtoint i32* %17 to i64
%20 = sub i64 %18, %19
%21 = sdiv exact i64 %20, 4
%22 = trunc i64 %21 to i32
%23 = call i32 @hexdump(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0), i32* %15, i32 %22)
%24 = load i32*, i32** %5
%25 = call i32* @ber_sscanf(i32* %24, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32* %7, i32* %8, i8** %9)
store i32* %25, i32** %6
%26 = load i32*, i32** %6
%27 = load i32*, i32** %4
%28 = getelementptr inbounds i32, i32* %27, i64 1
%29 = icmp eq i32* %26, %28
%30 = zext i1 %29 to i32
%31 = call i32 @assert(i32 %30)
%32 = load i32, i32* %7
%33 = icmp eq i32 %32, 64
%34 = zext i1 %33 to i32
%35 = call i32 @assert(i32 %34)
%36 = load i32, i32* %8
%37 = icmp eq i32 %36, 103
%38 = zext i1 %37 to i32
%39 = call i32 @assert(i32 %38)
%40 = load i8*, i8** %9
%41 = call i64 @strcmp(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %40)
%42 = icmp sle i64 %41, 0
%43 = zext i1 %42 to i32
%44 = call i32 @assert(i32 %43)
%45 = load i8*, i8** %9
%46 = call i32 @free(i8* %45)
%47 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str.1 = [57 x i8] c"ber_fprintf(\22%%u%%u%%s\22, 64, 103, \22testing_strings_123\22)\00"
@.str.2 = [7 x i8] c"%u%u%s\00"
@.str.3 = [20 x i8] c"testing_strings_123\00"
@.str.4 = [1 x i8] zeroinitializer
@.str.5 = [8 x i8] c"%u%u%ms\00"
@str = [38 x i8] c"# Testing fprintf syntax-like coding.\00"
define void @ber_fprintf_test(i32* nocapture readnone %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = bitcast i8** %5 to i8*
store i8* null, i8** %5
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
%10 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32* @ber_fprintf(i32* %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 64, i32 103, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%12 = getelementptr inbounds i32, i32* %11, i64 1
%13 = ptrtoint i32* %1 to i64
%14 = ptrtoint i32* %11 to i64
%15 = sub i64 %13, %14
%16 = lshr exact i64 %15, 2
%17 = trunc i64 %16 to i32
%18 = tail call i32 @hexdump(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0), i32* %12, i32 %17)
%19 = call i32* @ber_sscanf(i32* %11, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32* %3, i32* %4, i8** %5)
%20 = getelementptr inbounds i32, i32* %1, i64 1
%21 = icmp eq i32* %19, %20
%22 = zext i1 %21 to i32
%23 = call i32 @assert(i32 %22)
%24 = load i32, i32* %3
%25 = icmp eq i32 %24, 64
%26 = zext i1 %25 to i32
%27 = call i32 @assert(i32 %26)
%28 = load i32, i32* %4
%29 = icmp eq i32 %28, 103
%30 = zext i1 %29 to i32
%31 = call i32 @assert(i32 %30)
%32 = load i8*, i8** %5
%33 = call i64 @strcmp(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %32)
%34 = icmp slt i64 %33, 1
%35 = zext i1 %34 to i32
%36 = call i32 @assert(i32 %35)
%37 = load i8*, i8** %5
%38 = call i32 @free(i8* %37)
%39 = call i32 @putchar(i32 10)
ret void
}