#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 */
scalar_t__ EINVAL;
scalar_t__ ERANGE;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int setup_socket();
int /*<<< orphan*/ stderr;
size_t strlen(char const *);
long strtol(char *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ usage();
int /*<<< orphan*/ write(int, char *, size_t);
int main(int argc, char *argv[]) {
  int sfd;
  long perc;
  char buf[63 + 1];
  size_t len;
  const char *msg;
  if (argc == 3) {
    msg = argv[2];
    len = strlen(msg);
  } else if (argc == 2) {
    msg = NULL;
    len = 0;
  } else {
    usage();
    return 1;
  }
  errno = 0;
  perc = strtol(argv[1], NULL, 10);
  if (errno == EINVAL || errno == ERANGE || perc < 0 || perc > 100) {
    fprintf(stderr, "Invalid value. Percentage is a value"
                    "between 0 and 100\n");
    return 1;
  }
  buf[0] = (char)(unsigned char)perc;
  if (len > 63 - 1) {
    fprintf(stderr, "Max length is %u. Message will be truncated", 63 - 1);
    len = 63 - 1;
  }
  if (msg)
    memcpy(&buf[1], msg, len);
  buf[len + 1] = '\0';
  sfd = setup_socket();
  if (sfd == -1) {
    fprintf(stderr, "Could not connect to dietsplash on private socket\n");
    return 1;
  }
  write(sfd, buf, len + 2);
  close(sfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jne    11be <main+0x3e>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x70(%rbp)
mov    -0x70(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x68(%rbp)
jmp    11f5 <main+0x75>
cmpl   $0x2,-0x8(%rbp)
jne    11dd <main+0x5d>
movq   $0x0,-0x70(%rbp)
movq   $0x0,-0x68(%rbp)
jmp    11f0 <main+0x70>
mov    $0x0,%al
call   10a0 <usage@plt>
movl   $0x1,-0x4(%rbp)
jmp    1343 <main+0x1c3>
jmp    11f5 <main+0x75>
mov    0x2dd4(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %rax,-0x20(%rbp)
mov    0x2dac(%rip),%rax
mov    (%rax),%rax
mov    0x2d9a(%rip),%rcx
cmp    (%rcx),%rax
je     1267 <main+0xe7>
mov    0x2d92(%rip),%rax
mov    (%rax),%rax
mov    0x2da0(%rip),%rcx
cmp    (%rcx),%rax
je     1267 <main+0xe7>
cmpq   $0x0,-0x20(%rbp)
jl     1267 <main+0xe7>
cmpq   $0x64,-0x20(%rbp)
jle    128a <main+0x10a>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
lea    0xd89(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1343 <main+0x1c3>
mov    -0x20(%rbp),%rax
mov    %al,-0x60(%rbp)
cmpq   $0x3e,-0x68(%rbp)
jbe    12c0 <main+0x140>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
lea    0xd8b(%rip),%rsi
mov    $0x3e,%edx
mov    $0x0,%al
call   1070 <fprintf@plt>
movq   $0x3e,-0x68(%rbp)
cmpq   $0x0,-0x70(%rbp)
je     12e0 <main+0x160>
lea    -0x60(%rbp),%rdi
add    $0x1,%rdi
mov    -0x70(%rbp),%rsi
mov    -0x68(%rbp),%rdx
call   1090 <memcpy@plt>
mov    -0x68(%rbp),%rax
movb   $0x0,-0x5f(%rbp,%rax,1)
mov    $0x0,%al
call   1060 <setup_socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1320 <main+0x1a0>
mov    0x2cd4(%rip),%rax
mov    (%rax),%edi
lea    0xd56(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1343 <main+0x1c3>
mov    -0x14(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    -0x68(%rbp),%rdx
add    $0x2,%rdx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %rsi,%rbp
cmp    $0x2,%edi
je     11a8 <main+0x28>
cmp    $0x3,%edi
jne    11fc <main+0x7c>
mov    0x10(%rbp),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
jmp    11ae <main+0x2e>
xor    %r15d,%r15d
xor    %r14d,%r14d
mov    0x2e1b(%rip),%rbx
movq   $0x0,(%rbx)
mov    0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    (%rbx),%rcx
mov    0x2df2(%rip),%rdx
cmp    (%rdx),%rcx
jne    120d <main+0x8d>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
lea    0xe15(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%ebp
jmp    1295 <main+0x115>
xor    %eax,%eax
call   10a0 <usage@plt>
mov    $0x1,%ebp
jmp    1295 <main+0x115>
mov    0x2dd4(%rip),%rdx
cmp    (%rdx),%rcx
je     11db <main+0x5b>
test   %rax,%rax
js     11db <main+0x5b>
cmp    $0x65,%rax
jge    11db <main+0x5b>
mov    %al,(%rsp)
cmp    $0x3f,%r15
jb     124f <main+0xcf>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
lea    0xdfa(%rip),%rsi
mov    $0x3e,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x3e,%r15d
test   %r14,%r14
je     1264 <main+0xe4>
lea    0x1(%rsp),%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1090 <memcpy@plt>
mov    %rsp,%rax
movb   $0x0,0x1(%r15,%rax,1)
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <setup_socket@plt>
cmp    $0xffffffff,%eax
je     12a2 <main+0x122>
mov    %eax,%ebx
add    $0x2,%r15
mov    %rsp,%rsi
mov    %eax,%edi
mov    %r15,%rdx
call   1030 <write@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
lea    0xdb1(%rip),%rsi
jmp    11eb <main+0x6b>

LLVM IR (-O0):
@errno = global i64 0
@EINVAL = global i64 0
@ERANGE = global i64 0
@stderr = global i32 0
@.str = [55 x i8] c"Invalid value. Percentage is a valuebetween 0 and 100\0A\00"
@.str.1 = [44 x i8] c"Max length is %u. Message will be truncated\00"
@.str.2 = [51 x i8] c"Could not connect to dietsplash on private socket\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca [64 x i8]
%9 = alloca i64
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, 3
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 2
%16 = load i8*, i8** %15
store i8* %16, i8** %10
%17 = load i8*, i8** %10
%18 = call i64 @strlen(i8* %17)
store i64 %18, i64* %9
br label %26
19: ; preds = %2
%20 = load i32, i32* %4
%21 = icmp eq i32 %20, 2
br i1 %21, label %22, label %23
22: ; preds = %19
store i8* null, i8** %10
store i64 0, i64* %9
br label %25
23: ; preds = %19
%24 = call i32 (...) @usage()
store i32 1, i32* %3
br label %82
25: ; preds = %22
br label %26
26: ; preds = %25, %13
store i64 0, i64* @errno
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i64 @strtol(i8* %29, i32* null, i32 10)
store i64 %30, i64* %7
%31 = load i64, i64* @errno
%32 = load i64, i64* @EINVAL
%33 = icmp eq i64 %31, %32
br i1 %33, label %44, label %34
34: ; preds = %26
%35 = load i64, i64* @errno
%36 = load i64, i64* @ERANGE
%37 = icmp eq i64 %35, %36
br i1 %37, label %44, label %38
38: ; preds = %34
%39 = load i64, i64* %7
%40 = icmp slt i64 %39, 0
br i1 %40, label %44, label %41
41: ; preds = %38
%42 = load i64, i64* %7
%43 = icmp sgt i64 %42, 100
br i1 %43, label %44, label %47
44: ; preds = %41, %38, %34, %26
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %82
47: ; preds = %41
%48 = load i64, i64* %7
%49 = trunc i64 %48 to i8
%50 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
store i8 %49, i8* %50
%51 = load i64, i64* %9
%52 = icmp ugt i64 %51, 62
br i1 %52, label %53, label %56
53: ; preds = %47
%54 = load i32, i32* @stderr
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 62)
store i64 62, i64* %9
br label %56
56: ; preds = %53, %47
%57 = load i8*, i8** %10
%58 = icmp ne i8* %57, null
br i1 %58, label %59, label %64
59: ; preds = %56
%60 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 1
%61 = load i8*, i8** %10
%62 = load i64, i64* %9
%63 = call i32 @memcpy(i8* %60, i8* %61, i64 %62)
br label %64
64: ; preds = %59, %56
%65 = load i64, i64* %9
%66 = add i64 %65, 1
%67 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 %66
store i8 0, i8* %67
%68 = call i32 (...) @setup_socket()
store i32 %68, i32* %6
%69 = load i32, i32* %6
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %74
71: ; preds = %64
%72 = load i32, i32* @stderr
%73 = call i32 (i32, i8*, ...) @fprintf(i32 %72, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %82
74: ; preds = %64
%75 = load i32, i32* %6
%76 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%77 = load i64, i64* %9
%78 = add i64 %77, 2
%79 = call i32 @write(i32 %75, i8* %76, i64 %78)
%80 = load i32, i32* %6
%81 = call i32 @close(i32 %80)
store i32 0, i32* %3
br label %82
82: ; preds = %74, %71, %44, %23
%83 = load i32, i32* %3
ret i32 %83
}

LLVM IR (-O2):
@errno = global i64 0
@EINVAL = global i64 0
@ERANGE = global i64 0
@stderr = global i32 0
@.str = [55 x i8] c"Invalid value. Percentage is a valuebetween 0 and 100\0A\00"
@.str.1 = [44 x i8] c"Max length is %u. Message will be truncated\00"
@.str.2 = [51 x i8] c"Could not connect to dietsplash on private socket\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
switch i32 %0, label %9 [
i32 3, label %5
i32 2, label %11
]
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 2
%7 = load i8*, i8** %6
%8 = tail call i64 @strlen(i8* dereferenceable(1) %7)
br label %11
9: ; preds = %2
%10 = tail call i32 (...) @usage()
br label %54
11: ; preds = %2, %5
%12 = phi i64 [ %8, %5 ], [ 0, %2 ]
%13 = phi i8* [ %7, %5 ], [ null, %2 ]
store i64 0, i64* @errno
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i64 @strtol(i8* nocapture %15, i32* null, i32 10)
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINVAL
%19 = icmp eq i64 %17, %18
br i1 %19, label %27, label %20
20: ; preds = %11
%21 = load i64, i64* @ERANGE
%22 = icmp eq i64 %17, %21
%23 = icmp slt i64 %16, 0
%24 = select i1 %22, i1 true, i1 %23
%25 = icmp sgt i64 %16, 100
%26 = select i1 %24, i1 true, i1 %25
br i1 %26, label %27, label %30
27: ; preds = %20, %11
%28 = load i32, i32* @stderr
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0))
br label %54
30: ; preds = %20
%31 = trunc i64 %16 to i8
store i8 %31, i8* %4
%32 = icmp ugt i64 %12, 62
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = load i32, i32* @stderr
%35 = tail call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 62)
br label %36
36: ; preds = %33, %30
%37 = phi i64 [ 62, %33 ], [ %12, %30 ]
%38 = icmp eq i8* %13, null
br i1 %38, label %42, label %39
39: ; preds = %36
%40 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 1
%41 = call i32 @memcpy(i8* %40, i8* %13, i64 %37)
br label %42
42: ; preds = %39, %36
%43 = add nuw nsw i64 %37, 1
%44 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 %43
store i8 0, i8* %44
%45 = call i32 (...) @setup_socket()
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %50
47: ; preds = %42
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0))
br label %54
50: ; preds = %42
%51 = add nuw nsw i64 %37, 2
%52 = call i32 @write(i32 %45, i8* %4, i64 %51)
%53 = call i32 @close(i32 %45)
br label %54
54: ; preds = %50, %47, %27, %9
%55 = phi i32 [ 1, %27 ], [ 1, %47 ], [ 0, %50 ], [ 1, %9 ]
ret i32 %55
}