#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_add(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;
    }
    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_add>:
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     1245 <builtin_add+0xd5>
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_add+0x8f>
mov    -0x38(%rbp),%rax
mov    0x2df2(%rip),%rcx
cmp    (%rcx),%rax
jne    1228 <builtin_add+0xb8>
mov    0x2dca(%rip),%rax
cmpq   $0x0,(%rax)
je     1228 <builtin_add+0xb8>
lea    0xde9(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1343 <builtin_add+0x1d3>
mov    -0x38(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    11a9 <builtin_add+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     12bc <builtin_add+0x14c>
jmp    127c <builtin_add+0x10c>
mov    -0x44(%rbp),%eax
sub    $0x6f,%eax
je     12da <builtin_add+0x16a>
jmp    128d <builtin_add+0x11d>
mov    -0x44(%rbp),%eax
sub    $0x78,%eax
jne    12f8 <builtin_add+0x188>
jmp    129e <builtin_add+0x12e>
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x32,%esi
lea    0xd5b(%rip),%rdx
call   1040 <snprintf@plt>
jmp    1311 <builtin_add+0x1a1>
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x32,%esi
lea    0xd44(%rip),%rdx
call   1040 <snprintf@plt>
jmp    1311 <builtin_add+0x1a1>
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x32,%esi
lea    0xd2d(%rip),%rdx
call   1040 <snprintf@plt>
jmp    1311 <builtin_add+0x1a1>
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x32,%esi
lea    0xd15(%rip),%rdx
call   1040 <snprintf@plt>
cmpq   $0x0,-0x20(%rbp)
je     132c <builtin_add+0x1bc>
mov    -0x40(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
jmp    133c <builtin_add+0x1cc>
mov    -0x40(%rbp),%rsi
lea    0xcef(%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_add>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,(%rsp)
mov    %rsi,%rbx
mov    %rdi,%r15
call   1080 <assert@plt>
mov    (%rbx),%rdi
call   1080 <assert@plt>
mov    0x8(%rbx),%rdi
test   %rdi,%rdi
je     11f0 <builtin_add+0x80>
add    $0x10,%rbx
xor    %r12d,%r12d
mov    0x2e2f(%rip),%r13
mov    0x2e20(%rip),%rbp
mov    0x2e31(%rip),%r14
jmp    11da <builtin_add+0x6a>
nopl   0x0(%rax)
cmpq   $0x0,0x0(%rbp)
jne    1283 <builtin_add+0x113>
add    %rax,%r12
mov    (%rbx),%rdi
add    $0x8,%rbx
test   %rdi,%rdi
je     11f3 <builtin_add+0x83>
xor    %esi,%esi
xor    %edx,%edx
call   1070 <strtoll@plt>
cmp    0x0(%r13),%rax
je     11c0 <builtin_add+0x50>
cmp    (%r14),%rax
je     11c0 <builtin_add+0x50>
jmp    11cb <builtin_add+0x5b>
xor    %r12d,%r12d
mov    $0x32,%edi
call   1060 <malloc_trap@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x32,%edx
call   1050 <memset@plt>
movsbl (%r15),%eax
cmp    $0x78,%eax
je     122b <builtin_add+0xbb>
cmp    $0x6f,%eax
je     1234 <builtin_add+0xc4>
cmp    $0x58,%eax
jne    123d <builtin_add+0xcd>
lea    0xdeb(%rip),%rdx
jmp    1244 <builtin_add+0xd4>
lea    0xddb(%rip),%rdx
jmp    1244 <builtin_add+0xd4>
lea    0xde0(%rip),%rdx
jmp    1244 <builtin_add+0xd4>
lea    0xddd(%rip),%rdx
mov    (%rsp),%rbp
mov    %rbx,%rdi
mov    $0x32,%esi
mov    %r12,%rcx
call   1040 <snprintf@plt>
test   %rbp,%rbp
je     1263 <builtin_add+0xf3>
mov    %rbx,0x0(%rbp)
jmp    1272 <builtin_add+0x102>
lea    0xdbc(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd76(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
jmp    1274 <builtin_add+0x104>

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_add(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 = %46, %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 %49
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 %83
42: ; preds = %37, %33
%43 = load i64, i64* %10
%44 = load i64, i64* %8
%45 = add nsw i64 %44, %43
store i64 %45, i64* %8
br label %46
46: ; preds = %42
%47 = load i64, i64* %9
%48 = add i64 %47, 1
store i64 %48, i64* %9
br label %18
49: ; preds = %18
%50 = call i8* @malloc_trap(i32 50)
store i8* %50, i8** %11
%51 = load i8*, i8** %11
%52 = call i32 @memset(i8* %51, i32 0, i32 50)
%53 = load i8*, i8** %5
%54 = getelementptr inbounds i8, i8* %53, i64 0
%55 = load i8, i8* %54
%56 = sext i8 %55 to i32
switch i32 %56, label %69 [
i32 120, label %57
i32 88, label %61
i32 111, label %65
]
57: ; preds = %49
%58 = load i8*, i8** %11
%59 = load i64, i64* %8
%60 = call i32 @snprintf(i8* %58, i32 50, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64 %59)
br label %73
61: ; preds = %49
%62 = load i8*, i8** %11
%63 = load i64, i64* %8
%64 = call i32 @snprintf(i8* %62, i32 50, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i64 %63)
br label %73
65: ; preds = %49
%66 = load i8*, i8** %11
%67 = load i64, i64* %8
%68 = call i32 @snprintf(i8* %66, i32 50, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i64 %67)
br label %73
69: ; preds = %49
%70 = load i8*, i8** %11
%71 = load i64, i64* %8
%72 = call i32 @snprintf(i8* %70, i32 50, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i64 %71)
br label %73
73: ; preds = %69, %65, %61, %57
%74 = load i8**, i8*** %7
%75 = icmp ne i8** %74, null
br i1 %75, label %76, label %79
76: ; preds = %73
%77 = load i8*, i8** %11
%78 = load i8**, i8*** %7
store i8* %77, i8** %78
br label %82
79: ; preds = %73
%80 = load i8*, i8** %11
%81 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %80)
br label %82
82: ; preds = %79, %76
store i32 0, i32* %4
br label %83
83: ; preds = %82, %40
%84 = load i32, i32* %4
ret i32 %84
}

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_add(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 %34, label %10
10: ; preds = %3, %26
%11 = phi i8* [ %30, %26 ], [ %8, %3 ]
%12 = phi i64 [ %27, %26 ], [ 0, %3 ]
%13 = phi i64 [ %28, %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 %32, label %26
23: ; preds = %10
%24 = load i64, i64* @errno
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %17, %23
%27 = add nsw i64 %14, %12
%28 = add i64 %13, 1
%29 = getelementptr inbounds i8*, i8** %1, i64 %28
%30 = load i8*, i8** %29
%31 = icmp eq i8* %30, null
br i1 %31, label %34, label %10
32: ; preds = %17, %23
%33 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %50
34: ; preds = %26, %3
%35 = phi i64 [ 0, %3 ], [ %27, %26 ]
%36 = tail call i8* @malloc_trap(i32 50)
%37 = tail call i32 @memset(i8* %36, i32 0, i32 50)
%38 = load i8, i8* %0
%39 = sext i8 %38 to i32
switch i32 %39, label %42 [
i32 120, label %43
i32 88, label %40
i32 111, label %41
]
40: ; preds = %34
br label %43
41: ; preds = %34
br label %43
42: ; preds = %34
br label %43
43: ; preds = %34, %42, %41, %40
%44 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), %42 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), %41 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), %40 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %34 ]
%45 = tail call i32 @snprintf(i8* %36, i32 50, i8* %44, i64 %35)
%46 = icmp eq i8** %2, null
br i1 %46, label %48, label %47
47: ; preds = %43
store i8* %36, i8** %2
br label %50
48: ; preds = %43
%49 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %36)
br label %50
50: ; preds = %32, %47, %48
%51 = phi i32 [ -1, %32 ], [ 0, %48 ], [ 0, %47 ]
ret i32 %51
}