#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_div(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_div>:
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     1267 <builtin_div+0xf7>
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_div+0x8f>
mov    -0x38(%rbp),%rax
mov    0x2df2(%rip),%rcx
cmp    (%rcx),%rax
jne    1228 <builtin_div+0xb8>
mov    0x2dca(%rip),%rax
cmpq   $0x0,(%rax)
je     1228 <builtin_div+0xb8>
lea    0xde9(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1365 <builtin_div+0x1f5>
cmpq   $0x1,-0x30(%rbp)
jne    1240 <builtin_div+0xd0>
mov    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1251 <builtin_div+0xe1>
mov    -0x38(%rbp),%rcx
mov    -0x28(%rbp),%rax
cqto
idiv   %rcx
mov    %rax,-0x28(%rbp)
jmp    1256 <builtin_div+0xe6>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    11a9 <builtin_div+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     12de <builtin_div+0x16e>
jmp    129e <builtin_div+0x12e>
mov    -0x44(%rbp),%eax
sub    $0x6f,%eax
je     12fc <builtin_div+0x18c>
jmp    12af <builtin_div+0x13f>
mov    -0x44(%rbp),%eax
sub    $0x78,%eax
jne    131a <builtin_div+0x1aa>
jmp    12c0 <builtin_div+0x150>
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x32,%esi
lea    0xd39(%rip),%rdx
call   1040 <snprintf@plt>
jmp    1333 <builtin_div+0x1c3>
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x32,%esi
lea    0xd22(%rip),%rdx
call   1040 <snprintf@plt>
jmp    1333 <builtin_div+0x1c3>
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x32,%esi
lea    0xd0b(%rip),%rdx
call   1040 <snprintf@plt>
jmp    1333 <builtin_div+0x1c3>
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x32,%esi
lea    0xcf3(%rip),%rdx
call   1040 <snprintf@plt>
cmpq   $0x0,-0x20(%rbp)
je     134e <builtin_div+0x1de>
mov    -0x40(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
jmp    135e <builtin_div+0x1ee>
mov    -0x40(%rbp),%rsi
lea    0xccd(%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_div>:
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     1220 <builtin_div+0xb0>
xor    %ebp,%ebp
mov    0x2e2c(%rip),%r14
mov    0x2e1d(%rip),%r15
xor    %ebx,%ebx
jmp    11dc <builtin_div+0x6c>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rax
cqto
idiv   %r13
mov    %rax,%r13
mov    0x10(%r12,%rbp,8),%rdi
add    $0x1,%rbp
mov    %r13,%rbx
test   %rdi,%rdi
je     1223 <builtin_div+0xb3>
xor    %esi,%esi
xor    %edx,%edx
call   1070 <strtoll@plt>
mov    %rax,%r13
cmp    (%r14),%rax
je     11f9 <builtin_div+0x89>
mov    0x2df4(%rip),%rax
cmp    (%rax),%r13
jne    1203 <builtin_div+0x93>
cmpq   $0x0,(%r15)
jne    12b7 <builtin_div+0x147>
test   %rbp,%rbp
je     11cb <builtin_div+0x5b>
mov    %r13,%rax
or     %rbx,%rax
shr    $0x20,%rax
jne    11c0 <builtin_div+0x50>
mov    %ebx,%eax
xor    %edx,%edx
div    %r13d
mov    %eax,%r13d
jmp    11cb <builtin_div+0x5b>
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     125f <builtin_div+0xef>
cmp    $0x6f,%eax
je     1268 <builtin_div+0xf8>
cmp    $0x58,%eax
jne    1271 <builtin_div+0x101>
lea    0xdb7(%rip),%rdx
jmp    1278 <builtin_div+0x108>
lea    0xda7(%rip),%rdx
jmp    1278 <builtin_div+0x108>
lea    0xdac(%rip),%rdx
jmp    1278 <builtin_div+0x108>
lea    0xda9(%rip),%rdx
mov    0x8(%rsp),%rbx
mov    %r12,%rdi
mov    $0x32,%esi
mov    %r13,%rcx
call   1040 <snprintf@plt>
test   %rbx,%rbx
je     1297 <builtin_div+0x127>
mov    %r12,(%rbx)
jmp    12a6 <builtin_div+0x136>
lea    0xd88(%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    0xd42(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
jmp    12a8 <builtin_div+0x138>

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