#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 int /*<<< orphan*/ cmd;

/* Variables and functions */
scalar_t__ send_command_to_variable_server(int, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int strlen(char const *);
int /*<<< orphan*/ strncat(char *, char const *, int);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int set_copy_mode(int socket, int mode) {
  char cmd[512];
  const char *prefix = "trick.var_set_copy_mode(";
  char mod[4];
  const char *suffix = ")";
  snprintf(mod, 4, "%i", mode);
  if (sizeof(cmd) < (strlen(prefix) + strlen(mod) + strlen(suffix) + 1)) {
    return -1;
  } else {
    strncpy(cmd, prefix, sizeof(cmd));
    strncat(cmd, mod, (sizeof(cmd) - strlen(cmd) - 1));
    strncat(cmd, suffix, (sizeof(cmd) - strlen(cmd) - 1));
    if (send_command_to_variable_server(socket, cmd) < 0) {
      return -1;
    } else {
      return 0;
    }
  }
}


x86 assembly (clang, -O0):
<set_copy_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    0xe98(%rip),%rax
mov    %rax,-0x218(%rbp)
lea    0xea3(%rip),%rax
mov    %rax,-0x228(%rbp)
lea    -0x21c(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x4,%esi
lea    0xe88(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x218(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x230(%rbp)
lea    -0x21c(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x230(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x22c(%rbp)
mov    -0x228(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x22c(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
movslq %eax,%rcx
mov    $0x200,%eax
cmp    %rcx,%rax
jae    11fc <set_copy_mode+0xac>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d8 <set_copy_mode+0x188>
lea    -0x210(%rbp),%rdi
mov    -0x218(%rbp),%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
lea    -0x210(%rbp),%rax
mov    %rax,-0x250(%rbp)
lea    -0x21c(%rbp),%rax
mov    %rax,-0x248(%rbp)
lea    -0x210(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x250(%rbp),%rdi
mov    -0x248(%rbp),%rsi
movslq %eax,%rcx
mov    $0x200,%eax
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%edx
call   1060 <strncat@plt>
lea    -0x210(%rbp),%rax
mov    %rax,-0x240(%rbp)
mov    -0x228(%rbp),%rax
mov    %rax,-0x238(%rbp)
lea    -0x210(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x240(%rbp),%rdi
mov    -0x238(%rbp),%rsi
movslq %eax,%rcx
mov    $0x200,%eax
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%edx
call   1060 <strncat@plt>
mov    -0x8(%rbp),%edi
lea    -0x210(%rbp),%rsi
call   1070 <send_command_to_variable_server@plt>
cmp    $0x0,%rax
jge    12d1 <set_copy_mode+0x181>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d8 <set_copy_mode+0x188>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_copy_mode>:
push   %rbp
push   %r14
push   %rbx
sub    $0x210,%rsp
mov    %esi,%ecx
mov    %edi,%r14d
lea    0xeb4(%rip),%rdx
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x4,%esi
call   1050 <snprintf@plt>
lea    0xe80(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
lea    0xe81(%rip),%rdi
call   1040 <strlen@plt>
add    %ebp,%ebx
lea    (%rax,%rbx,1),%ecx
add    $0xfffffe00,%ecx
mov    $0xffffffff,%eax
cmp    $0xfffffdff,%ecx
jb     1219 <set_copy_mode+0xc9>
lea    0xe44(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x200,%edx
call   1030 <strncpy@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    $0x1ff,%ebp
mov    $0x1ff,%edx
sub    %eax,%edx
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
call   1060 <strncat@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
sub    %eax,%ebp
lea    0xe19(%rip),%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
call   1060 <strncat@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1070 <send_command_to_variable_server@plt>
sar    $0x3f,%rax
add    $0x210,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"trick.var_set_copy_mode(\00"
@.str.1 = [2 x i8] c")\00"
@.str.2 = [3 x i8] c"%i\00"
define i32 @set_copy_mode(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [512 x i8]
%7 = alloca i8*
%8 = alloca [4 x i8]
%9 = alloca i8*
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8** %7
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %9
%10 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 0
%11 = load i32, i32* %5
%12 = call i32 @snprintf(i8* %10, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %11)
%13 = load i8*, i8** %7
%14 = call i32 @strlen(i8* %13)
%15 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 0
%16 = call i32 @strlen(i8* %15)
%17 = add nsw i32 %14, %16
%18 = load i8*, i8** %9
%19 = call i32 @strlen(i8* %18)
%20 = add nsw i32 %17, %19
%21 = add nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = icmp ult i64 512, %22
br i1 %23, label %24, label %25
24: ; preds = %2
store i32 -1, i32* %3
br label %53
25: ; preds = %2
%26 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%27 = load i8*, i8** %7
%28 = call i32 @strncpy(i8* %26, i8* %27, i32 512)
%29 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%30 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 0
%31 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%32 = call i32 @strlen(i8* %31)
%33 = sext i32 %32 to i64
%34 = sub i64 512, %33
%35 = sub i64 %34, 1
%36 = trunc i64 %35 to i32
%37 = call i32 @strncat(i8* %29, i8* %30, i32 %36)
%38 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%39 = load i8*, i8** %9
%40 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%41 = call i32 @strlen(i8* %40)
%42 = sext i32 %41 to i64
%43 = sub i64 512, %42
%44 = sub i64 %43, 1
%45 = trunc i64 %44 to i32
%46 = call i32 @strncat(i8* %38, i8* %39, i32 %45)
%47 = load i32, i32* %4
%48 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%49 = call i64 @send_command_to_variable_server(i32 %47, i8* %48)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %25
store i32 -1, i32* %3
br label %53
52: ; preds = %25
store i32 0, i32* %3
br label %53
53: ; preds = %52, %51, %24
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
@.str = [25 x i8] c"trick.var_set_copy_mode(\00"
@.str.1 = [2 x i8] c")\00"
@.str.2 = [3 x i8] c"%i\00"
define i32 @set_copy_mode(i32 %0, i32 %1) {
%3 = alloca [512 x i8]
%4 = alloca [4 x i8]
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%7 = call i32 @snprintf(i8* %6, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %1)
%8 = call i32 @strlen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @strlen(i8* %6)
%10 = call i32 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = add i32 %8, -512
%12 = add i32 %11, %9
%13 = add i32 %12, %10
%14 = icmp ult i32 %13, -513
br i1 %14, label %26, label %15
15: ; preds = %2
%16 = call i32 @strncpy(i8* %5, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 512)
%17 = call i32 @strlen(i8* %5)
%18 = sub i32 511, %17
%19 = call i32 @strncat(i8* %5, i8* %6, i32 %18)
%20 = call i32 @strlen(i8* %5)
%21 = sub i32 511, %20
%22 = call i32 @strncat(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = call i64 @send_command_to_variable_server(i32 %0, i8* %5)
%24 = ashr i64 %23, 63
%25 = trunc i64 %24 to i32
br label %26
26: ; preds = %15, %2
%27 = phi i32 [ -1, %2 ], [ %25, %15 ]
ret i32 %27
}