#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 union id3_field {
  int dummy;
} id3_field;
struct id3_tag {
  int dummy;
};
struct id3_frame {
  int dummy;
};

/* Variables and functions */
int id3_field_setint(union id3_field *, char);
union id3_field *id3_frame_field(struct id3_frame *, int);
struct id3_frame *id3_frame_new(char *);
int /*<<< orphan*/ id3_tag_attachframe(struct id3_tag *, struct id3_frame *);
struct id3_frame *id3_tag_findframe(struct id3_tag *, char *,
                                    int /*<<< orphan*/);
int id3_metadata_setrating(struct id3_tag *tag, char value) {
  union id3_field *field;
  struct id3_frame *frame;
  char popm[] = {3, 53, 104, 154, 205, 255};
  if (value < '0' || value > '5')
    return -1;
  frame = id3_tag_findframe(tag, "POPM", 0);
  if (frame == 0) {
    frame = id3_frame_new("POPM");
    id3_tag_attachframe(tag, frame);
  }
  field = id3_frame_field(frame, 1);
  if (field == 0)
    return 0;
  return id3_field_setint(field, popm[value - '0']);
}


x86 assembly (clang, -O0):
<id3_metadata_setrating>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %sil,%al
mov    %rdi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    0xe98(%rip),%eax
mov    %eax,-0x2e(%rbp)
mov    0xe92(%rip),%ax
mov    %ax,-0x2a(%rbp)
movsbl -0x11(%rbp),%eax
cmp    $0x30,%eax
jl     1190 <id3_metadata_setrating+0x40>
movsbl -0x11(%rbp),%eax
cmp    $0x35,%eax
jle    119c <id3_metadata_setrating+0x4c>
movl   $0xffffffff,-0x4(%rbp)
jmp    121d <id3_metadata_setrating+0xcd>
mov    -0x10(%rbp),%rdi
lea    0xe5f(%rip),%rsi
xor    %edx,%edx
call   1040 <id3_tag_findframe@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11da <id3_metadata_setrating+0x8a>
lea    0xe42(%rip),%rdi
call   1030 <id3_frame_new@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1060 <id3_tag_attachframe@plt>
mov    -0x28(%rbp),%rdi
mov    $0x1,%esi
call   1050 <id3_frame_field@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1203 <id3_metadata_setrating+0xb3>
movl   $0x0,-0x4(%rbp)
jmp    121d <id3_metadata_setrating+0xcd>
mov    -0x20(%rbp),%rdi
movsbl -0x11(%rbp),%eax
sub    $0x30,%eax
cltq
movsbl -0x2e(%rbp,%rax,1),%esi
call   1070 <id3_field_setint@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<id3_metadata_setrating>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
lea    -0x36(%rbx),%eax
mov    $0xffffffff,%r15d
cmp    $0xfa,%al
jb     11ce <id3_metadata_setrating+0x7e>
mov    %rdi,%r14
lea    0xe96(%rip),%rsi
xor    %r15d,%r15d
xor    %edx,%edx
call   1040 <id3_tag_findframe@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    119c <id3_metadata_setrating+0x4c>
lea    0xe7d(%rip),%rdi
call   1030 <id3_frame_new@plt>
mov    %rax,%rbp
mov    %r14,%rdi
mov    %rax,%rsi
call   1060 <id3_tag_attachframe@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
call   1050 <id3_frame_field@plt>
test   %rax,%rax
je     11ce <id3_metadata_setrating+0x7e>
add    $0xffffffd0,%ebx
lea    0xe48(%rip),%rcx
movsbl (%rbx,%rcx,1),%esi
mov    %rax,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <id3_field_setint@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.id3_tag = type { i32 }
%union.id3_field = type { i32 }
%struct.id3_frame = type { i32 }
@__const.id3_metadata_setrating.popm = [6 x i8] c"\035h\9A\CD\FF"
@.str = [5 x i8] c"POPM\00"
define i32 @id3_metadata_setrating(%struct.id3_tag* %0, i8 signext %1) {
%3 = alloca i32
%4 = alloca %struct.id3_tag*
%5 = alloca i8
%6 = alloca %union.id3_field*
%7 = alloca %struct.id3_frame*
%8 = alloca [6 x i8]
store %struct.id3_tag* %0, %struct.id3_tag** %4
store i8 %1, i8* %5
%9 = bitcast [6 x i8]* %8 to i8*
%10 = load i8, i8* %5
%11 = sext i8 %10 to i32
%12 = icmp slt i32 %11, 48
br i1 %12, label %17, label %13
13: ; preds = %2
%14 = load i8, i8* %5
%15 = sext i8 %14 to i32
%16 = icmp sgt i32 %15, 53
br i1 %16, label %17, label %18
17: ; preds = %13, %2
store i32 -1, i32* %3
br label %43
18: ; preds = %13
%19 = load %struct.id3_tag*, %struct.id3_tag** %4
%20 = call %struct.id3_frame* @id3_tag_findframe(%struct.id3_tag* %19, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 0)
store %struct.id3_frame* %20, %struct.id3_frame** %7
%21 = load %struct.id3_frame*, %struct.id3_frame** %7
%22 = icmp eq %struct.id3_frame* %21, null
br i1 %22, label %23, label %28
23: ; preds = %18
%24 = call %struct.id3_frame* @id3_frame_new(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store %struct.id3_frame* %24, %struct.id3_frame** %7
%25 = load %struct.id3_tag*, %struct.id3_tag** %4
%26 = load %struct.id3_frame*, %struct.id3_frame** %7
%27 = call i32 @id3_tag_attachframe(%struct.id3_tag* %25, %struct.id3_frame* %26)
br label %28
28: ; preds = %23, %18
%29 = load %struct.id3_frame*, %struct.id3_frame** %7
%30 = call %union.id3_field* @id3_frame_field(%struct.id3_frame* %29, i32 1)
store %union.id3_field* %30, %union.id3_field** %6
%31 = load %union.id3_field*, %union.id3_field** %6
%32 = icmp eq %union.id3_field* %31, null
br i1 %32, label %33, label %34
33: ; preds = %28
store i32 0, i32* %3
br label %43
34: ; preds = %28
%35 = load %union.id3_field*, %union.id3_field** %6
%36 = load i8, i8* %5
%37 = sext i8 %36 to i32
%38 = sub nsw i32 %37, 48
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [6 x i8], [6 x i8]* %8, i64 0, i64 %39
%41 = load i8, i8* %40
%42 = call i32 @id3_field_setint(%union.id3_field* %35, i8 signext %41)
store i32 %42, i32* %3
br label %43
43: ; preds = %34, %33, %17
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
%struct.id3_tag = type { i32 }
%struct.id3_frame = type { i32 }
%union.id3_field = type { i32 }
@__const.id3_metadata_setrating.popm = [6 x i8] c"\035h\9A\CD\FF"
@.str = [5 x i8] c"POPM\00"
define i32 @id3_metadata_setrating(%struct.id3_tag* %0, i8 signext %1) {
%3 = sext i8 %1 to i64
%4 = add i8 %1, -54
%5 = icmp ult i8 %4, -6
br i1 %5, label %22, label %6
6: ; preds = %2
%7 = tail call %struct.id3_frame* @id3_tag_findframe(%struct.id3_tag* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 0)
%8 = icmp eq %struct.id3_frame* %7, null
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = tail call %struct.id3_frame* @id3_frame_new(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @id3_tag_attachframe(%struct.id3_tag* %0, %struct.id3_frame* %10)
br label %12
12: ; preds = %9, %6
%13 = phi %struct.id3_frame* [ %10, %9 ], [ %7, %6 ]
%14 = tail call %union.id3_field* @id3_frame_field(%struct.id3_frame* %13, i32 1)
%15 = icmp eq %union.id3_field* %14, null
br i1 %15, label %22, label %16
16: ; preds = %12
%17 = add nsw i64 %3, 4294967248
%18 = and i64 %17, 4294967295
%19 = getelementptr inbounds [6 x i8], [6 x i8]* @__const.id3_metadata_setrating.popm, i64 0, i64 %18
%20 = load i8, i8* %19
%21 = tail call i32 @id3_field_setint(%union.id3_field* %14, i8 signext %20)
br label %22
22: ; preds = %12, %2, %16
%23 = phi i32 [ %21, %16 ], [ -1, %2 ], [ 0, %12 ]
ret i32 %23
}