id
int64
0
14.1k
problem_id
int64
1
1.31k
problem_title
stringclasses
441 values
difficulty
stringclasses
3 values
c_source
stringclasses
441 values
architecture
stringclasses
4 values
optimization
stringclasses
4 values
compiler
stringclasses
8 values
assembly
stringlengths
31
174k
200
7
Reverse Integer
Medium
/* 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100. Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases? For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. Note: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. */ int reverse(int x) { int d, k = 0; // 2147483647 //-2147483648 while (x) { d = x % 10; if ((x > 0 && k > (0x7fffffff - d) / 10) || (x < 0 && k < ((signed)0x80000000 - d) / 10)) { return 0; // overflow } k = k * 10 + d; x = x / 10; } return k; //(int)k == k ? (int)k : 0; } /* Difficulty:Easy Total Accepted:275.6K Total Submissions:1.1M Companies Bloomberg Apple Related Topics Math Similar Questions String to Integer (atoi) */
mips64
-O0
mips64 clang 21.1.0
reverse: .Lfunc_begin0 = .Ltmp0 daddiu $sp, $sp, -32 sd $ra, 24($sp) sd $fp, 16($sp) move $fp, $sp move $1, $4 sw $1, 8($fp) sw $zero, 0($fp) b .LBB0_1 nop .LBB0_1: lw $1, 8($fp) beqz $1, .LBB0_13 nop b .LBB0_3 nop .LBB0_3: lw $1, 8($fp) lui $2, 26214 ori $2, $2, 26215 mult $1, $2 mfhi $2 srl $3, $2, 31 sra $2, $2, 2 addu $2, $2, $3 sll $3, $2, 1 sll $2, $2, 3 addu $2, $2, $3 subu $1, $1, $2 sw $1, 4($fp) lw $1, 8($fp) blez $1, .LBB0_7 nop b .LBB0_5 nop .LBB0_5: lw $2, 0($fp) lw $1, 4($fp) lui $3, 32767 ori $3, $3, 65535 xor $1, $1, $3 lui $3, 26214 ori $3, $3, 26215 multu $1, $3 mfhi $1 srl $1, $1, 2 slt $1, $1, $2 bnez $1, .LBB0_11 nop b .LBB0_7 nop .LBB0_7: lw $1, 8($fp) bgez $1, .LBB0_12 nop b .LBB0_9 nop .LBB0_9: lw $1, 0($fp) lw $3, 4($fp) lui $2, 32768 subu $2, $2, $3 lui $3, 26214 ori $3, $3, 26215 mult $2, $3 mfhi $2 srl $3, $2, 31 sra $2, $2, 2 addu $2, $2, $3 slt $1, $1, $2 beqz $1, .LBB0_12 nop b .LBB0_11 nop .LBB0_11: sw $zero, 12($fp) b .LBB0_14 nop .LBB0_12: lw $1, 0($fp) sll $2, $1, 1 sll $1, $1, 3 addu $1, $1, $2 lw $2, 4($fp) addu $1, $1, $2 sw $1, 0($fp) lw $1, 8($fp) lui $2, 26214 ori $2, $2, 26215 mult $1, $2 mfhi $1 srl $2, $1, 31 sra $1, $1, 2 addu $1, $1, $2 sw $1, 8($fp) b .LBB0_1 nop .LBB0_13: lw $1, 0($fp) sw $1, 12($fp) b .LBB0_14 nop .LBB0_14: lw $2, 12($fp) move $sp, $fp ld $fp, 16($sp) ld $ra, 24($sp) daddiu $sp, $sp, 32 jr $ra nop
201
7
Reverse Integer
Medium
/* 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100. Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases? For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. Note: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. */ int reverse(int x) { int d, k = 0; // 2147483647 //-2147483648 while (x) { d = x % 10; if ((x > 0 && k > (0x7fffffff - d) / 10) || (x < 0 && k < ((signed)0x80000000 - d) / 10)) { return 0; // overflow } k = k * 10 + d; x = x / 10; } return k; //(int)k == k ? (int)k : 0; } /* Difficulty:Easy Total Accepted:275.6K Total Submissions:1.1M Companies Bloomberg Apple Related Topics Math Similar Questions String to Integer (atoi) */
mips64
-O1
mips64 clang 21.1.0
reverse: .Lfunc_begin0 = .Ltmp0 daddiu $sp, $sp, -16 sd $ra, 8($sp) sd $fp, 0($sp) move $fp, $sp beqz $4, .LBB0_9 addiu $2, $zero, 0 lui $1, 26214 ori $3, $1, 26215 lui $1, 32767 ori $5, $1, 65535 b .LBB0_3 lui $6, 32768 .LBB0_2: sll $1, $2, 1 sll $2, $2, 3 addu $1, $2, $1 addu $2, $1, $8 addiu $1, $4, 9 sltiu $1, $1, 19 bnez $1, .LBB0_9 move $4, $7 .LBB0_3: mult $4, $3 mfhi $1 srl $7, $1, 31 sra $1, $1, 2 addu $7, $1, $7 sll $1, $7, 1 sll $8, $7, 3 addu $1, $8, $1 blez $4, .LBB0_5 subu $8, $4, $1 xor $1, $8, $5 multu $1, $3 mfhi $1 srl $1, $1, 2 slt $1, $1, $2 bnez $1, .LBB0_8 nop .LBB0_5: bgez $4, .LBB0_2 nop subu $1, $6, $8 mult $1, $3 mfhi $1 srl $9, $1, 31 sra $1, $1, 2 addu $1, $1, $9 slt $1, $2, $1 beqz $1, .LBB0_2 nop b .LBB0_9 addiu $2, $zero, 0 .LBB0_8: addiu $2, $zero, 0 .LBB0_9: sll $2, $2, 0 move $sp, $fp ld $fp, 0($sp) ld $ra, 8($sp) jr $ra daddiu $sp, $sp, 16
202
7
Reverse Integer
Medium
/* 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100. Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases? For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. Note: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. */ int reverse(int x) { int d, k = 0; // 2147483647 //-2147483648 while (x) { d = x % 10; if ((x > 0 && k > (0x7fffffff - d) / 10) || (x < 0 && k < ((signed)0x80000000 - d) / 10)) { return 0; // overflow } k = k * 10 + d; x = x / 10; } return k; //(int)k == k ? (int)k : 0; } /* Difficulty:Easy Total Accepted:275.6K Total Submissions:1.1M Companies Bloomberg Apple Related Topics Math Similar Questions String to Integer (atoi) */
mips64
-O2
mips64 clang 21.1.0
reverse: .Lfunc_begin0 = .Ltmp0 daddiu $sp, $sp, -16 sd $ra, 8($sp) sd $fp, 0($sp) move $fp, $sp beqz $4, .LBB0_8 addiu $2, $zero, 0 lui $1, 26214 ori $3, $1, 26215 lui $1, 32767 ori $5, $1, 65535 lui $6, 32768 lui $1, 52428 b .LBB0_4 ori $7, $1, 52429 .LBB0_2: xor $1, $9, $6 multu $1, $7 mfhi $1 srl $1, $1, 3 negu $1, $1 slt $1, $2, $1 bnez $1, .LBB0_7 nop .LBB0_3: sll $1, $2, 1 sll $2, $2, 3 addu $1, $2, $1 addu $2, $1, $9 addiu $1, $4, 9 sltiu $1, $1, 19 bnez $1, .LBB0_8 move $4, $8 .LBB0_4: mult $4, $3 mfhi $1 srl $8, $1, 31 sra $1, $1, 2 addu $8, $1, $8 sll $1, $8, 1 sll $9, $8, 3 addu $1, $9, $1 blez $4, .LBB0_2 subu $9, $4, $1 xor $1, $9, $5 multu $1, $3 mfhi $1 srl $1, $1, 2 slt $1, $1, $2 beqz $1, .LBB0_3 nop b .LBB0_8 addiu $2, $zero, 0 .LBB0_7: addiu $2, $zero, 0 .LBB0_8: sll $2, $2, 0 move $sp, $fp ld $fp, 0($sp) ld $ra, 8($sp) jr $ra daddiu $sp, $sp, 16
203
7
Reverse Integer
Medium
/* 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100. Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases? For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. Note: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. */ int reverse(int x) { int d, k = 0; // 2147483647 //-2147483648 while (x) { d = x % 10; if ((x > 0 && k > (0x7fffffff - d) / 10) || (x < 0 && k < ((signed)0x80000000 - d) / 10)) { return 0; // overflow } k = k * 10 + d; x = x / 10; } return k; //(int)k == k ? (int)k : 0; } /* Difficulty:Easy Total Accepted:275.6K Total Submissions:1.1M Companies Bloomberg Apple Related Topics Math Similar Questions String to Integer (atoi) */
mips64
-O3
mips64 clang 21.1.0
reverse: .Lfunc_begin0 = .Ltmp0 daddiu $sp, $sp, -16 sd $ra, 8($sp) sd $fp, 0($sp) move $fp, $sp beqz $4, .LBB0_8 addiu $2, $zero, 0 lui $1, 26214 lui $6, 32768 ori $3, $1, 26215 lui $1, 32767 ori $5, $1, 65535 lui $1, 52428 b .LBB0_4 ori $7, $1, 52429 .LBB0_2: xor $1, $9, $6 multu $1, $7 mfhi $1 srl $1, $1, 3 negu $1, $1 slt $1, $2, $1 bnez $1, .LBB0_7 nop .LBB0_3: sll $1, $2, 1 sll $2, $2, 3 addu $1, $2, $1 addu $2, $1, $9 addiu $1, $4, 9 sltiu $1, $1, 19 bnez $1, .LBB0_8 move $4, $8 .LBB0_4: mult $4, $3 mfhi $1 srl $8, $1, 31 sra $1, $1, 2 addu $8, $1, $8 sll $1, $8, 1 sll $9, $8, 3 addu $1, $9, $1 blez $4, .LBB0_2 subu $9, $4, $1 xor $1, $9, $5 multu $1, $3 mfhi $1 srl $1, $1, 2 slt $1, $1, $2 beqz $1, .LBB0_3 nop b .LBB0_8 addiu $2, $zero, 0 .LBB0_7: addiu $2, $zero, 0 .LBB0_8: sll $2, $2, 0 move $sp, $fp ld $fp, 0($sp) ld $ra, 8($sp) jr $ra daddiu $sp, $sp, 16
204
7
Reverse Integer
Medium
/* 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100. Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases? For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. Note: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. */ int reverse(int x) { int d, k = 0; // 2147483647 //-2147483648 while (x) { d = x % 10; if ((x > 0 && k > (0x7fffffff - d) / 10) || (x < 0 && k < ((signed)0x80000000 - d) / 10)) { return 0; // overflow } k = k * 10 + d; x = x / 10; } return k; //(int)k == k ? (int)k : 0; } /* Difficulty:Easy Total Accepted:275.6K Total Submissions:1.1M Companies Bloomberg Apple Related Topics Math Similar Questions String to Integer (atoi) */
mips64
-O0
mips64 gcc 15.2.0
reverse: daddiu $sp,$sp,-48 sd $fp,40($sp) move $fp,$sp move $2,$4 sll $2,$2,0 sw $2,16($fp) sw $0,0($fp) b .L2 nop .L7: lw $4,16($fp) move $3,$4 move $2,$3 dsll $2,$2,1 daddu $2,$2,$3 dsll $5,$2,4 daddu $2,$2,$5 dsll $5,$2,8 daddu $2,$2,$5 dsll $5,$2,16 daddu $2,$2,$5 dsll $2,$2,1 daddu $2,$2,$3 dsrl $2,$2,32 sll $2,$2,0 sra $3,$2,2 sra $2,$4,31 subu $3,$3,$2 move $2,$3 sll $2,$2,2 addu $2,$2,$3 sll $2,$2,1 subu $2,$4,$2 sw $2,4($fp) lw $2,16($fp) blez $2,.L3 nop li $2,2147418112 # 0x7fff0000 ori $3,$2,0xffff lw $2,4($fp) subu $2,$3,$2 move $5,$2 move $3,$5 move $2,$3 dsll $2,$2,1 daddu $2,$2,$3 dsll $4,$2,4 daddu $2,$2,$4 dsll $4,$2,8 daddu $2,$2,$4 dsll $4,$2,16 daddu $2,$2,$4 dsll $2,$2,1 daddu $2,$2,$3 dsrl $2,$2,32 sll $2,$2,0 sra $3,$2,2 sra $2,$5,31 subu $2,$3,$2 lw $3,0($fp) slt $2,$2,$3 bne $2,$0,.L4 nop .L3: lw $2,16($fp) bgez $2,.L5 nop li $3,-2147483648 # 0xffffffff80000000 lw $2,4($fp) subu $2,$3,$2 move $5,$2 move $3,$5 move $2,$3 dsll $2,$2,1 daddu $2,$2,$3 dsll $4,$2,4 daddu $2,$2,$4 dsll $4,$2,8 daddu $2,$2,$4 dsll $4,$2,16 daddu $2,$2,$4 dsll $2,$2,1 daddu $2,$2,$3 dsrl $2,$2,32 sll $2,$2,0 sra $3,$2,2 sra $2,$5,31 subu $2,$3,$2 lw $3,0($fp) slt $2,$3,$2 beq $2,$0,.L5 nop .L4: move $2,$0 b .L6 nop .L5: lw $3,0($fp) move $2,$3 sll $2,$2,2 addu $2,$2,$3 sll $2,$2,1 lw $3,4($fp) addu $2,$3,$2 sw $2,0($fp) lw $5,16($fp) move $3,$5 move $2,$3 dsll $2,$2,1 daddu $2,$2,$3 dsll $4,$2,4 daddu $2,$2,$4 dsll $4,$2,8 daddu $2,$2,$4 dsll $4,$2,16 daddu $2,$2,$4 dsll $2,$2,1 daddu $2,$2,$3 dsrl $2,$2,32 sll $2,$2,0 sra $3,$2,2 sra $2,$5,31 subu $2,$3,$2 sw $2,16($fp) .L2: lw $2,16($fp) bne $2,$0,.L7 nop lw $2,0($fp) .L6: move $sp,$fp ld $fp,40($sp) daddiu $sp,$sp,48 jr $31 nop
205
7
Reverse Integer
Medium
/* 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100. Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases? For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. Note: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. */ int reverse(int x) { int d, k = 0; // 2147483647 //-2147483648 while (x) { d = x % 10; if ((x > 0 && k > (0x7fffffff - d) / 10) || (x < 0 && k < ((signed)0x80000000 - d) / 10)) { return 0; // overflow } k = k * 10 + d; x = x / 10; } return k; //(int)k == k ? (int)k : 0; } /* Difficulty:Easy Total Accepted:275.6K Total Submissions:1.1M Companies Bloomberg Apple Related Topics Math Similar Questions String to Integer (atoi) */
mips64
-O1
mips64 gcc 15.2.0
reverse: beq $4,$0,.L6 li $5,-2147483648 # 0xffffffff80000000 move $2,$0 li $6,2147418112 # 0x7fff0000 b .L5 ori $6,$6,0xffff .L3: subu $8,$5,$7 subu $9,$5,$7 dsll $3,$9,1 daddu $3,$3,$9 dsll $10,$3,4 daddu $3,$3,$10 dsll $10,$3,8 daddu $3,$3,$10 dsll $10,$3,16 daddu $3,$3,$10 dsll $3,$3,1 daddu $3,$3,$9 dsra $3,$3,32 sra $3,$3,2 sra $8,$8,31 subu $3,$3,$8 slt $3,$2,$3 bne $3,$0,.L8 sll $3,$2,2 .L12: addu $3,$3,$2 sll $3,$3,1 addu $2,$3,$7 dsll $3,$4,1 daddu $3,$3,$4 dsll $7,$3,4 daddu $3,$3,$7 dsll $7,$3,8 daddu $3,$3,$7 dsll $7,$3,16 daddu $3,$3,$7 dsll $3,$3,1 daddu $3,$3,$4 dsra $3,$3,32 sra $3,$3,2 sra $7,$4,31 beq $3,$7,.L13 subu $4,$3,$7 .L5: dsll $3,$4,1 daddu $3,$3,$4 dsll $7,$3,4 daddu $3,$3,$7 dsll $7,$3,8 daddu $3,$3,$7 dsll $7,$3,16 daddu $3,$3,$7 dsll $3,$3,1 daddu $3,$3,$4 dsra $3,$3,32 sra $3,$3,2 sra $7,$4,31 subu $3,$3,$7 sll $7,$3,2 addu $7,$7,$3 sll $7,$7,1 blez $4,.L3 subu $7,$4,$7 subu $8,$6,$7 subu $9,$6,$7 dsll $3,$9,1 daddu $3,$3,$9 dsll $10,$3,4 daddu $3,$3,$10 dsll $10,$3,8 daddu $3,$3,$10 dsll $10,$3,16 daddu $3,$3,$10 dsll $3,$3,1 daddu $3,$3,$9 dsra $3,$3,32 sra $3,$3,2 sra $8,$8,31 subu $3,$3,$8 slt $3,$3,$2 beq $3,$0,.L12 sll $3,$2,2 jr $31 move $2,$0 .L13: jr $31 nop .L6: jr $31 move $2,$4 .L8: jr $31 move $2,$0
206
7
Reverse Integer
Medium
/* 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100. Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases? For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. Note: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. */ int reverse(int x) { int d, k = 0; // 2147483647 //-2147483648 while (x) { d = x % 10; if ((x > 0 && k > (0x7fffffff - d) / 10) || (x < 0 && k < ((signed)0x80000000 - d) / 10)) { return 0; // overflow } k = k * 10 + d; x = x / 10; } return k; //(int)k == k ? (int)k : 0; } /* Difficulty:Easy Total Accepted:275.6K Total Submissions:1.1M Companies Bloomberg Apple Related Topics Math Similar Questions String to Integer (atoi) */
mips64
-O2
mips64 gcc 15.2.0
reverse: beq $4,$0,.L15 move $2,$0 li $8,2147418112 # 0x7fff0000 li $7,-2147483648 # 0xffffffff80000000 b .L5 ori $8,$8,0xffff .L13: bne $6,$0,.L8 nop addu $2,$11,$10 .L14: beq $5,$9,.L15 subu $4,$5,$9 .L5: dsll $5,$4,1 daddu $3,$5,$4 dsll $6,$3,4 daddu $3,$3,$6 dsll $6,$3,8 daddu $3,$3,$6 dsll $9,$3,16 daddu $5,$5,$4 dsll $6,$5,4 daddu $3,$3,$9 daddu $5,$5,$6 dsll $3,$3,1 dsll $6,$5,8 daddu $3,$3,$4 daddu $5,$5,$6 dsra $3,$3,32 daddu $5,$5,$9 sra $3,$3,2 sra $9,$4,31 subu $3,$3,$9 sll $10,$3,2 addu $10,$10,$3 sll $10,$10,1 subu $10,$4,$10 subu $13,$7,$10 subu $12,$8,$10 dsll $3,$13,1 dsll $12,$12,32 dsrl $12,$12,32 daddu $3,$3,$13 dsll $14,$3,4 dsll $6,$12,1 daddu $6,$6,$12 daddu $3,$3,$14 dsll $15,$6,4 dsll $14,$3,8 daddu $6,$6,$15 daddu $3,$3,$14 dsll $14,$3,16 dsll $15,$6,8 daddu $6,$6,$15 daddu $3,$3,$14 dsll $3,$3,1 dsll $14,$6,16 daddu $3,$3,$13 daddu $6,$6,$14 dsll $5,$5,1 dsll $6,$6,2 subu $13,$7,$10 dsra $3,$3,32 sll $11,$2,2 daddu $5,$5,$4 daddu $6,$6,$12 sra $3,$3,2 sra $13,$13,31 addu $11,$11,$2 dsra $5,$5,32 dsrl $6,$6,35 subu $3,$3,$13 sll $11,$11,1 sra $5,$5,2 slt $6,$6,$2 bgtz $4,.L13 slt $3,$2,$3 beql $3,$0,.L14 addu $2,$11,$10 .L8: jr $31 move $2,$0 .L15: jr $31 nop
207
7
Reverse Integer
Medium
/* 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100. Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases? For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. Note: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. */ int reverse(int x) { int d, k = 0; // 2147483647 //-2147483648 while (x) { d = x % 10; if ((x > 0 && k > (0x7fffffff - d) / 10) || (x < 0 && k < ((signed)0x80000000 - d) / 10)) { return 0; // overflow } k = k * 10 + d; x = x / 10; } return k; //(int)k == k ? (int)k : 0; } /* Difficulty:Easy Total Accepted:275.6K Total Submissions:1.1M Companies Bloomberg Apple Related Topics Math Similar Questions String to Integer (atoi) */
mips64
-O3
mips64 gcc 15.2.0
reverse: beq $4,$0,.L15 move $2,$0 li $8,2147418112 # 0x7fff0000 li $7,-2147483648 # 0xffffffff80000000 b .L5 ori $8,$8,0xffff .L13: bne $6,$0,.L8 nop addu $2,$11,$10 .L14: beq $5,$9,.L15 subu $4,$5,$9 .L5: dsll $5,$4,1 daddu $3,$5,$4 dsll $6,$3,4 daddu $3,$3,$6 dsll $6,$3,8 daddu $3,$3,$6 dsll $9,$3,16 daddu $5,$5,$4 dsll $6,$5,4 daddu $3,$3,$9 daddu $5,$5,$6 dsll $3,$3,1 dsll $6,$5,8 daddu $3,$3,$4 daddu $5,$5,$6 dsra $3,$3,32 daddu $5,$5,$9 sra $3,$3,2 sra $9,$4,31 subu $3,$3,$9 sll $10,$3,2 addu $10,$10,$3 sll $10,$10,1 subu $10,$4,$10 subu $13,$7,$10 subu $12,$8,$10 dsll $3,$13,1 dsll $12,$12,32 dsrl $12,$12,32 daddu $3,$3,$13 dsll $14,$3,4 dsll $6,$12,1 daddu $6,$6,$12 daddu $3,$3,$14 dsll $15,$6,4 dsll $14,$3,8 daddu $6,$6,$15 daddu $3,$3,$14 dsll $14,$3,16 dsll $15,$6,8 daddu $6,$6,$15 daddu $3,$3,$14 dsll $3,$3,1 dsll $14,$6,16 daddu $3,$3,$13 daddu $6,$6,$14 dsll $5,$5,1 dsll $6,$6,2 subu $13,$7,$10 dsra $3,$3,32 sll $11,$2,2 daddu $5,$5,$4 daddu $6,$6,$12 sra $3,$3,2 sra $13,$13,31 addu $11,$11,$2 dsra $5,$5,32 dsrl $6,$6,35 subu $3,$3,$13 sll $11,$11,1 sra $5,$5,2 slt $6,$6,$2 bgtz $4,.L13 slt $3,$2,$3 beql $3,$0,.L14 addu $2,$11,$10 .L8: jr $31 move $2,$0 .L15: jr $31 nop
208
7
Reverse Integer
Medium
/* 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100. Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases? For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. Note: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. */ int reverse(int x) { int d, k = 0; // 2147483647 //-2147483648 while (x) { d = x % 10; if ((x > 0 && k > (0x7fffffff - d) / 10) || (x < 0 && k < ((signed)0x80000000 - d) / 10)) { return 0; // overflow } k = k * 10 + d; x = x / 10; } return k; //(int)k == k ? (int)k : 0; } /* Difficulty:Easy Total Accepted:275.6K Total Submissions:1.1M Companies Bloomberg Apple Related Topics Math Similar Questions String to Integer (atoi) */
riscv64
-O0
RISC-V 64 clang 21.1.0
reverse: addi sp, sp, -32 sd ra, 24(sp) sd s0, 16(sp) addi s0, sp, 32 sw a0, -24(s0) li a0, 0 sw a0, -32(s0) j .LBB0_1 .LBB0_1: lw a0, -24(s0) beqz a0, .LBB0_8 j .LBB0_2 .LBB0_2: lw a0, -24(s0) lui a1, 419430 addi a1, a1, 1639 mul a1, a0, a1 srli a2, a1, 63 srai a1, a1, 34 addw a1, a1, a2 slliw a2, a1, 1 slliw a1, a1, 3 addw a1, a1, a2 subw a0, a0, a1 sw a0, -28(s0) lw a1, -24(s0) li a0, 0 bge a0, a1, .LBB0_4 j .LBB0_3 .LBB0_3: lw a1, -32(s0) lwu a0, -28(s0) lui a2, 524288 addiw a2, a2, -1 xor a0, a0, a2 lui a2, 419430 addi a2, a2, 1639 mul a0, a0, a2 srli a0, a0, 34 blt a0, a1, .LBB0_6 j .LBB0_4 .LBB0_4: lw a0, -24(s0) bgez a0, .LBB0_7 j .LBB0_5 .LBB0_5: lw a0, -32(s0) lw a2, -28(s0) lui a1, 524288 subw a1, a1, a2 lui a2, 419430 addi a2, a2, 1639 mul a1, a1, a2 srli a2, a1, 63 srai a1, a1, 34 add a1, a1, a2 bge a0, a1, .LBB0_7 j .LBB0_6 .LBB0_6: li a0, 0 sw a0, -20(s0) j .LBB0_9 .LBB0_7: lw a0, -32(s0) slliw a1, a0, 1 slliw a0, a0, 3 addw a0, a0, a1 lw a1, -28(s0) addw a0, a0, a1 sw a0, -32(s0) lw a0, -24(s0) lui a1, 419430 addi a1, a1, 1639 mul a0, a0, a1 srli a1, a0, 63 srai a0, a0, 34 addw a0, a0, a1 sw a0, -24(s0) j .LBB0_1 .LBB0_8: lw a0, -32(s0) sw a0, -20(s0) j .LBB0_9 .LBB0_9: lw a0, -20(s0) ld ra, 24(sp) ld s0, 16(sp) addi sp, sp, 32 ret
209
7
Reverse Integer
Medium
/* 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100. Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases? For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. Note: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. */ int reverse(int x) { int d, k = 0; // 2147483647 //-2147483648 while (x) { d = x % 10; if ((x > 0 && k > (0x7fffffff - d) / 10) || (x < 0 && k < ((signed)0x80000000 - d) / 10)) { return 0; // overflow } k = k * 10 + d; x = x / 10; } return k; //(int)k == k ? (int)k : 0; } /* Difficulty:Easy Total Accepted:275.6K Total Submissions:1.1M Companies Bloomberg Apple Related Topics Math Similar Questions String to Integer (atoi) */
riscv64
-O1
RISC-V 64 clang 21.1.0
reverse: beqz a0, .LBB0_8 mv a1, a0 li a0, 0 lui a2, 419430 lui a6, 524288 addi t2, a2, 1639 addi t0, a6, -1 slli t1, t2, 32 li a7, 18 j .LBB0_3 .LBB0_2: slli a2, a0, 1 slli a0, a0, 3 add a0, a0, a2 addiw a2, a1, 9 addw a0, a0, a4 mv a1, a5 bgeu a7, a2, .LBB0_8 .LBB0_3: mul a2, a1, t2 srli a4, a2, 63 srai a2, a2, 34 addw a5, a2, a4 slli a4, a5, 1 slli a2, a5, 3 add a2, a2, a4 subw a4, a1, a2 blez a1, .LBB0_5 xor a2, a4, t0 slli a2, a2, 32 mulhu a2, a2, t1 srli a2, a2, 34 blt a2, a0, .LBB0_7 .LBB0_5: bgez a1, .LBB0_2 subw a2, a6, a4 mul a2, a2, t2 srli a3, a2, 63 srai a2, a2, 34 add a2, a2, a3 bge a0, a2, .LBB0_2 .LBB0_7: li a0, 0 .LBB0_8: ret
210
7
Reverse Integer
Medium
/* 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100. Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases? For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. Note: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. */ int reverse(int x) { int d, k = 0; // 2147483647 //-2147483648 while (x) { d = x % 10; if ((x > 0 && k > (0x7fffffff - d) / 10) || (x < 0 && k < ((signed)0x80000000 - d) / 10)) { return 0; // overflow } k = k * 10 + d; x = x / 10; } return k; //(int)k == k ? (int)k : 0; } /* Difficulty:Easy Total Accepted:275.6K Total Submissions:1.1M Companies Bloomberg Apple Related Topics Math Similar Questions String to Integer (atoi) */
riscv64
-O2
RISC-V 64 clang 21.1.0
reverse: beqz a0, .LBB0_7 mv a1, a0 li a0, 0 lui a2, 419430 lui a6, 524288 li t0, 18 lui a3, 838861 addi a4, a2, 1639 addi t1, a6, -1 addi a7, a3, -819 slli t2, a4, 32 slli a7, a7, 32 j .LBB0_4 .LBB0_2: xor a2, a3, a6 slli a2, a2, 32 mulhu a2, a2, a7 srli a2, a2, 35 neg a2, a2 blt a0, a2, .LBB0_6 .LBB0_3: slli a2, a0, 1 slli a0, a0, 3 add a0, a0, a2 addiw a2, a1, 9 addw a0, a0, a3 mv a1, a5 bgeu t0, a2, .LBB0_7 .LBB0_4: mul a3, a1, a4 srli a5, a3, 63 srai a3, a3, 34 addw a5, a5, a3 slli a2, a5, 1 slli a3, a5, 3 add a2, a2, a3 subw a3, a1, a2 blez a1, .LBB0_2 xor a2, a3, t1 slli a2, a2, 32 mulhu a2, a2, t2 srli a2, a2, 34 bge a2, a0, .LBB0_3 .LBB0_6: li a0, 0 .LBB0_7: ret
211
7
Reverse Integer
Medium
/* 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100. Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases? For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. Note: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. */ int reverse(int x) { int d, k = 0; // 2147483647 //-2147483648 while (x) { d = x % 10; if ((x > 0 && k > (0x7fffffff - d) / 10) || (x < 0 && k < ((signed)0x80000000 - d) / 10)) { return 0; // overflow } k = k * 10 + d; x = x / 10; } return k; //(int)k == k ? (int)k : 0; } /* Difficulty:Easy Total Accepted:275.6K Total Submissions:1.1M Companies Bloomberg Apple Related Topics Math Similar Questions String to Integer (atoi) */
riscv64
-O3
RISC-V 64 clang 21.1.0
reverse: beqz a0, .LBB0_7 mv a1, a0 li a0, 0 lui a2, 419430 lui a6, 524288 li t0, 18 lui a3, 838861 addi a4, a2, 1639 addi t1, a6, -1 addi a7, a3, -819 slli t2, a4, 32 slli a7, a7, 32 j .LBB0_4 .LBB0_2: xor a2, a3, a6 slli a2, a2, 32 mulhu a2, a2, a7 srli a2, a2, 35 neg a2, a2 blt a0, a2, .LBB0_6 .LBB0_3: slli a2, a0, 1 slli a0, a0, 3 add a0, a0, a2 addiw a2, a1, 9 addw a0, a0, a3 mv a1, a5 bgeu t0, a2, .LBB0_7 .LBB0_4: mul a3, a1, a4 srli a5, a3, 63 srai a3, a3, 34 addw a5, a5, a3 slli a2, a5, 1 slli a3, a5, 3 add a2, a2, a3 subw a3, a1, a2 blez a1, .LBB0_2 xor a2, a3, t1 slli a2, a2, 32 mulhu a2, a2, t2 srli a2, a2, 34 bge a2, a0, .LBB0_3 .LBB0_6: li a0, 0 .LBB0_7: ret
212
7
Reverse Integer
Medium
/* 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100. Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases? For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. Note: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. */ int reverse(int x) { int d, k = 0; // 2147483647 //-2147483648 while (x) { d = x % 10; if ((x > 0 && k > (0x7fffffff - d) / 10) || (x < 0 && k < ((signed)0x80000000 - d) / 10)) { return 0; // overflow } k = k * 10 + d; x = x / 10; } return k; //(int)k == k ? (int)k : 0; } /* Difficulty:Easy Total Accepted:275.6K Total Submissions:1.1M Companies Bloomberg Apple Related Topics Math Similar Questions String to Integer (atoi) */
riscv64
-O0
RISC-V 64 gcc 15.2.0
reverse: addi sp,sp,-48 sd ra,40(sp) sd s0,32(sp) addi s0,sp,48 mv a5,a0 sw a5,-36(s0) sw zero,-20(s0) j .L2 .L7: lw a5,-36(s0) mv a4,a5 sext.w a3,a4 li a5,1717985280 addi a5,a5,1639 mul a5,a3,a5 srli a5,a5,32 sraiw a5,a5,2 mv a3,a5 sraiw a5,a4,31 subw a5,a3,a5 mv a3,a5 mv a5,a3 slliw a5,a5,2 addw a5,a5,a3 slliw a5,a5,1 subw a5,a4,a5 sw a5,-24(s0) lw a5,-36(s0) sext.w a5,a5 ble a5,zero,.L3 li a5,-2147483648 addiw a5,a5,-1 lw a4,-24(s0) subw a5,a5,a4 sext.w a5,a5 mv a3,a5 sext.w a4,a3 li a5,1717985280 addi a5,a5,1639 mul a5,a4,a5 srli a5,a5,32 sraiw a5,a5,2 mv a4,a5 sraiw a5,a3,31 subw a5,a4,a5 sext.w a5,a5 lw a4,-20(s0) sext.w a4,a4 bgt a4,a5,.L4 .L3: lw a5,-36(s0) sext.w a5,a5 bge a5,zero,.L5 li a5,-2147483648 lw a4,-24(s0) subw a5,a5,a4 sext.w a5,a5 mv a3,a5 sext.w a4,a3 li a5,1717985280 addi a5,a5,1639 mul a5,a4,a5 srli a5,a5,32 sraiw a5,a5,2 mv a4,a5 sraiw a5,a3,31 subw a5,a4,a5 sext.w a5,a5 lw a4,-20(s0) sext.w a4,a4 bge a4,a5,.L5 .L4: li a5,0 j .L6 .L5: lw a5,-20(s0) mv a4,a5 mv a5,a4 slliw a5,a5,2 addw a5,a5,a4 slliw a5,a5,1 sext.w a5,a5 lw a4,-24(s0) addw a5,a4,a5 sw a5,-20(s0) lw a5,-36(s0) mv a3,a5 sext.w a4,a3 li a5,1717985280 addi a5,a5,1639 mul a5,a4,a5 srli a5,a5,32 sraiw a5,a5,2 mv a4,a5 sraiw a5,a3,31 subw a5,a4,a5 sw a5,-36(s0) .L2: lw a5,-36(s0) sext.w a5,a5 bne a5,zero,.L7 lw a5,-20(s0) .L6: mv a0,a5 ld ra,40(sp) ld s0,32(sp) addi sp,sp,48 jr ra
213
7
Reverse Integer
Medium
/* 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100. Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases? For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. Note: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. */ int reverse(int x) { int d, k = 0; // 2147483647 //-2147483648 while (x) { d = x % 10; if ((x > 0 && k > (0x7fffffff - d) / 10) || (x < 0 && k < ((signed)0x80000000 - d) / 10)) { return 0; // overflow } k = k * 10 + d; x = x / 10; } return k; //(int)k == k ? (int)k : 0; } /* Difficulty:Easy Total Accepted:275.6K Total Submissions:1.1M Companies Bloomberg Apple Related Topics Math Similar Questions String to Integer (atoi) */
riscv64
-O1
RISC-V 64 gcc 15.2.0
reverse: mv a5,a0 beq a0,zero,.L6 li a0,0 li a2,1717985280 addi a2,a2,1639 li a7,-2147483648 addiw a6,a7,-1 j .L5 .L3: subw a1,a7,a4 mul a3,a1,a2 srai a3,a3,34 sraiw a1,a1,31 subw a3,a3,a1 bgt a3,a0,.L8 .L4: slliw a3,a0,2 addw a3,a3,a0 slliw a3,a3,1 addw a0,a3,a4 mul a4,a5,a2 srai a4,a4,34 sraiw a5,a5,31 subw a5,a4,a5 beq a5,zero,.L10 .L5: mul a3,a5,a2 srai a3,a3,34 sraiw a4,a5,31 subw a3,a3,a4 slliw a4,a3,2 addw a4,a4,a3 slliw a4,a4,1 subw a4,a5,a4 ble a5,zero,.L3 subw a1,a6,a4 mul a3,a1,a2 srai a3,a3,34 sraiw a1,a1,31 subw a3,a3,a1 bge a3,a0,.L4 li a0,0 ret .L10: ret .L6: ret .L8: li a0,0 ret
214
7
Reverse Integer
Medium
/* 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100. Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases? For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. Note: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. */ int reverse(int x) { int d, k = 0; // 2147483647 //-2147483648 while (x) { d = x % 10; if ((x > 0 && k > (0x7fffffff - d) / 10) || (x < 0 && k < ((signed)0x80000000 - d) / 10)) { return 0; // overflow } k = k * 10 + d; x = x / 10; } return k; //(int)k == k ? (int)k : 0; } /* Difficulty:Easy Total Accepted:275.6K Total Submissions:1.1M Companies Bloomberg Apple Related Topics Math Similar Questions String to Integer (atoi) */
riscv64
-O2
RISC-V 64 gcc 15.2.0
reverse: mv a5,a0 beq a0,zero,.L8 li t1,-2147483648 li a6,1717985280 addi a6,a6,1639 addiw t3,t1,-1 li a0,0 j .L5 .L12: mul a3,a3,a6 srai a3,a3,34 subw a3,a3,t4 blt a3,a0,.L8 .L4: mul a5,a5,a6 addw a0,a2,a4 srai a5,a5,34 subw a5,a5,a7 beq a5,zero,.L11 .L5: mul a3,a5,a6 sraiw a7,a5,31 slliw a4,a0,2 addw a4,a4,a0 slliw a2,a4,1 srai a3,a3,34 subw a3,a3,a7 slliw a4,a3,2 addw a4,a4,a3 slliw a4,a4,1 subw a4,a5,a4 subw a3,t3,a4 subw a1,t1,a4 sraiw t4,a3,31 sraiw t5,a1,31 bgt a5,zero,.L12 mul a1,a1,a6 srai a1,a1,34 subw a1,a1,t5 ble a1,a0,.L4 .L8: li a0,0 ret .L11: ret
215
7
Reverse Integer
Medium
/* 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100. Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases? For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. Note: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. */ int reverse(int x) { int d, k = 0; // 2147483647 //-2147483648 while (x) { d = x % 10; if ((x > 0 && k > (0x7fffffff - d) / 10) || (x < 0 && k < ((signed)0x80000000 - d) / 10)) { return 0; // overflow } k = k * 10 + d; x = x / 10; } return k; //(int)k == k ? (int)k : 0; } /* Difficulty:Easy Total Accepted:275.6K Total Submissions:1.1M Companies Bloomberg Apple Related Topics Math Similar Questions String to Integer (atoi) */
riscv64
-O3
RISC-V 64 gcc 15.2.0
reverse: mv a5,a0 beq a0,zero,.L8 li t1,-2147483648 li a6,1717985280 addi a6,a6,1639 addiw t3,t1,-1 li a0,0 j .L5 .L12: mul a3,a3,a6 srai a3,a3,34 subw a3,a3,t4 blt a3,a0,.L8 .L4: mul a5,a5,a6 addw a0,a2,a4 srai a5,a5,34 subw a5,a5,a7 beq a5,zero,.L11 .L5: mul a3,a5,a6 sraiw a7,a5,31 slliw a4,a0,2 addw a4,a4,a0 slliw a2,a4,1 srai a3,a3,34 subw a3,a3,a7 slliw a4,a3,2 addw a4,a4,a3 slliw a4,a4,1 subw a4,a5,a4 subw a3,t3,a4 subw a1,t1,a4 sraiw t4,a3,31 sraiw t5,a1,31 bgt a5,zero,.L12 mul a1,a1,a6 srai a1,a1,34 subw a1,a1,t5 ble a1,a0,.L4 .L8: li a0,0 ret .L11: ret
216
7
Reverse Integer
Medium
/* 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100. Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases? For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. Note: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. */ int reverse(int x) { int d, k = 0; // 2147483647 //-2147483648 while (x) { d = x % 10; if ((x > 0 && k > (0x7fffffff - d) / 10) || (x < 0 && k < ((signed)0x80000000 - d) / 10)) { return 0; // overflow } k = k * 10 + d; x = x / 10; } return k; //(int)k == k ? (int)k : 0; } /* Difficulty:Easy Total Accepted:275.6K Total Submissions:1.1M Companies Bloomberg Apple Related Topics Math Similar Questions String to Integer (atoi) */
x86-64
-O0
x86-64 clang 21.1.0
reverse: push rbp mov rbp, rsp mov dword ptr [rbp - 8], edi mov dword ptr [rbp - 16], 0 .LBB0_1: cmp dword ptr [rbp - 8], 0 je .LBB0_8 mov eax, dword ptr [rbp - 8] mov ecx, 10 cdq idiv ecx mov dword ptr [rbp - 12], edx cmp dword ptr [rbp - 8], 0 jle .LBB0_4 mov eax, dword ptr [rbp - 16] mov dword ptr [rbp - 20], eax mov eax, 2147483647 sub eax, dword ptr [rbp - 12] mov ecx, 10 cdq idiv ecx mov ecx, eax mov eax, dword ptr [rbp - 20] cmp eax, ecx jg .LBB0_6 .LBB0_4: cmp dword ptr [rbp - 8], 0 jge .LBB0_7 mov eax, dword ptr [rbp - 16] mov dword ptr [rbp - 24], eax mov eax, 2147483648 sub eax, dword ptr [rbp - 12] mov ecx, 10 cdq idiv ecx mov ecx, eax mov eax, dword ptr [rbp - 24] cmp eax, ecx jge .LBB0_7 .LBB0_6: mov dword ptr [rbp - 4], 0 jmp .LBB0_9 .LBB0_7: imul eax, dword ptr [rbp - 16], 10 add eax, dword ptr [rbp - 12] mov dword ptr [rbp - 16], eax mov eax, dword ptr [rbp - 8] mov ecx, 10 cdq idiv ecx mov dword ptr [rbp - 8], eax jmp .LBB0_1 .LBB0_8: mov eax, dword ptr [rbp - 16] mov dword ptr [rbp - 4], eax .LBB0_9: mov eax, dword ptr [rbp - 4] pop rbp ret
217
7
Reverse Integer
Medium
/* 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100. Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases? For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. Note: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. */ int reverse(int x) { int d, k = 0; // 2147483647 //-2147483648 while (x) { d = x % 10; if ((x > 0 && k > (0x7fffffff - d) / 10) || (x < 0 && k < ((signed)0x80000000 - d) / 10)) { return 0; // overflow } k = k * 10 + d; x = x / 10; } return k; //(int)k == k ? (int)k : 0; } /* Difficulty:Easy Total Accepted:275.6K Total Submissions:1.1M Companies Bloomberg Apple Related Topics Math Similar Questions String to Integer (atoi) */
x86-64
-O1
x86-64 clang 21.1.0
reverse: xor eax, eax test edi, edi jne .LBB0_3 .LBB0_1: ret .LBB0_2: lea eax, [rax + 4*rax] lea eax, [rdx + 2*rax] add edi, 9 cmp edi, 18 mov edi, ecx jbe .LBB0_1 .LBB0_3: movsxd rsi, edi imul rcx, rsi, 1717986919 mov rdx, rcx shr rdx, 63 sar rcx, 34 add ecx, edx lea edx, [rcx + rcx] lea r8d, [rdx + 4*rdx] mov edx, esi sub edx, r8d test esi, esi jle .LBB0_5 mov esi, edx xor esi, 2147483647 imul rsi, rsi, 1717986919 shr rsi, 34 cmp eax, esi jg .LBB0_8 .LBB0_5: test edi, edi jns .LBB0_2 mov esi, -2147483648 sub esi, edx movsxd rsi, esi imul rsi, rsi, 1717986919 mov r8, rsi shr r8, 63 sar rsi, 34 add esi, r8d cmp eax, esi jge .LBB0_2 .LBB0_8: xor eax, eax ret
218
7
Reverse Integer
Medium
/* 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100. Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases? For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. Note: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. */ int reverse(int x) { int d, k = 0; // 2147483647 //-2147483648 while (x) { d = x % 10; if ((x > 0 && k > (0x7fffffff - d) / 10) || (x < 0 && k < ((signed)0x80000000 - d) / 10)) { return 0; // overflow } k = k * 10 + d; x = x / 10; } return k; //(int)k == k ? (int)k : 0; } /* Difficulty:Easy Total Accepted:275.6K Total Submissions:1.1M Companies Bloomberg Apple Related Topics Math Similar Questions String to Integer (atoi) */
x86-64
-O2
x86-64 clang 21.1.0
reverse: xor eax, eax test edi, edi je .LBB0_7 mov ecx, 3435973837 jmp .LBB0_4 .LBB0_2: lea r8d, [rsi - 2147483648] imul r8, rcx shr r8, 35 neg r8d cmp eax, r8d jl .LBB0_6 .LBB0_3: lea eax, [rax + 4*rax] lea eax, [rsi + 2*rax] add edi, 9 cmp edi, 18 mov edi, edx jbe .LBB0_7 .LBB0_4: movsxd r8, edi imul rdx, r8, 1717986919 mov rsi, rdx shr rsi, 63 sar rdx, 34 add edx, esi lea esi, [rdx + rdx] lea r9d, [rsi + 4*rsi] mov esi, r8d sub esi, r9d test r8d, r8d jle .LBB0_2 mov r8d, esi xor r8d, 2147483647 imul r8, r8, 1717986919 shr r8, 34 cmp eax, r8d jle .LBB0_3 .LBB0_6: xor eax, eax .LBB0_7: ret
219
7
Reverse Integer
Medium
/* 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100. Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases? For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. Note: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. */ int reverse(int x) { int d, k = 0; // 2147483647 //-2147483648 while (x) { d = x % 10; if ((x > 0 && k > (0x7fffffff - d) / 10) || (x < 0 && k < ((signed)0x80000000 - d) / 10)) { return 0; // overflow } k = k * 10 + d; x = x / 10; } return k; //(int)k == k ? (int)k : 0; } /* Difficulty:Easy Total Accepted:275.6K Total Submissions:1.1M Companies Bloomberg Apple Related Topics Math Similar Questions String to Integer (atoi) */
x86-64
-O3
x86-64 clang 21.1.0
reverse: xor eax, eax test edi, edi je .LBB0_7 mov ecx, 3435973837 jmp .LBB0_4 .LBB0_2: lea r8d, [rsi - 2147483648] imul r8, rcx shr r8, 35 neg r8d cmp eax, r8d jl .LBB0_6 .LBB0_3: lea eax, [rax + 4*rax] lea eax, [rsi + 2*rax] add edi, 9 cmp edi, 18 mov edi, edx jbe .LBB0_7 .LBB0_4: movsxd r8, edi imul rdx, r8, 1717986919 mov rsi, rdx shr rsi, 63 sar rdx, 34 add edx, esi lea esi, [rdx + rdx] lea r9d, [rsi + 4*rsi] mov esi, r8d sub esi, r9d test r8d, r8d jle .LBB0_2 mov r8d, esi xor r8d, 2147483647 imul r8, r8, 1717986919 shr r8, 34 cmp eax, r8d jle .LBB0_3 .LBB0_6: xor eax, eax .LBB0_7: ret
220
7
Reverse Integer
Medium
/* 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100. Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases? For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. Note: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. */ int reverse(int x) { int d, k = 0; // 2147483647 //-2147483648 while (x) { d = x % 10; if ((x > 0 && k > (0x7fffffff - d) / 10) || (x < 0 && k < ((signed)0x80000000 - d) / 10)) { return 0; // overflow } k = k * 10 + d; x = x / 10; } return k; //(int)k == k ? (int)k : 0; } /* Difficulty:Easy Total Accepted:275.6K Total Submissions:1.1M Companies Bloomberg Apple Related Topics Math Similar Questions String to Integer (atoi) */
x86-64
-O0
x86-64 gcc 15.2
reverse: push rbp mov rbp, rsp mov DWORD PTR [rbp-20], edi mov DWORD PTR [rbp-4], 0 jmp .L2 .L7: mov edx, DWORD PTR [rbp-20] movsx rax, edx imul rax, rax, 1717986919 shr rax, 32 mov ecx, eax sar ecx, 2 mov eax, edx sar eax, 31 sub ecx, eax mov eax, ecx sal eax, 2 add eax, ecx add eax, eax sub edx, eax mov DWORD PTR [rbp-8], edx cmp DWORD PTR [rbp-20], 0 jle .L3 mov eax, 2147483647 sub eax, DWORD PTR [rbp-8] movsx rdx, eax imul rdx, rdx, 1717986919 shr rdx, 32 mov ecx, edx sar ecx, 2 cdq mov eax, ecx sub eax, edx cmp DWORD PTR [rbp-4], eax jg .L4 .L3: cmp DWORD PTR [rbp-20], 0 jns .L5 mov eax, -2147483648 sub eax, DWORD PTR [rbp-8] movsx rdx, eax imul rdx, rdx, 1717986919 shr rdx, 32 mov ecx, edx sar ecx, 2 cdq mov eax, ecx sub eax, edx cmp DWORD PTR [rbp-4], eax jge .L5 .L4: mov eax, 0 jmp .L6 .L5: mov edx, DWORD PTR [rbp-4] mov eax, edx sal eax, 2 add eax, edx add eax, eax mov edx, eax mov eax, DWORD PTR [rbp-8] add eax, edx mov DWORD PTR [rbp-4], eax mov eax, DWORD PTR [rbp-20] movsx rdx, eax imul rdx, rdx, 1717986919 shr rdx, 32 mov ecx, edx sar ecx, 2 cdq mov eax, ecx sub eax, edx mov DWORD PTR [rbp-20], eax .L2: cmp DWORD PTR [rbp-20], 0 jne .L7 mov eax, DWORD PTR [rbp-4] .L6: pop rbp ret
221
7
Reverse Integer
Medium
/* 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100. Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases? For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. Note: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. */ int reverse(int x) { int d, k = 0; // 2147483647 //-2147483648 while (x) { d = x % 10; if ((x > 0 && k > (0x7fffffff - d) / 10) || (x < 0 && k < ((signed)0x80000000 - d) / 10)) { return 0; // overflow } k = k * 10 + d; x = x / 10; } return k; //(int)k == k ? (int)k : 0; } /* Difficulty:Easy Total Accepted:275.6K Total Submissions:1.1M Companies Bloomberg Apple Related Topics Math Similar Questions String to Integer (atoi) */
x86-64
-O1
x86-64 gcc 15.2
reverse: mov edx, edi test edi, edi je .L6 mov esi, 0 mov r8d, -2147483648 mov edi, 2147483647 jmp .L5 .L3: mov r9d, r8d sub r9d, eax movsx rcx, r9d imul rcx, rcx, 1717986919 sar rcx, 34 sar r9d, 31 sub ecx, r9d cmp ecx, esi jg .L8 .L4: lea ecx, [rsi+rsi*4] lea esi, [rax+rcx*2] movsx rax, edx imul rax, rax, 1717986919 sar rax, 34 sar edx, 31 sub eax, edx mov edx, eax je .L1 .L5: movsx rax, edx imul rax, rax, 1717986919 sar rax, 34 mov ecx, edx sar ecx, 31 sub eax, ecx lea ecx, [rax+rax*4] add ecx, ecx mov eax, edx sub eax, ecx test edx, edx jle .L3 mov r9d, edi sub r9d, eax movsx rcx, r9d imul rcx, rcx, 1717986919 sar rcx, 34 sar r9d, 31 sub ecx, r9d cmp ecx, esi jge .L4 mov esi, 0 .L1: mov eax, esi ret .L6: mov esi, edi jmp .L1 .L8: mov esi, 0 jmp .L1
222
7
Reverse Integer
Medium
/* 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100. Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases? For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. Note: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. */ int reverse(int x) { int d, k = 0; // 2147483647 //-2147483648 while (x) { d = x % 10; if ((x > 0 && k > (0x7fffffff - d) / 10) || (x < 0 && k < ((signed)0x80000000 - d) / 10)) { return 0; // overflow } k = k * 10 + d; x = x / 10; } return k; //(int)k == k ? (int)k : 0; } /* Difficulty:Easy Total Accepted:275.6K Total Submissions:1.1M Companies Bloomberg Apple Related Topics Math Similar Questions String to Integer (atoi) */
x86-64
-O2
x86-64 gcc 15.2
reverse: mov esi, edi xor edi, edi test esi, esi je .L1 mov r8d, 3435973837 jmp .L5 .L11: mov edx, 2147483647 sub edx, eax imul rdx, r8 shr rdx, 35 cmp edx, edi jl .L8 .L4: lea edx, [rdi+rdi*4] sar esi, 31 lea edi, [rax+rdx*2] imul rax, rcx, 1717986919 sar rax, 34 sub eax, esi mov esi, eax je .L1 .L5: movsx rcx, esi mov edx, esi imul rax, rcx, 1717986919 sar edx, 31 sar rax, 34 sub eax, edx lea edx, [rax+rax*4] mov eax, esi add edx, edx sub eax, edx test esi, esi jg .L11 mov r11d, -2147483648 sub r11d, eax movsx rdx, r11d sar r11d, 31 imul rdx, rdx, 1717986919 sar rdx, 34 sub edx, r11d cmp edx, edi jle .L4 .L8: xor edi, edi .L1: mov eax, edi ret
223
7
Reverse Integer
Medium
/* 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100. Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases? For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. Note: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. */ int reverse(int x) { int d, k = 0; // 2147483647 //-2147483648 while (x) { d = x % 10; if ((x > 0 && k > (0x7fffffff - d) / 10) || (x < 0 && k < ((signed)0x80000000 - d) / 10)) { return 0; // overflow } k = k * 10 + d; x = x / 10; } return k; //(int)k == k ? (int)k : 0; } /* Difficulty:Easy Total Accepted:275.6K Total Submissions:1.1M Companies Bloomberg Apple Related Topics Math Similar Questions String to Integer (atoi) */
x86-64
-O3
x86-64 gcc 15.2
reverse: mov esi, edi xor edi, edi test esi, esi je .L1 mov r8d, 3435973837 jmp .L5 .L11: mov edx, 2147483647 sub edx, eax imul rdx, r8 shr rdx, 35 cmp edx, edi jl .L8 .L4: lea edx, [rdi+rdi*4] sar esi, 31 lea edi, [rax+rdx*2] imul rax, rcx, 1717986919 sar rax, 34 sub eax, esi mov esi, eax je .L1 .L5: movsx rcx, esi mov edx, esi imul rax, rcx, 1717986919 sar edx, 31 sar rax, 34 sub eax, edx lea edx, [rax+rax*4] mov eax, esi add edx, edx sub eax, edx test esi, esi jg .L11 mov r11d, -2147483648 sub r11d, eax movsx rdx, r11d sar r11d, 31 imul rdx, rdx, 1717986919 sar rdx, 34 sub edx, r11d cmp edx, edi jle .L4 .L8: xor edi, edi .L1: mov eax, edi ret
224
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
aarch64
-O0
ARM64 gcc 15.2.0
myAtoi: sub sp, sp, #32 str x0, [sp, 8] str wzr, [sp, 28] str wzr, [sp, 24] ldr x0, [sp, 8] cmp x0, 0 bne .L4 mov w0, 0 b .L3 .L5: ldr x0, [sp, 8] add x0, x0, 1 str x0, [sp, 8] .L4: ldr x0, [sp, 8] ldrb w0, [x0] cmp w0, 32 beq .L5 ldr x0, [sp, 8] ldrb w0, [x0] cmp w0, 43 bne .L7 ldr x0, [sp, 8] add x0, x0, 1 str x0, [sp, 8] mov w0, 1 str w0, [sp, 24] ldr x0, [sp, 8] add x0, x0, 1 str x0, [sp, 8] b .L7 .L12: ldr x0, [sp, 8] ldrb w0, [x0] sub w0, w0, #48 str w0, [sp, 20] ldr x0, [sp, 8] add x0, x0, 1 str x0, [sp, 8] ldr w0, [sp, 24] cmp w0, 0 bne .L8 mov w1, 2147483647 ldr w0, [sp, 20] sub w0, w1, w0 mov w1, 26215 movk w1, 0x6666, lsl 16 smull x1, w0, w1 lsr x1, x1, 32 asr w1, w1, 2 asr w0, w0, 31 sub w0, w1, w0 ldr w1, [sp, 28] cmp w1, w0 ble .L9 mov w0, 2147483647 b .L3 .L9: ldr w1, [sp, 28] mov w0, w1 lsl w0, w0, 2 add w0, w0, w1 lsl w0, w0, 1 mov w1, w0 ldr w0, [sp, 20] add w0, w0, w1 str w0, [sp, 28] b .L7 .L8: ldr w1, [sp, 20] mov w0, -2147483648 add w0, w1, w0 mov w1, 26215 movk w1, 0x6666, lsl 16 smull x1, w0, w1 lsr x1, x1, 32 asr w1, w1, 2 asr w0, w0, 31 sub w0, w1, w0 ldr w1, [sp, 28] cmp w1, w0 bge .L10 mov w0, -2147483648 b .L3 .L10: ldr w1, [sp, 28] mov w0, w1 lsl w0, w0, 2 add w0, w0, w1 lsl w0, w0, 1 mov w1, w0 ldr w0, [sp, 20] sub w0, w1, w0 str w0, [sp, 28] .L7: ldr x0, [sp, 8] ldrb w0, [x0] cmp w0, 47 bls .L11 ldr x0, [sp, 8] ldrb w0, [x0] cmp w0, 57 bls .L12 .L11: ldr w0, [sp, 28] .L3: add sp, sp, 32 ret
225
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
aarch64
-O1
ARM64 gcc 15.2.0
myAtoi: mov x2, x0 cbz x0, .L9 ldrb w0, [x0] cmp w0, 32 bne .L3 .L4: ldrb w0, [x2, 1]! cmp w0, 32 beq .L4 .L3: add x1, x2, 2 cmp w0, 43 csel x2, x1, x2, eq cset w5, eq ldrb w1, [x2] sub w3, w1, #48 and w3, w3, 255 mov w0, 0 cmp w3, 9 bhi .L17 mov w8, 65488 movk w8, 0x7fff, lsl 16 mov w6, 26215 movk w6, 0x6666, lsl 16 mov w7, 2147483647 b .L8 .L6: add w1, w1, w8 smull x4, w1, w6 asr x4, x4, 34 sub w1, w4, w1, asr 31 cmp w1, w0 bgt .L12 add w0, w0, w0, lsl 2 lsl w0, w0, 1 sub w0, w0, w3 .L7: ldrb w1, [x2, 1]! sub w3, w1, #48 and w3, w3, 255 cmp w3, 9 bhi .L1 .L8: sub w3, w1, #48 cbnz w5, .L6 sub w1, w7, w3 smull x4, w1, w6 asr x4, x4, 34 sub w1, w4, w1, asr 31 cmp w1, w0 blt .L11 add w0, w0, w0, lsl 2 add w0, w3, w0, lsl 1 b .L7 .L17: mov w0, 0 b .L1 .L9: mov w0, 0 b .L1 .L11: mov w0, 2147483647 .L1: ret .L12: mov w0, -2147483648 b .L1
226
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
aarch64
-O2
ARM64 gcc 15.2.0
myAtoi: mov x3, x0 cbz x0, .L11 ldrb w0, [x0] cmp w0, 32 bne .L3 .L4: ldrb w0, [x3, 1]! cmp w0, 32 beq .L4 .L3: add x1, x3, 2 cmp w0, 43 csel x3, x1, x3, eq cset w0, eq ldrb w2, [x3] sub w1, w2, #48 and w4, w1, 255 cmp w4, 9 bhi .L11 cbnz w0, .L13 mov w4, 52429 mov w5, 2147483647 movk w4, 0xcccc, lsl 16 b .L10 .L20: add w0, w0, w0, lsl 2 add w0, w1, w0, lsl 1 ldrb w1, [x3, 1]! sub w1, w1, #48 and w2, w1, 255 cmp w2, 9 bhi .L1 .L10: sub w2, w5, w1 umull x2, w2, w4 lsr x2, x2, 35 cmp w2, w0 bge .L20 mov w0, 2147483647 .L1: ret .L13: mov w6, 65488 mov w5, 26215 mov w0, 0 movk w6, 0x7fff, lsl 16 movk w5, 0x6666, lsl 16 b .L6 .L21: add w0, w0, w0, lsl 2 ldrb w2, [x3, 1]! lsl w0, w0, 1 sub w0, w0, w1 sub w1, w2, #48 and w4, w1, 255 cmp w4, 9 bhi .L1 .L6: add w2, w2, w6 smull x4, w2, w5 asr x4, x4, 34 sub w2, w4, w2, asr 31 cmp w2, w0 ble .L21 mov w0, -2147483648 ret .L11: mov w0, 0 ret
227
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
aarch64
-O3
ARM64 gcc 15.2.0
myAtoi: mov x2, x0 cbz x0, .L9 ldrb w1, [x0] cmp w1, 32 bne .L3 .L4: ldrb w1, [x2, 1]! cmp w1, 32 beq .L4 .L3: cmp w1, 43 beq .L20 sub w3, w1, #48 mov w5, 52429 and w1, w3, 255 mov w0, 0 mov w6, 2147483647 movk w5, 0xcccc, lsl 16 cmp w1, 9 bls .L8 b .L1 .L21: ldrb w1, [x2, 1]! add w0, w3, w4, lsl 1 sub w3, w1, #48 and w1, w3, 255 cmp w1, 9 bhi .L1 .L8: sub w1, w6, w3 add w4, w0, w0, lsl 2 umull x1, w1, w5 lsr x1, x1, 35 cmp w1, w0 bge .L21 mov w0, 2147483647 .L1: ret .L20: ldrb w1, [x2, 2] sub w5, w1, #48 and w0, w5, 255 cmp w0, 9 mov w0, 0 bhi .L1 mov w7, 65488 mov w6, 26215 add x2, x2, 2 movk w7, 0x7fff, lsl 16 movk w6, 0x6666, lsl 16 b .L6 .L22: ldrb w1, [x2, 1]! sub w0, w4, w5 sub w5, w1, #48 and w3, w5, 255 cmp w3, 9 bhi .L1 .L6: add w1, w1, w7 add w3, w0, w0, lsl 2 lsl w4, w3, 1 smull x3, w1, w6 asr x3, x3, 34 sub w1, w3, w1, asr 31 cmp w1, w0 ble .L22 mov w0, -2147483648 ret .L9: mov w0, 0 ret
228
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
aarch64
-O0
armv8-a clang 21.1.0
myAtoi: sub sp, sp, #32 str x0, [sp, #16] str wzr, [sp, #8] str wzr, [sp, #4] ldr x8, [sp, #16] cbnz x8, .LBB0_2 b .LBB0_1 .LBB0_1: str wzr, [sp, #28] b .LBB0_20 .LBB0_2: b .LBB0_3 .LBB0_3: ldr x8, [sp, #16] ldrb w8, [x8] subs w8, w8, #32 b.ne .LBB0_5 b .LBB0_4 .LBB0_4: ldr x8, [sp, #16] add x8, x8, #1 str x8, [sp, #16] b .LBB0_3 .LBB0_5: ldr x8, [sp, #16] ldrb w8, [x8] subs w8, w8, #43 b.ne .LBB0_7 b .LBB0_6 .LBB0_6: ldr x8, [sp, #16] add x8, x8, #1 str x8, [sp, #16] mov w8, #1 str w8, [sp, #4] ldr x8, [sp, #16] add x8, x8, #1 str x8, [sp, #16] b .LBB0_7 .LBB0_7: b .LBB0_8 .LBB0_8: ldr x8, [sp, #16] ldrb w9, [x8] mov w8, #0 subs w9, w9, #48 str w8, [sp] b.lt .LBB0_10 b .LBB0_9 .LBB0_9: ldr x8, [sp, #16] ldrb w8, [x8] subs w8, w8, #57 cset w8, le str w8, [sp] b .LBB0_10 .LBB0_10: ldr w8, [sp] tbz w8, #0, .LBB0_19 b .LBB0_11 .LBB0_11: ldr x8, [sp, #16] ldrb w8, [x8] subs w8, w8, #48 str w8, [sp, #12] ldr x8, [sp, #16] add x8, x8, #1 str x8, [sp, #16] ldr w8, [sp, #4] cbnz w8, .LBB0_15 b .LBB0_12 .LBB0_12: ldr w8, [sp, #8] ldr w10, [sp, #12] mov w9, #2147483647 subs w9, w9, w10 mov w10, #10 sdiv w9, w9, w10 subs w8, w8, w9 b.le .LBB0_14 b .LBB0_13 .LBB0_13: mov w8, #2147483647 str w8, [sp, #28] b .LBB0_20 .LBB0_14: ldr w8, [sp, #8] mov w9, #10 mul w8, w8, w9 ldr w9, [sp, #12] add w8, w8, w9 str w8, [sp, #8] b .LBB0_18 .LBB0_15: ldr w8, [sp, #8] ldr w10, [sp, #12] mov w9, #-2147483648 add w9, w9, w10 mov w10, #10 sdiv w9, w9, w10 subs w8, w8, w9 b.ge .LBB0_17 b .LBB0_16 .LBB0_16: mov w8, #-2147483648 str w8, [sp, #28] b .LBB0_20 .LBB0_17: ldr w8, [sp, #8] mov w9, #10 mul w8, w8, w9 ldr w9, [sp, #12] subs w8, w8, w9 str w8, [sp, #8] b .LBB0_18 .LBB0_18: b .LBB0_8 .LBB0_19: ldr w8, [sp, #8] str w8, [sp, #28] b .LBB0_20 .LBB0_20: ldr w0, [sp, #28] add sp, sp, #32 ret
229
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
aarch64
-O1
armv8-a clang 21.1.0
myAtoi: cbz x0, .LBB0_10 .LBB0_1: ldrb w9, [x0], #1 cmp w9, #32 b.eq .LBB0_1 cmp w9, #43 mov w8, #2 csel x10, x8, xzr, eq add x8, x0, x10 ldurb w15, [x8, #-1] sub w8, w15, #48 cmp w8, #9 b.hi .LBB0_10 mov w11, #65488 mov w12, #26215 mov w14, #52429 mov w8, wzr movk w11, #32767, lsl #16 movk w12, #26214, lsl #16 mov w13, #10 movk w14, #52428, lsl #16 b .LBB0_5 .LBB0_4: ldrb w15, [x0, x10] add x10, x10, #1 sub w16, w15, #48 cmp w16, #10 b.hs .LBB0_11 .LBB0_5: cmp w9, #43 sub w16, w15, #48 b.ne .LBB0_8 add w15, w15, w11 smull x15, w15, w12 asr x15, x15, #34 add w15, w15, w15, lsr #31 cmp w8, w15 b.lt .LBB0_13 neg w15, w16 madd w8, w8, w13, w15 b .LBB0_4 .LBB0_8: sub w15, w11, w15 add w15, w15, #95 umull x15, w15, w14 lsr x15, x15, #35 cmp w8, w15 b.gt .LBB0_12 madd w8, w8, w13, w16 b .LBB0_4 .LBB0_10: mov w8, wzr .LBB0_11: mov w0, w8 ret .LBB0_12: mov w0, #2147483647 ret .LBB0_13: mov w0, #-2147483648 ret
230
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
aarch64
-O2
armv8-a clang 21.1.0
myAtoi: cbz x0, .LBB0_10 .LBB0_1: ldrb w9, [x0], #1 cmp w9, #32 b.eq .LBB0_1 cmp w9, #43 mov w8, #2 csel x10, x8, xzr, eq add x8, x0, x10 ldurb w14, [x8, #-1] sub w8, w14, #48 cmp w8, #9 b.hi .LBB0_10 mov w11, #47 mov w12, #52429 mov w8, wzr movk w11, #32768, lsl #16 movk w12, #52428, lsl #16 mov w13, #10 b .LBB0_5 .LBB0_4: ldrb w14, [x0, x10] add x10, x10, #1 sub w15, w14, #48 cmp w15, #10 b.hs .LBB0_11 .LBB0_5: cmp w9, #43 sub w15, w14, #48 sub w14, w11, w14 b.ne .LBB0_8 add w14, w14, #1 umull x14, w14, w12 lsr x14, x14, #35 cmn w8, w14 b.lt .LBB0_13 neg w14, w15 madd w8, w8, w13, w14 b .LBB0_4 .LBB0_8: umull x14, w14, w12 lsr x14, x14, #35 cmp w8, w14 b.gt .LBB0_12 madd w8, w8, w13, w15 b .LBB0_4 .LBB0_10: mov w8, wzr .LBB0_11: mov w0, w8 ret .LBB0_12: mov w0, #2147483647 ret .LBB0_13: mov w0, #-2147483648 ret
231
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
aarch64
-O3
armv8-a clang 21.1.0
myAtoi: cbz x0, .LBB0_8 .LBB0_1: ldrb w8, [x0], #1 cmp w8, #32 b.eq .LBB0_1 cmp w8, #43 mov w9, #2 csel x9, x9, xzr, eq add x10, x0, x9 ldurb w10, [x10, #-1] sub w11, w10, #48 cmp w11, #9 b.hi .LBB0_8 mov w11, #65488 cmp w8, #43 mov w8, wzr movk w11, #32767, lsl #16 b.ne .LBB0_9 mov w12, #26215 mov w13, #10 movk w12, #26214, lsl #16 .LBB0_5: add w14, w10, w11 smull x14, w14, w12 asr x14, x14, #34 add w14, w14, w14, lsr #31 cmp w8, w14 b.lt .LBB0_12 neg w14, w10 ldrb w10, [x0, x9] madd w8, w8, w13, w14 add x9, x9, #1 sub w14, w10, #48 cmp w14, #10 add w8, w8, #48 b.lo .LBB0_5 .LBB0_7: mov w0, w8 ret .LBB0_8: mov w8, wzr mov w0, w8 ret .LBB0_9: mov w12, #52429 mov w13, #10 movk w12, #52428, lsl #16 .LBB0_10: sub w14, w11, w10 add w14, w14, #95 umull x14, w14, w12 lsr x14, x14, #35 cmp w8, w14 b.gt .LBB0_13 madd w8, w8, w13, w10 ldrb w10, [x0, x9] add x9, x9, #1 sub w14, w10, #48 cmp w14, #10 sub w8, w8, #48 b.lo .LBB0_10 b .LBB0_7 .LBB0_12: mov w0, #-2147483648 ret .LBB0_13: mov w0, #2147483647 ret
232
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
mips64
-O0
mips64 clang 21.1.0
myAtoi: .Lfunc_begin0 = .Ltmp0 daddiu $sp, $sp, -48 sd $ra, 40($sp) sd $fp, 32($sp) move $fp, $sp sd $4, 16($fp) sw $zero, 8($fp) sw $zero, 4($fp) ld $1, 16($fp) bnez $1, .LBB0_3 nop b .LBB0_2 nop .LBB0_2: sw $zero, 28($fp) b .LBB0_28 nop .LBB0_3: b .LBB0_4 nop .LBB0_4: ld $1, 16($fp) lb $1, 0($1) addiu $2, $zero, 32 bne $1, $2, .LBB0_7 nop b .LBB0_6 nop .LBB0_6: ld $1, 16($fp) daddiu $1, $1, 1 sd $1, 16($fp) b .LBB0_4 nop .LBB0_7: ld $1, 16($fp) lb $1, 0($1) addiu $2, $zero, 43 bne $1, $2, .LBB0_10 nop b .LBB0_9 nop .LBB0_9: ld $1, 16($fp) daddiu $1, $1, 1 sd $1, 16($fp) addiu $1, $zero, 1 sw $1, 4($fp) ld $1, 16($fp) daddiu $1, $1, 1 sd $1, 16($fp) b .LBB0_10 nop .LBB0_10: b .LBB0_11 nop .LBB0_11: ld $1, 16($fp) lb $1, 0($1) slti $1, $1, 48 addiu $2, $zero, 0 sw $2, 0($fp) bnez $1, .LBB0_14 nop b .LBB0_13 nop .LBB0_13: ld $1, 16($fp) lb $1, 0($1) slti $1, $1, 58 sw $1, 0($fp) b .LBB0_14 nop .LBB0_14: lw $1, 0($fp) andi $1, $1, 1 beqz $1, .LBB0_27 nop b .LBB0_16 nop .LBB0_16: ld $1, 16($fp) lb $1, 0($1) addiu $1, $1, -48 sw $1, 12($fp) ld $1, 16($fp) daddiu $1, $1, 1 sd $1, 16($fp) lw $1, 4($fp) bnez $1, .LBB0_22 nop b .LBB0_18 nop .LBB0_18: lw $2, 8($fp) lw $1, 12($fp) lui $3, 32767 ori $3, $3, 65535 xor $1, $1, $3 lui $3, 26214 ori $3, $3, 26215 multu $1, $3 mfhi $1 srl $1, $1, 2 slt $1, $1, $2 beqz $1, .LBB0_21 nop b .LBB0_20 nop .LBB0_20: lui $1, 32767 ori $1, $1, 65535 sw $1, 28($fp) b .LBB0_28 nop .LBB0_21: lw $1, 8($fp) sll $2, $1, 1 sll $1, $1, 3 addu $1, $1, $2 lw $2, 12($fp) addu $1, $1, $2 sw $1, 8($fp) b .LBB0_26 nop .LBB0_22: lw $1, 8($fp) lw $2, 12($fp) lui $3, 32768 addu $2, $2, $3 lui $3, 26214 ori $3, $3, 26215 mult $2, $3 mfhi $2 srl $3, $2, 31 sra $2, $2, 2 addu $2, $2, $3 slt $1, $1, $2 beqz $1, .LBB0_25 nop b .LBB0_24 nop .LBB0_24: lui $1, 32768 sw $1, 28($fp) b .LBB0_28 nop .LBB0_25: lw $1, 8($fp) sll $2, $1, 1 sll $1, $1, 3 addu $1, $1, $2 lw $2, 12($fp) subu $1, $1, $2 sw $1, 8($fp) b .LBB0_26 nop .LBB0_26: b .LBB0_11 nop .LBB0_27: lw $1, 8($fp) sw $1, 28($fp) b .LBB0_28 nop .LBB0_28: lw $2, 28($fp) move $sp, $fp ld $fp, 32($sp) ld $ra, 40($sp) daddiu $sp, $sp, 48 jr $ra nop
233
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
mips64
-O1
mips64 clang 21.1.0
myAtoi: .Lfunc_begin0 = .Ltmp0 beqz $4, .LBB0_13 addiu $9, $zero, 0 daddiu $sp, $sp, -16 sd $ra, 8($sp) sd $fp, 0($sp) move $fp, $sp addiu $3, $zero, 32 .LBB0_2: lbu $2, 0($4) beq $2, $3, .LBB0_2 daddiu $4, $4, 1 addiu $3, $zero, 43 xor $1, $2, $3 daddiu $5, $zero, 2 movn $5, $zero, $1 daddu $4, $4, $5 lbu $10, -1($4) addiu $1, $10, -48 andi $1, $1, 255 sltiu $1, $1, 10 beqz $1, .LBB0_12 nop addiu $7, $zero, 0 lui $1, 26214 ori $5, $1, 26215 lui $1, 32767 ori $8, $1, 65534 b .LBB0_6 ori $6, $1, 65535 .LBB0_5: daddiu $1, $4, 1 lbu $10, 0($4) addiu $4, $10, -48 andi $4, $4, 255 sltiu $11, $4, 10 move $4, $1 beqz $11, .LBB0_12 move $9, $7 .LBB0_6: addiu $1, $10, -48 bne $2, $3, .LBB0_9 andi $10, $1, 255 lui $9, 32768 or $1, $10, $9 mult $1, $5 mfhi $1 srl $11, $1, 31 sra $1, $1, 2 addu $1, $1, $11 slt $1, $7, $1 bnez $1, .LBB0_12 nop sll $1, $7, 1 sll $7, $7, 3 addu $1, $7, $1 b .LBB0_5 subu $7, $1, $10 .LBB0_9: xor $1, $10, $8 multu $1, $5 mfhi $1 srl $1, $1, 2 slt $1, $1, $7 bnez $1, .LBB0_11 nop sll $1, $7, 1 sll $7, $7, 3 addu $1, $7, $1 b .LBB0_5 addu $7, $1, $10 .LBB0_11: move $9, $6 .LBB0_12: move $sp, $fp ld $fp, 0($sp) ld $ra, 8($sp) daddiu $sp, $sp, 16 .LBB0_13: jr $ra sll $2, $9, 0
234
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
mips64
-O2
mips64 clang 21.1.0
myAtoi: .Lfunc_begin0 = .Ltmp0 beqz $4, .LBB0_13 addiu $10, $zero, 0 daddiu $sp, $sp, -16 sd $ra, 8($sp) sd $fp, 0($sp) move $fp, $sp addiu $3, $zero, 32 .LBB0_2: lbu $2, 0($4) beq $2, $3, .LBB0_2 daddiu $4, $4, 1 addiu $3, $zero, 43 xor $1, $2, $3 daddiu $5, $zero, 2 movn $5, $zero, $1 daddu $4, $4, $5 lbu $11, -1($4) addiu $1, $11, -48 andi $1, $1, 255 sltiu $1, $1, 10 beqz $1, .LBB0_12 nop addiu $6, $zero, 0 lui $1, 52428 ori $5, $1, 52429 lui $1, 32767 ori $7, $1, 65534 lui $8, 26214 ori $9, $8, 26215 b .LBB0_6 ori $8, $1, 65535 .LBB0_5: daddiu $1, $4, 1 lbu $11, 0($4) addiu $4, $11, -48 andi $4, $4, 255 sltiu $12, $4, 10 move $4, $1 beqz $12, .LBB0_12 move $10, $6 .LBB0_6: addiu $1, $11, -48 bne $2, $3, .LBB0_9 andi $11, $1, 255 lui $10, 32768 subu $1, $10, $11 multu $1, $5 mfhi $1 srl $1, $1, 3 negu $1, $1 slt $1, $6, $1 bnez $1, .LBB0_12 nop sll $1, $6, 1 sll $6, $6, 3 addu $1, $6, $1 b .LBB0_5 subu $6, $1, $11 .LBB0_9: xor $1, $11, $7 multu $1, $9 mfhi $1 srl $1, $1, 2 slt $1, $1, $6 bnez $1, .LBB0_11 nop sll $1, $6, 1 sll $6, $6, 3 addu $1, $6, $1 b .LBB0_5 addu $6, $1, $11 .LBB0_11: move $10, $8 .LBB0_12: move $sp, $fp ld $fp, 0($sp) ld $ra, 8($sp) daddiu $sp, $sp, 16 .LBB0_13: jr $ra sll $2, $10, 0
235
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
mips64
-O3
mips64 clang 21.1.0
myAtoi: .Lfunc_begin0 = .Ltmp0 beqz $4, .LBB0_15 addiu $2, $zero, 0 daddiu $sp, $sp, -16 sd $ra, 8($sp) sd $fp, 0($sp) move $fp, $sp addiu $3, $zero, 32 .LBB0_2: lbu $6, 0($4) beq $6, $3, .LBB0_2 daddiu $4, $4, 1 addiu $8, $zero, 43 daddiu $7, $zero, 2 xor $1, $6, $8 movn $7, $zero, $1 daddu $5, $4, $7 lbu $3, -1($5) addiu $1, $3, -48 andi $1, $1, 255 sltiu $1, $1, 10 beqz $1, .LBB0_14 nop bne $6, $8, .LBB0_9 nop lui $1, 52428 daddu $6, $4, $7 addiu $5, $zero, 0 ori $4, $1, 52429 .LBB0_6: addiu $1, $3, -48 lui $2, 32768 andi $3, $1, 255 subu $1, $2, $3 multu $1, $4 mfhi $1 srl $1, $1, 3 negu $1, $1 slt $1, $5, $1 bnez $1, .LBB0_14 nop sll $1, $5, 1 sll $2, $5, 3 addu $1, $2, $1 subu $5, $1, $3 lbu $3, 0($6) daddiu $1, $6, 1 move $6, $1 addiu $2, $3, -48 andi $2, $2, 255 sltiu $7, $2, 10 bnez $7, .LBB0_6 move $2, $5 b .LBB0_14 nop .LBB0_9: lui $1, 32767 lui $6, 26214 addiu $2, $zero, 0 ori $7, $6, 26215 ori $4, $1, 65534 ori $6, $1, 65535 .LBB0_10: addiu $1, $3, -48 andi $3, $1, 255 xor $1, $3, $4 multu $1, $7 mfhi $1 srl $1, $1, 2 slt $1, $1, $2 bnez $1, .LBB0_13 nop sll $1, $2, 1 sll $2, $2, 3 addu $1, $2, $1 addu $2, $1, $3 lbu $3, 0($5) daddiu $1, $5, 1 addiu $5, $3, -48 andi $5, $5, 255 sltiu $8, $5, 10 bnez $8, .LBB0_10 move $5, $1 b .LBB0_14 nop .LBB0_13: move $2, $6 .LBB0_14: move $sp, $fp ld $ra, 8($sp) ld $fp, 0($sp) daddiu $sp, $sp, 16 .LBB0_15: jr $ra sll $2, $2, 0
236
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
mips64
-O0
mips64 gcc 15.2.0
myAtoi: daddiu $sp,$sp,-48 sd $fp,40($sp) move $fp,$sp sd $4,16($fp) sw $0,0($fp) sw $0,4($fp) ld $2,16($fp) bne $2,$0,.L4 nop move $2,$0 b .L3 nop .L5: ld $2,16($fp) daddiu $2,$2,1 sd $2,16($fp) .L4: ld $2,16($fp) lb $2,0($2) li $3,32 # 0x20 beq $2,$3,.L5 nop ld $2,16($fp) lb $2,0($2) li $3,43 # 0x2b bne $2,$3,.L7 nop ld $2,16($fp) daddiu $2,$2,1 sd $2,16($fp) li $2,1 # 0x1 sw $2,4($fp) ld $2,16($fp) daddiu $2,$2,1 sd $2,16($fp) b .L7 nop .L12: ld $2,16($fp) lb $2,0($2) addiu $2,$2,-48 sw $2,8($fp) ld $2,16($fp) daddiu $2,$2,1 sd $2,16($fp) lw $2,4($fp) bne $2,$0,.L8 nop li $2,2147418112 # 0x7fff0000 ori $3,$2,0xffff lw $2,8($fp) subu $2,$3,$2 move $5,$2 move $3,$5 move $2,$3 dsll $2,$2,1 daddu $2,$2,$3 dsll $4,$2,4 daddu $2,$2,$4 dsll $4,$2,8 daddu $2,$2,$4 dsll $4,$2,16 daddu $2,$2,$4 dsll $2,$2,1 daddu $2,$2,$3 dsrl $2,$2,32 sll $2,$2,0 sra $3,$2,2 sra $2,$5,31 subu $2,$3,$2 lw $3,0($fp) slt $2,$2,$3 beq $2,$0,.L9 nop li $2,2147418112 # 0x7fff0000 ori $2,$2,0xffff b .L3 nop .L9: lw $3,0($fp) move $2,$3 sll $2,$2,2 addu $2,$2,$3 sll $2,$2,1 lw $3,8($fp) addu $2,$3,$2 sw $2,0($fp) b .L7 nop .L8: lw $2,8($fp) li $3,-2147483648 # 0xffffffff80000000 addu $2,$2,$3 move $5,$2 move $3,$5 move $2,$3 dsll $2,$2,1 daddu $2,$2,$3 dsll $4,$2,4 daddu $2,$2,$4 dsll $4,$2,8 daddu $2,$2,$4 dsll $4,$2,16 daddu $2,$2,$4 dsll $2,$2,1 daddu $2,$2,$3 dsrl $2,$2,32 sll $2,$2,0 sra $3,$2,2 sra $2,$5,31 subu $2,$3,$2 lw $3,0($fp) slt $2,$3,$2 beq $2,$0,.L10 nop li $2,-2147483648 # 0xffffffff80000000 b .L3 nop .L10: lw $3,0($fp) move $2,$3 sll $2,$2,2 addu $2,$2,$3 sll $2,$2,1 move $3,$2 lw $2,8($fp) subu $2,$3,$2 sw $2,0($fp) .L7: ld $2,16($fp) lb $2,0($2) slt $2,$2,48 bne $2,$0,.L11 nop ld $2,16($fp) lb $2,0($2) slt $2,$2,58 bne $2,$0,.L12 nop .L11: lw $2,0($fp) .L3: move $sp,$fp ld $fp,40($sp) daddiu $sp,$sp,48 jr $31 nop
237
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
mips64
-O1
mips64 gcc 15.2.0
myAtoi: beq $4,$0,.L9 li $3,32 # 0x20 lb $2,0($4) bnel $2,$3,.L21 li $3,43 # 0x2b daddiu $4,$4,1 .L22: lb $2,0($4) beql $2,$3,.L22 daddiu $4,$4,1 li $3,43 # 0x2b .L21: beq $2,$3,.L19 move $7,$0 .L5: lb $9,0($4) addiu $2,$9,-48 andi $2,$2,0x00ff sltu $5,$2,10 beq $5,$0,.L23 move $2,$0 li $6,2147418112 # 0x7fff0000 ori $8,$6,0xffd0 b .L8 ori $6,$6,0xffff .L19: daddiu $4,$4,2 b .L5 li $7,1 # 0x1 .L6: addu $10,$9,$8 addu $9,$8,$9 dsll $3,$9,1 daddu $3,$3,$9 dsll $11,$3,4 daddu $3,$3,$11 dsll $11,$3,8 daddu $3,$3,$11 dsll $11,$3,16 daddu $3,$3,$11 dsll $3,$3,1 daddu $3,$3,$9 dsra $3,$3,32 sra $3,$3,2 sra $10,$10,31 subu $3,$3,$10 slt $3,$2,$3 bne $3,$0,.L12 sll $3,$2,2 addu $3,$3,$2 sll $3,$3,1 subu $2,$3,$5 .L7: daddiu $4,$4,1 lb $9,0($4) addiu $5,$9,-48 andi $5,$5,0x00ff sltu $5,$5,10 beq $5,$0,.L23 nop .L8: addiu $5,$9,-48 dsll $5,$5,56 bne $7,$0,.L6 dsra $5,$5,56 subu $9,$6,$5 subu $10,$6,$5 dsll $3,$10,1 daddu $3,$3,$10 dsll $11,$3,4 daddu $3,$3,$11 dsll $11,$3,8 daddu $3,$3,$11 dsll $11,$3,16 daddu $3,$3,$11 dsll $3,$3,1 daddu $3,$3,$10 dsra $3,$3,32 sra $3,$3,2 sra $9,$9,31 subu $3,$3,$9 slt $3,$3,$2 bne $3,$0,.L11 sll $3,$2,2 addu $3,$3,$2 sll $3,$3,1 b .L7 addu $2,$3,$5 .L23: jr $31 nop .L9: jr $31 move $2,$0 .L11: li $2,2147418112 # 0x7fff0000 jr $31 ori $2,$2,0xffff .L12: jr $31 li $2,-2147483648 # 0xffffffff80000000
238
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
mips64
-O2
mips64 gcc 15.2.0
myAtoi: beq $4,$0,.L11 nop lb $3,0($4) li $2,32 # 0x20 bne $3,$2,.L23 li $5,43 # 0x2b .L4: lb $3,1($4) beq $3,$2,.L4 daddiu $4,$4,1 li $5,43 # 0x2b .L23: bne $3,$5,.L5 move $2,$0 daddiu $4,$4,2 li $2,1 # 0x1 .L5: lb $6,0($4) addiu $5,$6,-48 andi $3,$5,0x00ff sltu $3,$3,10 beq $3,$0,.L11 dsll $5,$5,56 bne $2,$0,.L13 dsra $5,$5,56 li $8,2147418112 # 0x7fff0000 b .L10 ori $8,$8,0xffff .L20: lb $3,0($4) addu $2,$6,$5 addiu $3,$3,-48 andi $5,$3,0x00ff sltu $5,$5,10 beq $5,$0,.L24 dsll $5,$3,56 dsra $5,$5,56 .L10: subu $6,$8,$5 dsll $6,$6,32 dsrl $6,$6,32 dsll $3,$6,1 daddu $3,$3,$6 dsll $7,$3,4 daddu $3,$3,$7 dsll $7,$3,8 daddu $3,$3,$7 dsll $7,$3,16 daddu $3,$3,$7 dsll $3,$3,2 daddu $3,$3,$6 dsrl $3,$3,35 sll $6,$2,2 addu $6,$6,$2 slt $3,$3,$2 daddiu $4,$4,1 beq $3,$0,.L20 sll $6,$6,1 li $2,2147418112 # 0x7fff0000 jr $31 ori $2,$2,0xffff .L13: li $8,2147418112 # 0x7fff0000 move $2,$0 b .L6 ori $8,$8,0xffd0 .L22: lb $6,0($4) subu $2,$2,$5 addiu $5,$6,-48 andi $3,$5,0x00ff sltu $3,$3,10 beq $3,$0,.L24 dsll $5,$5,56 dsra $5,$5,56 .L6: addu $7,$8,$6 dsll $3,$7,1 daddu $3,$3,$7 dsll $9,$3,4 daddu $3,$3,$9 dsll $9,$3,8 daddu $3,$3,$9 dsll $9,$3,16 daddu $3,$3,$9 dsll $3,$3,1 daddu $3,$3,$7 addu $6,$6,$8 dsra $3,$3,32 sra $3,$3,2 sra $6,$6,31 sll $7,$2,2 subu $3,$3,$6 addu $7,$7,$2 slt $3,$2,$3 daddiu $4,$4,1 beq $3,$0,.L22 sll $2,$7,1 jr $31 li $2,-2147483648 # 0xffffffff80000000 .L11: jr $31 move $2,$0 .L24: jr $31 nop
239
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
mips64
-O3
mips64 gcc 15.2.0
myAtoi: beq $4,$0,.L10 nop lb $3,0($4) li $2,32 # 0x20 bnel $3,$2,.L25 li $2,43 # 0x2b .L4: lb $3,1($4) beq $3,$2,.L4 daddiu $4,$4,1 li $2,43 # 0x2b .L25: beq $3,$2,.L20 addiu $3,$3,-48 andi $5,$3,0x00ff li $8,2147418112 # 0x7fff0000 sltu $5,$5,10 move $2,$0 bne $5,$0,.L8 ori $8,$8,0xffff jr $31 nop .L22: lb $3,0($4) addu $2,$5,$6 addiu $3,$3,-48 andi $5,$3,0x00ff sltu $5,$5,10 beq $5,$0,.L26 nop .L8: dsll $6,$3,56 dsra $6,$6,56 subu $5,$8,$6 dsll $5,$5,32 dsrl $5,$5,32 dsll $3,$5,1 daddu $3,$3,$5 dsll $7,$3,4 daddu $3,$3,$7 dsll $7,$3,8 daddu $3,$3,$7 dsll $7,$3,16 daddu $3,$3,$7 dsll $3,$3,2 daddu $3,$3,$5 dsrl $3,$3,35 sll $5,$2,2 addu $5,$5,$2 slt $3,$3,$2 daddiu $4,$4,1 beq $3,$0,.L22 sll $5,$5,1 li $2,2147418112 # 0x7fff0000 jr $31 ori $2,$2,0xffff .L20: lb $5,2($4) addiu $7,$5,-48 andi $2,$7,0x00ff sltu $2,$2,10 beq $2,$0,.L10 li $8,2147418112 # 0x7fff0000 daddiu $4,$4,2 move $2,$0 b .L6 ori $8,$8,0xffd0 .L24: lb $5,0($4) subu $2,$2,$7 addiu $7,$5,-48 andi $3,$7,0x00ff sltu $3,$3,10 beq $3,$0,.L26 nop .L6: addu $6,$8,$5 dsll $3,$6,1 daddu $3,$3,$6 dsll $9,$3,4 daddu $3,$3,$9 dsll $9,$3,8 daddu $3,$3,$9 dsll $9,$3,16 daddu $3,$3,$9 dsll $3,$3,1 daddu $3,$3,$6 addu $5,$5,$8 dsra $3,$3,32 sra $3,$3,2 sra $5,$5,31 sll $6,$2,2 subu $3,$3,$5 sll $7,$7,24 addu $6,$6,$2 slt $3,$2,$3 daddiu $4,$4,1 sra $7,$7,24 beq $3,$0,.L24 sll $2,$6,1 jr $31 li $2,-2147483648 # 0xffffffff80000000 .L26: jr $31 nop .L10: jr $31 move $2,$0
240
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
riscv64
-O0
RISC-V 64 clang 21.1.0
myAtoi: addi sp, sp, -64 sd ra, 56(sp) sd s0, 48(sp) addi s0, sp, 64 sd a0, -32(s0) li a0, 0 sw a0, -40(s0) sw a0, -44(s0) ld a0, -32(s0) bnez a0, .LBB0_2 j .LBB0_1 .LBB0_1: li a0, 0 sw a0, -20(s0) j .LBB0_20 .LBB0_2: j .LBB0_3 .LBB0_3: ld a0, -32(s0) lbu a0, 0(a0) li a1, 32 bne a0, a1, .LBB0_5 j .LBB0_4 .LBB0_4: ld a0, -32(s0) addi a0, a0, 1 sd a0, -32(s0) j .LBB0_3 .LBB0_5: ld a0, -32(s0) lbu a0, 0(a0) li a1, 43 bne a0, a1, .LBB0_7 j .LBB0_6 .LBB0_6: ld a0, -32(s0) addi a0, a0, 1 sd a0, -32(s0) li a0, 1 sw a0, -44(s0) ld a0, -32(s0) addi a0, a0, 1 sd a0, -32(s0) j .LBB0_7 .LBB0_7: j .LBB0_8 .LBB0_8: ld a0, -32(s0) lbu a0, 0(a0) li a2, 0 li a1, 48 sd a2, -56(s0) blt a0, a1, .LBB0_10 j .LBB0_9 .LBB0_9: ld a0, -32(s0) lbu a0, 0(a0) slti a0, a0, 58 sd a0, -56(s0) j .LBB0_10 .LBB0_10: ld a0, -56(s0) andi a0, a0, 1 beqz a0, .LBB0_19 j .LBB0_11 .LBB0_11: ld a0, -32(s0) lbu a0, 0(a0) addiw a0, a0, -48 sw a0, -36(s0) ld a0, -32(s0) addi a0, a0, 1 sd a0, -32(s0) lw a0, -44(s0) bnez a0, .LBB0_15 j .LBB0_12 .LBB0_12: lw a1, -40(s0) lwu a0, -36(s0) lui a2, 524288 addiw a2, a2, -1 xor a0, a0, a2 lui a2, 419430 addi a2, a2, 1639 mul a0, a0, a2 srli a0, a0, 34 bge a0, a1, .LBB0_14 j .LBB0_13 .LBB0_13: lui a0, 524288 addiw a0, a0, -1 sw a0, -20(s0) j .LBB0_20 .LBB0_14: lw a0, -40(s0) slliw a1, a0, 1 slliw a0, a0, 3 addw a0, a0, a1 lw a1, -36(s0) addw a0, a0, a1 sw a0, -40(s0) j .LBB0_18 .LBB0_15: lw a0, -40(s0) lw a1, -36(s0) lui a2, 524288 addw a1, a1, a2 lui a2, 419430 addi a2, a2, 1639 mul a1, a1, a2 srli a2, a1, 63 srai a1, a1, 34 add a1, a1, a2 bge a0, a1, .LBB0_17 j .LBB0_16 .LBB0_16: lui a0, 524288 sw a0, -20(s0) j .LBB0_20 .LBB0_17: lw a0, -40(s0) slliw a1, a0, 1 slliw a0, a0, 3 addw a0, a0, a1 lw a1, -36(s0) subw a0, a0, a1 sw a0, -40(s0) j .LBB0_18 .LBB0_18: j .LBB0_8 .LBB0_19: lw a0, -40(s0) sw a0, -20(s0) j .LBB0_20 .LBB0_20: lw a0, -20(s0) ld ra, 56(sp) ld s0, 48(sp) addi sp, sp, 64 ret
241
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
riscv64
-O1
RISC-V 64 clang 21.1.0
myAtoi: beqz a0, .LBB0_11 li a2, 32 .LBB0_2: lbu a1, 0(a0) addi a0, a0, 1 beq a1, a2, .LBB0_2 addi a2, a1, -43 seqz a2, a2 slli a2, a2, 1 add a2, a2, a0 lbu a3, -1(a2) addi a0, a3, -48 li a4, 9 bltu a4, a0, .LBB0_12 li a0, 0 lui a7, 524288 li a6, 43 lui t1, 419430 li t0, 10 lui a4, 838861 addi t3, a7, -48 addi t1, t1, 1639 addi t2, a4, -819 slli t2, t2, 32 addiw a7, a7, -1 j .LBB0_6 .LBB0_5: lbu a3, 0(a2) addi a4, a3, -48 addi a2, a2, 1 bgeu a4, t0, .LBB0_11 .LBB0_6: zext.b a4, a3 addi a3, a4, -48 bne a1, a6, .LBB0_9 addw a4, a4, t3 mul a4, a4, t1 srli a5, a4, 63 srai a4, a4, 34 add a4, a4, a5 blt a0, a4, .LBB0_14 slli a4, a0, 1 slli a0, a0, 3 add a0, a0, a4 subw a0, a0, a3 j .LBB0_5 .LBB0_9: subw a4, t3, a4 addi a4, a4, 95 slli a4, a4, 32 mulhu a4, a4, t2 srli a4, a4, 35 blt a4, a0, .LBB0_13 slli a4, a0, 1 slli a0, a0, 3 add a0, a0, a4 addw a0, a0, a3 j .LBB0_5 .LBB0_11: ret .LBB0_12: li a0, 0 ret .LBB0_13: mv a0, a7 ret .LBB0_14: lui a0, 524288 ret
242
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
riscv64
-O2
RISC-V 64 clang 21.1.0
myAtoi: beqz a0, .LBB0_11 li a2, 32 .LBB0_2: lbu a1, 0(a0) addi a0, a0, 1 beq a1, a2, .LBB0_2 addi a2, a1, -43 seqz a2, a2 slli a2, a2, 1 add a2, a2, a0 lbu a4, -1(a2) addi a0, a4, -48 li a3, 9 bltu a3, a0, .LBB0_12 li a0, 0 lui a6, 524288 li a7, 43 lui a3, 838861 li t0, 10 addi t1, a6, 47 addi a3, a3, -819 slli a3, a3, 32 addiw a6, a6, -1 j .LBB0_6 .LBB0_5: lbu a4, 0(a2) addi a5, a4, -48 addi a2, a2, 1 bgeu a5, t0, .LBB0_11 .LBB0_6: zext.b a5, a4 addi a4, a5, -48 subw a5, t1, a5 bne a1, a7, .LBB0_9 addi a5, a5, 1 slli a5, a5, 32 mulhu a5, a5, a3 srli a5, a5, 35 neg a5, a5 blt a0, a5, .LBB0_14 slli a5, a0, 1 slli a0, a0, 3 add a0, a0, a5 subw a0, a0, a4 j .LBB0_5 .LBB0_9: slli a5, a5, 32 mulhu a5, a5, a3 srli a5, a5, 35 blt a5, a0, .LBB0_13 slli a5, a0, 1 slli a0, a0, 3 add a0, a0, a5 addw a0, a0, a4 j .LBB0_5 .LBB0_11: ret .LBB0_12: li a0, 0 ret .LBB0_13: mv a0, a6 ret .LBB0_14: lui a0, 524288 ret
243
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
riscv64
-O3
RISC-V 64 clang 21.1.0
myAtoi: beqz a0, .LBB0_12 li a1, 32 .LBB0_2: lbu a4, 0(a0) addi a0, a0, 1 beq a4, a1, .LBB0_2 addi a1, a4, -43 seqz a5, a1 slli a7, a5, 1 add a3, a0, a7 lbu a2, -1(a3) addi a1, a2, -48 li a5, 9 bltu a5, a1, .LBB0_8 lui a6, 524288 li a5, 43 addi t0, a6, -48 bne a4, a5, .LBB0_9 mv a3, a0 li a0, 0 add a3, a3, a7 lui a4, 419430 addi a4, a4, 1639 li a6, 10 .LBB0_6: addw a1, a2, t0 mul a1, a1, a4 srli a5, a1, 63 srai a1, a1, 34 add a1, a1, a5 blt a0, a1, .LBB0_13 slli a1, a0, 1 slli a0, a0, 3 add a0, a0, a1 subw a0, a0, a2 lbu a2, 0(a3) addiw a0, a0, 48 addi a1, a2, -48 addi a3, a3, 1 bltu a1, a6, .LBB0_6 j .LBB0_12 .LBB0_8: li a0, 0 ret .LBB0_9: li a0, 0 lui a4, 838861 addiw a6, a6, -1 addi a5, a4, -819 slli a5, a5, 32 li a4, 10 .LBB0_10: subw a1, t0, a2 addi a1, a1, 95 slli a1, a1, 32 mulhu a1, a1, a5 srli a1, a1, 35 blt a1, a0, .LBB0_14 slli a1, a0, 1 slli a0, a0, 3 add a0, a0, a1 add a0, a0, a2 lbu a2, 0(a3) addiw a0, a0, -48 addi a1, a2, -48 addi a3, a3, 1 bltu a1, a4, .LBB0_10 .LBB0_12: ret .LBB0_13: lui a0, 524288 ret .LBB0_14: mv a0, a6 ret
244
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
riscv64
-O0
RISC-V 64 gcc 15.2.0
myAtoi: addi sp,sp,-48 sd ra,40(sp) sd s0,32(sp) addi s0,sp,48 sd a0,-40(s0) sw zero,-20(s0) sw zero,-24(s0) ld a5,-40(s0) bne a5,zero,.L4 li a5,0 j .L3 .L5: ld a5,-40(s0) addi a5,a5,1 sd a5,-40(s0) .L4: ld a5,-40(s0) lbu a5,0(a5) mv a4,a5 li a5,32 beq a4,a5,.L5 ld a5,-40(s0) lbu a5,0(a5) mv a4,a5 li a5,43 bne a4,a5,.L7 ld a5,-40(s0) addi a5,a5,1 sd a5,-40(s0) li a5,1 sw a5,-24(s0) ld a5,-40(s0) addi a5,a5,1 sd a5,-40(s0) j .L7 .L12: ld a5,-40(s0) lbu a5,0(a5) sext.w a5,a5 addiw a5,a5,-48 sw a5,-28(s0) ld a5,-40(s0) addi a5,a5,1 sd a5,-40(s0) lw a5,-24(s0) sext.w a5,a5 bne a5,zero,.L8 li a5,-2147483648 addiw a5,a5,-1 lw a4,-28(s0) subw a5,a5,a4 sext.w a5,a5 mv a3,a5 sext.w a4,a3 li a5,1717985280 addi a5,a5,1639 mul a5,a4,a5 srli a5,a5,32 sraiw a5,a5,2 mv a4,a5 sraiw a5,a3,31 subw a5,a4,a5 sext.w a5,a5 lw a4,-20(s0) sext.w a4,a4 ble a4,a5,.L9 li a5,-2147483648 xori a5,a5,-1 j .L3 .L9: lw a5,-20(s0) mv a4,a5 mv a5,a4 slliw a5,a5,2 addw a5,a5,a4 slliw a5,a5,1 sext.w a5,a5 lw a4,-28(s0) addw a5,a4,a5 sw a5,-20(s0) j .L7 .L8: lw a5,-28(s0) mv a4,a5 li a5,-2147483648 addw a5,a4,a5 sext.w a5,a5 mv a3,a5 sext.w a4,a3 li a5,1717985280 addi a5,a5,1639 mul a5,a4,a5 srli a5,a5,32 sraiw a5,a5,2 mv a4,a5 sraiw a5,a3,31 subw a5,a4,a5 sext.w a5,a5 lw a4,-20(s0) sext.w a4,a4 bge a4,a5,.L10 li a5,-2147483648 j .L3 .L10: lw a5,-20(s0) mv a4,a5 mv a5,a4 slliw a5,a5,2 addw a5,a5,a4 slliw a5,a5,1 sext.w a5,a5 lw a4,-28(s0) subw a5,a5,a4 sw a5,-20(s0) .L7: ld a5,-40(s0) lbu a5,0(a5) mv a4,a5 li a5,47 bleu a4,a5,.L11 ld a5,-40(s0) lbu a5,0(a5) mv a4,a5 li a5,57 bleu a4,a5,.L12 .L11: lw a5,-20(s0) .L3: mv a0,a5 ld ra,40(sp) ld s0,32(sp) addi sp,sp,48 jr ra
245
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
riscv64
-O1
RISC-V 64 gcc 15.2.0
myAtoi: mv a4,a0 beq a0,zero,.L9 lbu a5,0(a0) li a3,32 bne a5,a3,.L3 mv a3,a5 .L4: addi a4,a4,1 lbu a5,0(a4) beq a5,a3,.L4 .L3: li a3,43 li a6,0 beq a5,a3,.L18 .L5: lbu a5,0(a4) addiw a3,a5,-48 andi a3,a3,0xff li a2,9 li a0,0 bgtu a3,a2,.L17 li t3,-2147483648 addiw t3,t3,-48 li a1,1717985280 addi a1,a1,1639 li t1,-2147483648 addiw t1,t1,-1 mv a7,a2 j .L8 .L18: addi a4,a4,2 li a6,1 j .L5 .L6: addw a5,a5,t3 mul a3,a5,a1 srai a3,a3,34 sraiw a5,a5,31 subw a5,a3,a5 bgt a5,a0,.L12 slliw a5,a0,2 addw a5,a5,a0 slliw a5,a5,1 subw a0,a5,a2 .L7: addi a4,a4,1 lbu a5,0(a4) addiw a3,a5,-48 andi a3,a3,0xff bgtu a3,a7,.L19 .L8: addiw a2,a5,-48 bne a6,zero,.L6 subw a3,t1,a2 mul a5,a3,a1 srai a5,a5,34 sraiw a3,a3,31 subw a5,a5,a3 blt a5,a0,.L11 slliw a5,a0,2 addw a5,a5,a0 slliw a5,a5,1 addw a0,a5,a2 j .L7 .L17: li a0,0 ret .L19: ret .L9: li a0,0 ret .L11: li a0,-2147483648 xori a0,a0,-1 ret .L12: li a0,-2147483648 ret
246
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
riscv64
-O2
RISC-V 64 gcc 15.2.0
myAtoi: mv a5,a0 beq a0,zero,.L11 lbu a4,0(a0) li a3,32 bne a4,a3,.L3 .L4: lbu a4,1(a5) addi a5,a5,1 beq a4,a3,.L4 .L3: li a3,43 li a0,0 bne a4,a3,.L5 addi a5,a5,2 li a0,1 .L5: lbu a3,0(a5) li a6,9 addiw a4,a3,-48 andi a2,a4,0xff bgtu a2,a6,.L11 li t1,-2147483648 bne a0,zero,.L13 li a7,1717985280 addiw t1,t1,-1 addi a7,a7,1639 j .L10 .L19: lbu a3,0(a5) addw a0,a2,a4 addiw a4,a3,-48 andi a3,a4,0xff bgtu a3,a6,.L18 .L10: subw a1,t1,a4 mul a3,a1,a7 slliw a2,a0,2 sraiw a1,a1,31 addw a2,a2,a0 addi a5,a5,1 slliw a2,a2,1 srai a3,a3,34 subw a3,a3,a1 bge a3,a0,.L19 li a0,-2147483648 xori a0,a0,-1 ret .L13: li a7,1717985280 addiw t1,t1,-48 addi a7,a7,1639 li a0,0 j .L6 .L21: lbu a3,0(a5) subw a0,a1,a4 addiw a4,a3,-48 andi a2,a4,0xff bgtu a2,a6,.L20 .L6: addw a3,a3,t1 mul a2,a3,a7 slliw a1,a0,2 sraiw a3,a3,31 addw a1,a1,a0 addi a5,a5,1 slliw a1,a1,1 srai a2,a2,34 subw a3,a2,a3 ble a3,a0,.L21 li a0,-2147483648 ret .L11: li a0,0 ret .L20: ret .L18: ret
247
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
riscv64
-O3
RISC-V 64 gcc 15.2.0
myAtoi: mv a5,a0 beq a0,zero,.L10 lbu a4,0(a0) li a3,32 bne a4,a3,.L3 .L4: lbu a4,1(a5) addi a5,a5,1 beq a4,a3,.L4 .L3: li a3,43 beq a4,a3,.L19 addiw a1,a4,-48 li a7,-2147483648 li a6,1717985280 andi a4,a1,0xff li t1,9 addiw a7,a7,-1 addi a6,a6,1639 li a0,0 bleu a4,t1,.L8 ret .L21: lbu a4,0(a5) addw a0,a3,a1 addiw a1,a4,-48 andi a4,a1,0xff bgtu a4,t1,.L20 .L8: subw a2,a7,a1 mul a4,a2,a6 slliw a3,a0,2 sraiw a2,a2,31 addw a3,a3,a0 addi a5,a5,1 slliw a3,a3,1 srai a4,a4,34 subw a4,a4,a2 bge a4,a0,.L21 li a0,-2147483648 xori a0,a0,-1 ret .L19: lbu a4,2(a5) li t1,9 addiw a1,a4,-48 andi a3,a1,0xff bgtu a3,t1,.L10 li a7,-2147483648 li a6,1717985280 addi a5,a5,2 addiw a7,a7,-48 addi a6,a6,1639 li a0,0 j .L6 .L23: lbu a4,0(a5) subw a0,a2,a1 addiw a1,a4,-48 andi a3,a1,0xff bgtu a3,t1,.L22 .L6: addw a4,a4,a7 mul a3,a4,a6 slliw a2,a0,2 sraiw a4,a4,31 addw a2,a2,a0 addi a5,a5,1 slliw a2,a2,1 srai a3,a3,34 subw a4,a3,a4 ble a4,a0,.L23 li a0,-2147483648 ret .L22: ret .L20: ret .L10: li a0,0 ret
248
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
x86-64
-O0
x86-64 clang 21.1.0
myAtoi: push rbp mov rbp, rsp mov qword ptr [rbp - 16], rdi mov dword ptr [rbp - 24], 0 mov dword ptr [rbp - 28], 0 cmp qword ptr [rbp - 16], 0 jne .LBB0_2 mov dword ptr [rbp - 4], 0 jmp .LBB0_20 .LBB0_2: jmp .LBB0_3 .LBB0_3: mov rax, qword ptr [rbp - 16] movsx eax, byte ptr [rax] cmp eax, 32 jne .LBB0_5 mov rax, qword ptr [rbp - 16] add rax, 1 mov qword ptr [rbp - 16], rax jmp .LBB0_3 .LBB0_5: mov rax, qword ptr [rbp - 16] movsx eax, byte ptr [rax] cmp eax, 43 jne .LBB0_7 mov rax, qword ptr [rbp - 16] add rax, 1 mov qword ptr [rbp - 16], rax mov dword ptr [rbp - 28], 1 mov rax, qword ptr [rbp - 16] add rax, 1 mov qword ptr [rbp - 16], rax .LBB0_7: jmp .LBB0_8 .LBB0_8: mov rax, qword ptr [rbp - 16] movsx ecx, byte ptr [rax] xor eax, eax cmp ecx, 48 mov byte ptr [rbp - 29], al jl .LBB0_10 mov rax, qword ptr [rbp - 16] movsx eax, byte ptr [rax] cmp eax, 57 setle al mov byte ptr [rbp - 29], al .LBB0_10: mov al, byte ptr [rbp - 29] test al, 1 jne .LBB0_11 jmp .LBB0_19 .LBB0_11: mov rax, qword ptr [rbp - 16] movsx eax, byte ptr [rax] sub eax, 48 mov dword ptr [rbp - 20], eax mov rax, qword ptr [rbp - 16] add rax, 1 mov qword ptr [rbp - 16], rax cmp dword ptr [rbp - 28], 0 jne .LBB0_15 mov eax, dword ptr [rbp - 24] mov dword ptr [rbp - 36], eax mov eax, 2147483647 sub eax, dword ptr [rbp - 20] mov ecx, 10 cdq idiv ecx mov ecx, eax mov eax, dword ptr [rbp - 36] cmp eax, ecx jle .LBB0_14 mov dword ptr [rbp - 4], 2147483647 jmp .LBB0_20 .LBB0_14: imul eax, dword ptr [rbp - 24], 10 add eax, dword ptr [rbp - 20] mov dword ptr [rbp - 24], eax jmp .LBB0_18 .LBB0_15: mov eax, dword ptr [rbp - 24] mov dword ptr [rbp - 40], eax mov eax, dword ptr [rbp - 20] add eax, 2147483648 mov ecx, 10 cdq idiv ecx mov ecx, eax mov eax, dword ptr [rbp - 40] cmp eax, ecx jge .LBB0_17 mov dword ptr [rbp - 4], -2147483648 jmp .LBB0_20 .LBB0_17: imul eax, dword ptr [rbp - 24], 10 sub eax, dword ptr [rbp - 20] mov dword ptr [rbp - 24], eax .LBB0_18: jmp .LBB0_8 .LBB0_19: mov eax, dword ptr [rbp - 24] mov dword ptr [rbp - 4], eax .LBB0_20: mov eax, dword ptr [rbp - 4] pop rbp ret
249
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
x86-64
-O1
x86-64 clang 21.1.0
myAtoi: xor eax, eax test rdi, rdi je .LBB0_12 .LBB0_1: movzx ecx, byte ptr [rdi] inc rdi cmp cl, 32 je .LBB0_1 xor esi, esi cmp cl, 43 sete sil add esi, esi movzx edx, byte ptr [rdi + rsi - 1] lea r8d, [rdx - 48] cmp r8b, 9 ja .LBB0_12 add rdi, rsi xor eax, eax jmp .LBB0_4 .LBB0_11: movzx edx, byte ptr [rdi] lea esi, [rdx - 48] inc rdi cmp sil, 10 jae .LBB0_12 .LBB0_4: add dl, -48 movzx edx, dl cmp cl, 43 jne .LBB0_5 lea esi, [rdx - 2147483648] movsxd rsi, esi imul rsi, rsi, 1717986919 mov r8, rsi shr r8, 63 sar rsi, 34 add esi, r8d cmp eax, esi jl .LBB0_9 add eax, eax lea eax, [rax + 4*rax] sub eax, edx jmp .LBB0_11 .LBB0_5: mov esi, edx xor esi, 2147483646 imul rsi, rsi, 1717986919 shr rsi, 34 cmp eax, esi jg .LBB0_6 lea eax, [rax + 4*rax] lea eax, [rdx + 2*rax] jmp .LBB0_11 .LBB0_12: ret .LBB0_6: mov eax, 2147483647 ret .LBB0_9: mov eax, -2147483648 ret
250
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
x86-64
-O2
x86-64 clang 21.1.0
myAtoi: xor eax, eax test rdi, rdi je .LBB0_11 .LBB0_1: movzx ecx, byte ptr [rdi] inc rdi cmp cl, 32 je .LBB0_1 xor edx, edx cmp cl, 43 sete dl add edx, edx movzx r8d, byte ptr [rdi + rdx - 1] lea esi, [r8 - 48] cmp sil, 9 ja .LBB0_11 add rdi, rdx xor esi, esi mov edx, 3435973837 jmp .LBB0_4 .LBB0_10: movzx r8d, byte ptr [rdi] lea r9d, [r8 - 48] inc rdi mov eax, esi cmp r9b, 10 jae .LBB0_11 .LBB0_4: add r8b, -48 movzx r8d, r8b cmp cl, 43 jne .LBB0_5 mov eax, -2147483648 mov r9d, -2147483648 sub r9d, r8d imul r9, rdx shr r9, 35 neg r9d cmp esi, r9d jl .LBB0_11 add esi, esi lea esi, [rsi + 4*rsi] sub esi, r8d jmp .LBB0_10 .LBB0_5: mov eax, r8d xor eax, 2147483646 imul rax, rax, 1717986919 shr rax, 34 cmp esi, eax jg .LBB0_6 lea eax, [rsi + 4*rsi] lea esi, [r8 + 2*rax] jmp .LBB0_10 .LBB0_11: ret .LBB0_6: mov eax, 2147483647 ret
251
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
x86-64
-O3
x86-64 clang 21.1.0
myAtoi: xor eax, eax test rdi, rdi je .LBB0_11 .LBB0_1: movzx edx, byte ptr [rdi] inc rdi cmp dl, 32 je .LBB0_1 xor esi, esi cmp dl, 43 sete sil add esi, esi movzx ecx, byte ptr [rdi + rsi - 1] lea r8d, [rcx - 48] cmp r8b, 9 ja .LBB0_11 add rdi, rsi cmp dl, 43 jne .LBB0_4 xor edx, edx mov esi, 3435973837 .LBB0_8: add cl, -48 movzx ecx, cl mov eax, -2147483648 mov r8d, -2147483648 sub r8d, ecx imul r8, rsi shr r8, 35 neg r8d cmp edx, r8d jl .LBB0_11 add edx, edx lea edx, [rdx + 4*rdx] sub edx, ecx movzx ecx, byte ptr [rdi] lea r8d, [rcx - 48] inc rdi mov eax, edx cmp r8b, 10 jb .LBB0_8 jmp .LBB0_11 .LBB0_4: xor eax, eax .LBB0_5: add cl, -48 movzx ecx, cl mov edx, ecx xor edx, 2147483646 imul rdx, rdx, 1717986919 shr rdx, 34 cmp eax, edx jg .LBB0_6 lea eax, [rax + 4*rax] lea eax, [rcx + 2*rax] movzx ecx, byte ptr [rdi] lea edx, [rcx - 48] inc rdi cmp dl, 10 jb .LBB0_5 .LBB0_11: ret .LBB0_6: mov eax, 2147483647 ret
252
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
x86-64
-O0
x86-64 gcc 15.2
myAtoi: push rbp mov rbp, rsp mov QWORD PTR [rbp-24], rdi mov DWORD PTR [rbp-4], 0 mov DWORD PTR [rbp-8], 0 cmp QWORD PTR [rbp-24], 0 jne .L4 mov eax, 0 jmp .L3 .L5: add QWORD PTR [rbp-24], 1 .L4: mov rax, QWORD PTR [rbp-24] movzx eax, BYTE PTR [rax] cmp al, 32 je .L5 mov rax, QWORD PTR [rbp-24] movzx eax, BYTE PTR [rax] cmp al, 43 jne .L7 add QWORD PTR [rbp-24], 1 mov DWORD PTR [rbp-8], 1 add QWORD PTR [rbp-24], 1 jmp .L7 .L12: mov rax, QWORD PTR [rbp-24] movzx eax, BYTE PTR [rax] movsx eax, al sub eax, 48 mov DWORD PTR [rbp-12], eax add QWORD PTR [rbp-24], 1 cmp DWORD PTR [rbp-8], 0 jne .L8 mov eax, 2147483647 sub eax, DWORD PTR [rbp-12] movsx rdx, eax imul rdx, rdx, 1717986919 shr rdx, 32 mov ecx, edx sar ecx, 2 cdq mov eax, ecx sub eax, edx cmp DWORD PTR [rbp-4], eax jle .L9 mov eax, 2147483647 jmp .L3 .L9: mov edx, DWORD PTR [rbp-4] mov eax, edx sal eax, 2 add eax, edx add eax, eax mov edx, eax mov eax, DWORD PTR [rbp-12] add eax, edx mov DWORD PTR [rbp-4], eax jmp .L7 .L8: mov eax, DWORD PTR [rbp-12] add eax, -2147483648 movsx rdx, eax imul rdx, rdx, 1717986919 shr rdx, 32 mov ecx, edx sar ecx, 2 cdq mov eax, ecx sub eax, edx cmp DWORD PTR [rbp-4], eax jge .L10 mov eax, -2147483648 jmp .L3 .L10: mov edx, DWORD PTR [rbp-4] mov eax, edx sal eax, 2 add eax, edx add eax, eax sub eax, DWORD PTR [rbp-12] mov DWORD PTR [rbp-4], eax .L7: mov rax, QWORD PTR [rbp-24] movzx eax, BYTE PTR [rax] cmp al, 47 jle .L11 mov rax, QWORD PTR [rbp-24] movzx eax, BYTE PTR [rax] cmp al, 57 jle .L12 .L11: mov eax, DWORD PTR [rbp-4] .L3: pop rbp ret
253
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
x86-64
-O1
x86-64 gcc 15.2
myAtoi: mov rcx, rdi test rdi, rdi je .L9 movzx eax, BYTE PTR [rdi] cmp al, 32 jne .L3 .L4: add rcx, 1 movzx eax, BYTE PTR [rcx] cmp al, 32 je .L4 .L3: lea rdx, [rcx+2] cmp al, 43 cmove rcx, rdx sete r8b movzx r8d, r8b movzx eax, BYTE PTR [rcx] lea esi, [rax-48] mov edx, 0 mov r9d, 2147483647 cmp sil, 9 jbe .L8 mov edx, 0 jmp .L1 .L6: movsx eax, al add eax, 2147483600 movsx rdi, eax imul rdi, rdi, 1717986919 sar rdi, 34 sar eax, 31 sub edi, eax cmp edi, edx jg .L12 lea edx, [rdx+rdx*4] add edx, edx sub edx, esi .L7: add rcx, 1 movzx eax, BYTE PTR [rcx] lea esi, [rax-48] cmp sil, 9 ja .L1 .L8: lea esi, [rax-48] movsx esi, sil test r8d, r8d jne .L6 mov edi, r9d sub edi, esi movsx rax, edi imul rax, rax, 1717986919 sar rax, 34 sar edi, 31 sub eax, edi cmp eax, edx jl .L11 lea eax, [rdx+rdx*4] lea edx, [rsi+rax*2] jmp .L7 .L9: mov edx, 0 jmp .L1 .L11: mov edx, 2147483647 .L1: mov eax, edx ret .L12: mov edx, -2147483648 jmp .L1
254
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
x86-64
-O2
x86-64 gcc 15.2
myAtoi: test rdi, rdi jne .L19 jmp .L11 .L4: add rdi, 1 .L19: movzx eax, BYTE PTR [rdi] cmp al, 32 je .L4 cmp al, 43 lea rdx, [rdi+2] cmove rdi, rdx sete dl movzx edx, dl movsx ecx, BYTE PTR [rdi] lea eax, [rcx-48] cmp al, 9 ja .L11 movsx eax, al test edx, edx jne .L13 mov esi, 3435973837 jmp .L10 .L21: lea edx, [rdx+rdx*4] add rdi, 1 lea edx, [rax+rdx*2] movzx eax, BYTE PTR [rdi] sub eax, 48 cmp al, 9 ja .L1 movsx eax, al .L10: mov ecx, 2147483647 sub ecx, eax imul rcx, rsi shr rcx, 35 cmp ecx, edx jge .L21 mov edx, 2147483647 .L1: mov eax, edx ret .L13: xor edx, edx jmp .L6 .L22: lea edx, [rdx+rdx*4] movsx ecx, BYTE PTR [rdi+1] add rdi, 1 add edx, edx sub edx, eax lea eax, [rcx-48] cmp al, 9 ja .L1 movsx eax, al .L6: add ecx, 2147483600 movsx rsi, ecx sar ecx, 31 imul rsi, rsi, 1717986919 sar rsi, 34 sub esi, ecx cmp esi, edx jle .L22 mov edx, -2147483648 mov eax, edx ret .L11: xor edx, edx mov eax, edx ret
255
8
String to Integer (atoi)
Medium
/* 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned. */ int myAtoi(char* str) { int d, k = 0, negative = 0; if (!str) return 0; while (*str == ' ') str ++; if (*str == '+') { str ++; negative = 1; str ++; } while (*str >= '0' && *str <= '9') { d = *str - '0'; str ++; if (negative == 0) { if (k > (0x7fffffff - d) / 10) { // possitive number overflow return 0x7fffffff; } k = k * 10 + d; } else { if (k < ((signed)0x80000000 + d) / 10) { // negative number overflow return 0x80000000; } k = k * 10 - d; } } return k; } /* Difficulty:Medium */
x86-64
-O3
x86-64 gcc 15.2
myAtoi: test rdi, rdi jne .L19 jmp .L10 .L4: add rdi, 1 .L19: movzx edx, BYTE PTR [rdi] cmp dl, 32 je .L4 cmp dl, 43 je .L21 lea ecx, [rdx-48] xor eax, eax mov esi, 3435973837 cmp cl, 9 jbe .L8 ret .L23: lea eax, [rax+rax*4] add rdi, 1 lea eax, [rdx+rax*2] movzx edx, BYTE PTR [rdi] lea ecx, [rdx-48] cmp cl, 9 ja .L22 .L8: sub edx, 48 mov ecx, 2147483647 movsx edx, dl sub ecx, edx imul rcx, rsi shr rcx, 35 cmp ecx, eax jge .L23 mov eax, 2147483647 ret .L21: movzx ecx, BYTE PTR [rdi+2] lea eax, [rcx-48] cmp al, 9 ja .L10 add rdi, 2 xor eax, eax jmp .L6 .L25: lea eax, [rax+rax*4] sub ecx, 48 add rdi, 1 movsx ecx, cl add eax, eax sub eax, ecx movzx ecx, BYTE PTR [rdi] lea edx, [rcx-48] cmp dl, 9 ja .L24 .L6: movsx esi, cl add esi, 2147483600 movsx rdx, esi sar esi, 31 imul rdx, rdx, 1717986919 sar rdx, 34 sub edx, esi cmp edx, eax jle .L25 mov eax, -2147483648 ret .L24: ret .L22: ret .L10: xor eax, eax ret
256
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
aarch64
-O0
ARM64 gcc 15.2.0
isPalindrome: sub sp, sp, #32 str w0, [sp, 12] str wzr, [sp, 24] ldr w0, [sp, 12] cmp w0, 0 bge .L2 mov w0, 0 b .L3 .L2: ldr w0, [sp, 12] str w0, [sp, 28] b .L4 .L6: ldr w1, [sp, 28] mov w0, 10 sdiv w2, w1, w0 mov w0, w2 lsl w0, w0, 2 add w0, w0, w2 lsl w0, w0, 1 sub w0, w1, w0 str w0, [sp, 20] mov w1, 2147483647 ldr w0, [sp, 20] sub w0, w1, w0 mov w1, 26215 movk w1, 0x6666, lsl 16 smull x1, w0, w1 lsr x1, x1, 32 asr w1, w1, 2 asr w0, w0, 31 sub w0, w1, w0 ldr w1, [sp, 24] cmp w1, w0 ble .L5 mov w0, 0 b .L3 .L5: ldr w1, [sp, 24] mov w0, w1 lsl w0, w0, 2 add w0, w0, w1 lsl w0, w0, 1 mov w1, w0 ldr w0, [sp, 20] add w0, w0, w1 str w0, [sp, 24] ldr w0, [sp, 28] mov w1, 26215 movk w1, 0x6666, lsl 16 smull x1, w0, w1 lsr x1, x1, 32 asr w1, w1, 2 asr w0, w0, 31 sub w0, w1, w0 str w0, [sp, 28] .L4: ldr w0, [sp, 28] cmp w0, 0 bne .L6 ldr w1, [sp, 24] ldr w0, [sp, 12] cmp w1, w0 cset w0, eq and w0, w0, 255 .L3: add sp, sp, 32 ret
257
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
aarch64
-O1
ARM64 gcc 15.2.0
isPalindrome: tbnz w0, #31, .L5 cbz w0, .L6 mov w3, w0 mov w2, 0 mov w8, 10 mov w7, 2147483647 mov w6, 26215 movk w6, 0x6666, lsl 16 .L4: sdiv w1, w3, w8 add w1, w1, w1, lsl 2 sub w1, w3, w1, lsl 1 sub w4, w7, w1 smull x5, w4, w6 asr x5, x5, 34 sub w4, w5, w4, asr 31 cmp w4, w2 blt .L7 add w2, w2, w2, lsl 2 add w2, w1, w2, lsl 1 smull x1, w3, w6 asr x1, x1, 34 subs w3, w1, w3, asr 31 bne .L4 .L3: cmp w0, w2 cset w0, eq .L2: ret .L6: mov w2, w0 b .L3 .L5: mov w0, 0 b .L2 .L7: mov w0, 0 b .L2
258
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
aarch64
-O2
ARM64 gcc 15.2.0
isPalindrome: tbnz w0, #31, .L7 cbz w0, .L6 mov w5, 52429 mov w8, 26215 mov w4, w0 mov w3, 0 mov w7, 10 mov w6, 2147483647 movk w5, 0xcccc, lsl 16 movk w8, 0x6666, lsl 16 b .L4 .L10: add w3, w3, w3, lsl 2 add w3, w1, w3, lsl 1 smull x1, w4, w8 asr x1, x1, 34 subs w4, w1, w4, asr 31 beq .L3 .L4: sdiv w1, w4, w7 add w1, w1, w1, lsl 2 sub w1, w4, w1, lsl 1 sub w2, w6, w1 umull x2, w2, w5 lsr x2, x2, 35 cmp w2, w3 bge .L10 .L7: mov w0, 0 ret .L6: mov w3, 0 .L3: cmp w0, w3 cset w0, eq ret
259
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
aarch64
-O3
ARM64 gcc 15.2.0
isPalindrome: tbnz w0, #31, .L7 mov w5, 0 cbz w0, .L3 mov w8, 52429 mov w7, 26215 mov w4, w0 mov w10, 10 mov w9, 2147483647 movk w8, 0xcccc, lsl 16 movk w7, 0x6666, lsl 16 b .L4 .L10: add w5, w1, w6, lsl 1 subs w4, w2, w4, asr 31 beq .L3 .L4: sdiv w1, w4, w10 smull x2, w4, w7 add w6, w5, w5, lsl 2 asr x2, x2, 34 add w1, w1, w1, lsl 2 sub w1, w4, w1, lsl 1 sub w3, w9, w1 umull x3, w3, w8 lsr x3, x3, 35 cmp w3, w5 bge .L10 .L7: mov w0, 0 ret .L3: cmp w0, w5 cset w0, eq ret
260
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
aarch64
-O0
armv8-a clang 21.1.0
isPalindrome: sub sp, sp, #32 str w0, [sp, #24] str wzr, [sp, #12] ldr w8, [sp, #24] tbz w8, #31, .LBB0_2 b .LBB0_1 .LBB0_1: mov w8, wzr and w8, w8, #0x1 and w8, w8, #0x1 strb w8, [sp, #31] b .LBB0_8 .LBB0_2: ldr w8, [sp, #24] str w8, [sp, #20] b .LBB0_3 .LBB0_3: ldr w8, [sp, #20] cbz w8, .LBB0_7 b .LBB0_4 .LBB0_4: ldr w8, [sp, #20] mov w10, #10 sdiv w9, w8, w10 mul w9, w9, w10 subs w8, w8, w9 str w8, [sp, #16] ldr w8, [sp, #12] ldr w11, [sp, #16] mov w9, #2147483647 subs w9, w9, w11 sdiv w9, w9, w10 subs w8, w8, w9 b.le .LBB0_6 b .LBB0_5 .LBB0_5: mov w8, wzr and w8, w8, #0x1 and w8, w8, #0x1 strb w8, [sp, #31] b .LBB0_8 .LBB0_6: ldr w8, [sp, #12] mov w9, #10 mul w8, w8, w9 ldr w10, [sp, #16] add w8, w8, w10 str w8, [sp, #12] ldr w8, [sp, #20] sdiv w8, w8, w9 str w8, [sp, #20] b .LBB0_3 .LBB0_7: ldr w8, [sp, #12] ldr w9, [sp, #24] subs w8, w8, w9 cset w8, eq and w8, w8, #0x1 strb w8, [sp, #31] b .LBB0_8 .LBB0_8: ldrb w8, [sp, #31] and w0, w8, #0x1 add sp, sp, #32 ret
261
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
aarch64
-O1
armv8-a clang 21.1.0
isPalindrome: tbnz w0, #31, .LBB0_6 mov w8, wzr cbz w0, .LBB0_5 mov w9, #52429 mov w10, #10 mov w11, w0 movk w9, #52428, lsl #16 .LBB0_3: umull x12, w11, w9 lsr x12, x12, #35 msub w13, w12, w10, w11 eor w14, w13, #0x7ffffffe umull x14, w14, w9 lsr x14, x14, #35 cmp w8, w14 b.gt .LBB0_6 madd w8, w8, w10, w13 cmp w11, #9 mov w11, w12 b.hi .LBB0_3 .LBB0_5: cmp w8, w0 cset w0, eq ret .LBB0_6: mov w0, wzr ret
262
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
aarch64
-O2
armv8-a clang 21.1.0
isPalindrome: tbnz w0, #31, .LBB0_6 mov w8, wzr cbz w0, .LBB0_5 mov w9, #52429 mov w10, #10 mov w11, w0 movk w9, #52428, lsl #16 .LBB0_3: umull x12, w11, w9 lsr x12, x12, #35 msub w13, w12, w10, w11 eor w14, w13, #0x7ffffffe umull x14, w14, w9 lsr x14, x14, #35 cmp w8, w14 b.gt .LBB0_6 madd w8, w8, w10, w13 cmp w11, #9 mov w11, w12 b.hi .LBB0_3 .LBB0_5: cmp w8, w0 cset w0, eq ret .LBB0_6: mov w0, wzr ret
263
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
aarch64
-O3
armv8-a clang 21.1.0
isPalindrome: tbnz w0, #31, .LBB0_6 mov w8, wzr cbz w0, .LBB0_5 mov w9, #52429 mov w10, #10 mov w11, w0 movk w9, #52428, lsl #16 .LBB0_3: umull x12, w11, w9 lsr x12, x12, #35 msub w13, w12, w10, w11 eor w14, w13, #0x7ffffffe umull x14, w14, w9 lsr x14, x14, #35 cmp w8, w14 b.gt .LBB0_6 madd w8, w8, w10, w13 cmp w11, #9 mov w11, w12 b.hi .LBB0_3 .LBB0_5: cmp w8, w0 cset w0, eq ret .LBB0_6: mov w0, wzr ret
264
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
mips64
-O0
mips64 clang 21.1.0
isPalindrome: .Lfunc_begin0 = .Ltmp0 daddiu $sp, $sp, -48 sd $ra, 40($sp) sd $fp, 32($sp) move $fp, $sp move $1, $4 sw $1, 24($fp) sw $zero, 12($fp) lw $1, 24($fp) bgez $1, .LBB0_3 nop b .LBB0_2 nop .LBB0_2: addiu $1, $zero, 0 sb $zero, 31($fp) b .LBB0_11 nop .LBB0_3: lw $1, 24($fp) sw $1, 20($fp) b .LBB0_4 nop .LBB0_4: lw $1, 20($fp) beqz $1, .LBB0_10 nop b .LBB0_6 nop .LBB0_6: lw $1, 20($fp) lui $2, 26214 ori $3, $2, 26215 mult $1, $3 mfhi $2 srl $4, $2, 31 sra $2, $2, 2 addu $2, $2, $4 sll $4, $2, 1 sll $2, $2, 3 addu $2, $2, $4 subu $1, $1, $2 sw $1, 16($fp) lw $2, 12($fp) lw $1, 16($fp) lui $4, 32767 ori $4, $4, 65535 xor $1, $1, $4 multu $1, $3 mfhi $1 srl $1, $1, 2 slt $1, $1, $2 beqz $1, .LBB0_9 nop b .LBB0_8 nop .LBB0_8: addiu $1, $zero, 0 sb $zero, 31($fp) b .LBB0_11 nop .LBB0_9: lw $1, 12($fp) sll $2, $1, 1 sll $1, $1, 3 addu $1, $1, $2 lw $2, 16($fp) addu $1, $1, $2 sw $1, 12($fp) lw $1, 20($fp) lui $2, 26214 ori $2, $2, 26215 mult $1, $2 mfhi $1 srl $2, $1, 31 sra $1, $1, 2 addu $1, $1, $2 sw $1, 20($fp) b .LBB0_4 nop .LBB0_10: lw $1, 12($fp) lw $2, 24($fp) xor $1, $1, $2 sltiu $1, $1, 1 sb $1, 31($fp) b .LBB0_11 nop .LBB0_11: lbu $2, 31($fp) move $sp, $fp ld $fp, 32($sp) ld $ra, 40($sp) daddiu $sp, $sp, 48 jr $ra nop
265
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
mips64
-O1
mips64 clang 21.1.0
isPalindrome: .Lfunc_begin0 = .Ltmp0 daddiu $sp, $sp, -16 sd $ra, 8($sp) sd $fp, 0($sp) move $fp, $sp bltz $4, .LBB0_6 nop beqz $4, .LBB0_7 nop addiu $2, $zero, 0 lui $1, 52428 ori $3, $1, 52429 addiu $5, $zero, -8 lui $1, 26214 ori $6, $1, 26215 lui $1, 32767 ori $8, $1, 65534 addiu $7, $zero, 0 move $9, $4 .LBB0_3: multu $9, $3 mfhi $1 and $11, $1, $5 srl $10, $1, 3 sll $1, $10, 1 addu $1, $11, $1 subu $11, $9, $1 xor $1, $11, $8 multu $1, $6 mfhi $1 srl $1, $1, 2 slt $1, $1, $7 bnez $1, .LBB0_9 nop sll $1, $7, 1 sll $7, $7, 3 addu $1, $7, $1 addu $7, $1, $11 sltiu $1, $9, 10 beqz $1, .LBB0_3 move $9, $10 b .LBB0_8 nop .LBB0_6: b .LBB0_9 addiu $2, $zero, 0 .LBB0_7: addiu $7, $zero, 0 .LBB0_8: xor $1, $7, $4 sltiu $2, $1, 1 .LBB0_9: move $sp, $fp ld $fp, 0($sp) ld $ra, 8($sp) jr $ra daddiu $sp, $sp, 16
266
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
mips64
-O2
mips64 clang 21.1.0
isPalindrome: .Lfunc_begin0 = .Ltmp0 daddiu $sp, $sp, -16 sd $ra, 8($sp) sd $fp, 0($sp) move $fp, $sp bltz $4, .LBB0_6 nop beqz $4, .LBB0_7 nop addiu $2, $zero, 0 lui $1, 52428 ori $3, $1, 52429 addiu $5, $zero, -8 lui $1, 26214 ori $6, $1, 26215 lui $1, 32767 ori $8, $1, 65534 addiu $7, $zero, 0 move $9, $4 .LBB0_3: multu $9, $3 mfhi $1 and $11, $1, $5 srl $10, $1, 3 sll $1, $10, 1 addu $1, $11, $1 subu $11, $9, $1 xor $1, $11, $8 multu $1, $6 mfhi $1 srl $1, $1, 2 slt $1, $1, $7 bnez $1, .LBB0_9 nop sll $1, $7, 1 sll $7, $7, 3 addu $1, $7, $1 addu $7, $1, $11 sltiu $1, $9, 10 beqz $1, .LBB0_3 move $9, $10 b .LBB0_8 nop .LBB0_6: b .LBB0_9 addiu $2, $zero, 0 .LBB0_7: addiu $7, $zero, 0 .LBB0_8: xor $1, $7, $4 sltiu $2, $1, 1 .LBB0_9: move $sp, $fp ld $fp, 0($sp) ld $ra, 8($sp) jr $ra daddiu $sp, $sp, 16
267
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
mips64
-O3
mips64 clang 21.1.0
isPalindrome: .Lfunc_begin0 = .Ltmp0 daddiu $sp, $sp, -16 sd $ra, 8($sp) sd $fp, 0($sp) move $fp, $sp bltz $4, .LBB0_6 nop beqz $4, .LBB0_7 nop lui $1, 52428 addiu $2, $zero, 0 addiu $5, $zero, -8 addiu $7, $zero, 0 move $9, $4 ori $3, $1, 52429 lui $1, 26214 ori $6, $1, 26215 lui $1, 32767 ori $8, $1, 65534 .LBB0_3: multu $9, $3 mfhi $1 srl $10, $1, 3 and $11, $1, $5 sll $1, $10, 1 addu $1, $11, $1 subu $11, $9, $1 xor $1, $11, $8 multu $1, $6 mfhi $1 srl $1, $1, 2 slt $1, $1, $7 bnez $1, .LBB0_9 nop sll $1, $7, 1 sll $7, $7, 3 addu $1, $7, $1 addu $7, $1, $11 sltiu $1, $9, 10 beqz $1, .LBB0_3 move $9, $10 b .LBB0_8 nop .LBB0_6: b .LBB0_9 addiu $2, $zero, 0 .LBB0_7: addiu $7, $zero, 0 .LBB0_8: xor $1, $7, $4 sltiu $2, $1, 1 .LBB0_9: move $sp, $fp ld $fp, 0($sp) ld $ra, 8($sp) jr $ra daddiu $sp, $sp, 16
268
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
mips64
-O0
mips64 gcc 15.2.0
isPalindrome: daddiu $sp,$sp,-48 sd $fp,40($sp) move $fp,$sp move $2,$4 sll $2,$2,0 sw $2,16($fp) sw $0,4($fp) lw $2,16($fp) bgez $2,.L2 nop move $2,$0 b .L3 nop .L2: lw $2,16($fp) sw $2,0($fp) b .L4 nop .L6: lw $4,0($fp) move $3,$4 move $2,$3 dsll $2,$2,1 daddu $2,$2,$3 dsll $5,$2,4 daddu $2,$2,$5 dsll $5,$2,8 daddu $2,$2,$5 dsll $5,$2,16 daddu $2,$2,$5 dsll $2,$2,1 daddu $2,$2,$3 dsrl $2,$2,32 sll $2,$2,0 sra $3,$2,2 sra $2,$4,31 subu $3,$3,$2 move $2,$3 sll $2,$2,2 addu $2,$2,$3 sll $2,$2,1 subu $2,$4,$2 sw $2,8($fp) li $2,2147418112 # 0x7fff0000 ori $3,$2,0xffff lw $2,8($fp) subu $2,$3,$2 move $5,$2 move $3,$5 move $2,$3 dsll $2,$2,1 daddu $2,$2,$3 dsll $4,$2,4 daddu $2,$2,$4 dsll $4,$2,8 daddu $2,$2,$4 dsll $4,$2,16 daddu $2,$2,$4 dsll $2,$2,1 daddu $2,$2,$3 dsrl $2,$2,32 sll $2,$2,0 sra $3,$2,2 sra $2,$5,31 subu $2,$3,$2 lw $3,4($fp) slt $2,$2,$3 beq $2,$0,.L5 nop move $2,$0 b .L3 nop .L5: lw $3,4($fp) move $2,$3 sll $2,$2,2 addu $2,$2,$3 sll $2,$2,1 lw $3,8($fp) addu $2,$3,$2 sw $2,4($fp) lw $5,0($fp) move $3,$5 move $2,$3 dsll $2,$2,1 daddu $2,$2,$3 dsll $4,$2,4 daddu $2,$2,$4 dsll $4,$2,8 daddu $2,$2,$4 dsll $4,$2,16 daddu $2,$2,$4 dsll $2,$2,1 daddu $2,$2,$3 dsrl $2,$2,32 sll $2,$2,0 sra $3,$2,2 sra $2,$5,31 subu $2,$3,$2 sw $2,0($fp) .L4: lw $2,0($fp) bne $2,$0,.L6 nop lw $3,4($fp) lw $2,16($fp) xor $2,$3,$2 sltu $2,$2,1 andi $2,$2,0x00ff .L3: move $sp,$fp ld $fp,40($sp) daddiu $sp,$sp,48 jr $31 nop
269
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
mips64
-O1
mips64 gcc 15.2.0
isPalindrome: bltz $4,.L5 nop beq $4,$0,.L6 move $3,$4 move $5,$0 li $6,2147418112 # 0x7fff0000 ori $6,$6,0xffff .L4: dsll $2,$3,1 daddu $2,$2,$3 dsll $7,$2,4 daddu $2,$2,$7 dsll $7,$2,8 daddu $2,$2,$7 dsll $7,$2,16 daddu $2,$2,$7 dsll $2,$2,1 daddu $2,$2,$3 dsra $2,$2,32 sra $2,$2,2 sra $7,$3,31 subu $2,$2,$7 sll $7,$2,2 addu $7,$7,$2 sll $7,$7,1 subu $7,$3,$7 subu $8,$6,$7 subu $9,$6,$7 dsll $2,$9,1 daddu $2,$2,$9 dsll $10,$2,4 daddu $2,$2,$10 dsll $10,$2,8 daddu $2,$2,$10 dsll $10,$2,16 daddu $2,$2,$10 dsll $2,$2,1 daddu $2,$2,$9 dsra $2,$2,32 sra $2,$2,2 sra $8,$8,31 subu $2,$2,$8 slt $2,$2,$5 bne $2,$0,.L7 nop sll $2,$5,2 addu $2,$2,$5 sll $2,$2,1 addu $5,$2,$7 dsll $2,$3,1 daddu $2,$2,$3 dsll $7,$2,4 daddu $2,$2,$7 dsll $7,$2,8 daddu $2,$2,$7 dsll $7,$2,16 daddu $2,$2,$7 dsll $2,$2,1 daddu $2,$2,$3 dsra $2,$2,32 sra $2,$2,2 sra $7,$3,31 bne $2,$7,.L4 subu $3,$2,$7 .L3: xor $2,$4,$5 jr $31 sltu $2,$2,1 .L6: b .L3 move $5,$4 .L5: jr $31 move $2,$0 .L7: jr $31 move $2,$0
270
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
mips64
-O2
mips64 gcc 15.2.0
isPalindrome: bltz $4,.L7 nop beq $4,$0,.L6 li $8,2147418112 # 0x7fff0000 move $5,$4 move $2,$0 b .L4 ori $8,$8,0xffff .L11: addu $2,$7,$6 beq $3,$0,.L3 move $5,$3 .L4: dsll $3,$5,1 daddu $3,$3,$5 dsll $6,$3,4 daddu $3,$3,$6 dsll $6,$3,8 daddu $3,$3,$6 dsll $6,$3,16 daddu $3,$3,$6 dsll $3,$3,1 daddu $3,$3,$5 dsra $3,$3,32 sra $6,$5,31 sra $3,$3,2 subu $3,$3,$6 sll $6,$3,2 addu $6,$6,$3 sll $6,$6,1 subu $6,$5,$6 subu $7,$8,$6 dsll $7,$7,32 dsrl $7,$7,32 dsll $5,$7,1 daddu $5,$5,$7 dsll $9,$5,4 daddu $5,$5,$9 dsll $9,$5,8 daddu $5,$5,$9 dsll $9,$5,16 daddu $5,$5,$9 dsll $5,$5,2 daddu $5,$5,$7 dsrl $5,$5,35 sll $7,$2,2 addu $7,$7,$2 slt $5,$5,$2 beq $5,$0,.L11 sll $7,$7,1 .L7: jr $31 move $2,$0 .L6: move $2,$0 .L3: xor $2,$4,$2 jr $31 sltu $2,$2,1
271
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
mips64
-O3
mips64 gcc 15.2.0
isPalindrome: bltz $4,.L7 nop beq $4,$0,.L6 li $8,2147418112 # 0x7fff0000 move $5,$4 move $2,$0 b .L4 ori $8,$8,0xffff .L11: addu $2,$7,$6 beq $3,$0,.L3 move $5,$3 .L4: dsll $3,$5,1 daddu $3,$3,$5 dsll $6,$3,4 daddu $3,$3,$6 dsll $6,$3,8 daddu $3,$3,$6 dsll $6,$3,16 daddu $3,$3,$6 dsll $3,$3,1 daddu $3,$3,$5 dsra $3,$3,32 sra $6,$5,31 sra $3,$3,2 subu $3,$3,$6 sll $6,$3,2 addu $6,$6,$3 sll $6,$6,1 subu $6,$5,$6 subu $7,$8,$6 dsll $7,$7,32 dsrl $7,$7,32 dsll $5,$7,1 daddu $5,$5,$7 dsll $9,$5,4 daddu $5,$5,$9 dsll $9,$5,8 daddu $5,$5,$9 dsll $9,$5,16 daddu $5,$5,$9 dsll $5,$5,2 daddu $5,$5,$7 dsrl $5,$5,35 sll $7,$2,2 addu $7,$7,$2 slt $5,$5,$2 beq $5,$0,.L11 sll $7,$7,1 .L7: jr $31 move $2,$0 .L6: move $2,$0 .L3: xor $2,$4,$2 jr $31 sltu $2,$2,1
272
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
riscv64
-O0
RISC-V 64 clang 21.1.0
isPalindrome: addi sp, sp, -48 sd ra, 40(sp) sd s0, 32(sp) addi s0, sp, 48 sw a0, -24(s0) li a0, 0 sw a0, -36(s0) lw a0, -24(s0) bgez a0, .LBB0_2 j .LBB0_1 .LBB0_1: li a0, 0 sb a0, -17(s0) j .LBB0_8 .LBB0_2: lw a0, -24(s0) sw a0, -28(s0) j .LBB0_3 .LBB0_3: lw a0, -28(s0) beqz a0, .LBB0_7 j .LBB0_4 .LBB0_4: lw a0, -28(s0) lui a1, 419430 addi a2, a1, 1639 mul a1, a0, a2 srli a3, a1, 63 srai a1, a1, 34 addw a1, a1, a3 slliw a3, a1, 1 slliw a1, a1, 3 addw a1, a1, a3 subw a0, a0, a1 sw a0, -32(s0) lw a1, -36(s0) lwu a0, -32(s0) lui a3, 524288 addiw a3, a3, -1 xor a0, a0, a3 mul a0, a0, a2 srli a0, a0, 34 bge a0, a1, .LBB0_6 j .LBB0_5 .LBB0_5: li a0, 0 sb a0, -17(s0) j .LBB0_8 .LBB0_6: lw a0, -36(s0) slliw a1, a0, 1 slliw a0, a0, 3 addw a0, a0, a1 lw a1, -32(s0) addw a0, a0, a1 sw a0, -36(s0) lw a0, -28(s0) lui a1, 419430 addi a1, a1, 1639 mul a0, a0, a1 srli a1, a0, 63 srai a0, a0, 34 addw a0, a0, a1 sw a0, -28(s0) j .LBB0_3 .LBB0_7: lw a0, -36(s0) lw a1, -24(s0) xor a0, a0, a1 seqz a0, a0 sb a0, -17(s0) j .LBB0_8 .LBB0_8: lbu a0, -17(s0) ld ra, 40(sp) ld s0, 32(sp) addi sp, sp, 48 ret
273
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
riscv64
-O1
RISC-V 64 clang 21.1.0
isPalindrome: bltz a0, .LBB0_6 li a1, 0 beqz a0, .LBB0_5 lui a2, 205 lui a7, 524288 addi a2, a2, -819 addi a7, a7, -2 slli a2, a2, 12 addi t0, a2, -819 li a6, 9 mv a4, a0 .LBB0_3: mv a3, a4 slli a2, a4, 32 srli a2, a2, 32 mul a5, a2, t0 srli a4, a5, 35 slli a2, a4, 1 slli a5, a4, 3 add a2, a2, a5 subw a2, a3, a2 xor a5, a2, a7 slli a5, a5, 32 srli a5, a5, 32 mul a5, a5, t0 srli a5, a5, 35 blt a5, a1, .LBB0_6 slli a5, a1, 1 slli a1, a1, 3 add a1, a1, a5 addw a1, a1, a2 bltu a6, a3, .LBB0_3 .LBB0_5: xor a0, a0, a1 seqz a0, a0 ret .LBB0_6: li a0, 0 ret
274
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
riscv64
-O2
RISC-V 64 clang 21.1.0
isPalindrome: bltz a0, .LBB0_6 li a1, 0 beqz a0, .LBB0_5 lui a2, 205 lui a7, 524288 addi a2, a2, -819 addi a7, a7, -2 slli a2, a2, 12 addi t0, a2, -819 li a6, 9 mv a4, a0 .LBB0_3: mv a3, a4 slli a2, a4, 32 srli a2, a2, 32 mul a5, a2, t0 srli a4, a5, 35 slli a2, a4, 1 slli a5, a4, 3 add a2, a2, a5 subw a2, a3, a2 xor a5, a2, a7 slli a5, a5, 32 srli a5, a5, 32 mul a5, a5, t0 srli a5, a5, 35 blt a5, a1, .LBB0_6 slli a5, a1, 1 slli a1, a1, 3 add a1, a1, a5 addw a1, a1, a2 bltu a6, a3, .LBB0_3 .LBB0_5: xor a0, a0, a1 seqz a0, a0 ret .LBB0_6: li a0, 0 ret
275
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
riscv64
-O3
RISC-V 64 clang 21.1.0
isPalindrome: bltz a0, .LBB0_6 li a1, 0 beqz a0, .LBB0_5 lui a2, 205 lui a7, 524288 addi a2, a2, -819 addi a7, a7, -2 slli a2, a2, 12 addi t0, a2, -819 li a6, 9 mv a4, a0 .LBB0_3: mv a3, a4 slli a2, a4, 32 srli a2, a2, 32 mul a5, a2, t0 srli a4, a5, 35 slli a2, a4, 1 slli a5, a4, 3 add a2, a2, a5 subw a2, a3, a2 xor a5, a2, a7 slli a5, a5, 32 srli a5, a5, 32 mul a5, a5, t0 srli a5, a5, 35 blt a5, a1, .LBB0_6 slli a5, a1, 1 slli a1, a1, 3 add a1, a1, a5 addw a1, a1, a2 bltu a6, a3, .LBB0_3 .LBB0_5: xor a0, a0, a1 seqz a0, a0 ret .LBB0_6: li a0, 0 ret
276
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
riscv64
-O0
RISC-V 64 gcc 15.2.0
isPalindrome: addi sp,sp,-48 sd ra,40(sp) sd s0,32(sp) addi s0,sp,48 mv a5,a0 sw a5,-36(s0) sw zero,-24(s0) lw a5,-36(s0) sext.w a5,a5 bge a5,zero,.L2 li a5,0 j .L3 .L2: lw a5,-36(s0) sw a5,-20(s0) j .L4 .L6: lw a5,-20(s0) mv a4,a5 sext.w a3,a4 li a5,1717985280 addi a5,a5,1639 mul a5,a3,a5 srli a5,a5,32 sraiw a5,a5,2 mv a3,a5 sraiw a5,a4,31 subw a5,a3,a5 mv a3,a5 mv a5,a3 slliw a5,a5,2 addw a5,a5,a3 slliw a5,a5,1 subw a5,a4,a5 sw a5,-28(s0) li a5,-2147483648 addiw a5,a5,-1 lw a4,-28(s0) subw a5,a5,a4 sext.w a5,a5 mv a3,a5 sext.w a4,a3 li a5,1717985280 addi a5,a5,1639 mul a5,a4,a5 srli a5,a5,32 sraiw a5,a5,2 mv a4,a5 sraiw a5,a3,31 subw a5,a4,a5 sext.w a5,a5 lw a4,-24(s0) sext.w a4,a4 ble a4,a5,.L5 li a5,0 j .L3 .L5: lw a5,-24(s0) mv a4,a5 mv a5,a4 slliw a5,a5,2 addw a5,a5,a4 slliw a5,a5,1 sext.w a5,a5 lw a4,-28(s0) addw a5,a4,a5 sw a5,-24(s0) lw a5,-20(s0) mv a3,a5 sext.w a4,a3 li a5,1717985280 addi a5,a5,1639 mul a5,a4,a5 srli a5,a5,32 sraiw a5,a5,2 mv a4,a5 sraiw a5,a3,31 subw a5,a4,a5 sw a5,-20(s0) .L4: lw a5,-20(s0) sext.w a5,a5 bne a5,zero,.L6 lw a5,-24(s0) mv a4,a5 lw a5,-36(s0) sext.w a4,a4 sext.w a5,a5 sub a5,a4,a5 seqz a5,a5 andi a5,a5,0xff .L3: mv a0,a5 ld ra,40(sp) ld s0,32(sp) addi sp,sp,48 jr ra
277
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
riscv64
-O1
RISC-V 64 gcc 15.2.0
isPalindrome: blt a0,zero,.L5 beq a0,zero,.L6 mv a4,a0 li a1,0 li a6,1717985280 addi a6,a6,1639 li a7,-2147483648 addiw a7,a7,-1 .L4: mul a3,a4,a6 srai a3,a3,34 sraiw a5,a4,31 subw a3,a3,a5 slliw a5,a3,2 addw a5,a5,a3 slliw a5,a5,1 subw a5,a4,a5 subw a2,a7,a5 mul a3,a2,a6 srai a3,a3,34 sraiw a2,a2,31 subw a3,a3,a2 blt a3,a1,.L7 slliw a3,a1,2 addw a3,a3,a1 slliw a3,a3,1 addw a1,a3,a5 mul a5,a4,a6 srai a5,a5,34 sraiw a4,a4,31 subw a4,a5,a4 bne a4,zero,.L4 .L3: sub a0,a0,a1 seqz a0,a0 ret .L6: mv a1,a0 j .L3 .L5: li a0,0 ret .L7: li a0,0 ret
278
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
riscv64
-O2
RISC-V 64 gcc 15.2.0
isPalindrome: blt a0,zero,.L7 beq a0,zero,.L6 li t3,-2147483648 li t1,1717985280 addi t1,t1,1639 addiw t3,t3,-1 mv a1,a0 li a7,0 j .L4 .L10: addw a7,a2,a5 beq a1,zero,.L3 .L4: mul a4,a1,t1 sraiw a3,a1,31 slliw a5,a7,2 addw a5,a5,a7 slliw a2,a5,1 srai a4,a4,34 subw a4,a4,a3 slliw a5,a4,2 addw a5,a5,a4 slliw a5,a5,1 subw a5,a1,a5 subw a1,t3,a5 mul a3,a1,t1 sraiw a6,a1,31 mv a1,a4 srai a4,a3,34 subw a4,a4,a6 bge a4,a7,.L10 .L7: li a0,0 ret .L6: li a7,0 .L3: sub a0,a0,a7 seqz a0,a0 ret
279
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
riscv64
-O3
RISC-V 64 gcc 15.2.0
isPalindrome: blt a0,zero,.L7 beq a0,zero,.L6 li t3,-2147483648 li t1,1717985280 addi t1,t1,1639 addiw t3,t3,-1 mv a1,a0 li a7,0 j .L4 .L10: addw a7,a2,a5 beq a1,zero,.L3 .L4: mul a4,a1,t1 sraiw a3,a1,31 slliw a5,a7,2 addw a5,a5,a7 slliw a2,a5,1 srai a4,a4,34 subw a4,a4,a3 slliw a5,a4,2 addw a5,a5,a4 slliw a5,a5,1 subw a5,a1,a5 subw a1,t3,a5 mul a3,a1,t1 sraiw a6,a1,31 mv a1,a4 srai a4,a3,34 subw a4,a4,a6 bge a4,a7,.L10 .L7: li a0,0 ret .L6: li a7,0 .L3: sub a0,a0,a7 seqz a0,a0 ret
280
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
x86-64
-O0
x86-64 clang 21.1.0
isPalindrome: push rbp mov rbp, rsp mov dword ptr [rbp - 8], edi mov dword ptr [rbp - 20], 0 cmp dword ptr [rbp - 8], 0 jge .LBB0_2 mov byte ptr [rbp - 1], 0 jmp .LBB0_8 .LBB0_2: mov eax, dword ptr [rbp - 8] mov dword ptr [rbp - 12], eax .LBB0_3: cmp dword ptr [rbp - 12], 0 je .LBB0_7 mov eax, dword ptr [rbp - 12] mov ecx, 10 cdq idiv ecx mov dword ptr [rbp - 16], edx mov eax, dword ptr [rbp - 20] mov dword ptr [rbp - 24], eax mov eax, 2147483647 sub eax, dword ptr [rbp - 16] mov ecx, 10 cdq idiv ecx mov ecx, eax mov eax, dword ptr [rbp - 24] cmp eax, ecx jle .LBB0_6 mov byte ptr [rbp - 1], 0 jmp .LBB0_8 .LBB0_6: imul eax, dword ptr [rbp - 20], 10 add eax, dword ptr [rbp - 16] mov dword ptr [rbp - 20], eax mov eax, dword ptr [rbp - 12] mov ecx, 10 cdq idiv ecx mov dword ptr [rbp - 12], eax jmp .LBB0_3 .LBB0_7: mov eax, dword ptr [rbp - 20] cmp eax, dword ptr [rbp - 8] sete al and al, 1 mov byte ptr [rbp - 1], al .LBB0_8: mov al, byte ptr [rbp - 1] and al, 1 pop rbp ret
281
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
x86-64
-O1
x86-64 clang 21.1.0
isPalindrome: xor eax, eax test edi, edi js .LBB0_7 je .LBB0_6 xor eax, eax mov ecx, 3435973837 mov edx, edi .LBB0_3: mov esi, edx imul rsi, rcx shr rsi, 35 lea r8d, [rsi + rsi] lea r9d, [r8 + 4*r8] mov r8d, edx sub r8d, r9d mov r9d, r8d xor r9d, 2147483646 imul r9, r9, 1717986919 shr r9, 34 cmp eax, r9d jg .LBB0_4 lea eax, [rax + 4*rax] lea eax, [r8 + 2*rax] cmp edx, 9 mov edx, esi ja .LBB0_3 .LBB0_6: cmp eax, edi sete al .LBB0_7: ret .LBB0_4: xor eax, eax ret
282
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
x86-64
-O2
x86-64 clang 21.1.0
isPalindrome: xor eax, eax test edi, edi js .LBB0_7 je .LBB0_6 xor eax, eax mov ecx, 3435973837 mov edx, edi .LBB0_3: mov esi, edx imul rsi, rcx shr rsi, 35 lea r8d, [rsi + rsi] lea r9d, [r8 + 4*r8] mov r8d, edx sub r8d, r9d mov r9d, r8d xor r9d, 2147483646 imul r9, r9, 1717986919 shr r9, 34 cmp eax, r9d jg .LBB0_4 lea eax, [rax + 4*rax] lea eax, [r8 + 2*rax] cmp edx, 9 mov edx, esi ja .LBB0_3 .LBB0_6: cmp eax, edi sete al .LBB0_7: ret .LBB0_4: xor eax, eax ret
283
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
x86-64
-O3
x86-64 clang 21.1.0
isPalindrome: xor eax, eax test edi, edi js .LBB0_7 je .LBB0_6 xor eax, eax mov ecx, 3435973837 mov edx, edi .LBB0_3: mov esi, edx imul rsi, rcx shr rsi, 35 lea r8d, [rsi + rsi] lea r9d, [r8 + 4*r8] mov r8d, edx sub r8d, r9d mov r9d, r8d xor r9d, 2147483646 imul r9, r9, 1717986919 shr r9, 34 cmp eax, r9d jg .LBB0_4 lea eax, [rax + 4*rax] lea eax, [r8 + 2*rax] cmp edx, 9 mov edx, esi ja .LBB0_3 .LBB0_6: cmp eax, edi sete al .LBB0_7: ret .LBB0_4: xor eax, eax ret
284
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
x86-64
-O0
x86-64 gcc 15.2
isPalindrome: push rbp mov rbp, rsp mov DWORD PTR [rbp-20], edi mov DWORD PTR [rbp-8], 0 cmp DWORD PTR [rbp-20], 0 jns .L2 mov eax, 0 jmp .L3 .L2: mov eax, DWORD PTR [rbp-20] mov DWORD PTR [rbp-4], eax jmp .L4 .L6: mov edx, DWORD PTR [rbp-4] movsx rax, edx imul rax, rax, 1717986919 shr rax, 32 mov ecx, eax sar ecx, 2 mov eax, edx sar eax, 31 sub ecx, eax mov eax, ecx sal eax, 2 add eax, ecx add eax, eax sub edx, eax mov DWORD PTR [rbp-12], edx mov eax, 2147483647 sub eax, DWORD PTR [rbp-12] movsx rdx, eax imul rdx, rdx, 1717986919 shr rdx, 32 mov ecx, edx sar ecx, 2 cdq mov eax, ecx sub eax, edx cmp DWORD PTR [rbp-8], eax jle .L5 mov eax, 0 jmp .L3 .L5: mov edx, DWORD PTR [rbp-8] mov eax, edx sal eax, 2 add eax, edx add eax, eax mov edx, eax mov eax, DWORD PTR [rbp-12] add eax, edx mov DWORD PTR [rbp-8], eax mov eax, DWORD PTR [rbp-4] movsx rdx, eax imul rdx, rdx, 1717986919 shr rdx, 32 mov ecx, edx sar ecx, 2 cdq mov eax, ecx sub eax, edx mov DWORD PTR [rbp-4], eax .L4: cmp DWORD PTR [rbp-4], 0 jne .L6 mov eax, DWORD PTR [rbp-8] cmp eax, DWORD PTR [rbp-20] sete al .L3: pop rbp ret
285
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
x86-64
-O1
x86-64 gcc 15.2
isPalindrome: mov r8d, edi mov eax, 0 test edi, edi js .L1 je .L6 mov ecx, edi mov esi, 0 mov edi, 2147483647 .L4: movsx rax, ecx imul rax, rax, 1717986919 sar rax, 34 mov edx, ecx sar edx, 31 sub eax, edx lea edx, [rax+rax*4] add edx, edx mov eax, ecx sub eax, edx mov r9d, edi sub r9d, eax movsx rdx, r9d imul rdx, rdx, 1717986919 sar rdx, 34 sar r9d, 31 sub edx, r9d cmp edx, esi jl .L7 lea edx, [rsi+rsi*4] lea esi, [rax+rdx*2] movsx rax, ecx imul rax, rax, 1717986919 sar rax, 34 sar ecx, 31 sub eax, ecx mov ecx, eax jne .L4 .L3: cmp r8d, esi sete al ret .L6: mov esi, edi jmp .L3 .L7: mov eax, 0 .L1: ret
286
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
x86-64
-O2
x86-64 gcc 15.2
isPalindrome: mov r10d, edi xor eax, eax test edi, edi js .L1 je .L6 mov edx, edi xor ecx, ecx mov r8d, 3435973837 jmp .L4 .L10: lea edx, [rcx+rcx*4] lea ecx, [rax+rdx*2] mov edx, edi test edi, edi je .L3 .L4: movsx rax, edx mov esi, edx imul rax, rax, 1717986919 sar esi, 31 sar rax, 34 sub eax, esi lea esi, [rax+rax*4] mov edi, eax mov eax, edx mov edx, 2147483647 add esi, esi sub eax, esi sub edx, eax imul rdx, r8 shr rdx, 35 cmp edx, ecx jge .L10 xor eax, eax .L1: ret .L6: xor ecx, ecx .L3: cmp r10d, ecx sete al ret
287
9
Palindrome Number
Easy
/* 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting the integer to a string? */ bool isPalindrome(int x) { int n, d, k = 0; if (x < 0) return false; n = x; while (n) { d = n % 10; if (k > (0x7fffffff - d) / 10) return false; // overflow k = k * 10 + d; n = n / 10; } return (k == x); } /* Difficulty:Easy */
x86-64
-O3
x86-64 gcc 15.2
isPalindrome: mov r10d, edi xor eax, eax test edi, edi js .L1 je .L6 mov edx, edi xor ecx, ecx mov r8d, 3435973837 jmp .L4 .L10: lea edx, [rcx+rcx*4] lea ecx, [rax+rdx*2] mov edx, edi test edi, edi je .L3 .L4: movsx rax, edx mov esi, edx imul rax, rax, 1717986919 sar esi, 31 sar rax, 34 sub eax, esi lea esi, [rax+rax*4] mov edi, eax mov eax, edx mov edx, 2147483647 add esi, esi sub eax, esi sub edx, eax imul rdx, r8 shr rdx, 35 cmp edx, ecx jge .L10 xor eax, eax .L1: ret .L6: xor ecx, ecx .L3: cmp r10d, ecx sete al ret
288
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
aarch64
-O0
ARM64 gcc 15.2.0
match_recursive: stp x29, x30, [sp, -48]! mov x29, sp str x0, [sp, 40] str x1, [sp, 32] str x2, [sp, 24] ldr x0, [sp, 32] ldrb w0, [x0] cmp w0, 0 bne .L2 ldr x0, [sp, 40] ldrb w0, [x0] cmp w0, 0 cset w0, eq and w0, w0, 255 b .L3 .L2: ldr x0, [sp, 32] add x0, x0, 1 ldrb w0, [x0] cmp w0, 42 bne .L4 b .L5 .L10: ldr x0, [sp, 32] add x0, x0, 2 ldr x2, [sp, 24] mov x1, x0 ldr x0, [sp, 40] bl match_recursive and w0, w0, 255 and w0, w0, 1 cmp w0, 0 beq .L6 mov w0, 1 b .L3 .L6: ldr x0, [sp, 32] ldrb w0, [x0] cmp w0, 46 bne .L7 ldr x0, [sp, 40] ldrb w0, [x0] cmp w0, 0 beq .L8 .L7: ldr x0, [sp, 32] ldrb w0, [x0] cmp w0, 46 beq .L9 ldr x0, [sp, 40] ldrb w1, [x0] ldr x0, [sp, 32] ldrb w0, [x0] cmp w1, w0 beq .L9 .L8: mov w0, 0 b .L3 .L9: ldr x0, [sp, 40] add x0, x0, 1 str x0, [sp, 40] .L5: ldr x0, [sp, 24] ldr w0, [x0] cmp w0, 0 bne .L10 ldr x0, [sp, 24] str wzr, [x0] mov w0, 0 b .L3 .L4: ldr x0, [sp, 32] ldrb w0, [x0] cmp w0, 46 bne .L11 ldr x0, [sp, 40] ldrb w0, [x0] cmp w0, 0 beq .L12 .L11: ldr x0, [sp, 32] ldrb w0, [x0] cmp w0, 46 beq .L13 ldr x0, [sp, 40] ldrb w1, [x0] ldr x0, [sp, 32] ldrb w0, [x0] cmp w1, w0 beq .L13 .L12: mov w0, 0 b .L3 .L13: ldr x0, [sp, 40] add x3, x0, 1 ldr x0, [sp, 32] add x0, x0, 1 ldr x2, [sp, 24] mov x1, x0 mov x0, x3 bl match_recursive and w0, w0, 255 .L3: ldp x29, x30, [sp], 48 ret isMatch: stp x29, x30, [sp, -64]! mov x29, sp str x0, [sp, 24] str x1, [sp, 16] ldr x0, [sp, 24] bl strlen str w0, [sp, 52] ldr x0, [sp, 16] bl strlen str w0, [sp, 48] ldr w0, [sp, 52] add w1, w0, 1 ldr w0, [sp, 48] add w0, w0, 1 mul w0, w1, w0 sxtw x0, w0 mov x1, 4 bl calloc str x0, [sp, 40] ldr x0, [sp, 40] mov w1, 1 str w1, [x0] str wzr, [sp, 56] b .L15 .L17: ldrsw x0, [sp, 56] ldr x1, [sp, 16] add x0, x1, x0 ldrb w0, [x0] cmp w0, 42 bne .L16 ldr w0, [sp, 56] sub w0, w0, #2 sxtw x0, w0 add x0, x0, 1 lsl x0, x0, 2 ldr x1, [sp, 40] add x1, x1, x0 ldrsw x0, [sp, 56] add x0, x0, 1 lsl x0, x0, 2 ldr x2, [sp, 40] add x0, x2, x0 ldr w1, [x1] str w1, [x0] .L16: ldr w0, [sp, 56] add w0, w0, 1 str w0, [sp, 56] .L15: ldr w1, [sp, 56] ldr w0, [sp, 48] cmp w1, w0 blt .L17 str wzr, [sp, 60] b .L18 .L30: str wzr, [sp, 56] b .L19 .L29: ldrsw x0, [sp, 56] ldr x1, [sp, 16] add x0, x1, x0 ldrb w0, [x0] cmp w0, 42 beq .L20 ldr w0, [sp, 48] add w1, w0, 1 ldr w0, [sp, 60] mul w1, w1, w0 ldr w0, [sp, 56] sub w0, w0, #1 add w0, w1, w0 sxtw x0, w0 add x0, x0, 1 lsl x0, x0, 2 ldr x1, [sp, 40] add x0, x1, x0 ldr w0, [x0] cmp w0, 0 beq .L21 ldrsw x0, [sp, 60] ldr x1, [sp, 24] add x0, x1, x0 ldrb w1, [x0] ldrsw x0, [sp, 56] ldr x2, [sp, 16] add x0, x2, x0 ldrb w0, [x0] cmp w1, w0 beq .L22 ldrsw x0, [sp, 56] ldr x1, [sp, 16] add x0, x1, x0 ldrb w0, [x0] cmp w0, 46 bne .L21 .L22: mov w1, 1 b .L23 .L21: mov w1, 0 .L23: ldr w0, [sp, 60] add w2, w0, 1 ldr w0, [sp, 48] add w0, w0, 1 mul w2, w2, w0 ldr w0, [sp, 56] add w0, w2, w0 sxtw x0, w0 add x0, x0, 1 lsl x0, x0, 2 ldr x2, [sp, 40] add x0, x2, x0 str w1, [x0] b .L24 .L20: ldr w0, [sp, 60] add w1, w0, 1 ldr w0, [sp, 48] add w0, w0, 1 mul w1, w1, w0 ldr w0, [sp, 56] sub w0, w0, #2 add w0, w1, w0 sxtw x0, w0 add x0, x0, 1 lsl x0, x0, 2 ldr x1, [sp, 40] add x0, x1, x0 ldr w0, [x0] cmp w0, 0 bne .L25 ldr w0, [sp, 60] add w1, w0, 1 ldr w0, [sp, 48] add w0, w0, 1 mul w1, w1, w0 ldr w0, [sp, 56] sub w0, w0, #1 add w0, w1, w0 sxtw x0, w0 add x0, x0, 1 lsl x0, x0, 2 ldr x1, [sp, 40] add x0, x1, x0 ldr w0, [x0] cmp w0, 0 bne .L25 ldrsw x0, [sp, 60] ldr x1, [sp, 24] add x0, x1, x0 ldrb w1, [x0] ldrsw x0, [sp, 56] sub x0, x0, #1 ldr x2, [sp, 16] add x0, x2, x0 ldrb w0, [x0] cmp w1, w0 beq .L26 ldrsw x0, [sp, 56] sub x0, x0, #1 ldr x1, [sp, 16] add x0, x1, x0 ldrb w0, [x0] cmp w0, 46 bne .L27 .L26: ldr w0, [sp, 48] add w1, w0, 1 ldr w0, [sp, 60] mul w1, w1, w0 ldr w0, [sp, 56] add w0, w1, w0 sxtw x0, w0 add x0, x0, 1 lsl x0, x0, 2 ldr x1, [sp, 40] add x0, x1, x0 ldr w0, [x0] cmp w0, 0 beq .L27 .L25: mov w1, 1 b .L28 .L27: mov w1, 0 .L28: ldr w0, [sp, 60] add w2, w0, 1 ldr w0, [sp, 48] add w0, w0, 1 mul w2, w2, w0 ldr w0, [sp, 56] add w0, w2, w0 sxtw x0, w0 add x0, x0, 1 lsl x0, x0, 2 ldr x2, [sp, 40] add x0, x2, x0 str w1, [x0] .L24: ldr w0, [sp, 56] add w0, w0, 1 str w0, [sp, 56] .L19: ldr w1, [sp, 56] ldr w0, [sp, 48] cmp w1, w0 blt .L29 ldr w0, [sp, 60] add w0, w0, 1 str w0, [sp, 60] .L18: ldr w1, [sp, 60] ldr w0, [sp, 52] cmp w1, w0 blt .L30 ldr w0, [sp, 48] add w1, w0, 1 ldr w0, [sp, 52] mul w1, w1, w0 ldr w0, [sp, 48] sub w0, w0, #1 add w0, w1, w0 sxtw x0, w0 add x0, x0, 1 lsl x0, x0, 2 ldr x1, [sp, 40] add x0, x1, x0 ldr w0, [x0] str w0, [sp, 60] ldr x0, [sp, 40] bl free ldr w0, [sp, 60] cmp w0, 0 cset w0, ne and w0, w0, 255 ldp x29, x30, [sp], 64 ret
289
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
aarch64
-O1
ARM64 gcc 15.2.0
match_recursive: stp x29, x30, [sp, -48]! mov x29, sp stp x19, x20, [sp, 16] mov x19, x0 ldrb w0, [x1] cbz w0, .L18 stp x21, x22, [sp, 32] mov x20, x1 mov x21, x2 ldrb w1, [x1, 1] cmp w1, 42 beq .L19 cmp w0, 46 beq .L20 ldrb w2, [x19] mov w1, 0 cmp w2, w0 bne .L17 .L10: mov x2, x21 add x1, x20, 1 add x0, x19, 1 bl match_recursive and w1, w0, 255 ldp x21, x22, [sp, 32] b .L3 .L18: ldrb w0, [x19] cmp w0, 0 cset w1, eq .L3: mov w0, w1 ldp x19, x20, [sp, 16] ldp x29, x30, [sp], 48 ret .L19: ldr w0, [x2] cbz w0, .L5 add x22, x20, 2 b .L8 .L22: ldrb w0, [x19] cbz w0, .L21 .L7: add x19, x19, 1 ldr w0, [x21] cbz w0, .L5 .L8: mov x2, x21 mov x1, x22 mov x0, x19 bl match_recursive and w1, w0, 255 tbnz x0, 0, .L15 ldrb w0, [x20] cmp w0, 46 beq .L22 ldrb w2, [x19] cmp w2, w0 beq .L7 ldp x21, x22, [sp, 32] b .L3 .L21: ldp x21, x22, [sp, 32] b .L3 .L5: str wzr, [x21] mov w1, 0 ldp x21, x22, [sp, 32] b .L3 .L20: ldrb w0, [x19] mov w1, 0 cbnz w0, .L10 ldp x21, x22, [sp, 32] b .L3 .L15: ldp x21, x22, [sp, 32] b .L3 .L17: ldp x21, x22, [sp, 32] b .L3 isMatch: stp x29, x30, [sp, -80]! mov x29, sp stp x19, x20, [sp, 16] stp x21, x22, [sp, 32] stp x23, x24, [sp, 48] str x25, [sp, 64] mov x24, x0 mov x20, x1 bl strlen mov x25, x0 mov x0, x20 bl strlen mov x23, x0 mov w21, w0 add w19, w0, 1 add w22, w25, 1 madd w22, w22, w0, w22 mov x1, 4 sxtw x0, w22 bl calloc mov w1, 1 str w1, [x0] cmp w23, 0 ble .L24 mov x1, 0 sub x4, x0, #4 add x3, x0, 4 b .L26 .L25: add x1, x1, 1 cmp w21, w1 ble .L24 .L26: ldrb w2, [x20, x1] cmp w2, 42 bne .L25 ldr w2, [x4, x1, lsl 2] str w2, [x3, x1, lsl 2] b .L25 .L24: cmp w25, 0 ble .L27 add w6, w19, w23 mov w13, w20 sub w7, w19, w20 add w12, w23, 1 neg w11, w20 mov x8, x24 add x15, x24, 1 sub w25, w25, #1 add x15, x15, x25 mov w14, w11 mov w10, 1 b .L28 .L30: add w4, w4, w7 add x4, x0, w4, sxtw 2 str w5, [x4, 4] .L31: add x2, x2, 1 add w3, w3, 1 cmp w3, w6 beq .L35 .L33: mov x4, x2 ldrb w1, [x2] cmp w1, 42 beq .L29 add w5, w9, w2 add x5, x0, w5, sxtw 2 ldr w5, [x5, 4] cbz w5, .L30 ldrb w5, [x8] cmp w1, 46 ccmp w5, w1, 4, ne cset w5, eq b .L30 .L29: sub w1, w3, #2 add x1, x0, w1, sxtw 2 ldr w5, [x1, 4] mov w1, w10 cbnz w5, .L32 sub w1, w3, #1 add x1, x0, w1, sxtw 2 ldr w1, [x1, 4] cbnz w1, .L37 ldrb w4, [x2, -1] ldrb w5, [x8] cmp w4, 46 ccmp w5, w4, 4, ne bne .L32 add w1, w11, w2 add x1, x0, w1, sxtw 2 ldr w1, [x1, 4] cmp w1, 0 cset w1, ne b .L32 .L37: mov w1, w10 .L32: add x4, x0, w3, sxtw 2 str w1, [x4, 4] b .L31 .L35: add w6, w6, w19 add w7, w7, w19 add w11, w11, w12 add x8, x8, 1 add w14, w14, w19 cmp x8, x15 beq .L27 .L28: add w3, w7, w13 mov x2, x20 sub w9, w14, #1 cmp w21, 0 bgt .L33 b .L35 .L27: sub w1, w22, w19 sub w23, w23, #1 add w1, w1, w23 add x1, x0, w1, sxtw 2 ldr w19, [x1, 4] bl free cmp w19, 0 cset w0, ne ldp x19, x20, [sp, 16] ldp x21, x22, [sp, 32] ldp x23, x24, [sp, 48] ldr x25, [sp, 64] ldp x29, x30, [sp], 80 ret
290
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
aarch64
-O2
ARM64 gcc 15.2.0
match_recursive: stp x29, x30, [sp, -64]! mov x29, sp stp x19, x20, [sp, 16] mov x19, x0 mov x20, x1 ldrb w3, [x1] cbnz w3, .L2 b .L14 .L12: cmp w1, w0 bne .L8 .L13: add x19, x19, 1 add x20, x20, 1 cbz w3, .L14 .L2: mov w0, w3 ldrb w3, [x20, 1] cmp w3, 42 beq .L29 ldrb w1, [x19] cmp w0, 46 bne .L12 cbnz w1, .L13 .L8: mov w1, 0 .L3: ldp x19, x20, [sp, 16] mov w0, w1 ldp x29, x30, [sp], 64 ret .L14: ldrb w0, [x19] ldp x19, x20, [sp, 16] cmp w0, 0 cset w1, eq mov w0, w1 ldp x29, x30, [sp], 64 ret .L29: ldr w0, [x2] str x21, [sp, 32] add x21, x20, 2 cbnz w0, .L10 b .L11 .L6: cmp w0, w1 bne .L27 .L7: ldr w0, [x2] add x19, x19, 1 cbz w0, .L11 .L10: mov x1, x21 mov x0, x19 str x2, [sp, 56] bl match_recursive and w1, w0, 255 tbnz x0, 0, .L26 ldrb w0, [x20] ldr x2, [sp, 56] cmp w0, 46 ldrb w1, [x19] bne .L6 cbnz w1, .L7 .L27: ldr x21, [sp, 32] b .L8 .L11: ldr x21, [sp, 32] str wzr, [x2] b .L8 .L26: ldr x21, [sp, 32] b .L3 isMatch: stp x29, x30, [sp, -64]! mov x29, sp stp x19, x20, [sp, 16] mov x20, x1 stp x21, x22, [sp, 32] stp x23, x24, [sp, 48] mov x24, x0 bl strlen mov x23, x0 mov x0, x20 add w22, w23, 1 bl strlen mov x1, 4 mov x19, x0 add w21, w0, 1 madd w22, w22, w0, w22 sxtw x0, w22 bl calloc mov w1, 1 str w1, [x0] sub x4, x0, #4 add x3, x0, 4 mov x1, 0 cmp w19, 0 ble .L36 .L31: ldrb w2, [x20, x1] cmp w2, 42 bne .L35 ldr w2, [x4, x1, lsl 2] str w2, [x3, x1, lsl 2] .L35: add x1, x1, 1 cmp w19, w1 bgt .L31 .L36: cmp w23, 0 ble .L32 add x9, x24, 1 sub w23, w23, #1 mov x8, x24 add x9, x9, x23 cmp w19, 0 ble .L33 add w5, w21, w19 mvn w11, w19 mov w6, 0 .L34: add w7, w6, w21 sub x3, x20, #1 sub w6, w6, #1 mov w1, w7 b .L41 .L60: sub w2, w1, w7 add w2, w2, w6 add w2, w2, 1 ldr w2, [x0, x2, lsl 2] cbz w2, .L40 ldrb w2, [x8] cmp w4, 46 ccmp w2, w4, 4, ne cset w2, eq .L40: add w4, w1, 1 add w1, w1, 1 add x3, x3, 1 str w2, [x0, x4, lsl 2] cmp w5, w1 beq .L59 .L41: ldrb w4, [x3, 1] cmp w4, 42 bne .L60 sub w2, w1, #1 ldr w4, [x0, x2, lsl 2] mov w2, 1 cbnz w4, .L40 ldr w2, [x0, w1, uxtw 2] cbnz w2, .L44 ldrb w4, [x3] ldrb w10, [x8] cmp w4, 46 ccmp w10, w4, 4, ne bne .L40 add w2, w11, w1 add w4, w1, 1 add w2, w2, 1 add w1, w1, 1 add x3, x3, 1 ldr w2, [x0, x2, lsl 2] cmp w2, 0 cset w2, ne str w2, [x0, x4, lsl 2] cmp w5, w1 bne .L41 .L59: add x8, x8, 1 add w5, w5, w21 mov w6, w7 cmp x8, x9 bne .L34 .L32: sub w1, w22, w21 sub w19, w19, #1 add w1, w1, w19 add x1, x0, w1, sxtw 2 ldr w19, [x1, 4] bl free ldp x21, x22, [sp, 32] cmp w19, 0 ldp x23, x24, [sp, 48] cset w0, ne ldp x19, x20, [sp, 16] ldp x29, x30, [sp], 64 ret .L33: add x1, x8, 1 cmp x9, x1 beq .L32 add x8, x8, 2 cmp x8, x9 bne .L33 b .L32 .L44: mov w2, 1 b .L40
291
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
aarch64
-O3
ARM64 gcc 15.2.0
match_recursive: stp x29, x30, [sp, -64]! mov x29, sp stp x19, x20, [sp, 16] mov x19, x0 mov x20, x1 ldrb w3, [x1] cbnz w3, .L2 b .L14 .L12: cmp w1, w0 bne .L8 .L13: add x19, x19, 1 add x20, x20, 1 cbz w3, .L14 .L2: mov w0, w3 ldrb w3, [x20, 1] cmp w3, 42 beq .L29 ldrb w1, [x19] cmp w0, 46 bne .L12 cbnz w1, .L13 .L8: mov w1, 0 .L3: ldp x19, x20, [sp, 16] mov w0, w1 ldp x29, x30, [sp], 64 ret .L14: ldrb w0, [x19] ldp x19, x20, [sp, 16] cmp w0, 0 cset w1, eq mov w0, w1 ldp x29, x30, [sp], 64 ret .L29: ldr w0, [x2] str x21, [sp, 32] add x21, x20, 2 cbnz w0, .L10 b .L11 .L6: cmp w0, w1 bne .L27 .L7: ldr w0, [x2] cbz w0, .L11 .L10: mov x1, x21 mov x0, x19 str x2, [sp, 56] bl match_recursive and w1, w0, 255 tbnz x0, 0, .L26 ldrb w0, [x20] add x19, x19, 1 ldr x2, [sp, 56] cmp w0, 46 ldrb w1, [x19, -1] bne .L6 cbnz w1, .L7 .L27: ldr x21, [sp, 32] b .L8 .L11: ldr x21, [sp, 32] str wzr, [x2] b .L8 .L26: ldr x21, [sp, 32] b .L3 isMatch: stp x29, x30, [sp, -64]! mov x29, sp stp x19, x20, [sp, 16] mov x20, x1 stp x21, x22, [sp, 32] stp x23, x24, [sp, 48] mov x24, x0 bl strlen mov x23, x0 mov x0, x20 add w22, w23, 1 bl strlen mov x1, 4 mov x19, x0 add w21, w0, 1 madd w22, w22, w0, w22 sxtw x0, w22 bl calloc mov w2, 1 str w2, [x0] sub x4, x0, #4 add x3, x0, 4 mov x1, 0 cmp w19, 0 ble .L32 .L31: ldrb w2, [x20, x1] cmp w2, 42 bne .L34 ldr w2, [x4, x1, lsl 2] str w2, [x3, x1, lsl 2] .L34: add x1, x1, 1 cmp w19, w1 bgt .L31 cmp w23, 0 ble .L32 sub w9, w23, #1 add x1, x24, 1 mov x8, x24 add w5, w21, w19 add x9, x9, x1 mvn w11, w19 mov w6, 0 .L33: sub w7, w6, #1 add w6, w6, w21 mov w1, w6 sub x3, x20, #1 b .L40 .L57: sub w2, w1, w6 add w2, w2, w7 add w2, w2, 1 ldr w2, [x0, x2, lsl 2] cbz w2, .L37 ldrb w2, [x8] cmp w4, 46 ccmp w2, w4, 4, ne cset w2, eq .L37: add w4, w1, 1 add w1, w1, 1 add x3, x3, 1 str w2, [x0, x4, lsl 2] cmp w5, w1 beq .L56 .L40: ldrb w4, [x3, 1] cmp w4, 42 bne .L57 sub w2, w1, #1 mov w4, 1 ldr w2, [x0, x2, lsl 2] cbnz w2, .L39 ldr w4, [x0, w1, uxtw 2] cbnz w4, .L43 ldrb w2, [x3] ldrb w10, [x8] cmp w2, 46 ccmp w10, w2, 4, ne bne .L39 add w2, w11, w1 add w2, w2, 1 ldr w2, [x0, x2, lsl 2] cmp w2, 0 cset w4, ne .L39: add w2, w1, 1 add w1, w1, 1 add x3, x3, 1 str w4, [x0, x2, lsl 2] cmp w5, w1 bne .L40 .L56: add x8, x8, 1 add w5, w5, w21 cmp x8, x9 bne .L33 .L32: sub w1, w22, w21 sub w19, w19, #1 add w1, w1, w19 add x1, x0, w1, sxtw 2 ldr w19, [x1, 4] bl free ldp x21, x22, [sp, 32] cmp w19, 0 ldp x23, x24, [sp, 48] cset w0, ne ldp x19, x20, [sp, 16] ldp x29, x30, [sp], 64 ret .L43: mov w4, 1 b .L39
292
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
aarch64
-O0
armv8-a clang 21.1.0
match_recursive: sub sp, sp, #48 stp x29, x30, [sp, #32] add x29, sp, #32 str x0, [sp, #16] str x1, [sp, #8] str x2, [sp] ldr x8, [sp, #8] ldrb w8, [x8] cbnz w8, .LBB0_2 b .LBB0_1 .LBB0_1: ldr x8, [sp, #16] ldrb w8, [x8] subs w8, w8, #0 cset w8, eq and w8, w8, #0x1 sturb w8, [x29, #-1] b .LBB0_20 .LBB0_2: ldr x8, [sp, #8] ldrb w8, [x8, #1] subs w8, w8, #42 b.ne .LBB0_14 b .LBB0_3 .LBB0_3: b .LBB0_4 .LBB0_4: ldr x8, [sp] ldr w8, [x8] cbz w8, .LBB0_13 b .LBB0_5 .LBB0_5: ldr x0, [sp, #16] ldr x8, [sp, #8] add x1, x8, #2 ldr x2, [sp] bl match_recursive tbz w0, #0, .LBB0_7 b .LBB0_6 .LBB0_6: mov w8, #1 and w8, w8, #0x1 and w8, w8, #0x1 sturb w8, [x29, #-1] b .LBB0_20 .LBB0_7: ldr x8, [sp, #8] ldrb w8, [x8] subs w8, w8, #46 b.ne .LBB0_9 b .LBB0_8 .LBB0_8: ldr x8, [sp, #16] ldrb w8, [x8] cbz w8, .LBB0_11 b .LBB0_9 .LBB0_9: ldr x8, [sp, #8] ldrb w8, [x8] subs w8, w8, #46 b.eq .LBB0_12 b .LBB0_10 .LBB0_10: ldr x8, [sp, #16] ldrb w8, [x8] ldr x9, [sp, #8] ldrb w9, [x9] subs w8, w8, w9 b.eq .LBB0_12 b .LBB0_11 .LBB0_11: mov w8, wzr and w8, w8, #0x1 and w8, w8, #0x1 sturb w8, [x29, #-1] b .LBB0_20 .LBB0_12: ldr x8, [sp, #16] add x8, x8, #1 str x8, [sp, #16] b .LBB0_4 .LBB0_13: ldr x9, [sp] mov w8, wzr str wzr, [x9] and w8, w8, #0x1 and w8, w8, #0x1 sturb w8, [x29, #-1] b .LBB0_20 .LBB0_14: ldr x8, [sp, #8] ldrb w8, [x8] subs w8, w8, #46 b.ne .LBB0_16 b .LBB0_15 .LBB0_15: ldr x8, [sp, #16] ldrb w8, [x8] cbz w8, .LBB0_18 b .LBB0_16 .LBB0_16: ldr x8, [sp, #8] ldrb w8, [x8] subs w8, w8, #46 b.eq .LBB0_19 b .LBB0_17 .LBB0_17: ldr x8, [sp, #16] ldrb w8, [x8] ldr x9, [sp, #8] ldrb w9, [x9] subs w8, w8, w9 b.eq .LBB0_19 b .LBB0_18 .LBB0_18: mov w8, wzr and w8, w8, #0x1 and w8, w8, #0x1 sturb w8, [x29, #-1] b .LBB0_20 .LBB0_19: ldr x8, [sp, #16] add x0, x8, #1 ldr x8, [sp, #8] add x1, x8, #1 ldr x2, [sp] bl match_recursive and w8, w0, #0x1 and w8, w8, #0x1 sturb w8, [x29, #-1] b .LBB0_20 .LBB0_20: ldurb w8, [x29, #-1] and w0, w8, #0x1 ldp x29, x30, [sp, #32] add sp, sp, #48 ret isMatch: sub sp, sp, #80 stp x29, x30, [sp, #64] add x29, sp, #64 stur x0, [x29, #-8] stur x1, [x29, #-16] ldur x0, [x29, #-8] bl strlen mov w8, w0 str w8, [sp, #28] ldur x0, [x29, #-16] bl strlen mov w8, w0 str w8, [sp, #24] ldr w8, [sp, #28] mov w9, #1 str w9, [sp, #20] add w8, w8, #1 ldr w9, [sp, #24] add w9, w9, #1 mul w9, w8, w9 mov w8, w9 sxtw x0, w8 mov x1, #4 bl calloc ldr w8, [sp, #20] stur x0, [x29, #-24] ldur x9, [x29, #-24] str w8, [x9] str wzr, [sp, #32] b .LBB1_1 .LBB1_1: ldr w8, [sp, #32] ldr w9, [sp, #24] subs w8, w8, w9 b.ge .LBB1_6 b .LBB1_2 .LBB1_2: ldur x8, [x29, #-16] ldrsw x9, [sp, #32] ldrb w8, [x8, x9] subs w8, w8, #42 b.ne .LBB1_4 b .LBB1_3 .LBB1_3: ldur x8, [x29, #-24] ldr w9, [sp, #24] add w9, w9, #1 mov w10, wzr mul w9, w10, w9 ldr w11, [sp, #32] subs w11, w11, #2 add w9, w9, w11 add w9, w9, #1 ldr w8, [x8, w9, sxtw #2] ldur x9, [x29, #-24] ldr w11, [sp, #24] add w11, w11, #1 mul w10, w10, w11 ldr w11, [sp, #32] add w10, w10, w11 add w10, w10, #1 str w8, [x9, w10, sxtw #2] b .LBB1_4 .LBB1_4: b .LBB1_5 .LBB1_5: ldr w8, [sp, #32] add w8, w8, #1 str w8, [sp, #32] b .LBB1_1 .LBB1_6: stur wzr, [x29, #-28] b .LBB1_7 .LBB1_7: ldur w8, [x29, #-28] ldr w9, [sp, #28] subs w8, w8, w9 b.ge .LBB1_27 b .LBB1_8 .LBB1_8: str wzr, [sp, #32] b .LBB1_9 .LBB1_9: ldr w8, [sp, #32] ldr w9, [sp, #24] subs w8, w8, w9 b.ge .LBB1_25 b .LBB1_10 .LBB1_10: ldur x8, [x29, #-16] ldrsw x9, [sp, #32] ldrb w8, [x8, x9] subs w8, w8, #42 b.eq .LBB1_16 b .LBB1_11 .LBB1_11: ldur x8, [x29, #-24] ldur w9, [x29, #-28] subs w9, w9, #1 add w9, w9, #1 ldr w10, [sp, #24] add w10, w10, #1 mul w9, w9, w10 ldr w10, [sp, #32] subs w10, w10, #1 add w9, w9, w10 add w9, w9, #1 ldr w8, [x8, w9, sxtw #2] mov w9, #0 str w9, [sp, #16] cbz w8, .LBB1_15 b .LBB1_12 .LBB1_12: ldur x8, [x29, #-8] ldursw x9, [x29, #-28] ldrb w9, [x8, x9] ldur x8, [x29, #-16] ldrsw x10, [sp, #32] ldrb w10, [x8, x10] mov w8, #1 subs w9, w9, w10 str w8, [sp, #12] b.eq .LBB1_14 b .LBB1_13 .LBB1_13: ldur x8, [x29, #-16] ldrsw x9, [sp, #32] ldrb w8, [x8, x9] subs w8, w8, #46 cset w8, eq str w8, [sp, #12] b .LBB1_14 .LBB1_14: ldr w8, [sp, #12] str w8, [sp, #16] b .LBB1_15 .LBB1_15: ldr w8, [sp, #16] and w8, w8, #0x1 ldur x9, [x29, #-24] ldur w10, [x29, #-28] add w10, w10, #1 ldr w11, [sp, #24] add w11, w11, #1 mul w10, w10, w11 ldr w11, [sp, #32] add w10, w10, w11 add w10, w10, #1 str w8, [x9, w10, sxtw #2] b .LBB1_23 .LBB1_16: ldur x8, [x29, #-24] ldur w9, [x29, #-28] add w9, w9, #1 ldr w10, [sp, #24] add w10, w10, #1 mul w9, w9, w10 ldr w10, [sp, #32] subs w10, w10, #2 add w9, w9, w10 add w9, w9, #1 ldr w8, [x8, w9, sxtw #2] mov w9, #1 str w9, [sp, #8] cbnz w8, .LBB1_22 b .LBB1_17 .LBB1_17: ldur x8, [x29, #-24] ldur w9, [x29, #-28] add w9, w9, #1 ldr w10, [sp, #24] add w10, w10, #1 mul w9, w9, w10 ldr w10, [sp, #32] subs w10, w10, #1 add w9, w9, w10 add w9, w9, #1 ldr w8, [x8, w9, sxtw #2] mov w9, #1 str w9, [sp, #8] cbnz w8, .LBB1_22 b .LBB1_18 .LBB1_18: ldur x8, [x29, #-8] ldursw x9, [x29, #-28] ldrb w8, [x8, x9] ldur x9, [x29, #-16] ldr w10, [sp, #32] subs w10, w10, #1 ldrb w9, [x9, w10, sxtw] subs w8, w8, w9 b.eq .LBB1_20 b .LBB1_19 .LBB1_19: ldur x8, [x29, #-16] ldr w9, [sp, #32] subs w9, w9, #1 ldrb w9, [x8, w9, sxtw] mov w8, #0 subs w9, w9, #46 str w8, [sp, #4] b.ne .LBB1_21 b .LBB1_20 .LBB1_20: ldur x8, [x29, #-24] ldur w9, [x29, #-28] subs w9, w9, #1 add w9, w9, #1 ldr w10, [sp, #24] add w10, w10, #1 mul w9, w9, w10 ldr w10, [sp, #32] add w9, w9, w10 add w9, w9, #1 ldr w8, [x8, w9, sxtw #2] subs w8, w8, #0 cset w8, ne str w8, [sp, #4] b .LBB1_21 .LBB1_21: ldr w8, [sp, #4] str w8, [sp, #8] b .LBB1_22 .LBB1_22: ldr w8, [sp, #8] and w8, w8, #0x1 ldur x9, [x29, #-24] ldur w10, [x29, #-28] add w10, w10, #1 ldr w11, [sp, #24] add w11, w11, #1 mul w10, w10, w11 ldr w11, [sp, #32] add w10, w10, w11 add w10, w10, #1 str w8, [x9, w10, sxtw #2] b .LBB1_23 .LBB1_23: b .LBB1_24 .LBB1_24: ldr w8, [sp, #32] add w8, w8, #1 str w8, [sp, #32] b .LBB1_9 .LBB1_25: b .LBB1_26 .LBB1_26: ldur w8, [x29, #-28] add w8, w8, #1 stur w8, [x29, #-28] b .LBB1_7 .LBB1_27: ldur x8, [x29, #-24] ldr w9, [sp, #28] subs w9, w9, #1 add w9, w9, #1 ldr w10, [sp, #24] add w10, w10, #1 mul w9, w9, w10 ldr w10, [sp, #24] subs w10, w10, #1 add w9, w9, w10 add w9, w9, #1 ldr w8, [x8, w9, sxtw #2] stur w8, [x29, #-28] ldur x0, [x29, #-24] bl free ldur w8, [x29, #-28] subs w8, w8, #0 cset w0, ne ldp x29, x30, [sp, #64] add sp, sp, #80 ret
293
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
aarch64
-O1
armv8-a clang 21.1.0
match_recursive: stp x29, x30, [sp, #-48]! str x21, [sp, #16] stp x20, x19, [sp, #32] mov x29, sp mov x19, x2 mov x20, x0 add x21, x1, #2 b .LBB0_3 .LBB0_1: cbz w9, .LBB0_7 .LBB0_2: add x20, x20, #1 add x21, x21, #1 .LBB0_3: ldurb w8, [x21, #-2] cbz w8, .LBB0_8 ldurb w9, [x21, #-1] cmp w9, #42 b.eq .LBB0_9 ldrb w9, [x20] cmp w8, #46 b.eq .LBB0_1 cmp w9, w8 b.eq .LBB0_2 .LBB0_7: mov w0, wzr b .LBB0_11 .LBB0_8: ldrb w8, [x20] cmp w8, #0 cset w0, eq b .LBB0_11 .LBB0_9: ldr w8, [x19] cbnz w8, .LBB0_14 .LBB0_10: mov w0, wzr str wzr, [x19] .LBB0_11: and w0, w0, #0x1 ldp x20, x19, [sp, #32] ldr x21, [sp, #16] ldp x29, x30, [sp], #48 ret .LBB0_12: cmp w8, w9 b.ne .LBB0_11 .LBB0_13: ldr w8, [x19] add x20, x20, #1 cbz w8, .LBB0_10 .LBB0_14: mov x0, x20 mov x1, x21 mov x2, x19 bl match_recursive tbnz w0, #0, .LBB0_11 ldurb w9, [x21, #-2] ldrb w8, [x20] cmp w9, #46 b.ne .LBB0_12 cbnz w8, .LBB0_13 b .LBB0_11 isMatch: stp x29, x30, [sp, #-64]! stp x24, x23, [sp, #16] stp x22, x21, [sp, #32] stp x20, x19, [sp, #48] mov x29, sp mov x19, x1 mov x20, x0 bl strlen mov x21, x0 mov x0, x19 bl strlen mov x22, x0 mov w1, #4 add w23, w22, #1 madd w8, w23, w21, w23 sxtw x0, w8 bl calloc mov w8, #1 cmp w22, #1 str w8, [x0] b.lt .LBB1_5 and x8, x22, #0x7fffffff add x9, x0, #4 mov x10, x19 b .LBB1_3 .LBB1_2: subs x8, x8, #1 add x9, x9, #4 b.eq .LBB1_5 .LBB1_3: ldrb w11, [x10], #1 cmp w11, #42 b.ne .LBB1_2 ldur w11, [x9, #-8] str w11, [x9] b .LBB1_2 .LBB1_5: cmp w21, #1 b.lt .LBB1_21 mov w13, w22 mov x16, #-4294967296 mov x14, #4294967296 sbfiz x17, x13, #32, #32 sbfiz x15, x13, #2, #32 add x10, x0, #4 sxtw x11, w23 mov x8, xzr mov x9, xzr add x16, x17, x16 add x17, x17, x14 and x12, x21, #0x7fffffff and x13, x22, #0x7fffffff add x15, x15, #4 mov w18, #46 mov x1, x17 mov x2, x10 b .LBB1_8 .LBB1_7: add x9, x9, #1 add x2, x2, x15 add x16, x16, x17 cmp x9, x12 add x8, x8, x11 add x1, x1, x17 b.eq .LBB1_21 .LBB1_8: cmp w22, #1 b.lt .LBB1_7 add x4, x9, #1 mov x3, xzr mov x5, x1 mul x4, x4, x11 mov x6, x16 b .LBB1_13 .LBB1_10: asr x7, x6, #30 ldr w7, [x10, x7] cbz w7, .LBB1_16 .LBB1_11: mov w7, #1 .LBB1_12: add x24, x10, x4, lsl #2 add x6, x6, x14 add x5, x5, x14 str w7, [x24, x3, lsl #2] add x3, x3, #1 cmp x13, x3 b.eq .LBB1_7 .LBB1_13: ldrb w7, [x19, x3] cmp w7, #42 b.eq .LBB1_10 add w24, w8, w3 ldr w24, [x0, w24, sxtw #2] cbz w24, .LBB1_19 ldrb w24, [x20, x9] cmp w24, w7 ccmp w7, w18, #4, ne cset w7, eq b .LBB1_12 .LBB1_16: asr x7, x5, #30 ldr w7, [x0, x7] cbnz w7, .LBB1_11 add x7, x19, x3 ldrb w24, [x20, x9] ldurb w7, [x7, #-1] cmp w24, w7 b.eq .LBB1_20 cmp w7, #46 b.eq .LBB1_20 .LBB1_19: mov w7, wzr b .LBB1_12 .LBB1_20: ldr w7, [x2, x3, lsl #2] cmp w7, #0 cset w7, ne b .LBB1_12 .LBB1_21: madd w8, w23, w21, w22 ldr w19, [x0, w8, sxtw #2] bl free cmp w19, #0 cset w0, ne ldp x20, x19, [sp, #48] ldp x22, x21, [sp, #32] ldp x24, x23, [sp, #16] ldp x29, x30, [sp], #64 ret
294
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
aarch64
-O2
armv8-a clang 21.1.0
match_recursive: stp x29, x30, [sp, #-48]! str x21, [sp, #16] stp x20, x19, [sp, #32] mov x29, sp ldrb w8, [x1] mov x20, x0 cbz w8, .LBB0_8 mov x19, x2 add x21, x1, #2 b .LBB0_4 .LBB0_2: cmp w10, w8 b.ne .LBB0_7 .LBB0_3: add x20, x20, #1 add x21, x21, #1 mov w8, w9 cbz w9, .LBB0_8 .LBB0_4: ldurb w9, [x21, #-1] cmp w9, #42 b.eq .LBB0_10 ldrb w10, [x20] cmp w8, #46 b.ne .LBB0_2 cbnz w10, .LBB0_3 .LBB0_7: mov w0, wzr b .LBB0_9 .LBB0_8: ldrb w8, [x20] cmp w8, #0 cset w0, eq .LBB0_9: and w0, w0, #0x1 ldp x20, x19, [sp, #32] ldr x21, [sp, #16] ldp x29, x30, [sp], #48 ret .LBB0_10: ldr w8, [x19] cbnz w8, .LBB0_14 .LBB0_11: mov w0, wzr str wzr, [x19] b .LBB0_9 .LBB0_12: cmp w8, w9 b.ne .LBB0_9 .LBB0_13: ldr w8, [x19] add x20, x20, #1 cbz w8, .LBB0_11 .LBB0_14: mov x0, x20 mov x1, x21 mov x2, x19 bl match_recursive tbnz w0, #0, .LBB0_9 ldurb w9, [x21, #-2] ldrb w8, [x20] cmp w9, #46 b.ne .LBB0_12 cbnz w8, .LBB0_13 b .LBB0_9 isMatch: stp x29, x30, [sp, #-80]! str x25, [sp, #16] stp x24, x23, [sp, #32] stp x22, x21, [sp, #48] stp x20, x19, [sp, #64] mov x29, sp mov x19, x1 mov x20, x0 bl strlen mov x21, x0 mov x0, x19 bl strlen mov x22, x0 mov w1, #4 add w23, w22, #1 madd w8, w23, w21, w23 sxtw x0, w8 bl calloc mov w8, #1 cmp w22, #1 str w8, [x0] b.lt .LBB1_5 and x8, x22, #0x7fffffff add x9, x0, #4 mov x10, x19 b .LBB1_3 .LBB1_2: subs x8, x8, #1 add x9, x9, #4 b.eq .LBB1_5 .LBB1_3: ldrb w11, [x10], #1 cmp w11, #42 b.ne .LBB1_2 ldur w11, [x9, #-8] str w11, [x9] b .LBB1_2 .LBB1_5: cmp w21, #1 b.lt .LBB1_22 mov w12, w22 mov x14, #-4294967296 mov x8, xzr sbfiz x13, x12, #2, #32 sbfiz x15, x12, #32, #32 mov x12, #4294967296 mov x17, xzr add x9, x0, #4 and x10, x21, #0x7fffffff add x16, x13, x0 add x14, x15, x14 add x15, x15, x12 and x11, x22, #0x7fffffff add x13, x13, #4 add x16, x16, #8 mov w18, #46 mov x1, x15 mov x2, x0 b .LBB1_9 .LBB1_7: add x3, x17, #1 .LBB1_8: add x16, x16, x13 add x2, x2, x13 add x14, x14, x15 cmp x3, x10 add x1, x1, x15 add x8, x8, x15 mov x17, x3 b.eq .LBB1_22 .LBB1_9: cmp w22, #0 b.le .LBB1_7 mov x4, xzr add x3, x17, #1 mov x5, x8 mov x6, x1 mov x7, x14 b .LBB1_14 .LBB1_11: asr x24, x7, #30 ldr w24, [x9, x24] cbz w24, .LBB1_17 .LBB1_12: mov w24, #1 .LBB1_13: str w24, [x16, x4, lsl #2] add x4, x4, #1 add x7, x7, x12 cmp x11, x4 add x6, x6, x12 add x5, x5, x12 b.eq .LBB1_8 .LBB1_14: ldrb w24, [x19, x4] cmp w24, #42 b.eq .LBB1_11 asr x25, x5, #30 ldr w25, [x0, x25] cbz w25, .LBB1_20 ldrb w25, [x20, x17] cmp w25, w24 ccmp w24, w18, #4, ne cset w24, eq b .LBB1_13 .LBB1_17: asr x24, x6, #30 ldr w24, [x0, x24] cbnz w24, .LBB1_12 add x24, x19, x4 ldrb w25, [x20, x17] ldurb w24, [x24, #-1] cmp w25, w24 b.eq .LBB1_21 cmp w24, #46 b.eq .LBB1_21 .LBB1_20: mov w24, wzr b .LBB1_13 .LBB1_21: add x24, x2, x4, lsl #2 ldr w24, [x24, #4] cmp w24, #0 cset w24, ne b .LBB1_13 .LBB1_22: madd w8, w23, w21, w22 ldr w19, [x0, w8, sxtw #2] bl free cmp w19, #0 cset w0, ne ldp x20, x19, [sp, #64] ldr x25, [sp, #16] ldp x22, x21, [sp, #48] ldp x24, x23, [sp, #32] ldp x29, x30, [sp], #80 ret
295
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
aarch64
-O3
armv8-a clang 21.1.0
match_recursive: stp x29, x30, [sp, #-48]! str x21, [sp, #16] stp x20, x19, [sp, #32] mov x29, sp ldrb w8, [x1] mov x20, x0 cbz w8, .LBB0_9 mov x19, x2 add x21, x1, #2 b .LBB0_4 .LBB0_2: cmp w10, w8 b.ne .LBB0_17 .LBB0_3: add x20, x20, #1 add x21, x21, #1 mov w8, w9 cbz w9, .LBB0_9 .LBB0_4: ldurb w9, [x21, #-1] cmp w9, #42 b.eq .LBB0_10 ldrb w10, [x20] cmp w8, #46 b.ne .LBB0_2 cbnz w10, .LBB0_3 mov w0, wzr .LBB0_8: and w0, w0, #0x1 ldp x20, x19, [sp, #32] ldr x21, [sp, #16] ldp x29, x30, [sp], #48 ret .LBB0_9: ldrb w8, [x20] cmp w8, #0 cset w0, eq and w0, w0, #0x1 ldp x20, x19, [sp, #32] ldr x21, [sp, #16] ldp x29, x30, [sp], #48 ret .LBB0_10: ldr w8, [x19] cbnz w8, .LBB0_14 .LBB0_11: str wzr, [x19] and w0, wzr, #0x1 ldp x20, x19, [sp, #32] ldr x21, [sp, #16] ldp x29, x30, [sp], #48 ret .LBB0_12: cmp w8, w9 b.ne .LBB0_8 .LBB0_13: ldr w8, [x19] add x20, x20, #1 cbz w8, .LBB0_11 .LBB0_14: mov x0, x20 mov x1, x21 mov x2, x19 bl match_recursive tbnz w0, #0, .LBB0_8 ldurb w9, [x21, #-2] ldrb w8, [x20] cmp w9, #46 b.ne .LBB0_12 cbnz w8, .LBB0_13 b .LBB0_8 .LBB0_17: and w0, wzr, #0x1 ldp x20, x19, [sp, #32] ldr x21, [sp, #16] ldp x29, x30, [sp], #48 ret isMatch: stp x29, x30, [sp, #-80]! str x25, [sp, #16] stp x24, x23, [sp, #32] stp x22, x21, [sp, #48] stp x20, x19, [sp, #64] mov x29, sp mov x19, x1 mov x20, x0 bl strlen mov x21, x0 mov x0, x19 bl strlen mov x22, x0 mov w1, #4 add w23, w22, #1 madd w8, w23, w21, w23 sxtw x0, w8 bl calloc mov w8, #1 cmp w22, #1 str w8, [x0] b.lt .LBB1_20 and x8, x22, #0x7fffffff add x9, x0, #4 mov x11, x19 mov x10, x8 b .LBB1_3 .LBB1_2: subs x10, x10, #1 add x9, x9, #4 b.eq .LBB1_5 .LBB1_3: ldrb w12, [x11], #1 cmp w12, #42 b.ne .LBB1_2 ldur w12, [x9, #-8] str w12, [x9] b .LBB1_2 .LBB1_5: cmp w21, #1 b.lt .LBB1_20 ubfiz x10, x23, #2, #32 lsl x11, x23, #32 add x12, x0, #4 mov x15, #-8589934592 mov x9, xzr mov x18, xzr and x13, x21, #0x7fffffff add x14, x12, x10 add x15, x11, x15 mov x16, #4294967296 mov w17, #46 mov x1, x11 mov x2, x12 b .LBB1_8 .LBB1_7: add x14, x14, x10 add x2, x2, x10 add x15, x15, x11 cmp x3, x13 add x1, x1, x11 add x9, x9, x11 mov x18, x3 b.eq .LBB1_20 .LBB1_8: mov x4, xzr add x3, x18, #1 mov x5, x9 mov x6, x1 mov x7, x15 b .LBB1_12 .LBB1_9: asr x24, x7, #30 ldr w24, [x12, x24] cbz w24, .LBB1_15 .LBB1_10: mov w24, #1 .LBB1_11: str w24, [x14, x4, lsl #2] add x4, x4, #1 add x7, x7, x16 cmp x8, x4 add x6, x6, x16 add x5, x5, x16 b.eq .LBB1_7 .LBB1_12: ldrb w24, [x19, x4] cmp w24, #42 b.eq .LBB1_9 asr x25, x5, #30 ldr w25, [x0, x25] cbz w25, .LBB1_18 ldrb w25, [x20, x18] cmp w25, w24 ccmp w24, w17, #4, ne cset w24, eq b .LBB1_11 .LBB1_15: asr x24, x6, #30 ldr w24, [x0, x24] cbnz w24, .LBB1_10 add x24, x19, x4 ldrb w25, [x20, x18] ldurb w24, [x24, #-1] cmp w25, w24 b.eq .LBB1_19 cmp w24, #46 b.eq .LBB1_19 .LBB1_18: mov w24, wzr b .LBB1_11 .LBB1_19: ldr w24, [x2, x4, lsl #2] cmp w24, #0 cset w24, ne b .LBB1_11 .LBB1_20: madd w8, w23, w21, w22 ldr w19, [x0, w8, sxtw #2] bl free cmp w19, #0 cset w0, ne ldp x20, x19, [sp, #64] ldr x25, [sp, #16] ldp x22, x21, [sp, #48] ldp x24, x23, [sp, #32] ldp x29, x30, [sp], #80 ret
296
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
mips64
-O0
mips64 clang 21.1.0
match_recursive: .Lfunc_begin0 = .Ltmp0 daddiu $sp, $sp, -64 sd $ra, 56($sp) sd $fp, 48($sp) sd $gp, 40($sp) move $fp, $sp lui $1, %hi(%neg(%gp_rel(match_recursive))) daddu $1, $1, $25 daddiu $1, $1, %lo(%neg(%gp_rel(match_recursive))) sd $1, 0($fp) sd $4, 24($fp) sd $5, 16($fp) sd $6, 8($fp) ld $1, 16($fp) lb $1, 0($1) bnez $1, .LBB0_3 nop b .LBB0_2 nop .LBB0_2: ld $1, 24($fp) lb $1, 0($1) sltiu $1, $1, 1 sb $1, 39($fp) b .LBB0_32 nop .LBB0_3: ld $1, 16($fp) lb $1, 1($1) addiu $2, $zero, 42 bne $1, $2, .LBB0_22 nop b .LBB0_5 nop .LBB0_5: b .LBB0_6 nop .LBB0_6: ld $1, 8($fp) lw $1, 0($1) beqz $1, .LBB0_21 nop b .LBB0_8 nop .LBB0_8: ld $gp, 0($fp) ld $4, 24($fp) ld $1, 16($fp) daddiu $5, $1, 2 ld $6, 8($fp) ld $25, %call16(match_recursive)($gp) jalr $25 nop beqz $2, .LBB0_11 nop b .LBB0_10 nop .LBB0_10: addiu $1, $zero, 1 sb $1, 39($fp) b .LBB0_32 nop .LBB0_11: ld $1, 16($fp) lb $1, 0($1) addiu $2, $zero, 46 bne $1, $2, .LBB0_15 nop b .LBB0_13 nop .LBB0_13: ld $1, 24($fp) lb $1, 0($1) beqz $1, .LBB0_19 nop b .LBB0_15 nop .LBB0_15: ld $1, 16($fp) lb $1, 0($1) addiu $2, $zero, 46 beq $1, $2, .LBB0_20 nop b .LBB0_17 nop .LBB0_17: ld $1, 24($fp) lb $1, 0($1) ld $2, 16($fp) lb $2, 0($2) beq $1, $2, .LBB0_20 nop b .LBB0_19 nop .LBB0_19: addiu $1, $zero, 0 sb $zero, 39($fp) b .LBB0_32 nop .LBB0_20: ld $1, 24($fp) daddiu $1, $1, 1 sd $1, 24($fp) b .LBB0_6 nop .LBB0_21: ld $1, 8($fp) sw $zero, 0($1) addiu $1, $zero, 0 sb $zero, 39($fp) b .LBB0_32 nop .LBB0_22: ld $1, 16($fp) lb $1, 0($1) addiu $2, $zero, 46 bne $1, $2, .LBB0_26 nop b .LBB0_24 nop .LBB0_24: ld $1, 24($fp) lb $1, 0($1) beqz $1, .LBB0_30 nop b .LBB0_26 nop .LBB0_26: ld $1, 16($fp) lb $1, 0($1) addiu $2, $zero, 46 beq $1, $2, .LBB0_31 nop b .LBB0_28 nop .LBB0_28: ld $1, 24($fp) lb $1, 0($1) ld $2, 16($fp) lb $2, 0($2) beq $1, $2, .LBB0_31 nop b .LBB0_30 nop .LBB0_30: addiu $1, $zero, 0 sb $zero, 39($fp) b .LBB0_32 nop .LBB0_31: ld $gp, 0($fp) ld $1, 24($fp) daddiu $4, $1, 1 ld $1, 16($fp) daddiu $5, $1, 1 ld $6, 8($fp) ld $25, %call16(match_recursive)($gp) jalr $25 nop sb $2, 39($fp) b .LBB0_32 nop .LBB0_32: lbu $2, 39($fp) move $sp, $fp ld $gp, 40($sp) ld $fp, 48($sp) ld $ra, 56($sp) daddiu $sp, $sp, 64 jr $ra nop isMatch: .Lfunc_begin1 = .Ltmp16 daddiu $sp, $sp, -96 sd $ra, 88($sp) sd $fp, 80($sp) sd $gp, 72($sp) move $fp, $sp lui $1, %hi(%neg(%gp_rel(isMatch))) daddu $1, $1, $25 daddiu $gp, $1, %lo(%neg(%gp_rel(isMatch))) sd $gp, 24($fp) sd $4, 64($fp) sd $5, 56($fp) ld $4, 64($fp) ld $25, %call16(strlen)($gp) jalr $25 nop ld $gp, 24($fp) sw $2, 36($fp) ld $4, 56($fp) ld $25, %call16(strlen)($gp) ld $gp, 24($fp) jalr $25 nop sw $2, 32($fp) lw $1, 36($fp) addiu $1, $1, 1 lw $2, 32($fp) addiu $2, $2, 1 mul $1, $1, $2 move $4, $1 ld $25, %call16(calloc)($gp) daddiu $5, $zero, 4 jalr $25 nop sd $2, 48($fp) ld $2, 48($fp) addiu $1, $zero, 1 sw $1, 0($2) sw $zero, 40($fp) b .LBB1_1 nop .LBB1_1: lw $1, 40($fp) lw $2, 32($fp) slt $1, $1, $2 beqz $1, .LBB1_8 nop b .LBB1_3 nop .LBB1_3: ld $1, 56($fp) lw $2, 40($fp) daddu $1, $1, $2 lb $1, 0($1) addiu $2, $zero, 42 bne $1, $2, .LBB1_6 nop b .LBB1_5 nop .LBB1_5: ld $2, 48($fp) lw $3, 40($fp) addiu $4, $3, -2 move $1, $4 dsll $1, $1, 2 daddu $1, $1, $2 lw $1, 4($1) addiu $4, $3, 1 move $3, $4 dsll $3, $3, 2 daddu $2, $2, $3 sw $1, 0($2) b .LBB1_6 nop .LBB1_6: b .LBB1_7 nop .LBB1_7: lw $1, 40($fp) addiu $1, $1, 1 sw $1, 40($fp) b .LBB1_1 nop .LBB1_8: sw $zero, 44($fp) b .LBB1_9 nop .LBB1_9: lw $1, 44($fp) lw $2, 36($fp) slt $1, $1, $2 beqz $1, .LBB1_38 nop b .LBB1_11 nop .LBB1_11: sw $zero, 40($fp) b .LBB1_12 nop .LBB1_12: lw $1, 40($fp) lw $2, 32($fp) slt $1, $1, $2 beqz $1, .LBB1_36 nop b .LBB1_14 nop .LBB1_14: ld $1, 56($fp) lw $2, 40($fp) daddu $1, $1, $2 lb $1, 0($1) addiu $2, $zero, 42 beq $1, $2, .LBB1_23 nop b .LBB1_16 nop .LBB1_16: ld $2, 48($fp) lw $1, 44($fp) lw $3, 32($fp) addiu $3, $3, 1 mul $3, $1, $3 lw $1, 40($fp) addu $1, $1, $3 addiu $3, $1, -1 move $1, $3 dsll $1, $1, 2 daddu $1, $1, $2 lw $1, 4($1) addiu $2, $zero, 0 sw $2, 20($fp) beqz $1, .LBB1_22 nop b .LBB1_18 nop .LBB1_18: ld $1, 64($fp) lw $2, 44($fp) daddu $1, $1, $2 lb $1, 0($1) ld $2, 56($fp) lw $3, 40($fp) daddu $2, $2, $3 lb $2, 0($2) addiu $3, $zero, 1 sw $3, 16($fp) beq $1, $2, .LBB1_21 nop b .LBB1_20 nop .LBB1_20: ld $1, 56($fp) lw $2, 40($fp) daddu $1, $1, $2 lb $1, 0($1) addiu $2, $zero, 46 xor $1, $1, $2 sltiu $1, $1, 1 sw $1, 16($fp) b .LBB1_21 nop .LBB1_21: lw $1, 16($fp) sw $1, 20($fp) b .LBB1_22 nop .LBB1_22: lw $1, 20($fp) andi $1, $1, 1 ld $3, 48($fp) lw $2, 44($fp) addiu $2, $2, 1 lw $4, 32($fp) addiu $4, $4, 1 mul $2, $2, $4 lw $4, 40($fp) addu $4, $2, $4 move $2, $4 dsll $2, $2, 2 daddu $2, $2, $3 sw $1, 4($2) b .LBB1_34 nop .LBB1_23: ld $2, 48($fp) lw $1, 44($fp) addiu $1, $1, 1 lw $3, 32($fp) addiu $3, $3, 1 mul $3, $1, $3 lw $1, 40($fp) addu $1, $1, $3 addiu $3, $1, -2 move $1, $3 dsll $1, $1, 2 daddu $1, $1, $2 lw $1, 4($1) addiu $2, $zero, 1 sw $2, 12($fp) bnez $1, .LBB1_33 nop b .LBB1_25 nop .LBB1_25: ld $2, 48($fp) lw $1, 44($fp) addiu $1, $1, 1 lw $3, 32($fp) addiu $3, $3, 1 mul $3, $1, $3 lw $1, 40($fp) addu $1, $1, $3 addiu $3, $1, -1 move $1, $3 dsll $1, $1, 2 daddu $1, $1, $2 lw $1, 4($1) addiu $2, $zero, 1 sw $2, 12($fp) bnez $1, .LBB1_33 nop b .LBB1_27 nop .LBB1_27: ld $1, 64($fp) lw $2, 44($fp) daddu $1, $1, $2 lb $1, 0($1) ld $2, 56($fp) lw $3, 40($fp) addiu $4, $3, -1 move $3, $4 daddu $2, $2, $3 lb $2, 0($2) beq $1, $2, .LBB1_31 nop b .LBB1_29 nop .LBB1_29: ld $1, 56($fp) lw $2, 40($fp) addiu $3, $2, -1 move $2, $3 daddu $1, $1, $2 lb $1, 0($1) addiu $3, $zero, 0 addiu $2, $zero, 46 sw $3, 8($fp) bne $1, $2, .LBB1_32 nop b .LBB1_31 nop .LBB1_31: ld $2, 48($fp) lw $1, 44($fp) lw $3, 32($fp) addiu $3, $3, 1 mul $1, $1, $3 lw $3, 40($fp) addu $3, $1, $3 move $1, $3 dsll $1, $1, 2 daddu $1, $1, $2 lw $1, 4($1) sltu $1, $zero, $1 sw $1, 8($fp) b .LBB1_32 nop .LBB1_32: lw $1, 8($fp) sw $1, 12($fp) b .LBB1_33 nop .LBB1_33: lw $1, 12($fp) andi $1, $1, 1 ld $3, 48($fp) lw $2, 44($fp) addiu $2, $2, 1 lw $4, 32($fp) addiu $4, $4, 1 mul $2, $2, $4 lw $4, 40($fp) addu $4, $2, $4 move $2, $4 dsll $2, $2, 2 daddu $2, $2, $3 sw $1, 4($2) b .LBB1_34 nop .LBB1_34: b .LBB1_35 nop .LBB1_35: lw $1, 40($fp) addiu $1, $1, 1 sw $1, 40($fp) b .LBB1_12 nop .LBB1_36: b .LBB1_37 nop .LBB1_37: lw $1, 44($fp) addiu $1, $1, 1 sw $1, 44($fp) b .LBB1_9 nop .LBB1_38: ld $gp, 24($fp) ld $2, 48($fp) lw $3, 36($fp) lw $1, 32($fp) addiu $4, $1, 1 mul $3, $3, $4 addu $1, $1, $3 addiu $3, $1, -1 move $1, $3 dsll $1, $1, 2 daddu $1, $1, $2 lw $1, 4($1) sw $1, 44($fp) ld $4, 48($fp) ld $25, %call16(free)($gp) jalr $25 nop lw $1, 44($fp) sltu $2, $zero, $1 move $sp, $fp ld $gp, 72($sp) ld $fp, 80($sp) ld $ra, 88($sp) daddiu $sp, $sp, 96 jr $ra nop
297
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
mips64
-O1
mips64 clang 21.1.0
match_recursive: .Lfunc_begin0 = .Ltmp0 daddiu $sp, $sp, -64 sd $ra, 56($sp) sd $fp, 48($sp) sd $gp, 40($sp) sd $19, 32($sp) sd $18, 24($sp) sd $17, 16($sp) sd $16, 8($sp) move $fp, $sp lui $1, %hi(%neg(%gp_rel(match_recursive))) daddu $1, $1, $25 daddiu $gp, $1, %lo(%neg(%gp_rel(match_recursive))) move $16, $6 move $17, $4 daddiu $18, $5, 2 addiu $2, $zero, 42 b .LBB0_3 addiu $3, $zero, 46 .LBB0_1: bne $5, $4, .LBB0_18 nop .LBB0_2: daddiu $18, $18, 1 daddiu $17, $17, 1 .LBB0_3: lbu $4, -2($18) beqz $4, .LBB0_8 nop lbu $1, -1($18) beq $1, $2, .LBB0_9 nop lbu $1, 0($17) bne $4, $3, .LBB0_1 andi $5, $1, 255 bnez $5, .LBB0_2 nop b .LBB0_19 addiu $2, $zero, 0 .LBB0_8: lbu $1, 0($17) b .LBB0_19 sltiu $2, $1, 1 .LBB0_9: lw $1, 0($16) beqz $1, .LBB0_17 nop b .LBB0_13 addiu $19, $zero, 46 .LBB0_11: bne $3, $4, .LBB0_19 nop .LBB0_12: lw $1, 0($16) beqz $1, .LBB0_17 daddiu $17, $17, 1 .LBB0_13: ld $25, %call16(match_recursive)($gp) move $4, $17 move $5, $18 jalr $25 move $6, $16 bnez $2, .LBB0_19 nop lbu $1, 0($17) lbu $4, -2($18) bne $4, $19, .LBB0_11 andi $3, $1, 255 bnez $3, .LBB0_12 nop b .LBB0_19 nop .LBB0_17: sw $zero, 0($16) b .LBB0_19 addiu $2, $zero, 0 .LBB0_18: addiu $2, $zero, 0 .LBB0_19: move $sp, $fp ld $16, 8($sp) ld $17, 16($sp) ld $18, 24($sp) ld $19, 32($sp) ld $gp, 40($sp) ld $fp, 48($sp) ld $ra, 56($sp) jr $ra daddiu $sp, $sp, 64 isMatch: .Lfunc_begin1 = .Ltmp26 daddiu $sp, $sp, -128 sd $ra, 120($sp) sd $fp, 112($sp) sd $gp, 104($sp) sd $23, 96($sp) sd $22, 88($sp) sd $21, 80($sp) sd $20, 72($sp) sd $19, 64($sp) sd $18, 56($sp) sd $17, 48($sp) sd $16, 40($sp) move $fp, $sp lui $1, %hi(%neg(%gp_rel(isMatch))) daddu $1, $1, $25 daddiu $gp, $1, %lo(%neg(%gp_rel(isMatch))) move $16, $5 ld $25, %call16(strlen)($gp) jalr $25 sd $4, 24($fp) move $18, $2 sll $17, $18, 0 ld $25, %call16(strlen)($gp) sd $16, 32($fp) jalr $25 move $4, $16 move $19, $2 addiu $1, $17, 1 sll $21, $19, 0 addiu $22, $21, 1 mul $4, $22, $1 ld $25, %call16(calloc)($gp) daddiu $5, $zero, 4 jalr $25 sd $gp, 16($fp) move $4, $2 addiu $1, $zero, 1 sw $1, 0($2) blez $21, .LBB1_5 dext $2, $19, 0, 31 ld $7, 32($fp) daddu $3, $7, $2 daddiu $5, $4, 4 b .LBB1_3 addiu $6, $zero, 42 .LBB1_2: daddiu $7, $7, 1 beq $7, $3, .LBB1_5 daddiu $5, $5, 4 .LBB1_3: lbu $1, 0($7) bne $1, $6, .LBB1_2 nop lw $1, -8($5) b .LBB1_2 sw $1, 0($5) .LBB1_5: blez $17, .LBB1_20 sw $17, 12($fp) daddiu $17, $4, 4 lui $1, 65535 ori $1, $1, 65535 daddiu $6, $zero, 1 ld $3, 32($fp) daddu $3, $3, $2 dext $5, $18, 0, 31 dsll $1, $1, 32 dsll $6, $6, 32 sll $2, $21, 0 dsll $8, $2, 32 daddu $7, $8, $6 sll $gp, $22, 0 dsll $2, $2, 2 daddiu $9, $zero, 0 addiu $10, $zero, 0 daddu $20, $8, $1 daddiu $12, $2, 4 addiu $13, $zero, 42 addiu $14, $zero, 46 move $15, $7 b .LBB1_8 daddiu $24, $zero, 0 .LBB1_7: daddiu $24, $24, 1 daddu $15, $15, $7 addu $10, $10, $22 daddu $20, $20, $7 beq $24, $5, .LBB1_20 daddu $9, $9, $12 .LBB1_8: blez $21, .LBB1_7 nop daddiu $1, $24, 1 dmult $1, $gp mflo $25 ld $1, 24($fp) daddu $18, $1, $24 move $19, $15 ld $23, 32($fp) move $ra, $10 move $11, $20 b .LBB1_13 move $2, $17 .LBB1_10: sll $1, $ra, 0 dsll $1, $1, 2 daddu $1, $4, $1 lw $1, 0($1) beqz $1, .LBB1_19 nop andi $1, $16, 255 xor $8, $1, $14 sltiu $8, $8, 1 lbu $16, 0($18) xor $1, $16, $1 sltiu $1, $1, 1 or $16, $1, $8 .LBB1_12: dsll $1, $25, 2 daddu $1, $2, $1 sw $16, 0($1) daddu $19, $19, $6 daddu $11, $11, $6 daddiu $23, $23, 1 addiu $ra, $ra, 1 beq $23, $3, .LBB1_7 daddiu $2, $2, 4 .LBB1_13: lbu $16, 0($23) bne $16, $13, .LBB1_10 nop dsra $1, $11, 30 daddu $1, $17, $1 lw $1, 0($1) bnez $1, .LBB1_12 addiu $16, $zero, 1 dsra $1, $19, 30 daddu $1, $4, $1 lw $1, 0($1) bnez $1, .LBB1_12 nop lbu $8, -1($23) lbu $1, 0($18) beq $1, $8, .LBB1_18 nop bne $8, $14, .LBB1_12 addiu $16, $zero, 0 .LBB1_18: daddu $1, $2, $9 lw $1, 0($1) b .LBB1_12 sltu $16, $zero, $1 .LBB1_19: b .LBB1_12 addiu $16, $zero, 0 .LBB1_20: lw $1, 12($fp) mul $1, $22, $1 addu $1, $1, $21 dsll $1, $1, 2 daddu $1, $4, $1 lw $16, 0($1) ld $gp, 16($fp) ld $25, %call16(free)($gp) jalr $25 nop sltu $2, $zero, $16 move $sp, $fp ld $16, 40($sp) ld $17, 48($sp) ld $18, 56($sp) ld $19, 64($sp) ld $20, 72($sp) ld $21, 80($sp) ld $22, 88($sp) ld $23, 96($sp) ld $gp, 104($sp) ld $fp, 112($sp) ld $ra, 120($sp) jr $ra daddiu $sp, $sp, 128
298
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
mips64
-O2
mips64 clang 21.1.0
match_recursive: .Lfunc_begin0 = .Ltmp0 daddiu $sp, $sp, -64 sd $ra, 56($sp) sd $fp, 48($sp) sd $gp, 40($sp) sd $19, 32($sp) sd $18, 24($sp) sd $17, 16($sp) sd $16, 8($sp) move $fp, $sp lui $1, %hi(%neg(%gp_rel(match_recursive))) move $16, $4 lbu $4, 0($5) beqz $4, .LBB0_8 daddu $2, $1, $25 move $17, $6 daddiu $gp, $2, %lo(%neg(%gp_rel(match_recursive))) daddiu $18, $5, 2 addiu $2, $zero, 42 b .LBB0_4 addiu $3, $zero, 46 .LBB0_2: bne $4, $6, .LBB0_19 nop .LBB0_3: daddiu $18, $18, 1 daddiu $16, $16, 1 beqz $5, .LBB0_8 move $4, $5 .LBB0_4: lbu $5, -1($18) beq $5, $2, .LBB0_10 nop lbu $1, 0($16) andi $6, $4, 255 bne $6, $3, .LBB0_2 andi $4, $1, 255 bnez $4, .LBB0_3 nop b .LBB0_9 addiu $2, $zero, 0 .LBB0_8: lbu $1, 0($16) sltiu $2, $1, 1 .LBB0_9: move $sp, $fp ld $16, 8($sp) ld $17, 16($sp) ld $18, 24($sp) ld $19, 32($sp) ld $gp, 40($sp) ld $fp, 48($sp) ld $ra, 56($sp) jr $ra daddiu $sp, $sp, 64 .LBB0_10: lw $1, 0($17) beqz $1, .LBB0_18 nop b .LBB0_14 addiu $19, $zero, 46 .LBB0_12: bne $3, $4, .LBB0_9 nop .LBB0_13: lw $1, 0($17) beqz $1, .LBB0_18 daddiu $16, $16, 1 .LBB0_14: ld $25, %call16(match_recursive)($gp) move $4, $16 move $5, $18 jalr $25 move $6, $17 bnez $2, .LBB0_9 nop lbu $1, 0($16) lbu $4, -2($18) bne $4, $19, .LBB0_12 andi $3, $1, 255 bnez $3, .LBB0_13 nop b .LBB0_9 nop .LBB0_18: sw $zero, 0($17) b .LBB0_9 addiu $2, $zero, 0 .LBB0_19: b .LBB0_9 addiu $2, $zero, 0 isMatch: .Lfunc_begin1 = .Ltmp30 daddiu $sp, $sp, -112 sd $ra, 104($sp) sd $fp, 96($sp) sd $gp, 88($sp) sd $23, 80($sp) sd $22, 72($sp) sd $21, 64($sp) sd $20, 56($sp) sd $19, 48($sp) sd $18, 40($sp) sd $17, 32($sp) sd $16, 24($sp) move $fp, $sp lui $1, %hi(%neg(%gp_rel(isMatch))) daddu $1, $1, $25 daddiu $gp, $1, %lo(%neg(%gp_rel(isMatch))) move $16, $5 ld $25, %call16(strlen)($gp) jalr $25 sd $4, 8($fp) move $18, $2 sll $17, $18, 0 ld $25, %call16(strlen)($gp) sd $16, 16($fp) jalr $25 move $4, $16 move $19, $2 addiu $1, $17, 1 sll $21, $19, 0 addiu $2, $21, 1 sw $2, 0($fp) mul $4, $2, $1 ld $25, %call16(calloc)($gp) jalr $25 daddiu $5, $zero, 4 move $4, $2 addiu $1, $zero, 1 sw $1, 0($2) blez $21, .LBB1_5 dext $5, $19, 0, 31 ld $7, 16($fp) daddu $2, $7, $5 daddiu $3, $4, 4 b .LBB1_3 addiu $6, $zero, 42 .LBB1_2: daddiu $7, $7, 1 beq $7, $2, .LBB1_5 daddiu $3, $3, 4 .LBB1_3: lbu $1, 0($7) bne $1, $6, .LBB1_2 nop lw $1, -8($3) b .LBB1_2 sw $1, 0($3) .LBB1_5: blez $17, .LBB1_22 sw $17, 4($fp) daddiu $2, $4, 4 lui $1, 65535 ori $1, $1, 65535 sll $6, $21, 0 dsll $8, $6, 2 daddiu $3, $zero, 1 daddu $9, $8, $4 dsll $1, $1, 32 dsll $3, $3, 32 dsll $7, $6, 32 ld $6, 16($fp) daddu $5, $6, $5 daddu $6, $7, $3 daddu $7, $7, $1 daddiu $20, $9, 8 dext $9, $18, 0, 31 daddiu $10, $8, 4 daddiu $11, $zero, 0 addiu $12, $zero, 42 addiu $13, $zero, 46 move $14, $6 move $22, $2 b .LBB1_9 daddiu $8, $zero, 0 .LBB1_7: move $8, $24 .LBB1_8: daddu $11, $11, $6 daddu $14, $14, $6 daddu $7, $7, $6 daddu $22, $22, $10 beq $8, $9, .LBB1_22 daddu $20, $20, $10 .LBB1_9: blez $21, .LBB1_21 daddiu $24, $8, 1 ld $1, 8($fp) daddu $25, $1, $8 move $18, $11 move $19, $14 ld $23, 16($fp) move $ra, $7 move $15, $22 b .LBB1_14 move $8, $20 .LBB1_11: dsra $1, $18, 30 daddu $1, $4, $1 lw $1, 0($1) beqz $1, .LBB1_20 nop xor $1, $17, $13 sltiu $1, $1, 1 lbu $16, 0($25) xor $16, $16, $17 sltiu $16, $16, 1 or $17, $16, $1 .LBB1_13: sw $17, 0($8) daddu $18, $18, $3 daddu $19, $19, $3 daddu $ra, $ra, $3 daddiu $23, $23, 1 daddiu $15, $15, 4 beq $23, $5, .LBB1_7 daddiu $8, $8, 4 .LBB1_14: lbu $17, 0($23) bne $17, $12, .LBB1_11 nop dsra $1, $ra, 30 daddu $1, $2, $1 lw $1, 0($1) bnez $1, .LBB1_13 addiu $17, $zero, 1 dsra $1, $19, 30 daddu $1, $4, $1 lw $1, 0($1) bnez $1, .LBB1_13 nop lbu $16, -1($23) lbu $1, 0($25) beq $1, $16, .LBB1_19 nop bne $16, $13, .LBB1_13 addiu $17, $zero, 0 .LBB1_19: lw $1, 0($15) b .LBB1_13 sltu $17, $zero, $1 .LBB1_20: b .LBB1_13 addiu $17, $zero, 0 .LBB1_21: b .LBB1_8 move $8, $24 .LBB1_22: lw $1, 4($fp) lw $2, 0($fp) mul $1, $2, $1 addu $1, $1, $21 dsll $1, $1, 2 daddu $1, $4, $1 lw $16, 0($1) ld $25, %call16(free)($gp) jalr $25 nop sltu $2, $zero, $16 move $sp, $fp ld $16, 24($sp) ld $17, 32($sp) ld $18, 40($sp) ld $19, 48($sp) ld $20, 56($sp) ld $21, 64($sp) ld $22, 72($sp) ld $23, 80($sp) ld $gp, 88($sp) ld $fp, 96($sp) ld $ra, 104($sp) jr $ra daddiu $sp, $sp, 112
299
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
mips64
-O3
mips64 clang 21.1.0
match_recursive: .Lfunc_begin0 = .Ltmp0 daddiu $sp, $sp, -64 sd $ra, 56($sp) sd $fp, 48($sp) sd $gp, 40($sp) sd $19, 32($sp) sd $18, 24($sp) sd $17, 16($sp) sd $16, 8($sp) move $fp, $sp lui $1, %hi(%neg(%gp_rel(match_recursive))) move $16, $4 lbu $4, 0($5) beqz $4, .LBB0_8 daddu $2, $1, $25 daddiu $gp, $2, %lo(%neg(%gp_rel(match_recursive))) move $17, $6 daddiu $18, $5, 2 addiu $2, $zero, 42 b .LBB0_4 addiu $3, $zero, 46 .LBB0_2: bne $4, $6, .LBB0_19 nop .LBB0_3: daddiu $18, $18, 1 daddiu $16, $16, 1 beqz $5, .LBB0_8 move $4, $5 .LBB0_4: lbu $5, -1($18) beq $5, $2, .LBB0_10 nop lbu $1, 0($16) andi $6, $4, 255 bne $6, $3, .LBB0_2 andi $4, $1, 255 bnez $4, .LBB0_3 nop b .LBB0_9 addiu $2, $zero, 0 .LBB0_8: lbu $1, 0($16) sltiu $2, $1, 1 .LBB0_9: move $sp, $fp ld $16, 8($sp) ld $17, 16($sp) ld $18, 24($sp) ld $19, 32($sp) ld $gp, 40($sp) ld $fp, 48($sp) ld $ra, 56($sp) jr $ra daddiu $sp, $sp, 64 .LBB0_10: lw $1, 0($17) beqz $1, .LBB0_18 nop b .LBB0_14 addiu $19, $zero, 46 .LBB0_12: bne $3, $4, .LBB0_9 nop .LBB0_13: lw $1, 0($17) beqz $1, .LBB0_18 daddiu $16, $16, 1 .LBB0_14: ld $25, %call16(match_recursive)($gp) move $4, $16 move $5, $18 jalr $25 move $6, $17 bnez $2, .LBB0_9 nop lbu $1, 0($16) lbu $4, -2($18) bne $4, $19, .LBB0_12 andi $3, $1, 255 bnez $3, .LBB0_13 nop b .LBB0_9 nop .LBB0_18: sw $zero, 0($17) b .LBB0_9 addiu $2, $zero, 0 .LBB0_19: b .LBB0_9 addiu $2, $zero, 0 isMatch: .Lfunc_begin1 = .Ltmp28 daddiu $sp, $sp, -112 sd $ra, 104($sp) sd $fp, 96($sp) sd $gp, 88($sp) sd $23, 80($sp) sd $22, 72($sp) sd $21, 64($sp) sd $20, 56($sp) sd $19, 48($sp) sd $18, 40($sp) sd $17, 32($sp) sd $16, 24($sp) move $fp, $sp lui $1, %hi(%neg(%gp_rel(isMatch))) sd $4, 16($fp) daddu $1, $1, $25 daddiu $gp, $1, %lo(%neg(%gp_rel(isMatch))) ld $25, %call16(strlen)($gp) jalr $25 move $16, $5 ld $25, %call16(strlen)($gp) move $18, $2 move $4, $16 jalr $25 sll $17, $18, 0 move $19, $2 sw $17, 12($fp) addiu $1, $17, 1 ld $25, %call16(calloc)($gp) daddiu $5, $zero, 4 sll $17, $19, 0 addiu $2, $17, 1 mul $4, $2, $1 jalr $25 sw $2, 8($fp) move $4, $2 addiu $1, $zero, 1 sw $17, 4($fp) blez $17, .LBB1_19 sw $1, 0($2) dext $1, $19, 0, 31 daddiu $3, $4, 4 addiu $5, $zero, 42 move $6, $16 b .LBB1_3 daddu $2, $16, $1 .LBB1_2: daddiu $6, $6, 1 beq $6, $2, .LBB1_5 daddiu $3, $3, 4 .LBB1_3: lbu $1, 0($6) bne $1, $5, .LBB1_2 nop lw $1, -8($3) b .LBB1_2 sw $1, 0($3) .LBB1_5: lw $1, 12($fp) blez $1, .LBB1_19 nop lw $1, 8($fp) lui $6, 32767 daddiu $3, $4, 4 dext $9, $18, 0, 31 daddiu $10, $zero, 0 addiu $11, $zero, 42 addiu $12, $zero, 46 daddiu $24, $zero, 0 ori $6, $6, 65535 move $22, $3 dext $1, $1, 0, 32 dsll $7, $6, 33 dsll $6, $1, 32 dsll $5, $1, 2 daddiu $1, $zero, 1 daddu $7, $6, $7 daddu $20, $3, $5 dsll $13, $1, 32 b .LBB1_8 move $14, $6 .LBB1_7: daddu $10, $10, $6 daddu $14, $14, $6 daddu $7, $7, $6 daddu $22, $22, $5 beq $24, $9, .LBB1_19 daddu $20, $20, $5 .LBB1_8: ld $1, 16($fp) move $18, $16 move $19, $10 move $23, $14 move $ra, $7 move $15, $22 move $8, $20 daddu $25, $1, $24 b .LBB1_12 daddiu $24, $24, 1 .LBB1_9: dsra $1, $19, 30 daddu $1, $4, $1 lw $1, 0($1) beqz $1, .LBB1_18 nop lbu $17, 0($25) xor $1, $21, $12 sltiu $1, $1, 1 xor $17, $17, $21 sltiu $17, $17, 1 or $21, $17, $1 .LBB1_11: sw $21, 0($8) daddu $19, $19, $13 daddu $23, $23, $13 daddu $ra, $ra, $13 daddiu $18, $18, 1 daddiu $15, $15, 4 beq $18, $2, .LBB1_7 daddiu $8, $8, 4 .LBB1_12: lbu $21, 0($18) bne $21, $11, .LBB1_9 nop dsra $1, $ra, 30 daddu $1, $3, $1 lw $1, 0($1) bnez $1, .LBB1_11 addiu $21, $zero, 1 dsra $1, $23, 30 daddu $1, $4, $1 lw $1, 0($1) bnez $1, .LBB1_11 nop lbu $17, -1($18) lbu $1, 0($25) beq $1, $17, .LBB1_17 nop bne $17, $12, .LBB1_11 addiu $21, $zero, 0 .LBB1_17: lw $1, 0($15) b .LBB1_11 sltu $21, $zero, $1 .LBB1_18: b .LBB1_11 addiu $21, $zero, 0 .LBB1_19: lw $1, 12($fp) lw $2, 8($fp) ld $25, %call16(free)($gp) mul $1, $2, $1 lw $2, 4($fp) addu $1, $1, $2 dsll $1, $1, 2 daddu $1, $4, $1 lw $16, 0($1) jalr $25 nop sltu $2, $zero, $16 move $sp, $fp ld $16, 24($sp) ld $17, 32($sp) ld $18, 40($sp) ld $19, 48($sp) ld $20, 56($sp) ld $21, 64($sp) ld $22, 72($sp) ld $23, 80($sp) ld $gp, 88($sp) ld $fp, 96($sp) ld $ra, 104($sp) jr $ra daddiu $sp, $sp, 112