#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*/ sprintf(char *, char *, char const *);
int to_int(unsigned char const *, int const);
const unsigned char *coupon_val(const unsigned char *data, const int data_len,
                                const unsigned char *d, const char *name,
                                const int val_len, int *p_val, int *p_err_no,
                                int *p_err_posn, char err_msg[50]) {
  int val;
  if (d - data + val_len > data_len) {
    *p_err_no = 3;
    *p_err_posn = d - data + 1;
    sprintf(err_msg, "%s incomplete", name);
    return NULL;
  }
  val = to_int(d, val_len);
  if (val < 0) {
    *p_err_no = 3;
    *p_err_posn = d - data + 1;
    sprintf(err_msg, "Non-numeric %s", name);
    return NULL;
  }
  d += val_len;
  if (p_val) {
    *p_val = val;
  }
  return d;
}


x86 assembly (clang, -O0):
<coupon_val>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x20(%rbp),%rax
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
mov    %r9,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
movslq -0x2c(%rbp),%rcx
add    %rcx,%rax
movslq -0x14(%rbp),%rcx
cmp    %rcx,%rax
jle    11ac <coupon_val+0x8c>
mov    0x10(%rbp),%rax
movl   $0x3,(%rax)
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,%ecx
mov    0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    0x20(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xe66(%rip),%rsi
call   1040 <sprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1234 <coupon_val+0x114>
mov    -0x20(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1030 <to_int@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1207 <coupon_val+0xe7>
mov    0x10(%rbp),%rax
movl   $0x3,(%rax)
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,%ecx
mov    0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    0x20(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xe19(%rip),%rsi
call   1040 <sprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1234 <coupon_val+0x114>
mov    -0x2c(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     122c <coupon_val+0x10c>
mov    -0x3c(%rbp),%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<coupon_val>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9,%r12
mov    %rcx,%r14
mov    %rdx,%rbx
mov    0x50(%rsp),%r9
mov    0x48(%rsp),%rdx
mov    0x40(%rsp),%r15
mov    %rbx,%rbp
sub    %rdi,%rbp
movslq %r8d,%r13
mov    %rbp,%rax
add    %r13,%rax
movslq %esi,%rcx
cmp    %rcx,%rax
jle    1172 <coupon_val+0x52>
movl   $0x3,(%r15)
add    $0x1,%ebp
mov    %ebp,(%rdx)
lea    0xe93(%rip),%rsi
mov    %r9,%rdi
jmp    11ac <coupon_val+0x8c>
mov    %rbx,%rdi
mov    %r8d,%esi
call   1030 <to_int@plt>
test   %eax,%eax
js     118f <coupon_val+0x6f>
add    %r13,%rbx
test   %r12,%r12
je     11b6 <coupon_val+0x96>
mov    %eax,(%r12)
jmp    11b6 <coupon_val+0x96>
movl   $0x3,(%r15)
add    $0x1,%ebp
mov    0x48(%rsp),%rax
mov    %ebp,(%rax)
lea    0xe67(%rip),%rsi
mov    0x50(%rsp),%rdi
mov    %r14,%rdx
call   1040 <sprintf@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [14 x i8] c"%s incomplete\00"
@.str.1 = [15 x i8] c"Non-numeric %s\00"
define i8* @coupon_val(i8* %0, i32 %1, i8* %2, i8* %3, i32 %4, i32* writeonly %5, i32* nocapture writeonly %6, i32* nocapture writeonly %7, i8* nocapture writeonly %8) {
%10 = ptrtoint i8* %2 to i64
%11 = ptrtoint i8* %0 to i64
%12 = sub i64 %10, %11
%13 = sext i32 %4 to i64
%14 = add nsw i64 %12, %13
%15 = sext i32 %1 to i64
%16 = icmp sgt i64 %14, %15
br i1 %16, label %17, label %21
17: ; preds = %9
store i32 3, i32* %6
%18 = trunc i64 %12 to i32
%19 = add i32 %18, 1
store i32 %19, i32* %7
%20 = tail call i32 @sprintf(i8* dereferenceable(1) %8, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %3)
br label %32
21: ; preds = %9
%22 = tail call i32 @to_int(i8* %2, i32 %4)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %21
store i32 3, i32* %6
%25 = trunc i64 %12 to i32
%26 = add i32 %25, 1
store i32 %26, i32* %7
%27 = tail call i32 @sprintf(i8* dereferenceable(1) %8, i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %3)
br label %32
28: ; preds = %21
%29 = getelementptr inbounds i8, i8* %2, i64 %13
%30 = icmp eq i32* %5, null
br i1 %30, label %32, label %31
31: ; preds = %28
store i32 %22, i32* %5
br label %32
32: ; preds = %28, %31, %24, %17
%33 = phi i8* [ null, %17 ], [ null, %24 ], [ %29, %31 ], [ %29, %28 ]
ret i8* %33
}