#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*/ EINVAL;
int /*<<< orphan*/ ENOSYS;
int _printf_error(char const *, int);
long atoi(char const *);
int /*<<< orphan*/ errno;
char const fputc(char const, int /*<<< orphan*/);
int /*<<< orphan*/ fputs(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, unsigned long);
int /*<<< orphan*/ stdout;
unsigned long strtoul(char const *, int /*<<< orphan*/ *, int);
int _printf_format(char const **p, char const *arg) {
  long i;
  unsigned long u;
  switch (**p) {
  case 'c':
    if (fputc(arg[0], stdout) != arg[0])
      return _printf_error("stdout", 1);
    break;
  case 's':
    if (fputs(arg, stdout) != 0)
      return _printf_error("stdout", 1);
    break;
  case 'd':
  case 'i':
    i = atoi(arg);
    printf("%ld", i);
    break;
  case 'u':
    u = strtoul(arg, NULL, 10);
    printf("%lu", u);
    break;
  case 'x':
    u = strtoul(arg, NULL, 10);
    printf("%lx", u);
    break;
  case 'X':
    u = strtoul(arg, NULL, 10);
    printf("%lX", u);
    break;
  case '-':
  case '+':
  case '#':
  case '0':
  case '1':
  case '2':
  case '3':
  case '4':
  case '5':
  case '6':
  case '7':
  case '8':
  case '9':
  case 'e':
  case 'E':
  case 'f':
  case 'g':
  case 'G':
  case 'o':
    errno = ENOSYS;
    return _printf_error(*p, 1);
  default:
    errno = EINVAL;
    return _printf_error(*p, 1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<_printf_format>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
add    $0xffffffdd,%eax
mov    %eax,%ecx
mov    %rcx,-0x30(%rbp)
sub    $0x55,%eax
ja     12ee <_printf_format+0x18e>
mov    -0x30(%rbp),%rax
lea    0xe69(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x18(%rbp),%rax
mov    (%rax),%al
mov    0x2e1b(%rip),%rcx
mov    (%rcx),%esi
movsbl %al,%edi
call   1050 <fputc@plt>
movsbl %al,%eax
mov    -0x18(%rbp),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     11e2 <_printf_format+0x82>
lea    0xf88(%rip),%rdi
mov    $0x1,%esi
call   1060 <_printf_error@plt>
mov    %eax,-0x4(%rbp)
jmp    1320 <_printf_format+0x1c0>
jmp    1319 <_printf_format+0x1b9>
mov    -0x18(%rbp),%rdi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
call   1040 <fputs@plt>
cmp    $0x0,%eax
je     121b <_printf_format+0xbb>
lea    0xf4f(%rip),%rdi
mov    $0x1,%esi
call   1060 <_printf_error@plt>
mov    %eax,-0x4(%rbp)
jmp    1320 <_printf_format+0x1c0>
jmp    1319 <_printf_format+0x1b9>
mov    -0x18(%rbp),%rdi
call   1080 <atoi@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xf27(%rip),%rdi
call   1030 <printf@plt>
jmp    1319 <_printf_format+0x1b9>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1070 <strtoul@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xf00(%rip),%rdi
call   1030 <printf@plt>
jmp    1319 <_printf_format+0x1b9>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1070 <strtoul@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xed9(%rip),%rdi
call   1030 <printf@plt>
jmp    1319 <_printf_format+0x1b9>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1070 <strtoul@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xeb2(%rip),%rdi
call   1030 <printf@plt>
jmp    1319 <_printf_format+0x1b9>
mov    0x2d1e(%rip),%rax
mov    (%rax),%ecx
mov    0x2d05(%rip),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    $0x1,%esi
call   1060 <_printf_error@plt>
mov    %eax,-0x4(%rbp)
jmp    1320 <_printf_format+0x1c0>
mov    0x2cdb(%rip),%rax
mov    (%rax),%ecx
mov    0x2cda(%rip),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    $0x1,%esi
call   1060 <_printf_error@plt>
mov    %eax,-0x4(%rbp)
jmp    1320 <_printf_format+0x1c0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_printf_format>:
push   %rbx
mov    (%rdi),%rdi
movsbl (%rdi),%eax
add    $0xffffffdd,%eax
cmp    $0x55,%eax
ja     11b3 <_printf_format+0x53>
mov    %rsi,%rbx
lea    0xe87(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
mov    0x2e46(%rip),%rcx
mov    %eax,(%rcx)
mov    $0x1,%esi
pop    %rbx
jmp    1060 <_printf_error@plt>
mov    %rbx,%rdi
call   1080 <atoi@plt>
lea    0xfb1(%rip),%rdi
jmp    1237 <_printf_format+0xd7>
mov    0x2e16(%rip),%rax
jmp    1189 <_printf_format+0x29>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1070 <strtoul@plt>
lea    0xf99(%rip),%rdi
jmp    1237 <_printf_format+0xd7>
movsbl (%rbx),%edi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
call   1050 <fputc@plt>
cmp    (%rbx),%al
jne    1200 <_printf_format+0xa0>
jmp    123f <_printf_format+0xdf>
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1040 <fputs@plt>
test   %eax,%eax
je     123f <_printf_format+0xdf>
lea    0xf51(%rip),%rdi
jmp    1194 <_printf_format+0x34>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1070 <strtoul@plt>
lea    0xf44(%rip),%rdi
jmp    1237 <_printf_format+0xd7>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1070 <strtoul@plt>
lea    0xf30(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@stdout = global i32 0
@.str = [7 x i8] c"stdout\00"
@.str.1 = [4 x i8] c"%ld\00"
@.str.2 = [4 x i8] c"%lu\00"
@.str.3 = [4 x i8] c"%lx\00"
@.str.4 = [4 x i8] c"%lX\00"
@ENOSYS = global i32 0
@errno = global i32 0
@EINVAL = global i32 0
define i32 @_printf_format(i8** %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
store i8** %0, i8*** %4
store i8* %1, i8** %5
%8 = load i8**, i8*** %4
%9 = load i8*, i8** %8
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
switch i32 %11, label %60 [
i32 99, label %12
i32 115, label %27
i32 100, label %35
i32 105, label %35
i32 117, label %40
i32 120, label %45
i32 88, label %50
i32 45, label %55
i32 43, label %55
i32 35, label %55
i32 48, label %55
i32 49, label %55
i32 50, label %55
i32 51, label %55
i32 52, label %55
i32 53, label %55
i32 54, label %55
i32 55, label %55
i32 56, label %55
i32 57, label %55
i32 101, label %55
i32 69, label %55
i32 102, label %55
i32 103, label %55
i32 71, label %55
i32 111, label %55
]
12: ; preds = %2
%13 = load i8*, i8** %5
%14 = getelementptr inbounds i8, i8* %13, i64 0
%15 = load i8, i8* %14
%16 = load i32, i32* @stdout
%17 = call signext i8 @fputc(i8 signext %15, i32 %16)
%18 = sext i8 %17 to i32
%19 = load i8*, i8** %5
%20 = getelementptr inbounds i8, i8* %19, i64 0
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %18, %22
br i1 %23, label %24, label %26
24: ; preds = %12
%25 = call i32 @_printf_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 1)
store i32 %25, i32* %3
br label %66
26: ; preds = %12
br label %65
27: ; preds = %2
%28 = load i8*, i8** %5
%29 = load i32, i32* @stdout
%30 = call i32 @fputs(i8* %28, i32 %29)
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = call i32 @_printf_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 1)
store i32 %33, i32* %3
br label %66
34: ; preds = %27
br label %65
35: ; preds = %2, %2
%36 = load i8*, i8** %5
%37 = call i64 @atoi(i8* %36)
store i64 %37, i64* %6
%38 = load i64, i64* %6
%39 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64 %38)
br label %65
40: ; preds = %2
%41 = load i8*, i8** %5
%42 = call i64 @strtoul(i8* %41, i32* null, i32 10)
store i64 %42, i64* %7
%43 = load i64, i64* %7
%44 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64 %43)
br label %65
45: ; preds = %2
%46 = load i8*, i8** %5
%47 = call i64 @strtoul(i8* %46, i32* null, i32 10)
store i64 %47, i64* %7
%48 = load i64, i64* %7
%49 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i64 %48)
br label %65
50: ; preds = %2
%51 = load i8*, i8** %5
%52 = call i64 @strtoul(i8* %51, i32* null, i32 10)
store i64 %52, i64* %7
%53 = load i64, i64* %7
%54 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i64 %53)
br label %65
55: ; preds = %2, %2, %2, %2, %2, %2, %2, %2, %2, %2, %2, %2, %2, %2, %2, %2, %2, %2, %2
%56 = load i32, i32* @ENOSYS
store i32 %56, i32* @errno
%57 = load i8**, i8*** %4
%58 = load i8*, i8** %57
%59 = call i32 @_printf_error(i8* %58, i32 1)
store i32 %59, i32* %3
br label %66
60: ; preds = %2
%61 = load i32, i32* @EINVAL
store i32 %61, i32* @errno
%62 = load i8**, i8*** %4
%63 = load i8*, i8** %62
%64 = call i32 @_printf_error(i8* %63, i32 1)
store i32 %64, i32* %3
br label %66
65: ; preds = %50, %45, %40, %35, %34, %26
store i32 0, i32* %3
br label %66
66: ; preds = %65, %60, %55, %32, %24
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [7 x i8] c"stdout\00"
@.str.1 = [4 x i8] c"%ld\00"
@.str.2 = [4 x i8] c"%lu\00"
@.str.3 = [4 x i8] c"%lx\00"
@.str.4 = [4 x i8] c"%lX\00"
@ENOSYS = global i32 0
@errno = global i32 0
@EINVAL = global i32 0
define i32 @_printf_format(i8** nocapture %0, i8* %1) {
%3 = load i8*, i8** %0
%4 = load i8, i8* %3
%5 = sext i8 %4 to i32
switch i32 %5, label %35 [
i32 99, label %6
i32 115, label %14
i32 100, label %20
i32 105, label %20
i32 117, label %23
i32 120, label %26
i32 88, label %29
i32 45, label %32
i32 43, label %32
i32 35, label %32
i32 48, label %32
i32 49, label %32
i32 50, label %32
i32 51, label %32
i32 52, label %32
i32 53, label %32
i32 54, label %32
i32 55, label %32
i32 56, label %32
i32 57, label %32
i32 101, label %32
i32 69, label %32
i32 102, label %32
i32 103, label %32
i32 71, label %32
i32 111, label %32
]
6: ; preds = %2
%7 = load i8, i8* %1
%8 = load i32, i32* @stdout
%9 = tail call signext i8 @fputc(i8 signext %7, i32 %8)
%10 = load i8, i8* %1
%11 = icmp eq i8 %9, %10
br i1 %11, label %38, label %12
12: ; preds = %6
%13 = tail call i32 @_printf_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 1)
br label %38
14: ; preds = %2
%15 = load i32, i32* @stdout
%16 = tail call i32 @fputs(i8* %1, i32 %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %38, label %18
18: ; preds = %14
%19 = tail call i32 @_printf_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 1)
br label %38
20: ; preds = %2, %2
%21 = tail call i64 @atoi(i8* %1)
%22 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64 %21)
br label %38
23: ; preds = %2
%24 = tail call i64 @strtoul(i8* nocapture %1, i32* null, i32 10)
%25 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64 %24)
br label %38
26: ; preds = %2
%27 = tail call i64 @strtoul(i8* nocapture %1, i32* null, i32 10)
%28 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i64 %27)
br label %38
29: ; preds = %2
%30 = tail call i64 @strtoul(i8* nocapture %1, i32* null, i32 10)
%31 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i64 %30)
br label %38
32: ; preds = %2, %2, %2, %2, %2, %2, %2, %2, %2, %2, %2, %2, %2, %2, %2, %2, %2, %2, %2
%33 = load i32, i32* @ENOSYS
store i32 %33, i32* @errno
%34 = tail call i32 @_printf_error(i8* %3, i32 1)
br label %38
35: ; preds = %2
%36 = load i32, i32* @EINVAL
store i32 %36, i32* @errno
%37 = tail call i32 @_printf_error(i8* %3, i32 1)
br label %38
38: ; preds = %20, %23, %26, %29, %6, %14, %35, %32, %18, %12
%39 = phi i32 [ %37, %35 ], [ %34, %32 ], [ %19, %18 ], [ %13, %12 ], [ 0, %14 ], [ 0, %6 ], [ 0, %29 ], [ 0, %26 ], [ 0, %23 ], [ 0, %20 ]
ret i32 %39
}