#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*/ ungetc(char, int /*<<< orphan*/ *);
void restore(FILE *fp, char *str, int size) {
  int i;
  char *cp;
  ungetc(' ', fp);
  cp = &str[size - 1];
  for (i = 0; i < size; i++, cp--)
    ungetc(*cp, fp);
}


x86 assembly (clang, -O0):
<restore>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rsi
mov    $0x20,%edi
call   1030 <ungetc@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1184 <restore+0x74>
mov    -0x20(%rbp),%rax
mov    (%rax),%al
mov    -0x8(%rbp),%rsi
movsbl %al,%edi
call   1030 <ungetc@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    114c <restore+0x3c>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<restore>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edx,%r12d
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x20,%edi
mov    %r15,%rsi
call   1030 <ungetc@plt>
test   %r12d,%r12d
jle    1156 <restore+0x46>
lea    -0x1(%r12),%eax
add    %rax,%r14
neg    %r12d
xor    %ebx,%ebx
movsbl (%r14,%rbx,1),%edi
mov    %r15,%rsi
call   1030 <ungetc@plt>
add    $0xffffffffffffffff,%rbx
cmp    %ebx,%r12d
jne    1140 <restore+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @restore(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i32* %0, i32** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i32*, i32** %4
%10 = call i32 @ungetc(i8 signext 32, i32* %9)
%11 = load i8*, i8** %5
%12 = load i32, i32* %6
%13 = sub nsw i32 %12, 1
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %11, i64 %14
store i8* %15, i8** %8
store i32 0, i32* %7
br label %16
16: ; preds = %25, %3
%17 = load i32, i32* %7
%18 = load i32, i32* %6
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %30
20: ; preds = %16
%21 = load i8*, i8** %8
%22 = load i8, i8* %21
%23 = load i32*, i32** %4
%24 = call i32 @ungetc(i8 signext %22, i32* %23)
br label %25
25: ; preds = %20
%26 = load i32, i32* %7
%27 = add nsw i32 %26, 1
store i32 %27, i32* %7
%28 = load i8*, i8** %8
%29 = getelementptr inbounds i8, i8* %28, i32 -1
store i8* %29, i8** %8
br label %16
30: ; preds = %16
ret void
}

LLVM IR (-O2):
define void @restore(i32* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32 @ungetc(i8 signext 32, i32* %0)
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %18
6: ; preds = %3
%7 = add nsw i32 %2, -1
%8 = zext i32 %7 to i64
%9 = getelementptr inbounds i8, i8* %1, i64 %8
br label %10
10: ; preds = %6, %10
%11 = phi i8* [ %16, %10 ], [ %9, %6 ]
%12 = phi i32 [ %15, %10 ], [ 0, %6 ]
%13 = load i8, i8* %11
%14 = tail call i32 @ungetc(i8 signext %13, i32* %0)
%15 = add nuw nsw i32 %12, 1
%16 = getelementptr inbounds i8, i8* %11, i64 -1
%17 = icmp eq i32 %15, %2
br i1 %17, label %18, label %10
18: ; preds = %10, %3
ret void
}