#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*/ libc_stop(int);
int num_to_str(char *, int, unsigned long, int, int);
int str_to_str(char *, char const *, int);
int sprintf_fmt(char type, char *buf, unsigned long val, int zero, int count) {
  switch (type) {
  case 's':
    return str_to_str(buf, (const char *)val, count);
  case 'x':
    return num_to_str(buf, 16, val, zero, count);
  case 'u':
    return num_to_str(buf, 10, val, zero, count);
  default:
    libc_stop(0x00004511);
  }
  return 0;
}


x86 assembly (clang, -O0):
<sprintf_fmt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dil,%al
mov    %al,-0x5(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
movsbl -0x5(%rbp),%eax
mov    %eax,-0x24(%rbp)
sub    $0x73,%eax
je     1184 <sprintf_fmt+0x54>
jmp    1162 <sprintf_fmt+0x32>
mov    -0x24(%rbp),%eax
sub    $0x75,%eax
je     11bd <sprintf_fmt+0x8d>
jmp    1173 <sprintf_fmt+0x43>
mov    -0x24(%rbp),%eax
sub    $0x78,%eax
je     119c <sprintf_fmt+0x6c>
jmp    11de <sprintf_fmt+0xae>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1030 <str_to_str@plt>
mov    %eax,-0x4(%rbp)
jmp    11ef <sprintf_fmt+0xbf>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    -0x20(%rbp),%r8d
mov    $0x10,%esi
call   1050 <num_to_str@plt>
mov    %eax,-0x4(%rbp)
jmp    11ef <sprintf_fmt+0xbf>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    -0x20(%rbp),%r8d
mov    $0xa,%esi
call   1050 <num_to_str@plt>
mov    %eax,-0x4(%rbp)
jmp    11ef <sprintf_fmt+0xbf>
mov    $0x4511,%edi
call   1040 <libc_stop@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sprintf_fmt>:
cmp    $0x78,%edi
je     114d <sprintf_fmt+0x1d>
cmp    $0x75,%edi
je     115a <sprintf_fmt+0x2a>
cmp    $0x73,%edi
jne    1167 <sprintf_fmt+0x37>
mov    %rsi,%rdi
mov    %rdx,%rsi
mov    %r8d,%edx
jmp    1030 <str_to_str@plt>
mov    %rsi,%rdi
mov    $0x10,%esi
jmp    1050 <num_to_str@plt>
mov    %rsi,%rdi
mov    $0xa,%esi
jmp    1050 <num_to_str@plt>
push   %rax
mov    $0x4511,%edi
call   1040 <libc_stop@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
define i32 @sprintf_fmt(i8 signext %0, i8* %1, i64 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
store i8 %0, i8* %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%12 = load i8, i8* %7
%13 = sext i8 %12 to i32
switch i32 %13, label %32 [
i32 115, label %14
i32 120, label %20
i32 117, label %26
]
14: ; preds = %5
%15 = load i8*, i8** %8
%16 = load i64, i64* %9
%17 = inttoptr i64 %16 to i8*
%18 = load i32, i32* %11
%19 = call i32 @str_to_str(i8* %15, i8* %17, i32 %18)
store i32 %19, i32* %6
br label %35
20: ; preds = %5
%21 = load i8*, i8** %8
%22 = load i64, i64* %9
%23 = load i32, i32* %10
%24 = load i32, i32* %11
%25 = call i32 @num_to_str(i8* %21, i32 16, i64 %22, i32 %23, i32 %24)
store i32 %25, i32* %6
br label %35
26: ; preds = %5
%27 = load i8*, i8** %8
%28 = load i64, i64* %9
%29 = load i32, i32* %10
%30 = load i32, i32* %11
%31 = call i32 @num_to_str(i8* %27, i32 10, i64 %28, i32 %29, i32 %30)
store i32 %31, i32* %6
br label %35
32: ; preds = %5
%33 = call i32 @libc_stop(i32 17681)
br label %34
34: ; preds = %32
store i32 0, i32* %6
br label %35
35: ; preds = %34, %26, %20, %14
%36 = load i32, i32* %6
ret i32 %36
}

LLVM IR (-O2):
define i32 @sprintf_fmt(i8 signext %0, i8* %1, i64 %2, i32 %3, i32 %4) {
%6 = sext i8 %0 to i32
switch i32 %6, label %14 [
i32 115, label %7
i32 120, label %10
i32 117, label %12
]
7: ; preds = %5
%8 = inttoptr i64 %2 to i8*
%9 = tail call i32 @str_to_str(i8* %1, i8* %8, i32 %4)
br label %16
10: ; preds = %5
%11 = tail call i32 @num_to_str(i8* %1, i32 16, i64 %2, i32 %3, i32 %4)
br label %16
12: ; preds = %5
%13 = tail call i32 @num_to_str(i8* %1, i32 10, i64 %2, i32 %3, i32 %4)
br label %16
14: ; preds = %5
%15 = tail call i32 @libc_stop(i32 17681)
br label %16
16: ; preds = %14, %12, %10, %7
%17 = phi i32 [ 0, %14 ], [ %13, %12 ], [ %11, %10 ], [ %9, %7 ]
ret i32 %17
}