#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 */
typedef int /*<<< orphan*/ num;

/* Variables and functions */
int /*<<< orphan*/ Com_sprintf(char *, int, char *, int);
int strlen(char *);
int CG_DrawFieldWidth(int x, int y, int width, int value, int charWidth,
                      int charHeight) {
  char num[16], *ptr;
  int l;
  int totalwidth = 0;
  if (width < 1) {
    return 0;
  }
  if (width > 5) {
    width = 5;
  }
  switch (width) {
  case 1:
    value = value > 9 ? 9 : value;
    value = value < 0 ? 0 : value;
    break;
  case 2:
    value = value > 99 ? 99 : value;
    value = value < -9 ? -9 : value;
    break;
  case 3:
    value = value > 999 ? 999 : value;
    value = value < -99 ? -99 : value;
    break;
  case 4:
    value = value > 9999 ? 9999 : value;
    value = value < -999 ? -999 : value;
    break;
  }
  Com_sprintf(num, sizeof(num), "%i", value);
  l = strlen(num);
  if (l > width) {
    l = width;
  }
  ptr = num;
  while (*ptr && l) {
    totalwidth += charWidth;
    ptr++;
    l--;
  }
  return totalwidth;
}


x86 assembly (clang, -O0):
<CG_DrawFieldWidth>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8d,-0x18(%rbp)
mov    %r9d,-0x1c(%rbp)
movl   $0x0,-0x40(%rbp)
cmpl   $0x1,-0x10(%rbp)
jge    1159 <CG_DrawFieldWidth+0x39>
movl   $0x0,-0x4(%rbp)
jmp    1356 <CG_DrawFieldWidth+0x236>
cmpl   $0x5,-0x10(%rbp)
jle    116a <CG_DrawFieldWidth+0x4a>
movl   $0x5,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,%ecx
mov    %rcx,-0x48(%rbp)
sub    $0x3,%eax
ja     12c0 <CG_DrawFieldWidth+0x1a0>
mov    -0x48(%rbp),%rax
lea    0xe76(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
cmpl   $0x9,-0x14(%rbp)
jle    11aa <CG_DrawFieldWidth+0x8a>
mov    $0x9,%eax
mov    %eax,-0x4c(%rbp)
jmp    11b0 <CG_DrawFieldWidth+0x90>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11ca <CG_DrawFieldWidth+0xaa>
xor    %eax,%eax
mov    %eax,-0x50(%rbp)
jmp    11d0 <CG_DrawFieldWidth+0xb0>
mov    -0x14(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x50(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    12c0 <CG_DrawFieldWidth+0x1a0>
cmpl   $0x63,-0x14(%rbp)
jle    11f2 <CG_DrawFieldWidth+0xd2>
mov    $0x63,%eax
mov    %eax,-0x54(%rbp)
jmp    11f8 <CG_DrawFieldWidth+0xd8>
mov    -0x14(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0xfffffff7,-0x14(%rbp)
jge    1215 <CG_DrawFieldWidth+0xf5>
mov    $0xfffffff7,%eax
mov    %eax,-0x58(%rbp)
jmp    121b <CG_DrawFieldWidth+0xfb>
mov    -0x14(%rbp),%eax
mov    %eax,-0x58(%rbp)
mov    -0x58(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    12c0 <CG_DrawFieldWidth+0x1a0>
cmpl   $0x3e7,-0x14(%rbp)
jle    1240 <CG_DrawFieldWidth+0x120>
mov    $0x3e7,%eax
mov    %eax,-0x5c(%rbp)
jmp    1246 <CG_DrawFieldWidth+0x126>
mov    -0x14(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0xffffff9d,-0x14(%rbp)
jge    1263 <CG_DrawFieldWidth+0x143>
mov    $0xffffff9d,%eax
mov    %eax,-0x60(%rbp)
jmp    1269 <CG_DrawFieldWidth+0x149>
mov    -0x14(%rbp),%eax
mov    %eax,-0x60(%rbp)
mov    -0x60(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    12c0 <CG_DrawFieldWidth+0x1a0>
cmpl   $0x270f,-0x14(%rbp)
jle    128e <CG_DrawFieldWidth+0x16e>
mov    $0x270f,%eax
mov    %eax,-0x64(%rbp)
jmp    1294 <CG_DrawFieldWidth+0x174>
mov    -0x14(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    -0x64(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0xfffffc19,-0x14(%rbp)
jge    12b4 <CG_DrawFieldWidth+0x194>
mov    $0xfffffc19,%eax
mov    %eax,-0x68(%rbp)
jmp    12ba <CG_DrawFieldWidth+0x19a>
mov    -0x14(%rbp),%eax
mov    %eax,-0x68(%rbp)
mov    -0x68(%rbp),%eax
mov    %eax,-0x14(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x10,%esi
lea    0xd3d(%rip),%rdx
call   1040 <Com_sprintf@plt>
lea    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jle    12f6 <CG_DrawFieldWidth+0x1d6>
mov    -0x10(%rbp),%eax
mov    %eax,-0x3c(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x69(%rbp)
je     131d <CG_DrawFieldWidth+0x1fd>
cmpl   $0x0,-0x3c(%rbp)
setne  %al
mov    %al,-0x69(%rbp)
mov    -0x69(%rbp),%al
test   $0x1,%al
jne    132d <CG_DrawFieldWidth+0x20d>
jmp    1350 <CG_DrawFieldWidth+0x230>
mov    -0x18(%rbp),%eax
add    -0x40(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x3c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x3c(%rbp)
jmp    12fe <CG_DrawFieldWidth+0x1de>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CG_DrawFieldWidth>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
xor    %ebx,%ebx
test   %edx,%edx
jle    1213 <CG_DrawFieldWidth+0xf3>
mov    %r8d,%ebp
cmp    $0x5,%edx
mov    $0x5,%r15d
cmovb  %edx,%r15d
lea    -0x1(%r15),%eax
cmp    $0x3,%eax
ja     11b9 <CG_DrawFieldWidth+0x99>
lea    0xeac(%rip),%rdx
movslq (%rdx,%rax,4),%rax
add    %rdx,%rax
jmp    *%rax
cmp    $0x9,%ecx
mov    $0x9,%eax
cmovl  %ecx,%eax
xor    %ecx,%ecx
test   %eax,%eax
cmovns %eax,%ecx
jmp    11b9 <CG_DrawFieldWidth+0x99>
cmp    $0x63,%ecx
mov    $0x63,%eax
cmovl  %ecx,%eax
cmp    $0xfffffff8,%eax
mov    $0xfffffff7,%ecx
jmp    11b6 <CG_DrawFieldWidth+0x96>
cmp    $0x3e7,%ecx
mov    $0x3e7,%eax
cmovl  %ecx,%eax
cmp    $0xffffff9e,%eax
mov    $0xffffff9d,%ecx
jmp    11b6 <CG_DrawFieldWidth+0x96>
cmp    $0x270f,%ecx
mov    $0x270f,%eax
cmovl  %ecx,%eax
cmp    $0xfffffc1a,%eax
mov    $0xfffffc19,%ecx
cmovge %eax,%ecx
lea    0xe50(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x10,%esi
call   1040 <Com_sprintf@plt>
mov    %r14,%rdi
call   1030 <strlen@plt>
cmp    %r15d,%eax
cmovg  %r15d,%eax
cmpb   $0x0,(%rsp)
je     1213 <CG_DrawFieldWidth+0xf3>
test   %eax,%eax
je     1213 <CG_DrawFieldWidth+0xf3>
mov    %eax,%eax
xor    %ebx,%ebx
mov    $0x1,%ecx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    %ebp,%ebx
cmpb   $0x0,(%rsp,%rcx,1)
je     1213 <CG_DrawFieldWidth+0xf3>
lea    0x1(%rcx),%rdx
cmp    %ecx,%eax
mov    %rdx,%rcx
jne    1200 <CG_DrawFieldWidth+0xe0>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%i\00"
define i32 @CG_DrawFieldWidth(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca [16 x i8]
%15 = alloca i8*
%16 = alloca i32
%17 = alloca i32
store i32 %0, i32* %8
store i32 %1, i32* %9
store i32 %2, i32* %10
store i32 %3, i32* %11
store i32 %4, i32* %12
store i32 %5, i32* %13
store i32 0, i32* %17
%18 = load i32, i32* %10
%19 = icmp slt i32 %18, 1
br i1 %19, label %20, label %21
20: ; preds = %6
store i32 0, i32* %7
br label %120
21: ; preds = %6
%22 = load i32, i32* %10
%23 = icmp sgt i32 %22, 5
br i1 %23, label %24, label %25
24: ; preds = %21
store i32 5, i32* %10
br label %25
25: ; preds = %24, %21
%26 = load i32, i32* %10
switch i32 %26, label %87 [
i32 1, label %27
i32 2, label %42
i32 3, label %57
i32 4, label %72
]
27: ; preds = %25
%28 = load i32, i32* %11
%29 = icmp sgt i32 %28, 9
br i1 %29, label %30, label %31
30: ; preds = %27
br label %33
31: ; preds = %27
%32 = load i32, i32* %11
br label %33
33: ; preds = %31, %30
%34 = phi i32 [ 9, %30 ], [ %32, %31 ]
store i32 %34, i32* %11
%35 = load i32, i32* %11
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %33
br label %40
38: ; preds = %33
%39 = load i32, i32* %11
br label %40
40: ; preds = %38, %37
%41 = phi i32 [ 0, %37 ], [ %39, %38 ]
store i32 %41, i32* %11
br label %87
42: ; preds = %25
%43 = load i32, i32* %11
%44 = icmp sgt i32 %43, 99
br i1 %44, label %45, label %46
45: ; preds = %42
br label %48
46: ; preds = %42
%47 = load i32, i32* %11
br label %48
48: ; preds = %46, %45
%49 = phi i32 [ 99, %45 ], [ %47, %46 ]
store i32 %49, i32* %11
%50 = load i32, i32* %11
%51 = icmp slt i32 %50, -9
br i1 %51, label %52, label %53
52: ; preds = %48
br label %55
53: ; preds = %48
%54 = load i32, i32* %11
br label %55
55: ; preds = %53, %52
%56 = phi i32 [ -9, %52 ], [ %54, %53 ]
store i32 %56, i32* %11
br label %87
57: ; preds = %25
%58 = load i32, i32* %11
%59 = icmp sgt i32 %58, 999
br i1 %59, label %60, label %61
60: ; preds = %57
br label %63
61: ; preds = %57
%62 = load i32, i32* %11
br label %63
63: ; preds = %61, %60
%64 = phi i32 [ 999, %60 ], [ %62, %61 ]
store i32 %64, i32* %11
%65 = load i32, i32* %11
%66 = icmp slt i32 %65, -99
br i1 %66, label %67, label %68
67: ; preds = %63
br label %70
68: ; preds = %63
%69 = load i32, i32* %11
br label %70
70: ; preds = %68, %67
%71 = phi i32 [ -99, %67 ], [ %69, %68 ]
store i32 %71, i32* %11
br label %87
72: ; preds = %25
%73 = load i32, i32* %11
%74 = icmp sgt i32 %73, 9999
br i1 %74, label %75, label %76
75: ; preds = %72
br label %78
76: ; preds = %72
%77 = load i32, i32* %11
br label %78
78: ; preds = %76, %75
%79 = phi i32 [ 9999, %75 ], [ %77, %76 ]
store i32 %79, i32* %11
%80 = load i32, i32* %11
%81 = icmp slt i32 %80, -999
br i1 %81, label %82, label %83
82: ; preds = %78
br label %85
83: ; preds = %78
%84 = load i32, i32* %11
br label %85
85: ; preds = %83, %82
%86 = phi i32 [ -999, %82 ], [ %84, %83 ]
store i32 %86, i32* %11
br label %87
87: ; preds = %25, %85, %70, %55, %40
%88 = getelementptr inbounds [16 x i8], [16 x i8]* %14, i64 0, i64 0
%89 = load i32, i32* %11
%90 = call i32 @Com_sprintf(i8* %88, i32 16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %89)
%91 = getelementptr inbounds [16 x i8], [16 x i8]* %14, i64 0, i64 0
%92 = call i32 @strlen(i8* %91)
store i32 %92, i32* %16
%93 = load i32, i32* %16
%94 = load i32, i32* %10
%95 = icmp sgt i32 %93, %94
br i1 %95, label %96, label %98
96: ; preds = %87
%97 = load i32, i32* %10
store i32 %97, i32* %16
br label %98
98: ; preds = %96, %87
%99 = getelementptr inbounds [16 x i8], [16 x i8]* %14, i64 0, i64 0
store i8* %99, i8** %15
br label %100
100: ; preds = %110, %98
%101 = load i8*, i8** %15
%102 = load i8, i8* %101
%103 = sext i8 %102 to i32
%104 = icmp ne i32 %103, 0
br i1 %104, label %105, label %108
105: ; preds = %100
%106 = load i32, i32* %16
%107 = icmp ne i32 %106, 0
br label %108
108: ; preds = %105, %100
%109 = phi i1 [ false, %100 ], [ %107, %105 ]
br i1 %109, label %110, label %118
110: ; preds = %108
%111 = load i32, i32* %12
%112 = load i32, i32* %17
%113 = add nsw i32 %112, %111
store i32 %113, i32* %17
%114 = load i8*, i8** %15
%115 = getelementptr inbounds i8, i8* %114, i32 1
store i8* %115, i8** %15
%116 = load i32, i32* %16
%117 = add nsw i32 %116, -1
store i32 %117, i32* %16
br label %100
118: ; preds = %108
%119 = load i32, i32* %17
store i32 %119, i32* %7
br label %120
120: ; preds = %118, %20
%121 = load i32, i32* %7
ret i32 %121
}

LLVM IR (-O2):
@.str = [3 x i8] c"%i\00"
define i32 @CG_DrawFieldWidth(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca [16 x i8]
%8 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%9 = icmp slt i32 %2, 1
br i1 %9, label %54, label %10
10: ; preds = %6
%11 = icmp ult i32 %2, 5
%12 = select i1 %11, i32 %2, i32 5
switch i32 %12, label %33 [
i32 1, label %13
i32 2, label %18
i32 3, label %23
i32 4, label %28
]
13: ; preds = %10
%14 = icmp slt i32 %3, 9
%15 = select i1 %14, i32 %3, i32 9
%16 = icmp sgt i32 %15, 0
%17 = select i1 %16, i32 %15, i32 0
br label %33
18: ; preds = %10
%19 = icmp slt i32 %3, 99
%20 = select i1 %19, i32 %3, i32 99
%21 = icmp sgt i32 %20, -9
%22 = select i1 %21, i32 %20, i32 -9
br label %33
23: ; preds = %10
%24 = icmp slt i32 %3, 999
%25 = select i1 %24, i32 %3, i32 999
%26 = icmp sgt i32 %25, -99
%27 = select i1 %26, i32 %25, i32 -99
br label %33
28: ; preds = %10
%29 = icmp slt i32 %3, 9999
%30 = select i1 %29, i32 %3, i32 9999
%31 = icmp sgt i32 %30, -999
%32 = select i1 %31, i32 %30, i32 -999
br label %33
33: ; preds = %10, %28, %23, %18, %13
%34 = phi i32 [ %3, %10 ], [ %32, %28 ], [ %27, %23 ], [ %22, %18 ], [ %17, %13 ]
%35 = call i32 @Com_sprintf(i8* %8, i32 16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %34)
%36 = call i32 @strlen(i8* %8)
%37 = icmp sgt i32 %36, %12
%38 = select i1 %37, i32 %12, i32 %36
%39 = load i8, i8* %8
%40 = icmp ne i8 %39, 0
%41 = icmp ne i32 %38, 0
%42 = select i1 %40, i1 %41, i1 false
br i1 %42, label %43, label %54
43: ; preds = %33, %43
%44 = phi i32 [ %47, %43 ], [ 0, %33 ]
%45 = phi i32 [ %49, %43 ], [ %38, %33 ]
%46 = phi i8* [ %48, %43 ], [ %8, %33 ]
%47 = add nsw i32 %44, %4
%48 = getelementptr inbounds i8, i8* %46, i64 1
%49 = add nsw i32 %45, -1
%50 = load i8, i8* %48
%51 = icmp ne i8 %50, 0
%52 = icmp ne i32 %49, 0
%53 = select i1 %51, i1 %52, i1 false
br i1 %53, label %43, label %54
54: ; preds = %43, %33, %6
%55 = phi i32 [ 0, %6 ], [ 0, %33 ], [ %47, %43 ]
ret i32 %55
}