#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*/ EXIT_FAILURE;
int clamp(long, int /*<<< orphan*/, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ stderr;
long strtol(char const *, char **, int);
int parse_input(const char *input, int current_brightness, int max_brightness) {
  char *end;
  long value = strtol(input, &end, 10);
  if (*end != '\0') {
    if (*end == '%' && *(end + 1) == '\0') {
      value = max_brightness * value / 100;
    } else {
      fprintf(stderr, "Failed to parse input '%s'\n", input);
      exit(EXIT_FAILURE);
    }
  }
  if (input[0] == '+' || input[0] == '-') {
    return clamp(current_brightness + value, 0, max_brightness);
  } else {
    return clamp(value, 0, max_brightness);
  }
}


x86 assembly (clang, -O0):
<parse_input>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11e1 <parse_input+0xa1>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x25,%eax
jne    11b5 <parse_input+0x75>
mov    -0x20(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
jne    11b5 <parse_input+0x75>
movslq -0x18(%rbp),%rax
imul   -0x28(%rbp),%rax
mov    $0x64,%ecx
cqto
idiv   %rcx
mov    %rax,-0x28(%rbp)
jmp    11dc <parse_input+0x9c>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe37(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
jmp    11e1 <parse_input+0xa1>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2b,%eax
je     1201 <parse_input+0xc1>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2d,%eax
jne    121b <parse_input+0xdb>
movslq -0x14(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%edx
xor    %esi,%esi
call   1030 <clamp@plt>
mov    %eax,-0x4(%rbp)
jmp    122c <parse_input+0xec>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%edx
xor    %esi,%esi
call   1030 <clamp@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_input>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebx
mov    %esi,%r14d
mov    %rdi,%rbp
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %rax,%rdi
mov    0x8(%rsp),%rax
mov    (%rax),%cl
test   %cl,%cl
je     11a3 <parse_input+0x63>
cmp    $0x25,%cl
jne    11c5 <parse_input+0x85>
cmpb   $0x0,0x1(%rax)
jne    11c5 <parse_input+0x85>
movslq %ebx,%rax
imul   %rax,%rdi
movabs $0xa3d70a3d70a3d70b,%rcx
0a d7 a3
mov    %rdi,%rax
imul   %rcx
add    %rdi,%rdx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x6,%rdx
add    %rax,%rdx
mov    %rdx,%rdi
mov    0x0(%rbp),%al
cmp    $0x2d,%al
je     11ae <parse_input+0x6e>
cmp    $0x2b,%al
jne    11b4 <parse_input+0x74>
movslq %r14d,%rax
add    %rax,%rdi
xor    %esi,%esi
mov    %ebx,%edx
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <clamp@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
mov    %rbp,%rdx
call   1040 <fprintf@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [28 x i8] c"Failed to parse input '%s'\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @parse_input(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = call i64 @strtol(i8* %10, i8** %8, i32 10)
store i64 %11, i64* %9
%12 = load i8*, i8** %8
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %40
16: ; preds = %3
%17 = load i8*, i8** %8
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 37
br i1 %20, label %21, label %33
21: ; preds = %16
%22 = load i8*, i8** %8
%23 = getelementptr inbounds i8, i8* %22, i64 1
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %21
%28 = load i32, i32* %7
%29 = sext i32 %28 to i64
%30 = load i64, i64* %9
%31 = mul nsw i64 %29, %30
%32 = sdiv i64 %31, 100
store i64 %32, i64* %9
br label %39
33: ; preds = %21, %16
%34 = load i32, i32* @stderr
%35 = load i8*, i8** %5
%36 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %35)
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %27
br label %40
40: ; preds = %39, %3
%41 = load i8*, i8** %5
%42 = getelementptr inbounds i8, i8* %41, i64 0
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp eq i32 %44, 43
br i1 %45, label %52, label %46
46: ; preds = %40
%47 = load i8*, i8** %5
%48 = getelementptr inbounds i8, i8* %47, i64 0
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = icmp eq i32 %50, 45
br i1 %51, label %52, label %59
52: ; preds = %46, %40
%53 = load i32, i32* %6
%54 = sext i32 %53 to i64
%55 = load i64, i64* %9
%56 = add nsw i64 %54, %55
%57 = load i32, i32* %7
%58 = call i32 @clamp(i64 %56, i32 0, i32 %57)
store i32 %58, i32* %4
br label %63
59: ; preds = %46
%60 = load i64, i64* %9
%61 = load i32, i32* %7
%62 = call i32 @clamp(i64 %60, i32 0, i32 %61)
store i32 %62, i32* %4
br label %63
63: ; preds = %59, %52
%64 = load i32, i32* %4
ret i32 %64
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"Failed to parse input '%s'\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @parse_input(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
%6 = call i64 @strtol(i8* %0, i8** %4, i32 10)
%7 = load i8*, i8** %4
%8 = load i8, i8* %7
switch i8 %8, label %17 [
i8 0, label %22
i8 37, label %9
]
9: ; preds = %3
%10 = getelementptr inbounds i8, i8* %7, i64 1
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = sext i32 %2 to i64
%15 = mul nsw i64 %6, %14
%16 = sdiv i64 %15, 100
br label %22
17: ; preds = %3, %9
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %0)
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %3, %13
%23 = phi i64 [ %16, %13 ], [ %6, %3 ]
%24 = load i8, i8* %0
switch i8 %24, label %28 [
i8 43, label %25
i8 45, label %25
]
25: ; preds = %22, %22
%26 = sext i32 %1 to i64
%27 = add nsw i64 %23, %26
br label %28
28: ; preds = %22, %25
%29 = phi i64 [ %27, %25 ], [ %23, %22 ]
%30 = tail call i32 @clamp(i64 %29, i32 0, i32 %2)
ret i32 %30
}