#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 */
long long LLONG_MAX;
long long LLONG_MIN;
int /*<<< orphan*/ assert(char *);
scalar_t__ errno;
char *malloc_trap(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, long long);
long long strtoll(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int builtin_sub(char *nam, char **argv, char **stdout) {
  assert(nam);
  assert(argv[0]);
  long long int total = 0;
  for (size_t idx = 1; argv[idx] != 0; idx++) {
    long long int ret = strtoll(argv[idx], NULL, 0);
    if ((ret == LLONG_MIN || ret == LLONG_MAX) && errno) {
      perror("builtin: add");
      return -1;
    }
    if (idx == 1) {
      total = ret;
    } else {
      total -= ret;
    }
  }
  char *str = malloc_trap(50);
  memset(str, 0, 50);
  switch (nam[0]) {
  case 'x':
    snprintf(str, 50, "0x%llx", total);
    break;
  case 'X':
    snprintf(str, 50, "0x%llX", total);
    break;
  case 'o':
    snprintf(str, 50, "0%llo", total);
    break;
  default:
    snprintf(str, 50, "%lld", total);
    break;
  }
  if (stdout) {
    *stdout = str;
  } else {
    printf("%s\n", str);
  }
  return 0;
}


x86 assembly (clang, -O0):
<builtin_sub>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <assert@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1080 <assert@plt>
movq   $0x0,-0x28(%rbp)
movq   $0x1,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     1265 <builtin_sub+0xf5>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <strtoll@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    0x2df6(%rip),%rcx
cmp    (%rcx),%rax
je     11ff <builtin_sub+0x8f>
mov    -0x38(%rbp),%rax
mov    0x2df2(%rip),%rcx
cmp    (%rcx),%rax
jne    1228 <builtin_sub+0xb8>
mov    0x2dca(%rip),%rax
cmpq   $0x0,(%rax)
je     1228 <builtin_sub+0xb8>
lea    0xde9(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1363 <builtin_sub+0x1f3>
cmpq   $0x1,-0x30(%rbp)
jne    1240 <builtin_sub+0xd0>
mov    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    124f <builtin_sub+0xdf>
mov    -0x38(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    1254 <builtin_sub+0xe4>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    11a9 <builtin_sub+0x39>
mov    $0x32,%edi
mov    %edi,-0x48(%rbp)
call   1060 <malloc_trap@plt>
mov    -0x48(%rbp),%edx
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x44(%rbp)
sub    $0x58,%eax
je     12dc <builtin_sub+0x16c>
jmp    129c <builtin_sub+0x12c>
mov    -0x44(%rbp),%eax
sub    $0x6f,%eax
je     12fa <builtin_sub+0x18a>
jmp    12ad <builtin_sub+0x13d>
mov    -0x44(%rbp),%eax
sub    $0x78,%eax
jne    1318 <builtin_sub+0x1a8>
jmp    12be <builtin_sub+0x14e>
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x32,%esi
lea    0xd3b(%rip),%rdx
call   1040 <snprintf@plt>
jmp    1331 <builtin_sub+0x1c1>
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x32,%esi
lea    0xd24(%rip),%rdx
call   1040 <snprintf@plt>
jmp    1331 <builtin_sub+0x1c1>
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x32,%esi
lea    0xd0d(%rip),%rdx
call   1040 <snprintf@plt>
jmp    1331 <builtin_sub+0x1c1>
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x32,%esi
lea    0xcf5(%rip),%rdx
call   1040 <snprintf@plt>
cmpq   $0x0,-0x20(%rbp)
je     134c <builtin_sub+0x1dc>
mov    -0x40(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
jmp    135c <builtin_sub+0x1ec>
mov    -0x40(%rbp),%rsi
lea    0xccf(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<builtin_sub>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,0x8(%rsp)
mov    %rsi,%r12
mov    %rdi,0x10(%rsp)
call   1080 <assert@plt>
mov    (%r12),%rdi
call   1080 <assert@plt>
mov    0x8(%r12),%rdi
test   %rdi,%rdi
je     11f8 <builtin_sub+0x88>
xor    %ebx,%ebx
mov    0x2e2c(%rip),%rbp
mov    0x2e1d(%rip),%r14
mov    0x2e2e(%rip),%r15
xor    %r13d,%r13d
jmp    11e2 <builtin_sub+0x72>
nop
cmpq   $0x0,(%r14)
jne    128f <builtin_sub+0x11f>
sub    %rax,%r13
test   %rbx,%rbx
cmove  %rax,%r13
mov    0x10(%r12,%rbx,8),%rdi
add    $0x1,%rbx
test   %rdi,%rdi
je     11fb <builtin_sub+0x8b>
xor    %esi,%esi
xor    %edx,%edx
call   1070 <strtoll@plt>
cmp    0x0(%rbp),%rax
je     11c0 <builtin_sub+0x50>
cmp    (%r15),%rax
je     11c0 <builtin_sub+0x50>
jmp    11ca <builtin_sub+0x5a>
xor    %r13d,%r13d
mov    $0x32,%edi
call   1060 <malloc_trap@plt>
mov    %rax,%r12
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x32,%edx
call   1050 <memset@plt>
mov    0x10(%rsp),%rax
movsbl (%rax),%eax
cmp    $0x78,%eax
je     1237 <builtin_sub+0xc7>
cmp    $0x6f,%eax
je     1240 <builtin_sub+0xd0>
cmp    $0x58,%eax
jne    1249 <builtin_sub+0xd9>
lea    0xddf(%rip),%rdx
jmp    1250 <builtin_sub+0xe0>
lea    0xdcf(%rip),%rdx
jmp    1250 <builtin_sub+0xe0>
lea    0xdd4(%rip),%rdx
jmp    1250 <builtin_sub+0xe0>
lea    0xdd1(%rip),%rdx
mov    0x8(%rsp),%rbx
mov    %r12,%rdi
mov    $0x32,%esi
mov    %r13,%rcx
call   1040 <snprintf@plt>
test   %rbx,%rbx
je     126f <builtin_sub+0xff>
mov    %r12,(%rbx)
jmp    127e <builtin_sub+0x10e>
lea    0xdb0(%rip),%rdi
mov    %r12,%rsi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd6a(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
jmp    1280 <builtin_sub+0x110>

LLVM IR (-O0):
@LLONG_MIN = global i64 0
@LLONG_MAX = global i64 0
@errno = global i64 0
@.str = [13 x i8] c"builtin: add\00"
@.str.1 = [7 x i8] c"0x%llx\00"
@.str.2 = [7 x i8] c"0x%llX\00"
@.str.3 = [6 x i8] c"0%llo\00"
@.str.4 = [5 x i8] c"%lld\00"
@.str.5 = [4 x i8] c"%s\0A\00"
define i32 @builtin_sub(i8* %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i8*
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%12 = load i8*, i8** %5
%13 = call i32 @assert(i8* %12)
%14 = load i8**, i8*** %6
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 @assert(i8* %16)
store i64 0, i64* %8
store i64 1, i64* %9
br label %18
18: ; preds = %52, %3
%19 = load i8**, i8*** %6
%20 = load i64, i64* %9
%21 = getelementptr inbounds i8*, i8** %19, i64 %20
%22 = load i8*, i8** %21
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %55
24: ; preds = %18
%25 = load i8**, i8*** %6
%26 = load i64, i64* %9
%27 = getelementptr inbounds i8*, i8** %25, i64 %26
%28 = load i8*, i8** %27
%29 = call i64 @strtoll(i8* %28, i32* null, i32 0)
store i64 %29, i64* %10
%30 = load i64, i64* %10
%31 = load i64, i64* @LLONG_MIN
%32 = icmp eq i64 %30, %31
br i1 %32, label %37, label %33
33: ; preds = %24
%34 = load i64, i64* %10
%35 = load i64, i64* @LLONG_MAX
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %42
37: ; preds = %33, %24
%38 = load i64, i64* @errno
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %89
42: ; preds = %37, %33
%43 = load i64, i64* %9
%44 = icmp eq i64 %43, 1
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = load i64, i64* %10
store i64 %46, i64* %8
br label %51
47: ; preds = %42
%48 = load i64, i64* %10
%49 = load i64, i64* %8
%50 = sub nsw i64 %49, %48
store i64 %50, i64* %8
br label %51
51: ; preds = %47, %45
br label %52
52: ; preds = %51
%53 = load i64, i64* %9
%54 = add i64 %53, 1
store i64 %54, i64* %9
br label %18
55: ; preds = %18
%56 = call i8* @malloc_trap(i32 50)
store i8* %56, i8** %11
%57 = load i8*, i8** %11
%58 = call i32 @memset(i8* %57, i32 0, i32 50)
%59 = load i8*, i8** %5
%60 = getelementptr inbounds i8, i8* %59, i64 0
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
switch i32 %62, label %75 [
i32 120, label %63
i32 88, label %67
i32 111, label %71
]
63: ; preds = %55
%64 = load i8*, i8** %11
%65 = load i64, i64* %8
%66 = call i32 @snprintf(i8* %64, i32 50, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64 %65)
br label %79
67: ; preds = %55
%68 = load i8*, i8** %11
%69 = load i64, i64* %8
%70 = call i32 @snprintf(i8* %68, i32 50, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i64 %69)
br label %79
71: ; preds = %55
%72 = load i8*, i8** %11
%73 = load i64, i64* %8
%74 = call i32 @snprintf(i8* %72, i32 50, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i64 %73)
br label %79
75: ; preds = %55
%76 = load i8*, i8** %11
%77 = load i64, i64* %8
%78 = call i32 @snprintf(i8* %76, i32 50, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i64 %77)
br label %79
79: ; preds = %75, %71, %67, %63
%80 = load i8**, i8*** %7
%81 = icmp ne i8** %80, null
br i1 %81, label %82, label %85
82: ; preds = %79
%83 = load i8*, i8** %11
%84 = load i8**, i8*** %7
store i8* %83, i8** %84
br label %88
85: ; preds = %79
%86 = load i8*, i8** %11
%87 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %86)
br label %88
88: ; preds = %85, %82
store i32 0, i32* %4
br label %89
89: ; preds = %88, %40
%90 = load i32, i32* %4
ret i32 %90
}

LLVM IR (-O2):
@LLONG_MIN = global i64 0
@LLONG_MAX = global i64 0
@errno = global i64 0
@.str = [13 x i8] c"builtin: add\00"
@.str.1 = [7 x i8] c"0x%llx\00"
@.str.2 = [7 x i8] c"0x%llX\00"
@.str.3 = [6 x i8] c"0%llo\00"
@.str.4 = [5 x i8] c"%lld\00"
@.str.5 = [4 x i8] c"%s\0A\00"
define i32 @builtin_sub(i8* %0, i8** nocapture %1, i8** writeonly %2) {
%4 = tail call i32 @assert(i8* %0)
%5 = load i8*, i8** %1
%6 = tail call i32 @assert(i8* %5)
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = icmp eq i8* %8, null
br i1 %9, label %36, label %10
10: ; preds = %3, %26
%11 = phi i8* [ %32, %26 ], [ %8, %3 ]
%12 = phi i64 [ %29, %26 ], [ 0, %3 ]
%13 = phi i64 [ %30, %26 ], [ 1, %3 ]
%14 = tail call i64 @strtoll(i8* nocapture %11, i32* null, i32 0)
%15 = load i64, i64* @LLONG_MIN
%16 = icmp eq i64 %14, %15
br i1 %16, label %23, label %17
17: ; preds = %10
%18 = load i64, i64* @LLONG_MAX
%19 = icmp eq i64 %14, %18
%20 = load i64, i64* @errno
%21 = icmp ne i64 %20, 0
%22 = select i1 %19, i1 %21, i1 false
br i1 %22, label %34, label %26
23: ; preds = %10
%24 = load i64, i64* @errno
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %34
26: ; preds = %17, %23
%27 = icmp eq i64 %13, 1
%28 = sub nsw i64 %12, %14
%29 = select i1 %27, i64 %14, i64 %28
%30 = add i64 %13, 1
%31 = getelementptr inbounds i8*, i8** %1, i64 %30
%32 = load i8*, i8** %31
%33 = icmp eq i8* %32, null
br i1 %33, label %36, label %10
34: ; preds = %17, %23
%35 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %52
36: ; preds = %26, %3
%37 = phi i64 [ 0, %3 ], [ %29, %26 ]
%38 = tail call i8* @malloc_trap(i32 50)
%39 = tail call i32 @memset(i8* %38, i32 0, i32 50)
%40 = load i8, i8* %0
%41 = sext i8 %40 to i32
switch i32 %41, label %44 [
i32 120, label %45
i32 88, label %42
i32 111, label %43
]
42: ; preds = %36
br label %45
43: ; preds = %36
br label %45
44: ; preds = %36
br label %45
45: ; preds = %36, %44, %43, %42
%46 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), %44 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), %43 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), %42 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %36 ]
%47 = tail call i32 @snprintf(i8* %38, i32 50, i8* %46, i64 %37)
%48 = icmp eq i8** %2, null
br i1 %48, label %50, label %49
49: ; preds = %45
store i8* %38, i8** %2
br label %52
50: ; preds = %45
%51 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %38)
br label %52
52: ; preds = %34, %49, %50
%53 = phi i32 [ -1, %34 ], [ 0, %50 ], [ 0, %49 ]
ret i32 %53
}