#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 EOF;
scalar_t__ ferror(int /*<<< orphan*/);
int getchar();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stdin;
int encode_hex(void) {
  int val;
  for (val = getchar(); val >= 0 && val <= 0xff; val = getchar())
    printf("%02x", val);
  return val != EOF || ferror(stdin);
}


x86 assembly (clang, -O0):
<encode_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1050 <getchar@plt>
mov    %eax,-0x4(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x4(%rbp)
mov    %al,-0x5(%rbp)
jl     115e <encode_hex+0x2e>
cmpl   $0xff,-0x4(%rbp)
setle  %al
mov    %al,-0x5(%rbp)
mov    -0x5(%rbp),%al
test   $0x1,%al
jne    116e <encode_hex+0x3e>
jmp    118c <encode_hex+0x5c>
mov    -0x4(%rbp),%esi
lea    0xe88(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <getchar@plt>
mov    %eax,-0x4(%rbp)
jmp    1142 <encode_hex+0x12>
mov    -0x4(%rbp),%ecx
mov    $0x1,%al
mov    0x2e48(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x6(%rbp)
jne    11bb <encode_hex+0x8b>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
call   1030 <ferror@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x6(%rbp)
mov    -0x6(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encode_hex>:
push   %rbx
xor    %eax,%eax
call   1050 <getchar@plt>
cmp    $0xff,%eax
ja     1168 <encode_hex+0x38>
lea    0xeba(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <getchar@plt>
cmp    $0x100,%eax
jb     1150 <encode_hex+0x20>
mov    0x2e71(%rip),%rdx
mov    $0x1,%ecx
cmp    (%rdx),%eax
jne    118e <encode_hex+0x5e>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
call   1030 <ferror@plt>
xor    %ecx,%ecx
test   %rax,%rax
setne  %cl
mov    %ecx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%02x\00"
@EOF = global i32 0
@stdin = global i32 0
define i32 @encode_hex() {
%1 = alloca i32
%2 = call i32 (...) @getchar()
store i32 %2, i32* %1
br label %3
3: ; preds = %14, %0
%4 = load i32, i32* %1
%5 = icmp sge i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = load i32, i32* %1
%8 = icmp sle i32 %7, 255
br label %9
9: ; preds = %6, %3
%10 = phi i1 [ false, %3 ], [ %8, %6 ]
br i1 %10, label %11, label %16
11: ; preds = %9
%12 = load i32, i32* %1
%13 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %12)
br label %14
14: ; preds = %11
%15 = call i32 (...) @getchar()
store i32 %15, i32* %1
br label %3
16: ; preds = %9
%17 = load i32, i32* %1
%18 = load i32, i32* @EOF
%19 = icmp ne i32 %17, %18
br i1 %19, label %24, label %20
20: ; preds = %16
%21 = load i32, i32* @stdin
%22 = call i64 @ferror(i32 %21)
%23 = icmp ne i64 %22, 0
br label %24
24: ; preds = %20, %16
%25 = phi i1 [ true, %16 ], [ %23, %20 ]
%26 = zext i1 %25 to i32
ret i32 %26
}

LLVM IR (-O2):
@.str = [5 x i8] c"%02x\00"
@EOF = global i32 0
@stdin = global i32 0
define i32 @encode_hex() {
%1 = tail call i32 (...) @getchar()
%2 = icmp ult i32 %1, 256
br i1 %2, label %3, label %8
3: ; preds = %0, %3
%4 = phi i32 [ %6, %3 ], [ %1, %0 ]
%5 = tail call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = tail call i32 (...) @getchar()
%7 = icmp ult i32 %6, 256
br i1 %7, label %3, label %8
8: ; preds = %3, %0
%9 = phi i32 [ %1, %0 ], [ %6, %3 ]
%10 = load i32, i32* @EOF
%11 = icmp eq i32 %9, %10
br i1 %11, label %12, label %17
12: ; preds = %8
%13 = load i32, i32* @stdin
%14 = tail call i64 @ferror(i32 %13)
%15 = icmp ne i64 %14, 0
%16 = zext i1 %15 to i32
br label %17
17: ; preds = %12, %8
%18 = phi i32 [ 1, %8 ], [ %16, %12 ]
ret i32 %18
}