#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 scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ dprintf(int, char *, ...);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ snprintf(char *, size_t, char *, char const *);
void copy_cmdarg(char *dest, const char *src, size_t destlen,
                 const char *argname) {
  ssize_t olen = snprintf(dest, destlen, "%s", src);
  if (olen < 0)
    do {
      dprintf(2, "snprintf failed on %s; your system is broken?", argname);
      dprintf(2, "\n");
      exit(EXIT_FAILURE);
    } while (0);
  if ((size_t)olen >= destlen)
    do {
      dprintf(2, "snprintf would truncate %s arg; it's too long", argname);
      dprintf(2, "\n");
      exit(EXIT_FAILURE);
    } while (0);
}


x86 assembly (clang, -O0):
<copy_cmdarg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rcx
lea    0xea5(%rip),%rdx
call   1030 <snprintf@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    11ac <copy_cmdarg+0x7c>
jmp    1174 <copy_cmdarg+0x44>
mov    -0x20(%rbp),%rdx
mov    $0x2,%edi
lea    0xe7f(%rip),%rsi
mov    $0x0,%al
call   1040 <dprintf@plt>
mov    $0x2,%edi
lea    0xe9a(%rip),%rsi
mov    $0x0,%al
call   1040 <dprintf@plt>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     11f7 <copy_cmdarg+0xc7>
jmp    11bf <copy_cmdarg+0x8f>
mov    -0x20(%rbp),%rdx
mov    $0x2,%edi
lea    0xe64(%rip),%rsi
mov    $0x0,%al
call   1040 <dprintf@plt>
mov    $0x2,%edi
lea    0xe4f(%rip),%rsi
mov    $0x0,%al
call   1040 <dprintf@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_cmdarg>:
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%rcx
lea    0xebc(%rip),%rdx
mov    %rbx,%rsi
call   1030 <snprintf@plt>
test   %rax,%rax
js     115e <copy_cmdarg+0x2e>
cmp    %rbx,%rax
jae    1167 <copy_cmdarg+0x37>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe9e(%rip),%rsi
jmp    116e <copy_cmdarg+0x3e>
lea    0xec5(%rip),%rsi
mov    $0x2,%edi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <dprintf@plt>
lea    0xead(%rip),%rsi
mov    $0x2,%edi
xor    %eax,%eax
call   1040 <dprintf@plt>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@.str.1 = [46 x i8] c"snprintf failed on %s; your system is broken?\00"
@.str.2 = [2 x i8] c"\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [46 x i8] c"snprintf would truncate %s arg; it's too long\00"
define void @copy_cmdarg(i8* %0, i8* %1, i64 %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i8* %3, i8** %8
%10 = load i8*, i8** %5
%11 = load i64, i64* %7
%12 = load i8*, i8** %6
%13 = call i64 @snprintf(i8* %10, i64 %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %12)
store i64 %13, i64* %9
%14 = load i64, i64* %9
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %4
br label %17
17: ; preds = %16
%18 = load i8*, i8** %8
%19 = call i32 (i32, i8*, ...) @dprintf(i32 2, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = call i32 (i32, i8*, ...) @dprintf(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; No predecessors!
br label %24
24: ; preds = %23, %4
%25 = load i64, i64* %9
%26 = load i64, i64* %7
%27 = icmp uge i64 %25, %26
br i1 %27, label %28, label %36
28: ; preds = %24
br label %29
29: ; preds = %28
%30 = load i8*, i8** %8
%31 = call i32 (i32, i8*, ...) @dprintf(i32 2, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i8* %30)
%32 = call i32 (i32, i8*, ...) @dprintf(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; No predecessors!
br label %36
36: ; preds = %35, %24
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@.str.1 = [46 x i8] c"snprintf failed on %s; your system is broken?\00"
@.str.2 = [2 x i8] c"\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [46 x i8] c"snprintf would truncate %s arg; it's too long\00"
define void @copy_cmdarg(i8* %0, i8* %1, i64 %2, i8* %3) {
%5 = tail call i64 @snprintf(i8* %0, i64 %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %1)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %4
%8 = tail call i32 (i32, i8*, ...) @dprintf(i32 2, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%9 = tail call i32 (i32, i8*, ...) @dprintf(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %4
%13 = icmp ult i64 %5, %2
br i1 %13, label %19, label %14
14: ; preds = %12
%15 = tail call i32 (i32, i8*, ...) @dprintf(i32 2, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i8* %3)
%16 = tail call i32 (i32, i8*, ...) @dprintf(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %12
ret void
}