#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*/ LC_NUMERIC;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ perror(char *);
char *setlocale(int /*<<< orphan*/, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int, int, double);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char *);
void deg_to_sexigesimal_str(float deg, char *dst, char hrs) {
  char buf[15] = {0};
  int hour = (int)deg;
  if (hrs) {
    hour /= 15;
  }
  int min = (int)((deg - hour) * 60);
  double min_frac = (double)min / 60;
  double sec = (deg - hour - min_frac) * 3600;
  char *saved_locale, *old_locale = setlocale(LC_NUMERIC, NULL);
  saved_locale = strdup(old_locale);
  if (saved_locale == NULL) {
    perror("Unable get current locale, probably out of memory.\n");
    return;
  }
  if (min < 0) {
    min *= -1;
  }
  if (sec < 0) {
    sec *= -1;
  }
  setlocale(LC_NUMERIC, "en_US");
  snprintf(buf, 14, "%i:%i:%.5f", hour, min, sec);
  strcpy(dst, buf);
  setlocale(LC_NUMERIC, saved_locale);
  free(saved_locale);
}


x86 assembly (clang, -O0):
<deg_to_sexigesimal_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %sil,%al
movss  %xmm0,-0x4(%rbp)
mov    %rdi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xf,%edx
call   1060 <memset@plt>
cvttss2si -0x4(%rbp),%eax
mov    %eax,-0x24(%rbp)
cmpb   $0x0,-0x11(%rbp)
je     11b7 <deg_to_sexigesimal_str+0x47>
mov    -0x24(%rbp),%eax
mov    $0xf,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
movss  -0x4(%rbp),%xmm0
cvtsi2ssl -0x24(%rbp),%xmm1
subss  %xmm1,%xmm0
movss  0xe4b(%rip),%xmm1
mulss  %xmm1,%xmm0
cvttss2si %xmm0,%eax
mov    %eax,-0x28(%rbp)
cvtsi2sdl -0x28(%rbp),%xmm0
movsd  0xe23(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movss  -0x4(%rbp),%xmm0
cvtsi2ssl -0x24(%rbp),%xmm1
subss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
subsd  -0x30(%rbp),%xmm0
movsd  0xdf3(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <setlocale@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1255 <deg_to_sexigesimal_str+0xe5>
lea    0xdd1(%rip),%rdi
call   1080 <perror@plt>
jmp    12e4 <deg_to_sexigesimal_str+0x174>
cmpl   $0x0,-0x28(%rbp)
jge    1266 <deg_to_sexigesimal_str+0xf6>
imul   $0xffffffff,-0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x38(%rbp),%xmm0
jbe    1286 <deg_to_sexigesimal_str+0x116>
movsd  0xd94(%rip),%xmm0
mulsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
lea    0xdba(%rip),%rsi
call   1070 <setlocale@plt>
lea    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%ecx
mov    -0x28(%rbp),%r8d
movsd  -0x38(%rbp),%xmm0
mov    $0xe,%esi
lea    0xd9f(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
mov    -0x40(%rbp),%rsi
call   1070 <setlocale@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<deg_to_sexigesimal_str>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r14
movq   $0x0,0x1f(%rsp)
movq   $0x0,0x18(%rsp)
movaps %xmm0,(%rsp)
cvttss2si %xmm0,%ebx
test   %sil,%sil
je     11aa <deg_to_sexigesimal_str+0x4a>
movslq %ebx,%rbx
imul   $0xffffffff88888889,%rbx,%rax
shr    $0x20,%rax
add    %eax,%ebx
mov    %ebx,%eax
shr    $0x1f,%eax
sar    $0x3,%ebx
add    %eax,%ebx
mov    0x2e37(%rip),%r13
mov    0x0(%r13),%edi
xor    %esi,%esi
call   1060 <setlocale@plt>
mov    %rax,%rdi
call   1080 <strdup@plt>
test   %rax,%rax
je     129f <deg_to_sexigesimal_str+0x13f>
mov    %rax,%r15
xorps  %xmm0,%xmm0
cvtsi2ss %ebx,%xmm0
movaps (%rsp),%xmm1
subss  %xmm0,%xmm1
xorps  %xmm0,%xmm0
cvtss2sd %xmm1,%xmm0
mulss  0xe12(%rip),%xmm1
cvttss2si %xmm1,%eax
cvttps2dq %xmm1,%xmm1
cvtdq2pd %xmm1,%xmm1
divsd  0xe06(%rip),%xmm1
addsd  %xmm0,%xmm1
mulsd  0xe02(%rip),%xmm1
mov    %eax,%ebp
neg    %ebp
cmovs  %eax,%ebp
movapd 0xe03(%rip),%xmm0
xorpd  %xmm1,%xmm0
xorpd  %xmm2,%xmm2
movapd %xmm1,%xmm3
cmpltsd %xmm2,%xmm3
andpd  %xmm3,%xmm0
andnpd %xmm1,%xmm3
orpd   %xmm0,%xmm3
movapd %xmm3,(%rsp)
mov    0x0(%r13),%edi
lea    0xe1a(%rip),%rsi
call   1060 <setlocale@plt>
lea    0xe14(%rip),%rdx
lea    0x18(%rsp),%r12
mov    %r12,%rdi
mov    $0xe,%esi
mov    %ebx,%ecx
mov    %ebp,%r8d
movaps (%rsp),%xmm0
call   1050 <snprintf@plt>
mov    %r14,%rdi
mov    %r12,%rsi
call   1040 <strcpy@plt>
mov    0x0(%r13),%edi
mov    %r15,%rsi
call   1060 <setlocale@plt>
mov    %r15,%rdi
call   1030 <free@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd8a(%rip),%rdi
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <perror@plt>

LLVM IR (-O0):
@LC_NUMERIC = global i32 0
@.str = [52 x i8] c"Unable get current locale, probably out of memory.\0A\00"
@.str.1 = [6 x i8] c"en_US\00"
@.str.2 = [11 x i8] c"%i:%i:%.5f\00"
define void @deg_to_sexigesimal_str(float %0, i8* %1, i8 signext %2) {
%4 = alloca float
%5 = alloca i8*
%6 = alloca i8
%7 = alloca [15 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca double
%11 = alloca double
%12 = alloca i8*
%13 = alloca i8*
store float %0, float* %4
store i8* %1, i8** %5
store i8 %2, i8* %6
%14 = bitcast [15 x i8]* %7 to i8*
%15 = load float, float* %4
%16 = fptosi float %15 to i32
store i32 %16, i32* %8
%17 = load i8, i8* %6
%18 = icmp ne i8 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %3
%20 = load i32, i32* %8
%21 = sdiv i32 %20, 15
store i32 %21, i32* %8
br label %22
22: ; preds = %19, %3
%23 = load float, float* %4
%24 = load i32, i32* %8
%25 = sitofp i32 %24 to float
%26 = fsub float %23, %25
%27 = fmul float %26, 6.000000e+01
%28 = fptosi float %27 to i32
store i32 %28, i32* %9
%29 = load i32, i32* %9
%30 = sitofp i32 %29 to double
%31 = fdiv double %30, 6.000000e+01
store double %31, double* %10
%32 = load float, float* %4
%33 = load i32, i32* %8
%34 = sitofp i32 %33 to float
%35 = fsub float %32, %34
%36 = fpext float %35 to double
%37 = load double, double* %10
%38 = fsub double %36, %37
%39 = fmul double %38, 3.600000e+03
store double %39, double* %11
%40 = load i32, i32* @LC_NUMERIC
%41 = call i8* @setlocale(i32 %40, i8* null)
store i8* %41, i8** %13
%42 = load i8*, i8** %13
%43 = call i8* @strdup(i8* %42)
store i8* %43, i8** %12
%44 = load i8*, i8** %12
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %48
46: ; preds = %22
%47 = call i32 @perror(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0))
br label %76
48: ; preds = %22
%49 = load i32, i32* %9
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i32, i32* %9
%53 = mul nsw i32 %52, -1
store i32 %53, i32* %9
br label %54
54: ; preds = %51, %48
%55 = load double, double* %11
%56 = fcmp olt double %55, 0.000000e+00
br i1 %56, label %57, label %60
57: ; preds = %54
%58 = load double, double* %11
%59 = fmul double %58, -1.000000e+00
store double %59, double* %11
br label %60
60: ; preds = %57, %54
%61 = load i32, i32* @LC_NUMERIC
%62 = call i8* @setlocale(i32 %61, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%63 = getelementptr inbounds [15 x i8], [15 x i8]* %7, i64 0, i64 0
%64 = load i32, i32* %8
%65 = load i32, i32* %9
%66 = load double, double* %11
%67 = call i32 @snprintf(i8* %63, i32 14, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %64, i32 %65, double %66)
%68 = load i8*, i8** %5
%69 = getelementptr inbounds [15 x i8], [15 x i8]* %7, i64 0, i64 0
%70 = call i32 @strcpy(i8* %68, i8* %69)
%71 = load i32, i32* @LC_NUMERIC
%72 = load i8*, i8** %12
%73 = call i8* @setlocale(i32 %71, i8* %72)
%74 = load i8*, i8** %12
%75 = call i32 @free(i8* %74)
br label %76
76: ; preds = %60, %46
ret void
}

LLVM IR (-O2):
@LC_NUMERIC = global i32 0
@.str = [52 x i8] c"Unable get current locale, probably out of memory.\0A\00"
@.str.1 = [6 x i8] c"en_US\00"
@.str.2 = [11 x i8] c"%i:%i:%.5f\00"
define void @deg_to_sexigesimal_str(float %0, i8* %1, i8 signext %2) {
%4 = alloca [15 x i8]
%5 = getelementptr inbounds [15 x i8], [15 x i8]* %4, i64 0, i64 0
%6 = fptosi float %0 to i32
%7 = icmp eq i8 %2, 0
%8 = sdiv i32 %6, 15
%9 = select i1 %7, i32 %6, i32 %8
%10 = sitofp i32 %9 to float
%11 = fsub float %0, %10
%12 = fmul float %11, 6.000000e+01
%13 = fptosi float %12 to i32
%14 = sitofp i32 %13 to double
%15 = fdiv double %14, 6.000000e+01
%16 = fpext float %11 to double
%17 = fsub double %16, %15
%18 = fmul double %17, 3.600000e+03
%19 = load i32, i32* @LC_NUMERIC
%20 = tail call i8* @setlocale(i32 %19, i8* null)
%21 = tail call i8* @strdup(i8* %20)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %25
23: ; preds = %3
%24 = tail call i32 @perror(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0))
br label %37
25: ; preds = %3
%26 = tail call i32 @llvm.abs.i32(i32 %13, i1 true)
%27 = fcmp olt double %18, 0.000000e+00
%28 = fneg double %18
%29 = select i1 %27, double %28, double %18
%30 = load i32, i32* @LC_NUMERIC
%31 = tail call i8* @setlocale(i32 %30, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%32 = call i32 @snprintf(i8* %5, i32 14, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %9, i32 %26, double %29)
%33 = call i32 @strcpy(i8* %1, i8* %5)
%34 = load i32, i32* @LC_NUMERIC
%35 = call i8* @setlocale(i32 %34, i8* %21)
%36 = call i32 @free(i8* %21)
br label %37
37: ; preds = %25, %23
ret void
}