#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 */
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *ht16k337Seg_intTo4Chars(int value) {
  char *str = malloc(sizeof(char) * 5);
  memset(str, 0, sizeof(char) * 5);
  if ((value < 0) || (value > 9999)) {
    strncpy(str, "XXXX", 4);
    return str;
  }
  for (int pos = 0; pos < 4; pos++) {
    int rem = value % 10;
    value = value / 10;
    str[3 - pos] = rem + 48;
  }
  return str;
}


x86 assembly (clang, -O0):
<ht16k337Seg_intTo4Chars>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    $0x5,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x5,%edx
call   1040 <memset@plt>
cmpl   $0x0,-0xc(%rbp)
jl     1170 <ht16k337Seg_intTo4Chars+0x40>
cmpl   $0x270f,-0xc(%rbp)
jle    1192 <ht16k337Seg_intTo4Chars+0x62>
mov    -0x18(%rbp),%rdi
lea    0xe85(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ef <ht16k337Seg_intTo4Chars+0xbf>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x4,-0x1c(%rbp)
jge    11e7 <ht16k337Seg_intTo4Chars+0xb7>
mov    -0xc(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %edx,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0xc(%rbp)
mov    -0x20(%rbp),%eax
add    $0x30,%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    $0x3,%ecx
sub    -0x1c(%rbp),%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1199 <ht16k337Seg_intTo4Chars+0x69>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ht16k337Seg_intTo4Chars>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    $0x5,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x5,%edx
call   1040 <memset@plt>
cmp    $0x270f,%ebp
jbe    116f <ht16k337Seg_intTo4Chars+0x3f>
lea    0xea0(%rip),%rsi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1030 <strncpy@plt>
jmp    11eb <ht16k337Seg_intTo4Chars+0xbb>
movzwl %bp,%eax
imul   $0xcccd,%eax,%ecx
shr    $0x13,%ecx
lea    (%rcx,%rcx,1),%edx
lea    (%rdx,%rdx,4),%edx
sub    %edx,%ebp
or     $0x30,%bpl
mov    %bpl,0x3(%rbx)
imul   $0xcccd,%ecx,%edx
shr    $0x12,%edx
and    $0xfffffffe,%edx
lea    (%rdx,%rdx,4),%edx
sub    %edx,%ecx
mov    %eax,%edx
shr    $0x2,%edx
imul   $0x147b,%edx,%edx
shr    $0x11,%edx
or     $0x30,%cl
mov    %cl,0x2(%rbx)
movzbl %dl,%ecx
imul   $0xcd,%ecx,%edx
shr    $0xa,%edx
and    $0xfffffffe,%edx
lea    (%rdx,%rdx,4),%edx
sub    %dl,%cl
shr    $0x3,%eax
imul   $0x20c5,%eax,%eax
shr    $0x14,%eax
or     $0x30,%cl
mov    %cl,0x1(%rbx)
imul   $0xcd,%eax,%ecx
shr    $0xa,%ecx
and    $0xfffffffe,%ecx
lea    (%rcx,%rcx,4),%ecx
sub    %cl,%al
or     $0x30,%al
mov    %al,(%rbx)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"XXXX\00"
define i8* @ht16k337Seg_intTo4Chars(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
%7 = call i8* @malloc(i32 5)
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = call i32 @memset(i8* %8, i32 0, i32 5)
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %1
%13 = load i32, i32* %3
%14 = icmp sgt i32 %13, 9999
br i1 %14, label %15, label %19
15: ; preds = %12, %1
%16 = load i8*, i8** %4
%17 = call i32 @strncpy(i8* %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%18 = load i8*, i8** %4
store i8* %18, i8** %2
br label %41
19: ; preds = %12
store i32 0, i32* %5
br label %20
20: ; preds = %36, %19
%21 = load i32, i32* %5
%22 = icmp slt i32 %21, 4
br i1 %22, label %23, label %39
23: ; preds = %20
%24 = load i32, i32* %3
%25 = srem i32 %24, 10
store i32 %25, i32* %6
%26 = load i32, i32* %3
%27 = sdiv i32 %26, 10
store i32 %27, i32* %3
%28 = load i32, i32* %6
%29 = add nsw i32 %28, 48
%30 = trunc i32 %29 to i8
%31 = load i8*, i8** %4
%32 = load i32, i32* %5
%33 = sub nsw i32 3, %32
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %31, i64 %34
store i8 %30, i8* %35
br label %36
36: ; preds = %23
%37 = load i32, i32* %5
%38 = add nsw i32 %37, 1
store i32 %38, i32* %5
br label %20
39: ; preds = %20
%40 = load i8*, i8** %4
store i8* %40, i8** %2
br label %41
41: ; preds = %39, %15
%42 = load i8*, i8** %2
ret i8* %42
}

LLVM IR (-O2):
@.str = [5 x i8] c"XXXX\00"
define i8* @ht16k337Seg_intTo4Chars(i32 %0) {
%2 = tail call dereferenceable_or_null(5) i8* @malloc(i32 5)
%3 = tail call i32 @memset(i8* %2, i32 0, i32 5)
%4 = icmp ugt i32 %0, 9999
br i1 %4, label %28, label %5
5: ; preds = %1
%6 = trunc i32 %0 to i16
%7 = urem i16 %6, 10
%8 = trunc i32 %0 to i16
%9 = udiv i16 %8, 10
%10 = trunc i16 %7 to i8
%11 = or i8 %10, 48
%12 = getelementptr inbounds i8, i8* %2, i64 3
store i8 %11, i8* %12
%13 = urem i16 %9, 10
%14 = trunc i32 %0 to i16
%15 = udiv i16 %14, 100
%16 = trunc i16 %13 to i8
%17 = or i8 %16, 48
%18 = getelementptr inbounds i8, i8* %2, i64 2
store i8 %17, i8* %18
%19 = trunc i16 %15 to i8
%20 = urem i8 %19, 10
%21 = trunc i32 %0 to i16
%22 = udiv i16 %21, 1000
%23 = or i8 %20, 48
%24 = getelementptr inbounds i8, i8* %2, i64 1
store i8 %23, i8* %24
%25 = trunc i16 %22 to i8
%26 = urem i8 %25, 10
%27 = or i8 %26, 48
store i8 %27, i8* %2
br label %30
28: ; preds = %1
%29 = tail call i32 @strncpy(i8* %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
br label %30
30: ; preds = %5, %28
ret i8* %2
}