#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*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, ...);
void ex4() {
  int consumidor, codigo;
  float kwh[3] = {0.5, 0.7, 1.2}, consumo, custo, total[3] = {0, 0, 0},
        count[3] = {0, 0, 0};
  while (1) {
    printf("Informe o numero do consumidor [0 para terminar]: ");
    scanf("%d", &consumidor);
    if (!consumidor)
      break;
    printf("Informe o consumo em kWh: ");
    scanf("%f", &consumo);
    printf("Informe o tipo do consumidor (codigo):\n");
    printf("\t0-Residencial\n\t1-Comercial\n\t2-Industrial\n");
    scanf("%d", &codigo);
    custo = kwh[codigo] * consumo;
    total[codigo] += custo;
    count[codigo]++;
    printf("Custo total: R$%.2f\n\n", custo);
  }
  printf("Media de consumo:\n");
  printf("\tResidencial:\t%.2f\n\tComercial:\t%.2f\n\tIndustrial:\t%.2f\n",
         total[0] / count[0], total[1] / count[1], total[2] / count[2]);
}


x86 assembly (clang, -O0):
<ex4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0xec5(%rip),%rax
mov    %rax,-0x14(%rbp)
mov    0xec3(%rip),%eax
mov    %eax,-0xc(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
lea    -0x34(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
lea    0xe9d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xec2(%rip),%rdi
lea    -0x4(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
cmpl   $0x0,-0x4(%rbp)
jne    119b <ex4+0x6b>
jmp    1246 <ex4+0x116>
lea    0xea4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeb1(%rip),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
lea    0xea2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xebc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe65(%rip),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
movslq -0x8(%rbp),%rax
movss  -0x14(%rbp,%rax,4),%xmm0
mulss  -0x18(%rbp),%xmm0
movss  %xmm0,-0x1c(%rbp)
movss  -0x1c(%rbp),%xmm0
movslq -0x8(%rbp),%rax
addss  -0x28(%rbp,%rax,4),%xmm0
movss  %xmm0,-0x28(%rbp,%rax,4)
movslq -0x8(%rbp),%rax
movss  0xde2(%rip),%xmm0
addss  -0x34(%rbp,%rax,4),%xmm0
movss  %xmm0,-0x34(%rbp,%rax,4)
movss  -0x1c(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xe7d(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
jmp    116c <ex4+0x3c>
lea    0xe80(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movss  -0x28(%rbp),%xmm0
divss  -0x34(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movss  -0x24(%rbp),%xmm1
divss  -0x30(%rbp),%xmm1
cvtss2sd %xmm1,%xmm1
movss  -0x20(%rbp),%xmm2
divss  -0x2c(%rbp),%xmm2
cvtss2sd %xmm2,%xmm2
lea    0xe5b(%rip),%rdi
mov    $0x3,%al
call   1030 <printf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ex4>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
movl   $0x0,0x20(%rsp)
movq   $0x0,0x18(%rsp)
movl   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0xea9(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xece(%rip),%rdi
lea    0x4(%rsp),%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
cmpl   $0x0,0x4(%rsp)
je     1290 <ex4+0x160>
lea    0xeb0(%rip),%rbp
lea    0x34(%rsp),%r14
lea    0xe65(%rip),%rbx
lea    0xebe(%rip),%r15
lea    0xe63(%rip),%r12
lea    0x4(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    0xe7f(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe8c(%rip),%rdi
lea    0x30(%rsp),%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
lea    0xedb(%rip),%rdi
call   1030 <puts@plt>
lea    0xef6(%rip),%rdi
call   1030 <puts@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
movslq 0x34(%rsp),%rax
movss  (%rbx,%rax,4),%xmm0
mulss  0x30(%rsp),%xmm0
movss  0x18(%rsp,%rax,4),%xmm1
addss  %xmm0,%xmm1
movss  %xmm1,0x18(%rsp,%rax,4)
movss  0x8(%rsp,%rax,4),%xmm1
addss  0xdcc(%rip),%xmm1
movss  %xmm1,0x8(%rsp,%rax,4)
cvtss2sd %xmm0,%xmm0
mov    %r15,%rdi
mov    $0x1,%al
call   1040 <printf@plt>
mov    %r12,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
cmpl   $0x0,0x4(%rsp)
jne    11c0 <ex4+0x90>
movss  0x18(%rsp),%xmm5
movss  0x1c(%rsp),%xmm4
movss  0x8(%rsp),%xmm0
movss  0xc(%rsp),%xmm1
movss  0x20(%rsp),%xmm3
movss  0x10(%rsp),%xmm2
jmp    12a2 <ex4+0x172>
xorps  %xmm2,%xmm2
xorps  %xmm3,%xmm3
xorps  %xmm1,%xmm1
xorps  %xmm4,%xmm4
xorps  %xmm0,%xmm0
xorps  %xmm5,%xmm5
divss  %xmm2,%xmm3
movss  %xmm3,0x2c(%rsp)
divss  %xmm1,%xmm4
movss  %xmm4,0x28(%rsp)
divss  %xmm0,%xmm5
movss  %xmm5,0x24(%rsp)
lea    0xdea(%rip),%rdi
call   1030 <puts@plt>
movss  0x24(%rsp),%xmm0
cvtss2sd %xmm0,%xmm0
movss  0x28(%rsp),%xmm1
cvtss2sd %xmm1,%xmm1
movss  0x2c(%rsp),%xmm2
cvtss2sd %xmm2,%xmm2
lea    0xd89(%rip),%rdi
mov    $0x3,%al
call   1040 <printf@plt>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.ex4.kwh = [3 x float] [float 5.000000e-01, float 0x3FE6666660000000, float 0x3FF3333340000000]
@.str = [51 x i8] c"Informe o numero do consumidor [0 para terminar]: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [27 x i8] c"Informe o consumo em kWh: \00"
@.str.3 = [3 x i8] c"%f\00"
@.str.4 = [40 x i8] c"Informe o tipo do consumidor (codigo):\0A\00"
@.str.5 = [43 x i8] c"\090-Residencial\0A\091-Comercial\0A\092-Industrial\0A\00"
@.str.6 = [22 x i8] c"Custo total: R$%.2f\0A\0A\00"
@.str.7 = [19 x i8] c"Media de consumo:\0A\00"
@.str.8 = [55 x i8] c"\09Residencial:\09%.2f\0A\09Comercial:\09%.2f\0A\09Industrial:\09%.2f\0A\00"
define void @ex4() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [3 x float]
%4 = alloca float
%5 = alloca float
%6 = alloca [3 x float]
%7 = alloca [3 x float]
%8 = bitcast [3 x float]* %3 to i8*
%9 = bitcast [3 x float]* %6 to i8*
%10 = bitcast [3 x float]* %7 to i8*
br label %11
11: ; preds = %0, %17
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
%13 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%14 = load i32, i32* %1
%15 = icmp ne i32 %14, 0
br i1 %15, label %17, label %16
16: ; preds = %11
br label %43
17: ; preds = %11
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), float* %4)
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0))
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0))
%22 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%23 = load i32, i32* %2
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [3 x float], [3 x float]* %3, i64 0, i64 %24
%26 = load float, float* %25
%27 = load float, float* %4
%28 = fmul float %26, %27
store float %28, float* %5
%29 = load float, float* %5
%30 = load i32, i32* %2
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [3 x float], [3 x float]* %6, i64 0, i64 %31
%33 = load float, float* %32
%34 = fadd float %33, %29
store float %34, float* %32
%35 = load i32, i32* %2
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [3 x float], [3 x float]* %7, i64 0, i64 %36
%38 = load float, float* %37
%39 = fadd float %38, 1.000000e+00
store float %39, float* %37
%40 = load float, float* %5
%41 = fpext float %40 to double
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), double %41)
br label %11
43: ; preds = %16
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0))
%45 = getelementptr inbounds [3 x float], [3 x float]* %6, i64 0, i64 0
%46 = load float, float* %45
%47 = getelementptr inbounds [3 x float], [3 x float]* %7, i64 0, i64 0
%48 = load float, float* %47
%49 = fdiv float %46, %48
%50 = fpext float %49 to double
%51 = getelementptr inbounds [3 x float], [3 x float]* %6, i64 0, i64 1
%52 = load float, float* %51
%53 = getelementptr inbounds [3 x float], [3 x float]* %7, i64 0, i64 1
%54 = load float, float* %53
%55 = fdiv float %52, %54
%56 = fpext float %55 to double
%57 = getelementptr inbounds [3 x float], [3 x float]* %6, i64 0, i64 2
%58 = load float, float* %57
%59 = getelementptr inbounds [3 x float], [3 x float]* %7, i64 0, i64 2
%60 = load float, float* %59
%61 = fdiv float %58, %60
%62 = fpext float %61 to double
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.8, i64 0, i64 0), double %50, double %56, double %62)
ret void
}

LLVM IR (-O2):
@__const.ex4.kwh = [3 x float] [float 5.000000e-01, float 0x3FE6666660000000, float 0x3FF3333340000000]
@.str = [51 x i8] c"Informe o numero do consumidor [0 para terminar]: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [27 x i8] c"Informe o consumo em kWh: \00"
@.str.3 = [3 x i8] c"%f\00"
@.str.6 = [22 x i8] c"Custo total: R$%.2f\0A\0A\00"
@.str.8 = [55 x i8] c"\09Residencial:\09%.2f\0A\09Comercial:\09%.2f\0A\09Industrial:\09%.2f\0A\00"
@str = [18 x i8] c"Media de consumo:\00"
@str.9 = [39 x i8] c"Informe o tipo do consumidor (codigo):\00"
@str.10 = [42 x i8] c"\090-Residencial\0A\091-Comercial\0A\092-Industrial\00"
define void @ex4() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca float
%4 = alloca [3 x float]
%5 = alloca [3 x float]
%6 = bitcast i32* %1 to i8*
%7 = bitcast i32* %2 to i8*
%8 = bitcast float* %3 to i8*
%9 = bitcast [3 x float]* %4 to i8*
%10 = bitcast [3 x float]* %5 to i8*
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
%12 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%13 = load i32, i32* %1
%14 = icmp eq i32 %13, 0
br i1 %14, label %52, label %15
15: ; preds = %0, %15
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), float* %3)
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.9, i64 0, i64 0))
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str.10, i64 0, i64 0))
%20 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%21 = load i32, i32* %2
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [3 x float], [3 x float]* @__const.ex4.kwh, i64 0, i64 %22
%24 = load float, float* %23
%25 = load float, float* %3
%26 = fmul float %24, %25
%27 = getelementptr inbounds [3 x float], [3 x float]* %4, i64 0, i64 %22
%28 = load float, float* %27
%29 = fadd float %28, %26
store float %29, float* %27
%30 = getelementptr inbounds [3 x float], [3 x float]* %5, i64 0, i64 %22
%31 = load float, float* %30
%32 = fadd float %31, 1.000000e+00
store float %32, float* %30
%33 = fpext float %26 to double
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), double %33)
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
%36 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%37 = load i32, i32* %1
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %15
39: ; preds = %15
%40 = getelementptr inbounds [3 x float], [3 x float]* %4, i64 0, i64 0
%41 = load float, float* %40
%42 = getelementptr inbounds [3 x float], [3 x float]* %5, i64 0, i64 0
%43 = load float, float* %42
%44 = getelementptr inbounds [3 x float], [3 x float]* %4, i64 0, i64 1
%45 = load float, float* %44
%46 = getelementptr inbounds [3 x float], [3 x float]* %5, i64 0, i64 1
%47 = load float, float* %46
%48 = getelementptr inbounds [3 x float], [3 x float]* %4, i64 0, i64 2
%49 = load float, float* %48
%50 = getelementptr inbounds [3 x float], [3 x float]* %5, i64 0, i64 2
%51 = load float, float* %50
br label %52
52: ; preds = %39, %0
%53 = phi float [ %51, %39 ], [ 0.000000e+00, %0 ]
%54 = phi float [ %49, %39 ], [ 0.000000e+00, %0 ]
%55 = phi float [ %47, %39 ], [ 0.000000e+00, %0 ]
%56 = phi float [ %45, %39 ], [ 0.000000e+00, %0 ]
%57 = phi float [ %43, %39 ], [ 0.000000e+00, %0 ]
%58 = phi float [ %41, %39 ], [ 0.000000e+00, %0 ]
%59 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%60 = fdiv float %58, %57
%61 = fpext float %60 to double
%62 = fdiv float %56, %55
%63 = fpext float %62 to double
%64 = fdiv float %54, %53
%65 = fpext float %64 to double
%66 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @.str.8, i64 0, i64 0), double %61, double %63, double %65)
ret void
}