#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*/ free(char *);
int /*<<< orphan*/ printf(char *);
char *safe_malloc(int);
int take_coin(int *, char *);
int /*<<< orphan*/ take_coin_input(char *);
int accept_payment(int coin_buffer[8], int required) {
  int payment = 0;
  while (payment < required) {
    int coinval;
    char *coin_input = safe_malloc(sizeof(*coin_input) * 5);
    take_coin_input(coin_input);
    coinval = take_coin(coin_buffer, coin_input);
    free(coin_input);
    if (coinval == 0) {
      printf("Invalid coin denomination.\n");
    } else if (coinval == -1) {
      return -1;
    } else {
      payment += coinval;
    }
  }
  return payment - required;
}


x86 assembly (clang, -O0):
<accept_payment>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11e6 <accept_payment+0x96>
mov    $0x5,%edi
call   1040 <safe_malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <take_coin_input@plt>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1070 <take_coin@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    11bd <accept_payment+0x6d>
lea    0xe4d(%rip),%rdi
call   1050 <printf@plt>
jmp    11e1 <accept_payment+0x91>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11d3 <accept_payment+0x83>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ef <accept_payment+0x9f>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11e1 <accept_payment+0x91>
jmp    1166 <accept_payment+0x16>
mov    -0x18(%rbp),%eax
sub    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<accept_payment>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r12d
test   %esi,%esi
jle    11cd <accept_payment+0x7d>
mov    %rdi,%r14
xor    %r13d,%r13d
lea    0xe91(%rip),%r15
jmp    118d <accept_payment+0x3d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1050 <puts@plt>
cmp    %r12d,%r13d
jge    11c8 <accept_payment+0x78>
mov    $0x5,%edi
call   1040 <safe_malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <take_coin_input@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1070 <take_coin@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
test   %ebp,%ebp
je     1180 <accept_payment+0x30>
cmp    $0xffffffff,%ebp
je     11d5 <accept_payment+0x85>
add    %ebp,%r13d
cmp    %r12d,%r13d
jl     118d <accept_payment+0x3d>
sub    %r12d,%r13d
jmp    11d8 <accept_payment+0x88>
xor    %r13d,%r13d
sub    %r12d,%r13d
jmp    11d8 <accept_payment+0x88>
mov    %ebp,%r13d
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"Invalid coin denomination.\0A\00"
define i32 @accept_payment(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 0, i32* %6
br label %9
9: ; preds = %35, %2
%10 = load i32, i32* %6
%11 = load i32, i32* %5
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %36
13: ; preds = %9
%14 = call i8* @safe_malloc(i32 5)
store i8* %14, i8** %8
%15 = load i8*, i8** %8
%16 = call i32 @take_coin_input(i8* %15)
%17 = load i32*, i32** %4
%18 = load i8*, i8** %8
%19 = call i32 @take_coin(i32* %17, i8* %18)
store i32 %19, i32* %7
%20 = load i8*, i8** %8
%21 = call i32 @free(i8* %20)
%22 = load i32, i32* %7
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %13
%25 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %35
26: ; preds = %13
%27 = load i32, i32* %7
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %30
29: ; preds = %26
store i32 -1, i32* %3
br label %40
30: ; preds = %26
%31 = load i32, i32* %7
%32 = load i32, i32* %6
%33 = add nsw i32 %32, %31
store i32 %33, i32* %6
br label %34
34: ; preds = %30
br label %35
35: ; preds = %34, %24
br label %9
36: ; preds = %9
%37 = load i32, i32* %6
%38 = load i32, i32* %5
%39 = sub nsw i32 %37, %38
store i32 %39, i32* %3
br label %40
40: ; preds = %36, %29
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@str = [27 x i8] c"Invalid coin denomination.\00"
define i32 @accept_payment(i32* %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %17
4: ; preds = %2, %14
%5 = phi i32 [ %15, %14 ], [ 0, %2 ]
%6 = tail call i8* @safe_malloc(i32 5)
%7 = tail call i32 @take_coin_input(i8* %6)
%8 = tail call i32 @take_coin(i32* %0, i8* %6)
%9 = tail call i32 @free(i8* %6)
switch i32 %8, label %12 [
i32 0, label %10
i32 -1, label %20
]
10: ; preds = %4
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
br label %14
12: ; preds = %4
%13 = add nsw i32 %8, %5
br label %14
14: ; preds = %10, %12
%15 = phi i32 [ %5, %10 ], [ %13, %12 ]
%16 = icmp slt i32 %15, %1
br i1 %16, label %4, label %17
17: ; preds = %14, %2
%18 = phi i32 [ 0, %2 ], [ %15, %14 ]
%19 = sub nsw i32 %18, %1
br label %20
20: ; preds = %4, %17
%21 = phi i32 [ %19, %17 ], [ %8, %4 ]
ret i32 %21
}