#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*/ perr(char *, char const *);
char *strchr(char const *, int);
int check_spec(int spec, const char *valid, const char *what) {
  char *p;
  if (spec == 0)
    return 0;
  p = strchr(valid, spec);
  if (p == NULL) {
    perr("Invalid %s specifier", what);
    return 0;
  }
  return p - valid;
}


x86 assembly (clang, -O0):
<check_spec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1149 <check_spec+0x29>
movl   $0x0,-0x4(%rbp)
jmp    118e <check_spec+0x6e>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1030 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1180 <check_spec+0x60>
mov    -0x18(%rbp),%rsi
lea    0xe91(%rip),%rdi
call   1040 <perr@plt>
movl   $0x0,-0x4(%rbp)
jmp    118e <check_spec+0x6e>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_spec>:
push   %r15
push   %r14
push   %rbx
xor    %ebx,%ebx
test   %edi,%edi
je     1156 <check_spec+0x36>
mov    %rdx,%r15
mov    %rsi,%r14
mov    %edi,%esi
mov    %r14,%rdi
call   1030 <strchr@plt>
test   %rax,%rax
je     1147 <check_spec+0x27>
sub    %r14d,%eax
mov    %eax,%ebx
jmp    1156 <check_spec+0x36>
lea    0xeb2(%rip),%rdi
mov    %r15,%rsi
call   1040 <perr@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [21 x i8] c"Invalid %s specifier\00"
define i32 @check_spec(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%9 = load i32, i32* %5
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %3
store i32 0, i32* %4
br label %28
12: ; preds = %3
%13 = load i8*, i8** %6
%14 = load i32, i32* %5
%15 = call i8* @strchr(i8* %13, i32 %14)
store i8* %15, i8** %8
%16 = load i8*, i8** %8
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = load i8*, i8** %7
%20 = call i32 @perr(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %19)
store i32 0, i32* %4
br label %28
21: ; preds = %12
%22 = load i8*, i8** %8
%23 = load i8*, i8** %6
%24 = ptrtoint i8* %22 to i64
%25 = ptrtoint i8* %23 to i64
%26 = sub i64 %24, %25
%27 = trunc i64 %26 to i32
store i32 %27, i32* %4
br label %28
28: ; preds = %21, %18, %11
%29 = load i32, i32* %4
ret i32 %29
}

LLVM IR (-O2):
@.str = [21 x i8] c"Invalid %s specifier\00"
define i32 @check_spec(i32 %0, i8* %1, i8* %2) {
%4 = icmp eq i32 %0, 0
br i1 %4, label %15, label %5
5: ; preds = %3
%6 = tail call i8* @strchr(i8* dereferenceable(1) %1, i32 %0)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @perr(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %2)
br label %15
10: ; preds = %5
%11 = ptrtoint i8* %6 to i64
%12 = ptrtoint i8* %1 to i64
%13 = sub i64 %11, %12
%14 = trunc i64 %13 to i32
br label %15
15: ; preds = %3, %10, %8
%16 = phi i32 [ 0, %8 ], [ %14, %10 ], [ 0, %3 ]
ret i32 %16
}