#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 */
size_t strlen(char const *);
int /*<<< orphan*/ ungetc(char const, int /*<<< orphan*/ *);
const char *ungets(const char *s, FILE *stream) {
  if (!s)
    return NULL;
  for (size_t i = strlen(s); i > 0; --i) {
    ungetc(s[i - 1], stream);
  }
  return s;
}


x86 assembly (clang, -O0):
<ungets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1148 <ungets+0x28>
movq   $0x0,-0x8(%rbp)
jmp    1194 <ungets+0x74>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    118c <ungets+0x6c>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
mov    (%rax,%rcx,1),%al
mov    -0x18(%rbp),%rsi
movsbl %al,%edi
call   1040 <ungetc@plt>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    1155 <ungets+0x35>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ungets>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
test   %rdi,%rdi
je     1154 <ungets+0x34>
mov    %rsi,%r14
mov    %r15,%rdi
call   1030 <strlen@plt>
test   %rax,%rax
je     1154 <ungets+0x34>
mov    %rax,%rbx
movsbl -0x1(%r15,%rbx,1),%edi
mov    %r14,%rsi
call   1040 <ungetc@plt>
add    $0xffffffffffffffff,%rbx
jne    1140 <ungets+0x20>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @ungets(i8* %0, i32* nocapture %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %14, label %4
4: ; preds = %2
%5 = tail call i64 @strlen(i8* %0)
%6 = icmp eq i64 %5, 0
br i1 %6, label %14, label %7
7: ; preds = %4, %7
%8 = phi i64 [ %9, %7 ], [ %5, %4 ]
%9 = add i64 %8, -1
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = load i8, i8* %10
%12 = tail call i32 @ungetc(i8 signext %11, i32* %1)
%13 = icmp eq i64 %9, 0
br i1 %13, label %14, label %7
14: ; preds = %7, %4, %2
ret i8* %0
}