#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int strlen(char *);
int /*<<< orphan*/ ungetch(char);
void ungets(char s[]) {
  int len;
  if ((len = strlen(s)) > 100) {
    printf("string too long to buffer\n");
    exit(EXIT_FAILURE);
  }
  while (len > 0)
    ungetch(s[--len]);
}


x86 assembly (clang, -O0):
<ungets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x64,%eax
jle    117b <ungets+0x3b>
lea    0xe98(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e6c(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
jmp    1180 <ungets+0x40>
cmpl   $0x0,-0xc(%rbp)
jle    11a8 <ungets+0x68>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0xc(%rbp)
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%edi
call   1050 <ungetch@plt>
jmp    1180 <ungets+0x40>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ungets>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <strlen@plt>
cmp    $0x64,%eax
jg     117f <ungets+0x3f>
test   %eax,%eax
jle    1177 <ungets+0x37>
mov    %eax,%ebx
add    $0x1,%rbx
nopl   0x0(%rax,%rax,1)
lea    -0x2(%rbx),%eax
movsbl (%r14,%rax,1),%edi
call   1050 <ungetch@plt>
add    $0xffffffffffffffff,%rbx
cmp    $0x1,%rbx
ja     1160 <ungets+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe7a(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"string too long to buffer\0A\00"
@EXIT_FAILURE = global i32 0
define void @ungets(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = call i32 @strlen(i8* %4)
store i32 %5, i32* %3
%6 = icmp sgt i32 %5, 100
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %1
br label %12
12: ; preds = %15, %11
%13 = load i32, i32* %3
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %12
%16 = load i8*, i8** %2
%17 = load i32, i32* %3
%18 = add nsw i32 %17, -1
store i32 %18, i32* %3
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %16, i64 %19
%21 = load i8, i8* %20
%22 = call i32 @ungetch(i8 signext %21)
br label %12
23: ; preds = %12
ret void
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@str = [26 x i8] c"string too long to buffer\00"
define void @ungets(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp sgt i32 %2, 100
br i1 %3, label %8, label %4
4: ; preds = %1
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %20
6: ; preds = %4
%7 = zext i32 %2 to i64
br label %12
8: ; preds = %1
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %6, %12
%13 = phi i64 [ %7, %6 ], [ %14, %12 ]
%14 = add nsw i64 %13, -1
%15 = and i64 %14, 4294967295
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = load i8, i8* %16
%18 = tail call i32 @ungetch(i8 signext %17)
%19 = icmp ugt i64 %13, 1
br i1 %19, label %12, label %20
20: ; preds = %12, %4
ret void
}